function trim(text)
{
	var newstr = text.replace(/^\s*/, "");
	newstr = newstr.replace(/\s*$/, "");
	return newstr;
}

function decimal_ctrl(keypress)
{
	if ((keypress < 48 || keypress > 57) && keypress != 46) {
		return 0;
	}
	return keypress;
}

function toUnicode(elmnt,content){
    if (content.length==elmnt.maxLength){
      next=elmnt.tabIndex
      if (next<document.forms[0].elements.length){
        document.forms[0].elements[next].focus()
    }
  }
}


function popup_window2(myurl, widths, heights, tops, lefts) {
  txt = 'width=' + widths + ', height=' + heights + ', top=' + tops + ', left=' + lefts + ', resizable=0, scrollbars=yes' ;
  popup = window.open(myurl, 'popup_window2', txt);
  popup.focus();
}



function showDiv() { 
    document.all.btnprint.style.visibility = 'hidden'; 
    document.all.btnaccept.style.visibility = 'hidden'; 
    document.all.btnreject.style.visibility = 'hidden'; 
	document.all.PrintObj.style.visibility = 'visible';
	document.all.btnprint.value = 'Print'; 
    document.all.btnprint.style.visibility = 'visible'; 
    document.all.btnprint.focus(); 
}

function hideDiv() { 
	document.all.PrintObj.style.visibility = 'hidden'; 
	document.all.btnprint.style.visibility = 'hidden'; 
    document.all.btnaccept.style.visibility = 'visible'; 
    document.all.btnreject.style.visibility = 'visible'; 

}



function checksearch()
{
	var doc = document.frmsearch
	
	if ((doc.txtcat.value =='') && (doc.keyword.value =='')&& (doc.txtid.value =='')&& (doc.txtpts1.value =='')&& (doc.txtpts2.value =='')) {
			alert('Please select Catalog/Category/Point or fill in keyword input!');
			doc.txtid.focus();
			return false;	
	}

	if (doc.keyword.value =='') {

				if (doc.txtid.value =='') {
					alert('Please select Catalog input!');
					doc.txtid.focus();
					return false;
				}else if(doc.txtcat.value =='') {
					alert('Please select Category input!');
					doc.txtcat.focus();
					return false;
				}else if((doc.txtpts1.value =='')&& (doc.txtpts2.value ==''))  {
					alert('Please input Point!');
					doc.txtpts1.focus();
					return false;
				}
	}


	doc.submit();
}

function gotoprint()
{
	
	var doc = document.selection;

	txtname = trim(doc.txtname.value);
	if (txtname =='') {
			alert('Please fill in your name');
			doc.txtname.focus();
			return false;
	}

	txtcardno1 = trim(doc.txtcardno1.value);
	txtcardno2 = trim(doc.txtcardno2.value);
	txtcardno3 = trim(doc.txtcardno3.value);
	txtcardno4 = trim(doc.txtcardno4.value);
	
	if ( txtcardno1 ==''||txtcardno2 ==''||txtcardno3 ==''||txtcardno4 ==''||txtcardno1.length != 4 ||txtcardno2.length != 4 ||txtcardno3.length != 4 ||txtcardno4.length != 4) {
			alert('Please fill in your card no');
			
			if (txtcardno1.length != 4) {
				doc.txtcardno1.focus();
				return false;
			}else if (txtcardno2.length != 4) {
				doc.txtcardno2.focus();
				return false;
			}else if (txtcardno3.length != 4) {
				doc.txtcardno3.focus();
				return false;
			}else if (txtcardno4.length != 4) {
				doc.txtcardno4.focus();
				return false;
			}
	}
	
	txtcontact = trim(doc.txtcontact.value);
	if (txtcontact =='') {
			alert('Please fill in your contact number');
			doc.txtcontact.focus();
			return false;
	}
	
	
	document.selection.action = 'print.php';
	document.selection.submit();
}

function reFillList()
{
   var doc = document.frmsearch;
   
  	if (doc.txtcat.value ==21)  {
			doc.txtpts1.value = 3;
			doc.txtpts1.focus();
	} 

  	if (doc.txtcat.value ==24)  {
			doc.txtpts1.value = 2;
			doc.txtpts1.focus();
	} 

  	if (doc.txtcat.value ==25)  {
			doc.txtpts1.value = 3;
			doc.txtpts1.focus();
	} 

   
}

function goto1()
{
	var doc = document.terms;
	var r=confirm("Do you accept the Terms and Conditions ?")
	if (r==true) {
    	doc.submit();
    }
}

function goto2()
{
	window.location="catalog.php";
}

function echeck(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}

