//Start of calendar functions
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }
function padout(number) { return (number < 10) ? '0' + number : number; }

var today = new Date();
var day = today.getDate(), month = today.getMonth(), year = y2k(today.getYear()), whichOne = 0;

function restart() {
    mywindow.close();
}

function chk() 
{
    var myday;
    var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
    mywindow.close();
    myday = '' + padout(day) + '-' + months[month] + '-' + year;
    window.location.href="ttsh_chkph.asp?Dday=" + myday;
}


function newWindow(number) {
    whichOne = number;
    day = today.getDate(), month = today.getMonth(), year = y2k(today.getYear());
    mywindow=open('../scripts/cal.htm','myname','resizable=no,width=350,height=270');
    mywindow.location.href = '../scripts/cal.htm';
    if (mywindow.opener == null) mywindow.opener = self;
}
//End of calendar functions
function trim(s)
{
	while(''+s.charAt(s.length-1)==' ')
		{s=s.substring(0,s.length-1);}
	while(''+s.charAt(0)==' ')
		{s=s.substring(1,s.length)}
return s;
}
function isEmail(strVal){		
	var supported = 0;
	if (window.RegExp){
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr))
			supported = 1;
	}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

function checkPhone(strVal){
	num = new String(trim(strVal));
	if (num != ""){		
		var GoodChars = "0123456789- ";			
		for (var i = 0; i <= num.length - 1; i++){
			if (GoodChars.indexOf(num.charAt(i)) == -1){
				return false;				
			}
		}
	}
	return true;
}

function checkform()
{
	var temp;
	temp = trim(document.frmLogin.txtUserName.value);
	if (temp.length == 0){
		alert("Please enter your User id");
		document.frmLogin.txtUserName.focus();
		window.event.returnValue = false;
		//return false;
	}
	temp = document.frmLogin.txtPassword.value;
	if (temp.length == 0){
		alert("Please enter your password")
		document.frmLogin.txtPassword.focus();
		window.event.returnValue = false;
		//return false;
	}
	//document.frmLogin.submit();
}


function checkNum()
{	
	var a = frmAddToCart.qty.value;
	var qty_len = trim(frmAddToCart.qty.value).length;
	var chr="";
	
	if (isNaN(a) || qty_len == 0)
	{
		alert ('Please enter a number');
		return false;
	}
	if (a == 0)
	{
		alert('Quantity should not be 0');
		return false;
	}
	for (var i=0; i<qty_len; i++)
	{
		chr = a.charAt(i);		
		if (chr==".")
		{
			alert('Quantity should not be a decimal');
			return false;
		}
		if (chr=="-")
		{
			alert('Quantity should not be a negative');
			return false;
		}
	}
	return true;
//document.frmAddToCart.submit();

}
     
function doDelete(num)
  {
    document.frmQty.delnum.value=num;
    //document.frmQty.action ="ttsh_category.asp?action=delete"; 
    document.frmQty.action.value="delete";
  //  document.frmQty.submit();
  }

function doUpdate()
  {
   var b;
   var c;
   var a = document.frmQty.totalRec.value;
	
   for (b=1; b<=a; b++)
   {   
	var c = document.all('qty_'+b).value;
	var qty_len = c.length;
	var chr="";
	
	if (isNaN(c) || qty_len == 0)
	{
		alert ('Please enter a number');
		window.event.returnValue = false;
		//return false;
	}
	if (c == 0)
	{
		alert('Quantity should not be 0');
		window.event.returnValue = false;
 		//return false;
	}
	for (var i=0; i<qty_len; i++)
	{
		chr = c.charAt(i);		
		if (chr==".")
		{
			alert('Quantity should not be a decimal');
			window.event.returnValue = false;
			i=qty_len;
			//return false;
		}
	}	
   }
   document.frmQty.action.value="update";  

 //  document.frmQty.action ="ttsh_category.asp?action=update";
//   document.frmQty.submit();
  }


