var img;
function regImg(elem){
	img=elem;
}

function displayWindow(url, width, height){
	var Win = window.open(url,"displayWindow",'width=' + 510 + ',height=' + 510 + ',resizable=no,scrollbars=yes,menubar=no,status=no' );
}

function HideSubmit(){
	var obj2 = MM_findObj("trWarning");
	var obj = MM_findObj("imgSubmit");
	eval("obj.style.display='none'");
	eval("obj2.style.display=''");
}

function CopyInfo(){
	try{
		document.buyForm.User_CardName.value=document.buyForm.User_Fname.value + ' ' + document.buyForm.User_Lname.value;
		document.buyForm.User_CardAddress.value=document.buyForm.User_Address.value;
		document.buyForm.User_CardZip.value=document.buyForm.User_Zip.value;
		if(document.buyForm.User_Address.value!='' && document.buyForm.User_Apartement.value!='')
			document.buyForm.User_CardAddress.value+=", "+document.buyForm.User_Apartement.value;
	}
	catch(err){;}
}

function callValidate(){

	if(document.buyForm.hdnPrice.value==''){
		alert("Please choose product");
		return false;
	}
	if(document.buyForm.slcShip.value==''){
		alert("Please choose shipping type");
		document.buyForm.slcShip.focus;
		return false;
	}
	if(document.buyForm.User_Age.value==''){
		alert("Please enter your Age");
		document.buyForm.User_Age.focus;
		document.buyForm.User_Age.select;
		return false;
	}
	if(!isNumeric(document.buyForm.User_Age.value)){
		document.buyForm.User_Age.focus;
		document.buyForm.User_Age.select;
		alert("The Age you entered is invalid");
		return false;
	}

	if(document.buyForm.User_Fname.value==''){
		document.buyForm.User_Fname.focus;
		document.buyForm.User_Fname.select;
		alert("Please enter your first Name");
		return false;
	}
	
	if(document.buyForm.User_Lname.value==''){
		document.buyForm.User_Lname.focus;
		document.buyForm.User_Lname.select;
		alert("Please enter your last name");
		return false;
	}
	
	if(document.buyForm.User_Address.value==''){
	    document.buyForm.User_Address.focus;
	    document.buyForm.User_Address.select;
		alert("Please enter your street & house number");
		return false;
	}
	
	if(document.buyForm.User_City.value==''){
		alert("Please enter your city");
		document.buyForm.User_City.focus;
		document.buyForm.User_City.select;
		return false;
	}
	
	if(document.buyForm.User_Country.selectedIndex==0){
		alert("Please choose your country.");
		return false;	
	}
	
	if(document.buyForm.User_Zip.value==''){
		document.buyForm.User_Zip.value.focus;
		document.buyForm.User_Zip.value.select;
		alert("Please enter your zip code");
		
		return false;
	}
	
	if(document.buyForm.User_Email.value==''){
	    document.buyForm.User_Email.focus;
	    document.buyForm.User_Email.select;
		alert("Please enter your Email Address");
		return false;
	}
	if(!validateEmail(document.buyForm.User_Email.value)){
		document.buyForm.User_Email.focus;
		document.buyForm.User_Email.select;
		alert("The Email Address you entered is invalid");
		return false;
	}
	
	if(document.buyForm.User_Phone.value==''){
		document.buyForm.User_Phone.focus;
		document.buyForm.User_Phone.select;
		alert("Please enter your Personal Contact Phone number");
		return false;
	}
	if(!isStrNumeric(document.buyForm.User_Phone.value)){
		document.buyForm.User_Phone.focus;
		document.buyForm.User_Phone.select;
		alert("Your Contact Phone Number is Invalid");
		return false;
	}
	
	
	if(document.buyForm.User_Password.value==''){
		document.buyForm.User_Password.focus;
		document.buyForm.User_Password.select;
		alert("Please enter a password for re-orders");
		return false;
	}
	
	//if(!PL_validateForm('buyForm','','User_Fname','REQ','You must enter First Name','User_Address','REQ','You must Street & house number','User_Email','REQ;EMAIL','You must enter a vaild E-maill','User_Country','REQ','You must enter your Country','User_Password','REQ','You must enter Password for reorder'))
	//	return false;
	//if(!PL_validateForm('buyForm','','User_City','REQ','You must enter your City','User_Zip','REQ','You must your Zip Code','User_Phone','REQ','You must enter your Phone'))
	//	return false;
	//
	
	
	
	if(!PL_validateForm('buyForm','','User_CardName','REQ','You must enter  Name on Card:','User_CardNumber','REQ','You must enter Card Number','User_CardAddress','REQ','You must enter Billing Address','User_CardZip','REQ',' You must enter Billing Zip Code'))
		return false;
	
	if((document.buyForm._v_User_CardDate_Year.selectedIndex==0) || (document.buyForm._v_User_CardDate_Month.selectedIndex==0)){
	alert("Please Choose Credit Card Expire date");
	return false;
	}
	
	if(document.buyForm.User_CardType.selectedIndex==0){
		alert("Please choose your credit card type.");
		return false;
	}
	
	if(!((document.buyForm.User_CardNumber.value.charAt(i)<"9" && document.buyForm.User_CardNumber.value.charAt(i)>"0") || (document.buyForm.User_CardNumber.value.charAt(i)==" "))){
			alert("Card number is wrong.\nPlease use numbers and spaces only.");
			return false;
		}
	

	if(document.buyForm.User_Card3Digits.value==''){
		alert("Please enter your CVC code");
		document.buyForm.User_Card3Digits.focus;
		document.buyForm.User_Card3Digits.select;
		return false;
	}
	if(!isNumeric(document.buyForm.User_Card3Digits.value)){
		document.buyForm.User_Card3Digits.focus;
		document.buyForm.User_Card3Digits.select;
		alert("The CVC code you entered is invalid");
		return false;
	}

	if(document.buyForm.chkConfirm.checked==false || document.buyForm.chkConfirm2.checked==false){
		alert("Please confirm");
		document.buyForm.chkConfirm.select;
		return false;
	}

	/*Please enter date
	//for (i=0;i<document.buyForm.User_CardNumber.value.length;i++)
	//{
	//var a=new String;
	
		
		
	}
	
	if(document.buyForm.Order_Quantity.options[document.buyForm.Order_Quantity.selectedIndex].value==0){
		alert("Please select the amount of pills you wish to order");
		return false;
	}
	*/	
	HideSubmit();
	return true;
}