function goto3()
{
	var doc = document.selection;
	
	txtname = trim(doc.txtname.value);
	if (txtname =='') {
			alert('Please fill in your name');
			doc.txtname.focus();
			return false;
	}

	txtcardno1 = trim(doc.txtcardno1.value);
	txtcardno2 = trim(doc.txtcardno2.value);
	txtcardno3 = trim(doc.txtcardno3.value);
	txtcardno4 = trim(doc.txtcardno4.value);
	
	if (txtcardno1 ==''||txtcardno2 ==''||txtcardno3 ==''||txtcardno4 ==''||txtcardno1.length != 4 ||txtcardno2.length != 4 ||txtcardno3.length != 4 ||txtcardno4.length != 4) {
	//if (txtcardno1 ==''||txtcardno1.length != 4) {

			alert('Please fill in your card no');
			
			if (txtcardno1.length != 4) {
				doc.txtcardno1.focus();
				return false;
			}else if (txtcardno2.length != 4) {
				doc.txtcardno2.focus();
				return false;
			}else if (txtcardno3.length != 4) {
				doc.txtcardno3.focus();
				return false;
			}else if (txtcardno4.length != 4) {
				doc.txtcardno4.focus();
				return false;
			}
	}

	if (txtcardno1 !=''||txtcardno2 !=''||txtcardno3 !=''||txtcardno4 !=''||txtcardno1.length == 4 ||txtcardno2.length == 4 ||txtcardno3.length == 4 ||txtcardno4.length == 4) {

	   var ccNum;
	   var odd = 1;
	   var even = 2;
	   var calcCard = 0;
	   var calcs = 0;
	   var ccNum2 = "";
	   var aChar = '';
	   var cc;
	   var r;

	   ccNum = txtcardno1 + txtcardno2 + txtcardno3 + txtcardno4; 
	  

	   for(var i = 0; i != ccNum.length; i++) {
	      aChar = ccNum.substring(i,i+1);
	      if(aChar == '-') {
	         continue;
	      }
	      ccNum2 = ccNum2 + aChar;
	   }
   
	   cc = parseInt(ccNum2);
	   if(cc == 0) {
	      return false;
	   }
	   r = ccNum.length / 2;
	   if(ccNum.length - (parseInt(r)*2) == 0) {
	      odd = 2;
	      even = 1;
	   }
   
	   for(var x = ccNum.length - 1; x > 0; x--) {
	      r = x / 2;
	      if(r < 1) {
	         r++;
	      }
	      if(x - (parseInt(r) * 2) != 0) {
	         calcs = (parseInt(ccNum.charAt(x - 1))) * odd;
	      }
	      else {
	         calcs = (parseInt(ccNum.charAt(x - 1))) * even;
	      }
	      if(calcs >= 10) {
	         calcs = calcs - 10 + 1;
	      }
	      calcCard = calcCard + calcs;
	   }
   
	   calcs = 10 - (calcCard % 10);
	   if(calcs == 10) {
	      calcs = 0;
	   }
	   
	   if(calcs == (parseInt(ccNum.charAt(ccNum.length - 1)))) {
	      //alert ("Success !");
	   }
	   else {
	      alert ("This is NOT a valid Credit Card Number !");
	      doc.txtcardno1.focus();
	      return false;
	
	   }

	}
	
	txtcontact = trim(doc.txtcontact.value);
	if (txtcontact =='') {
			alert('Please fill in your contact number');
			doc.txtcontact.focus();
			return false;
	}



	myOption = 4;
	for (var i=0; i < document.selection.radiobtn.length; i++){
		if (document.selection.radiobtn[i].checked){
			myOption = document.selection.radiobtn[i].value;
		}
	}

	if(myOption == 4){
		addr1 = trim(doc.txtaddress1.value);
		if (!addr1) {
			alert('Please fill in your address');
			doc.txtaddress1.focus();
			return false;
		}

		txtpostcode = trim(doc.txtpostcode.value);
		if (!txtpostcode) {
			alert('Please fill in your postcode');
			doc.txtpostcode.focus();
			return false;
		}

		if (txtpostcode.length != 5) {
			alert('Please fill in your correct postcode');
			doc.txtpostcode.focus();
			return false;
		}


		txtstate = trim(doc.txtstate.value);
		if (!txtstate) {
			alert('Please fill in your state');
			doc.txtstate.focus();
			return false;
		}		

	}
	
		
	txtemail = trim(doc.txtemail.value);
	if (txtemail !='') {
		if (echeck(txtemail)==false) {
			doc.txtemail.focus();
			return false;
		}
	}

	for (x = 0; x < doc.tmputility.value; x++) {
		enrichno = document.getElementById("nrichno"+x).value;
		if (!enrichno) {
			alert("You must key in your Enrich/Utility Payment number/Supplementary Card number!");
			document.getElementById("nrichno"+x).focus();
			return false;
		}
	}

	//alert (doc.enrichno[0].value);
	//enrich = trim(doc.flagenrich.value);
	//if (enrich == 1) {
	//	enrichno = trim(doc.enrichno.value);
	//	if (!enrichno) {
	//		alert("You must key in your Enrich Membership/Utility Payment number!");
	//		doc.enrichno.focus();
	//		return false;
	//	}
	//}
	
	doc.submit();
}

function goto4()
{
	window.location="catalog.php";
}

function goto5()
{
	var doc = document.summary;	
	doc.submit();
}

function goto6()
{
	window.location="selection.php";
}

function goto7()
{
	//doc.submit();
}




function login_page()
{
	//window.location="../../index.php?tpl=usr_login.tsl&uc=1";
	window.location="terms.php";

}

function verify(nombor,ms) {
	var doc = document.listing;

	eval('var URL = \'catalog.php?page='+ ms +'&sadd=sadd&prod=\' + doc.txtproduct_' + nombor + '.value + \'&v1=\' + doc.txtname1_' + nombor + '.value + \'&v2=\' + doc.txtname2_' + nombor + '.value + \'&v3=\' + doc.txtname3_' + nombor + '.value;');
	location.href(URL);
}