function doCheckout(form1)
  {
    if (form1.subtotal.value < 50)
   {
	alert ('Kindly take note that a minimum order of $50 is required before delivery can be made.');
	window.event.returnValue = false;
    }
 else 
    {
	form1.action.value="checkout";
	//alert(form1.action.value);
    }
  }


function doView(form1)
  {
    	form1.action.value="view";
//	alert (form1.action.value);
//	form1.submit();
  }


function doConfirmCart()
{
    var strEmail = trim(document.frmAddress.txtEmail.value);	
    if (strEmail.length == 0)
    {
 	alert('Your Email Address is missing. \n\nPlease refer to instructions on *Update Particulars* section above to update.');
	//document.frmAddress.txtEmail.focus();
	window.event.returnValue = false;	
    }     
    var hTel = trim(document.frmAddress.txtHomeTel.value);
    var oTel = trim(document.frmAddress.txtOffTel.value);
    var Hp = trim(document.frmAddress.txtHp.value);
    var Pager = trim(document.frmAddress.txtPager.value);
	
    if ((hTel.length == 0) && (oTel.length == 0) && (Hp.length == 0) && (Pager.length == 0))
    {
 	alert('Please enter at least one contact number (Home tel/Off.Tel/Pager/Hp). \n\nPlease refer to instructions on *Update Particulars* section above to update.');
	//document.frmAddress.txtHomeTel.focus();
	window.event.returnValue = false;
    }
    var chr="";	
    var a = trim(document.frmAddress.postcode.value);
    var len = trim(document.frmAddress.postcode.value).length;
	
    if (trim(document.frmAddress.line1.value).length==0)
    {
	alert('Please enter the street name');
	document.frmAddress.line1.focus();
	window.event.returnValue = false;
    }    

    if (len != 6)
    {
	alert('Please enter 6 digits for postal code');
	document.frmAddress.postcode.focus();
	//document.frmAddress.action.value ="checkout";
	//document.frmAddress.postcode.focus();
	//return false;
	window.event.returnValue = false;
    }

    if (isNaN(a))
    {
	alert ('Please enter only digits');
	document.frmAddress.postcode.focus();
	//document.frmAddress.action ="checkout";
	//return false;
	window.event.returnValue = false;
    }
    for (var i=0; i<len; i++)
    {
	chr = a.charAt(i);		
	if (chr==".") 
	{
		alert('Postcode should not contain these characters');
		document.frmAddress.postcode.focus();
	//	document.frmAddress.action ="checkout";
	//	return false;
		window.event.returnValue = false;
		i=len;
	}
    }
   document.frmAddress.action.value="confirm";
  // document.frmAddress.submit();
   return true;
   //window.event.returnValue = true;	
  
}


function doPopWin(actn)
{
    var popurl = "ttsh_updateParticulars.asp?action="+actn;
    //alert(popurl);
    winpops=window.open(popurl,"","width=300,height=400,scrollbars,resizable,");
    winpops.moveTo(10,150) 

}

function doPayMode()
  {
    //document.frmQty.action ="ttsh_category.asp?action=paymode";
    document.frmQty.action.value="paymode";
    document.frmQty.submit();
  }

function doSearch()
  {
    //alert('hello');
    if (trim(document.frmSearch.searchtxt.value).length==0)
    {
	alert('Please do not leave blank');
	document.frmSearch.searchtxt.focus();
	window.event.returnValue = false;
    }
    else
    {
	if (trim(document.frmSearch.searchtxt.value).length<3)
    	{
		alert('Please enter at least 3 characters');
		document.frmSearch.searchtxt.focus();
		window.event.returnValue = false;
    	}
	else
	{
		 document.frmSearch.action.value="search";
   		 document.frmSearch.submit();
	}
    }
    //document.frmSearch.action.value="search";
    //document.frmSearch.submit();
  }