function PL_isFormNumeric(strVal,strValidate){
	for(li=0;li<strVal.length;li++)
		if(strVal.charAt(li)!="0" && strVal.charAt(li)!="-" && strVal.charAt(li)!="." && !parseInt(strVal.charAt(li)))
			return false;
	strVal=parseFloat(strVal);
	var iRange=strValidate.indexOf("NUMBER")+6;
	var iRangeEnd=0,iLowLimit=0;iHighLimit=0;
	if(strValidate.charAt(iRange)=="("){
		iRangeEnd=strValidate.indexOf(")",iRange)-1;
		strValidate=strValidate.substr(iRange+1,(iRangeEnd-iRange))
		iLowLimit=parseFloat(strValidate.split("*")[0]);
		iHighLimit=parseFloat(strValidate.split("*")[1]);
		if(strVal<iLowLimit || strVal>iHighLimit){
			return false;
		}
	}
	return true;
}


function PL_validateForm(){
	var args=PL_validateForm.arguments;
	if(args.length==0)return;
	var strError="",bValid=true,bMes=false;;
	var objForm="document." + args[0];
	objForm=eval(objForm);
	if(args[1]!=""){
		var objMessages=eval(args[1]);bMes=true;		
	}
	for(i=2;i<args.length;i++){
		var objToValidate=eval("document." + args[0] + "." + args[i]);
		var strValidation=args[++i];
		var strErrorMessage=args[++i];
		var strToValidate=objToValidate.value;
		
		if(strValidation.indexOf("REQ")!=(-1)){
				if(strToValidate==""){
					if(bMes)strError+=strErrorMessage + "<br/>";
					else{alert(strErrorMessage);return false;}
					bValid=false;
				}
		}
		
		if(strValidation.indexOf("NUMBER")!=(-1)){
			if(strToValidate!="")
				if(!PL_isFormNumeric(strToValidate,strValidation)){
					if(bMes)strError+=strErrorMessage + "<br/>";
					else{alert(strErrorMessage);return false;}
					bValid=false;
				}
		}
		
		if(strValidation.indexOf("EMAIL")!=(-1)){
			if(strToValidate!="")
				if(strToValidate.length<6 || strToValidate.indexOf("@")<1 || strToValidate.indexOf(".")<3 || strToValidate.lastIndexOf(".")>(strToValidate.length-2)){
					if(bMes)strError+=strErrorMessage + "<br/>";
					else{alert(strErrorMessage);return false;}
					bValid=false;
				}
		}
		
		if(strValidation.indexOf("DATE")!=(-1)){
			var bTemp=false;
			if(strToValidate!="")
				if(strToValidate.length>5 && strToValidate.length<11 && strToValidate.indexOf("/")>0){
					if(strToValidate.split("/").length==3){
						if(PL_isFormNumeric(strToValidate.split("/")[0],'NUMBER(1*31)') && PL_isFormNumeric(strToValidate.split("/")[1],'NUMBER(1*12)') && PL_isFormNumeric(strToValidate.split("/")[2],'NUMBER'))
							bTemp=true;
					}
				}
			if(!bTemp){
				if(bMes)strError+=strErrorMessage + "<br/>";
				else{alert(strErrorMessage);return false;}
				bValid=false;
			}
		}
		
		if(strValidation.indexOf("SAMEAS")!=(-1)){
			if(strToValidate!="")
				if(strValidation.charAt(strValidation.indexOf("SAMEAS")+6)=="("){
					iPosStart=strValidation.indexOf("SAMEAS")+7;
					iPosEnd=strValidation.indexOf(")",iPosStart+1);
					strTemp=strValidation.substr(iPosStart,(iPosEnd-iPosStart));
					var objToValidate2=eval("document." + args[0] + "." + strTemp);
					if(strToValidate!=objToValidate2.value){
						if(bMes)strError+=strErrorMessage + "<br/>";
						else{alert(strErrorMessage);return false;}
						bValid=false;
					}
				}		
		}
		if(bMes && !bValid)objMessages.innerHTML=strError;
		else if(bMes)objMessages.innerHTML="";
	}
	return bValid;
}
 
function resetPrice(price){
	//alert(price);
	if(price==0){
		document.buyForm.Order_Price.value="0";
		document.buyForm.Order_Price2.value="0";
		document.buyForm.hdnPrice.value="0";
		return;
	}
	var p=eval("q" + price);
	//alert(p);
	document.buyForm.Order_Price.value=p;
	document.buyForm.Order_Price2.value=p;
	document.buyForm.hdnPrice.value=p;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function validateEmail(strToValidate){
	if(strToValidate.length<6 || strToValidate.indexOf("@")<1 || strToValidate.indexOf(".")<3 || strToValidate.lastIndexOf(".")>(strToValidate.length-2))
		return false;
	return true;
}
		
function isNumeric(strVal){
	for(li=0;li<strVal.length;li++)
		if(strVal.charAt(li)<"0" || strVal.charAt(li)>"9"){
			return false;
		}
  return true;
}	 

function isStrNumeric(strVal){
	for(li=0;li<strVal.length;li++)
		if((strVal.charAt(li)<"0" || strVal.charAt(li)>"9") && (strVal.charAt(li)!=" ") && (strVal.charAt(li)!="-")) {
			return false;
		}
	return true;
}	