//function doRedeem()
//  {
//     
//   if (isNaN(document.frmQty.rewardVal.value) || parseFloat(document.frmQty.rewardVal.value)<0  //       || document.frmQty.rewardVal.value.length==0)
//   {
//	alert ('Please enter only positive digits');
//	document.frmQty.rewardVal.focus();
//	window.event.returnValue = false;
//   }
//   else
//   {
//      if (parseFloat(document.frmQty.rewardVal.value) > 		//parseFloat(document.frmQty.TotalRedeemable.value))
//      {
//	  alert('You cannot redeem more than your accumulated redeemable reward dollars.');
//	  document.frmQty.rewardVal.focus();
//	  window.event.returnValue = false;
//    }
//      else
//      {
//          if(confirm('You have chosen to redeem $'+document.frmQty.rewardVal.value+' reward //dollars.'))
//          {
//	     document.frmQty.action.value="redeem";   
//	     return true;
//          }
//          else
//          {
//	     window.event.returnValue = false;
//          }
//       }
//    }

//    document.frmQty.submit();
//  }

function doAdminUpdate()
 {
  var a = document.frmQty.order_no.value;
  var qty_len = trim(document.frmQty.order_no.value).length;
  var chr="";

 if (qty_len==0)
    {
	alert('Please enter an order number');
	document.frmQty.order_no.focus();
	window.event.returnValue = false;
    }  

   document.frmQty.action.value="adminupdate";
 }

                        
function doAdminThankU(curDay,curMonth,curYear)
 {
    var chr="";	
    var a = trim(document.frmQty.postcode.value);
    var len = trim(document.frmQty.postcode.value).length;
    var optDay=document.frmQty.Dday.value;
    var optMonth=document.frmQty.Dmth.value;
    var optYear=document.frmQty.Dyear.value;
    
    if (document.frmQty.subtotal.value < 50)
    {
	alert ('Kindly take note that a minimum order of $50 is required before delivery can be made.');
	return false;
    }

	
    if (trim(document.frmQty.line1.value).length==0)
    {
	alert('Please enter the street name');
	document.frmQty.line1.focus();
	return false;
    }    

    if (len != 6)
    {
	alert('Please enter 6 digits for postal code');
	document.frmQty.postcode.focus();
	return false;
    }

    if (isNaN(a))
    {
	alert ('Please enter only digits');
	document.frmQty.postcode.focus();
	return false;
    }
    for (var i=0; i<len; i++)
    {
	chr = a.charAt(i);		
	if (chr==".") 
	{
		alert('Postcode should not contain these characters');
		document.frmQty.postcode.focus();
		return false;
		i=len;
	}
    }


    if (!isValidDate(optDay,optMonth,optYear))
 	{
 		alert('Invalid delivery date');
 		document.frmQty.Dday.focus();
		//window.event.returnValue = false;
		return false;
 	}

    if (isFutureDate(optDay,optMonth,optYear,curDay,curMonth,curYear) == -1)
        {
   		alert('Delivery date should not be earlier than today');
		document.frmQty.Dday.focus();
		//window.event.returnValue = false;
		return false;
	}
    if (isFutureDate(optDay,optMonth,optYear,curDay,curMonth,curYear) == 0)
        {
   		alert('Please allow at least 2 working days for delivery.');
		document.frmQty.Dday.focus();
		//window.event.returnValue = false;
		return false;
        }

    if (isNaN(document.frmQty.delivery_charges.value))
	{
		alert('Please enter a value for delivery charges');
		document.frmQty.delivery_charges.focus();
		//window.event.returnValue = false;
		return false;
	}

    if (parseInt(document.frmQty.delivery_charges.value) != parseFloat(document.frmQty.delivery_charges.value))
	{
		if ((document.frmQty.delivery_charges.value.charAt(document.frmQty.delivery_charges.value.length-3) != '.') && (document.frmQty.delivery_charges.value.charAt(document.frmQty.delivery_charges.value.length-2) != '.'))
			{
			alert("Do not enter more than two decimal places for delivery charges");
			document.frmQty.delivery_charges.focus();
			return false;
			}
	}

    if (trim(document.frmQty.mode_of_payment.value)=="CASH")       		
       {
		if (trim(document.frmQty.cash_fees.value).length==0) 
	           {
			alert('Please enter a value for cash handling fees');
			document.frmQty.cash_fees.focus();
			window.event.returnValue = false;
			//return false;
	    	   }
		else
	 	   {	    	         
			if (isNaN(document.frmQty.cash_fees.value))
		     	   {
				alert('Please enter a value for cash handling fees');
				document.frmQty.cash_fees.focus();
				window.event.returnValue = false;
		     	   }
			else
		     	   {
				return true;
		     	   }
	    	   }
    	}
     else
    	{
		if (trim(document.frmQty.cash_fees.value) != 0)
	   	   {
			alert('Please enter 0 cash handling fees for credit card payment');
			document.frmQty.cash_fees.focus();
			window.event.returnValue = false;
			//return false;
	
	           }
		else
	          {
			return true;
	          }
     	}

  }

function doAdminConfirmPurchase(curDay,curMonth,curYear)
  {
 
    if (doAdminThankU(curDay,curMonth,curYear)  && confirm('Have you confirm the new purchase?'))

	{
 		//alert('ok!');
		document.frmQty.action.value="adminthankU";
		return true;	
	}
	else
	{
		window.event.returnValue = false;
		//return false;
	}

  }
function doThankU(curDay,curMonth,curYear)
 {   
	document.frmQty.purchaseBtn.disabled = true;   
	var dd = document.frmQty.Dday.value;
	var ddArr = dd.split("/");
	var optDay = ddArr[0];
	var optMonth = ddArr[1];
	var optYear =ddArr[2];

	
	if (dd == "")
	{
		document.frmQty.purchaseBtn.disabled = false;   
		alert('Please select a delivery date');
 		document.frmQty.Dday.focus();
		window.event.returnValue = false;
		return false;
	}
	else
		if (document.frmQty.mode_of_payment.value != 'CASH') //for Credit Card
		{   
			alert('You will be brought to the eNETS secured page for your credit card payment.\nThe credit card payment processing may take about 2 minutes or more.\nPlease wait and do not close your browser until you have received your payment status.\nThank you.');
		
		}
		else
		{	
			alert('Your order is being processed now. Please wait.');		
		}
		document.frmQty.action.value="thankU";	
		document.frmQty.Dday.value = optDay;
		document.frmQty.Dmth.value = optMonth;
		document.frmQty.Dyear.value = optYear;
		document.frmQty.submit();		
  }//doThankU

function isValidDate(optDay, optMonth, optYear){ 
    if (( optMonth == 4 || optMonth == 6 || optMonth == 9 || optMonth == 11) && optDay > 30 ){         
        return false; 
		}
    else if ( optMonth == 2 && optDay > 29 ) {      
        return false; 
		} 
    else if (optMonth == 2 && optYear % 4 > 0 && optDay > 28){         
	return false; 
		}
    else {
	return true;
    	}  		
}

function isFutureDate(optDay, optMonth, optYear, curDay, curMonth, curYear){ 

    //var date_obj = new Date();
    var date_cur;
    date_cur = curMonth; //date_obj.getMonth() + 1;
    date_cur += "-";
    date_cur += curDay; //date_obj.getDate();
    date_cur += "-";
    date_cur += curYear; //date_obj.getFullYear();
	
    //alert('optDay=' + optDay + ', optMonth=' + optMonth + ', optYear=' + optYear + ', curDay=' + curDay + ', curMonth=' + curMonth + ', curYear=' + curYear);
    //alert(date_cur);

    if(compareDates ((optMonth+"-"+optDay+"-"+optYear),date_cur) == -1)
    {         
	//alert('less!');
        return -1; 
    }
  
    else if(compareDates ((optMonth+"-"+optDay+"-"+optYear),date_cur) == 1)
    {
	//alert('more!');
	return 1;
    }  	
    else
    { 
	return 0;
    }	
}

function compareDates (value1, value2) {
   var date1, date2;
   var month1, month2;
   var year1, year2;

   month1 = value1.substring (0, value1.indexOf ("-"));
   date1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
   year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);

   month2 = value2.substring (0, value2.indexOf ("-"));
   date2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
   year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);

   if (parseInt(year1) >= parseInt(year2)) return 1;
   else if (parseInt(year1) < parseInt(year2)) return -1;
   else if (parseInt(month1) > parseInt(month2)) return 1;
   else if (parseInt(month1) < parseInt(month2)) return -1;
   else if (parseInt(date1)-1 > parseInt(date2)) return 1;
   else if (parseInt(date1) < parseInt(date2)) return -1;
   else return 0;

} 

function doSubmitCC()
  {
    if (isEmpty(document.frmQty.card_name.value))
    {
    document.frmQty.purchaseBtn.disabled = false;       
	alert('Credit card name cannot be empty');
	document.frmQty.card_name.focus();
	return false;
    }

    if (isEmpty(document.frmQty.card_no.value)) 
    {
    document.frmQty.purchaseBtn.disabled = false;       
	alert('Please fill in your credit card number');
	document.frmQty.card_no.focus();
	return false;
    }
    if (!validcc(document.frmQty.card_no.value,document.frmQty.card_type.value)) 
    {
    document.frmQty.purchaseBtn.disabled = false;       
	//alert('Credit card number invalid');
	document.frmQty.card_no.focus();
	window.event.returnValue = false;
	return false;
    }   
    if (isEmpty(document.frmQty.CCmth.value))
    {
    document.frmQty.purchaseBtn.disabled = false;       
	alert('Credit card expire month cannot be empty');
	document.frmQty.CCmth.focus();
	return false;
    }
    if (isEmpty(document.frmQty.CCyr.value))
    {
    document.frmQty.purchaseBtn.disabled = false;       
	alert('Credit card expire year cannot be empty');
	document.frmQty.CCyr.focus();
	return false;
    }

    if (!isCreditCardExpire(document.frmQty.CCmth.value, document.frmQty.CCyr.value))
    {
    document.frmQty.purchaseBtn.disabled = false;       
	alert('Your credit card has expired');
	document.frmQty.CCmth.focus();
	return false;
    }    
    return true;
  }

function doList(form1)
  {
    form1.action.value="detail";
    //alert(form1.action.value);
    form1.submit();
  }

function doPromo()
  {
	//alert(document.frmSuperSavers.action.value);
	document.frmSuperSavers.action.value="promo";
	//alert(document.frmTopSavers.action.value);
	//document.frmSuperSavers.submit();
  }

function doPchHistory(formname,choice)
  {
	if (formname=="frmTop")
	{
		//alert('in top');
		document.frmTop.code.value=choice;
		document.frmTop.action.value="pchhis";
  	}
	else
	{	
		//alert('in pch');
		document.frmPCH.code.value=choice;
		document.frmPCH.action.value="pchhis";
 	} 
 }

function doPchDetail(no)
  {
	//alert(no);
	document.frmPCDetail.order_num.value=no;
	document.frmPCDetail.action.value="delivered";
 	//alert(document.frmPCDetail.action.value);
	document.frmPCDetail.submit();
  }


function ValidateAskPharmForm(){
		if (document.frmTTSH_AskPharmacist.textarea1.value  == "" ){
		alert("Please Enter Your Question.")
		document.frmTTSH_AskPharmacist.textarea1 .focus();
		return false;
	}	
					
		return true;
}

function TextCounterAskPharm(field, countfield) {
	var maxlimit = 1000;

	if (field.value.length > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
		alert("Sorry! You have exceeded the limit of " + maxlimit + " characters!");
		
		}
countfield.value = maxlimit - field.value.length;

}

function y2k(number) 
{ 
	return (number < 1000) ? number + 1900 : number; 
}

function daysElapsed(date1,date2) {
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}

//function daysElapsed(date1,date2) {
//    var difference = (date1.getTime() - date2.getTime());
//        difference = difference/(1000*60*60*24);
//    return difference;
//}