var bmlErrorCode = "0";

function isEmailAddr(str)
{
	if (str==null || str=="" || str==undefined) {
		return false;
	}
	var at = "@";
	var dot = ".";
	var at_pos = str.indexOf(at);
	var len = str.length;
	
	if (str.indexOf(at)==-1) {
		return false;
	}
	if (str.indexOf("..")>=0) {
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==len) {
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==len) {
		return false;
	}
	if (str.indexOf(at,(at_pos+1))!=-1) {
		return false;
	}
	if (str.substring(at_pos-1,at_pos)==dot || str.substring(at_pos+1,at_pos+2)==dot) {
		return false;
	}
	if (str.substring(len-1,len)==dot) {
		return false;
	}
	if (str.indexOf(dot,(at_pos+2))==-1) {
		return false;
	}
	if (str.indexOf(" ")!=-1) {
		return false;
	}
	return true;
}

function Validator(thisForm)
{
	if (!isEmailAddr(thisForm.email.value))
	{
		alert("Please enter a valid e-mail address in the form: yourname@yourdomain.comm");
		thisForm.email.focus();
		return (false);
	}
	return (true);
}

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 MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ProductValidator(theForm)
{
	if(theForm.quantity != undefined) 
	{
		if (theForm.quantity.selectedIndex < 0)
		{
			alert("Please select one of the \"Quantity\" options.");
			theForm.quantity.focus();
			return (false);
		}
	}

	if(theForm.product_color != undefined) 
	{
		if (theForm.product_color.selectedIndex < 0)
		{
			 alert("Please select one of the \"Color\" options.");
			 theForm.product_color.focus();
			 return (false);
		}
		if (theForm.product_color.selectedIndex == 0)
		{
			alert("The first \"Color\" option is not a valid selection.  Please choose one of the other options.");
			theForm.product_color.focus();
			return (false);
		}
	}

	if(theForm.width != undefined) 
	{
		if (theForm.width.selectedIndex < 0)
		{
			alert("Please select one of the \"WidthFraction\" options.");
			theForm.width.focus();
			return (false);
		}
	}
	
	if(theForm.height != undefined) 
	{
		if (theForm.height.selectedIndex < 0)
		{
			alert("Please select one of the \"HeightFraction\" options.");
			theForm.height.focus();
			return (false);
		}
	}

	if(theForm.mount != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.mount.length;  i++)
		{
		  if (theForm.mount[i].checked)
		      radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the \"Mount\" options.");
			theForm.mount[0].focus();
			return (false);
		}
	}

	try 
	{
		if (ValidateOptions(theForm) == false) {
			return(false);
		}
	}
	catch(errorObject){}

	// patchy workaround to validate Divider Rail Position should customer chooses Off-Center Divider Rail
	if(theForm.dividerrailloc != undefined)
	{
		if (glblDividerRail.indexOf('off') != -1 && theForm.dividerrailloc.value == '')
		{
			alert("Please select the Off-Center Divider Rail position.");
			theForm.dividerrailloc.focus();
			return (false);
		}
	}

	// another patchy workaround to validate whether customer selects divider rail whereever required
	if(typeof(strValidationElement) == 'object')
	{
		var railPosition = 0;
		//hardcoded for now and will remain forever
		if ((strShutterSKU=='713122') && (document.ProductForm.height.value>=64))
		{
			railPosition = 64;
		} 
		if ((strShutterSKU=='713120' || strShutterSKU=='713138' || strShutterSKU=='713140' || strShutterSKU=='713146' || strShutterSKU=='713148') && (document.ProductForm.height.value>=72))
		{
			railPosition = 72;
		} 
		if ((strShutterSKU=='713115' || strShutterSKU=='713130' || strShutterSKU=='713132' || strShutterSKU=='713134' || strShutterSKU=='713136' || strShutterSKU=='713142' || strShutterSKU=='713144') && (document.ProductForm.height.value>=60))
		{
			railPosition = 60;
		}

		if (railPosition > 0)
		{
			alert('A Divider Rail is required with a height of ' + railPosition + '" or more');
			strValidationElement.focus();
			return (false);
		}
	}

	if(theForm.lift != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.lift.length;  i++)
		{
			if (theForm.lift[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the \"Lift\" options.");
			theForm.lift[0].focus();
			return (false);
		}
	}

	if(theForm.tilt != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.tilt.length;  i++)
		{
			if (theForm.tilt[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the \"Tilt\" options.");
			theForm.tilt[0].focus();
			return (false);
		}
	}  

	if(theForm.lift2 != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.lift2.length;  i++)
		{
			if (theForm.lift2[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the second \"Lift\" options.");
			theForm.lift2[0].focus();
			return (false);
		}
	}
  
	if(theForm.tilt2 != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.tilt2.length;  i++)
		{
			if (theForm.tilt2[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the second \"Tilt\" options.");
			theForm.tilt2[0].focus();
			return (false);
		}
	}
	
	if(theForm.tilt_control != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.tilt_control.length;  i++)
		{
			if (theForm.tilt_control[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the \"Control\" options.");
			theForm.tilt_control[0].focus();
			return (false);
		}
	}

	if(theForm.tapecolor != undefined) 
	{
		if( theForm.tapecolor )
		{
			if (theForm.tapecolor.options.length > 1)
			{
				if (theForm.tapecolor.selectedIndex < 0)
				{
					alert("Please select one of the \"Tape color\" options.");
					theForm.tapecolor.focus();
					return (false);
				}
			
				if (theForm.tapecolor.selectedIndex == 0)
				{
					alert("The first \"Tape color\" option is not a valid selection.  Please choose one of the other options.");
					theForm.tapecolor.focus();
					return (false);
				}
			}
		}
	}
	
	if(theForm.satellite_eye != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.satellite_eye.length;  i++)
		{
			if (theForm.satellite_eye[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the \"Satellite Eye\" options.");
			theForm.satellite_eye[0].focus();
			return (false);
		}
	}
	
	if(theForm.ceiling != undefined) 
	{
		var radioSelected = false;
		for (i = 0;  i < theForm.ceiling.length;  i++)
		{
			if (theForm.ceiling[i].checked)
				radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the \"Bracket\" options.");
			theForm.ceiling[0].focus();
			return (false);
		}
	}
  
	if(theForm.stack != undefined) 
	{
		if (theForm.stack.selectedIndex <0)
		{
			alert("Please select one of the \"Stack\" options.");
			theForm.stack.focus();
			return (false);
		}
		
		if (theForm.stack.selectedIndex == 0)
		{
			alert("The first \"Stack\" option is not a valid selection.  Please choose one of the other options.");
			theForm.stack.focus();
			return (false);
		}
	}
  
 
//  if (theForm.tapecolor.selectedIndex == 0)
//	{
//	  alert("The first \"Trim color\" option is not a valid selection.  Please choose one of the other options.");
//	  theForm.tapecolor.focus();
//	  return (false);
//	}

//	if (theForm.tilt.selectedIndex == 0)
//	{
//	  alert("The first \"Hem Style\" option is not a valid selection.  Please choose one of the other options.");
//	  theForm.tilt.focus();
//	  return (false);
//	}

	if(theForm.headrail != undefined) 
	{
		var blind1=0;
		var blind2=0;
		var blind3=0;
		
		if(theForm.totalwidth != undefined)
		{
			totalwidth=theForm.totalwidth.value*1+theForm.totalwidthfraction.value*1;
			if(theForm.width1 != undefined) {blind1=theForm.width1.value*1+theForm.widthfraction1.value*1}
			if(theForm.width2 != undefined) {blind2=theForm.width2.value*1+theForm.widthfraction2.value*1}
			if(theForm.width3 != undefined) {blind3=theForm.width3.value*1+theForm.widthfraction3.value*1}
			
			widthsum = blind1 + blind2 + blind3;
			if (totalwidth != (widthsum))
			{	
				alert("The total width (" + totalwidth + ") does not equal the sum of each segment (" + widthsum + "), please adjust.");
				return (false);
			}
		}
	}
	return (true);
}

function settilt(theForm,side)
{
  if (side=='L')
	theForm.tilt[1].checked=true;
  else theForm.tilt[0].checked=true;
}

function setlift(theForm,side)
{
  if (side=='L')
	theForm.lift[1].checked=true;
  else theForm.lift[0].checked=true;
}

function settilt2(theForm,side){
if (side=='L')
	theForm.tilt2[1].checked=true;
else theForm.tilt2[0].checked=true;
}

function setlift2(theForm,side){
if (side=='L')
	theForm.lift2[1].checked=true;
else theForm.lift2[0].checked=true;
}

function settilt3(theForm,side){
if (side=='L')
	theForm.tilt3[1].checked=true;
else theForm.tilt3[0].checked=true;
}

function setlift3(theForm,side){
if (side=='L')
	theForm.lift3[1].checked=true;
else theForm.lift3[0].checked=true;
}

function ShippingValidator(theForm)
{
  if (document.frmShipping.ship_to_name.value == "")
  {
    alert("Please enter a value for the \"ship_to_name\" field.");
    document.frmShipping.ship_to_name.focus();
    return (false);
  }

  if (document.frmShipping.ship_to_last_name.value == "")
  {
    alert("Please enter a value for the \"ship_to_last_name\" field.");
    document.frmShipping.ship_to_last_name.focus();
    return (false);
  }

  if (document.frmShipping.ship_to_street.value == "")
  {
    alert("Please enter a value for the \"ship_to_street\" field.");
    document.frmShipping.ship_to_street.focus();
    return (false);
  }

  var s;
  s=document.frmShipping.ship_to_street.value;
  if  (s.match("PO box") ||s.match("po box")||s.match("pobox"))
  {
    alert("We do not ship to PO boxes.");
    document.frmShipping.ship_to_street.focus();
    return (false);

  }

  if (document.frmShipping.ship_to_city.value == "")
  {
    alert("Please enter a value for the \"ship_to_city\" field.");
    document.frmShipping.ship_to_city.focus();
    return (false);
  }

    if (document.frmShipping.ship_to_state.selectedIndex < 0)
  {
    alert("Please select one of the \"State\" options.");
    document.frmShipping.ship_to_state.focus();
    return (false);
  }

  if (document.frmShipping.ship_to_zip.value == "" || !isValidZipCode(document.frmShipping.ship_to_zip.value))
  {
    alert("Please enter a value for the \"ship_to_zip\" field.");
    document.frmShipping.ship_to_zip.focus();
    return (false);
  }

  if (document.frmShipping.ship_to_phone.value == "")
  {
    alert("Please enter a value for the \"ship_to_day_phone\" field.");
    document.frmShipping.ship_to_phone.focus();
    return (false);
  }
  
  if (!isValidPhoneNumber(document.frmShipping.ship_to_phone.value))
  {
    alert("Please enter your valid phone number");
    document.frmShipping.ship_to_phone.focus();
    return (false);
  }

  if (document.frmShipping.ship_to_phone2.value == "")
  {
    alert("Please enter a value for the \"ship_to_day_phone\" field.");
    document.frmShipping.ship_to_phone2.focus();
    return (false);
  }

  if (!isValidPhoneNumber(document.frmShipping.ship_to_phone2.value))
  {
    alert("Please enter your valid phone number.");
    document.frmShipping.ship_to_phone2.focus();
    return (false);
  }
  
  if (!isEmailAddr(document.frmShipping.ship_to_email.value))
  {
	alert("Please enter your valid email address");
    document.frmShipping.ship_to_email.focus();
    return (false);
  }
  
  if ((document.frmShipping.pswd1.value != "") && (document.frmShipping.pswd1.value.length < 6))
  {
  	 alert("Password must be  minimum of 6 characters.")
	 document.frmShipping.pswd1.value = "";
	 document.frmShipping.pswd2.value = "";
	 document.frmShipping.pswd1.focus();
	 return (false);
  }
  
  if ((document.frmShipping.pswd1.value != "") && (document.frmShipping.pswd1.value != document.frmShipping.pswd2.value))
  {
  		alert("The password entered does not match the re-typed password, please try again.");
		document.frmShipping.pswd2.focus();
		return (false);
  }

  if(!(  document.frmShipping.instructions.value == "" ))
  {
	var x = document.frmShipping.instructions.value
	if( x.length > 499 )
	{
		alert("The length of text in \"special instructions\" fields can not exceed 500 characters.");
		document.frmShipping.instructions.focus()
		return (false);
	}
  }

  document.frmShipping.submit()
  
  return (true);
}


function checkPaymentType(theForm)
{
	var type = "";
	if (theForm.pmtype[0].checked)
		type = "cc";
	else if (theForm.pmtype[1].checked)
		type = "bml";
	else if (theForm.paymeth.value != "")
		type = "other";	
	return type;
}


/*
function ShippingValidator()
{
	if (checkPaymentType() == 0)
	{
		alert("Please select the payment type.");
		return (false);
	}
	else if(checkPaymentType() == 2)
	{
		return passBMLCheck('shipping', 'BML');
	}
	else
	{
		if (document.frmShipping.ship_to_last_name.value == "")
		{
		alert("Please enter a value for the \"ship_to_last_name\" field.");
		document.frmShipping.ship_to_last_name.focus();
		return (false);
		}
		
		if (document.frmShipping.ship_to_street.value == "")
		{
		alert("Please enter a value for the \"ship_to_street\" field.");
		document.frmShipping.ship_to_street.focus();
		return (false);
		}
		
		var s;
		s=document.frmShipping.ship_to_street.value;
		if  (s.match("PO box") ||s.match("po box")||s.match("pobox"))
		{
		alert("We do not ship to PO boxes.");
		document.frmShipping.ship_to_street.focus();
		return (false);
		
		}
		
		if (document.frmShipping.ship_to_city.value == "")
		{
		alert("Please enter a value for the \"ship_to_city\" field.");
		document.frmShipping.ship_to_city.focus();
		return (false);
		}
		
		if (document.frmShipping.ship_to_state.selectedIndex < 0)
		{
		alert("Please select one of the \"State\" options.");
		document.frmShipping.ship_to_state.focus();
		return (false);
		}
		
		if (document.frmShipping.ship_to_zip.value == "" || !isValidZipCode(document.frmShipping.ship_to_zip.value))
		{
		alert("Please enter a value for the \"ship_to_zip\" field.");
		document.frmShipping.ship_to_zip.focus();
		return (false);
		}
		
		if (document.frmShipping.ship_to_phone.value == "")
		{
		alert("Please enter a value for the \"ship_to_day_phone\" field.");
		document.frmShipping.ship_to_phone.focus();
		return (false);
		}
		
		if (!isValidPhoneNumber(document.frmShipping.ship_to_phone.value))
		{
		alert("Please enter your valid phone number");
		document.frmShipping.ship_to_phone.focus();
		return (false);
		}
		
		if (document.frmShipping.ship_to_phone2.value == "")
		{
		alert("Please enter a value for the \"ship_to_day_phone\" field.");
		document.frmShipping.ship_to_phone2.focus();
		return (false);
		}
		
		if (!isValidPhoneNumber(document.frmShipping.ship_to_phone2.value))
		{
		alert("Please enter your valid phone number.");
		document.frmShipping.ship_to_phone2.focus();
		return (false);
		}
		
		if (!isEmailAddr(document.frmShipping.ship_to_email.value))
		{
		alert("Please enter your valid email address");
		document.frmShipping.ship_to_email.focus();
		return (false);
		}
		
		if ((document.frmShipping.pswd1.value != "") && (document.frmShipping.pswd1.value.length < 6))
		{
		alert("Password must be  minimum of 6 characters.")
		document.frmShipping.pswd1.value = "";
		document.frmShipping.pswd2.value = "";
		document.frmShipping.pswd1.focus();
		return (false);
		}
		
		if ((document.frmShipping.pswd1.value != "") && (document.frmShipping.pswd1.value != document.frmShipping.pswd2.value))
		{
		alert("The password entered does not match the re-typed password, please try again.");
		document.frmShipping.pswd2.focus();
		return (false);
		}
		
		if(!(  document.frmShipping.instructions.value == "" ))
		{
		var x = document.frmShipping.instructions.value
		if( x.length > 499 )
		{
		alert("The length of text in \"special instructions\" fields can not exceed 500 characters.");
		document.frmShipping.instructions.focus()
		return (false);
		}
		}
		
		document.frmShipping.submit()
		return (true);
	}
}
*/

function isValidPhoneNumber(val)
{
	var validChars = "0123456789()- ";
	var numChars = "0123456789";
	var isNum = true;
    var aChar;
	var num = "";

	var i;

 // check the valid chars
    for (i = 0; i < val.length && isNum == true; i++) 
    { 
      aChar = val.charAt(i); 
      if (validChars.indexOf(aChar) == -1) 
         return false;
   }

   // check the number length
    for (i = 0; i < val.length; i++) 
    { 
      aChar = val.charAt(i); 
      if (numChars.indexOf(aChar) > -1) 
         num = num + aChar;
   }
   
   if (num.length != 10) 
   		return false;
	
   return true;
}

function resetbutton()
{
	document.frmShipping.ship_to_name.value = ""
	document.frmShipping.ship_to_street.value = ""
	document.frmShipping.ship_to_city.value = ""
	document.frmShipping.ship_to_state.value = ""
	document.frmShipping.ship_to_zip.value = ""
	document.frmShipping.ship_to_phone.value = ""
	document.frmShipping.ship_to_phone2.value = ""
	document.frmShipping.ship_to_email.value = ""
	document.frmShipping.ship_to_howdidyoufind.value = ""
	document.frmShipping.ship_to_last_name.value = ""

}

function nextbutton()
{
	ShippingValidator()
}

function PaymentValidator(theForm)
{
	var paymentType = checkPaymentType(theForm);

	if (paymentType == "")
	{
		alert("Please select a payment type.");
		return (false);
	}
	/*
	// this else if condition code added for user who clicked the "other payment" first, and than selects the credit card or bml as payment type 
	else if (theForm.paymeth.value != "")
	{
		if (paymentType == "cc")
		{
			if (confirm("You selected 'Other Payment Methods', if you like to use credit card to make payment, please click the Ok button to contiune your credit card checkout, otherwise click Cancel to use the 'Other Payment Methods' to complete your transaction."))
			{
					theForm.paymeth.value = "";
					theForm.pmtype[0].checked = true;
			}
			else
				theForm.pmtype[0].checked = false;
			return false;
		}
		else
		{
			if (paymentType == "bml")
			{
				if (confirm("You selected 'Other Payment Methods', if you like to use Bill Me Later as your payment type, please click the Ok button to contiune your Bill Me Later checkout, otherwise click Cancel to use the 'Other Payment Methods' to complete your transaction."))
				{
					theForm.paymeth.value = "";
					theForm.pmtype[1].checked = true;
				}
				else
					theForm.pmtype[1].checked = true;
				return false;
			}
		}
	}
	*/
	
	if (paymentType == "bml")
	{
		if (theForm.registered_user.value == "" || theForm.registered_user.value == "0")
		{
			if (confirm("You have to create a new account or login if you already have an account first to use the Bill Me Later\u00AE checkout. \nTo continue with Bill Me Later\u00AE, click \"OK\" to login or create a new account.\nClick \"Cancel\" to select another payment option."))
			{
				bmlErrorCode = "16";
				document.PaymentForm.action = "checkout_login.asp?errorCode=" + bmlErrorCode;
				return true;
				//document.PaymentForm.submit();
			}
			else
			{
				document.PaymentForm.pmtype[1].checked = false;   
				return false;    
			}
		}	
	}

	// address cehck
	if (theForm.returning_user.value == "y")
	{
		if (!passReturningUserCheck(theForm, paymentType))
			return false;
	}
	else
	{
		if (!passNewUserCheck(theForm, paymentType))
		return false;
	}		

	if (paymentType == "bml")
	{
		if (!passCertCheck(theForm))
			return false;
		theForm.action = "/bml_application.asp";		  
		return true;
	}

	else 
	{
		if (paymentType == "other")
			;
		else  
		{
			// checks for credit card info
			if (((theForm._cc_number.value == "") || (theForm._cvv.value == "")) && (theForm.paymeth.value == ""))
			{
				alert("Please enter your credit card information or select an alternative payment method.");
				theForm._cc_number.focus();
				return (false);
			}
		}
		if (!passCertCheck(theForm))
			return false;
		if (!submitted)
		{
			submitted = true;
			return (true);
		}
		else
		{
			alert("Order already submitted!");
			return (false);
		}
	}	
}



function passCertCheck(theForm)
{
	if (!theForm.certify.checked)
	{
		alert("Please certify information accuracy.");
		theForm.certify.focus();
		return (false);
	}
	return true;
}
	
/*

function PaymentValidator(theForm)
{
	if (checkPaymentType() == 0)
	{
		alert("Please select the payment type.");
		return (false);
	}
	else if(checkPaymentType() == 2)
	{
		if (!passBMLCheck(theForm, "bml"))
			return false;
		if (!passCertCheck(theForm))
			return false;
		theForm.action = "bml_applicatoin.asp";
		return true;
	}
	else 
	{
		//return passCCCheck(theForm);
			if (theForm.bill_address_from[1].checked)
			{
				if (theForm.bill_to_name.value == "")
				{
					alert("Please enter first name.");
					theForm.bill_to_name.focus();
					return (false);
				}
		
				if (theForm.bill_to_last_name.value == "")
				{
					alert("Please enter last name.");
					theForm.bill_to_last_name.focus();
					return (false);
				}
			
				if (theForm.bill_to_street.value == "")
				{
					alert("Please enter street address.");
					theForm.bill_to_street.focus();
					return (false);
				}
		
				if (theForm.bill_to_city.value == "")
				{
					alert("Please enter city.");
					theForm.bill_to_city.focus();
					return (false);
				}
				
				if (theForm.bill_to_state.selectedIndex <= 0)
				{
					alert("Please select one of the state options.");
					theForm.bill_to_state.focus();
					return (false);
				}
		
				if (theForm.bill_to_zip.value == "" || !isValidZipCode(theForm.bill_to_zip.value))
				{
					alert("Please enter a valid ZIP code.");
					theForm.bill_to_zip.focus();
					return (false);
				}
				// end of for the payment.asp address validation
			}

			// checks for credit card info
			if (((theForm._cc_number.value == "") || (theForm._cvv.value == "")) && (theForm.paymeth.value == ""))
			{
				alert("Please enter your credit card information or select an alternative payment method.");
				theForm._cc_number.focus();
				return (false);
			}
		}
	}
	if (!theForm.certify.checked)
	{
		alert("Please certify information accuracy.");
		theForm.certify.focus();
		return (false);
	}
	
	if (!submitted)
	{
		submitted = true;
		return (true);
	}
	else
	{
		alert('Order already submitted!')
		return (false);
	}
}
*/


function isValidZipCode(zip)
{
	if (zip.length == 5 )
	{
		if ( isNaN(zip.substring(0, 5)) )
			return false;
	}
	else if (zip.length == 10)
	{
		if ( isNaN(zip.substring(0, 5)) )
			return false;
		if ( ! isNaN(zip.substring(5, 6)) )
			return false;
		if ( isNaN(zip.substring(6)) )
			return false;
	}
	else
		return false;
	return true;
}

function isValidStateCode(state)
{
 	var stateCodes = new Array();
    stateCodes[0] =  "AL";
	stateCodes[1] =	"AK"; 
	stateCodes[2] =  "AZ";
	stateCodes[3] =	"AR"; 
	stateCodes[4] =	"CA"; 
	stateCodes[5] =	"CO"; 
	stateCodes[6] =	"CT"; 
	stateCodes[7] =	"DE"; 
	stateCodes[8] =	"DC"; 
	stateCodes[9] =	"FL";
	stateCodes[10] = "GA";
	stateCodes[11] = "HI";
	stateCodes[12] = "ID";
	stateCodes[13] = "IL";
	stateCodes[14] = "IN";
	stateCodes[15] = "IA";
	stateCodes[16] = "KS";
	stateCodes[17] = "KY";
	stateCodes[18] = "LA";
	stateCodes[19] = "ME";
	stateCodes[20] = "MD";
	stateCodes[21] = "MA";
	stateCodes[22] = "MI";
	stateCodes[23] = "MN";
	stateCodes[24] = "MS";
	stateCodes[25] = "MO";
	stateCodes[26] = "MT";
	stateCodes[27] = "NE";
	stateCodes[28] = "NV";
	stateCodes[29] = "NH";
	stateCodes[30] = "NJ";
	stateCodes[31] = "NM";
	stateCodes[32] = "NY";
	stateCodes[33] = "NC";
	stateCodes[34] = "ND";
	stateCodes[35] = "OH";
	stateCodes[36] = "OK";
	stateCodes[37] = "OR";
	stateCodes[38] = "PA";
	stateCodes[39] = "RI";
	stateCodes[40] = "SC";
	stateCodes[41] = "SD";
	stateCodes[42] = "TN";
	stateCodes[43] = "TX";
	stateCodes[44] = "UT";
	stateCodes[45] = "VT";
	stateCodes[46] = "VA";
	stateCodes[47] = "WA";
	stateCodes[48] = "WV";
	stateCodes[49] = "WI";
	stateCodes[50] = "WY";

	for (i = 0; i < stateCodes.length; i++) 
	{
	 	if (stateCodes[i] == state.toUpperCase())
			return true; 
	}
	return false;
}

function daysInMonth()
{
	var i = 1;
	this[i++] = 31;
	this[i++] = 29;
	this[i++] = 31;
	this[i++] = 30;
	this[i++] = 31;
	this[i++] = 30;
	this[i++] = 31;
	this[i++] = 31;
	this[i++] = 30;
	this[i++] = 31;
	this[i++] = 30;
	this[i  ] = 31;
}

function BmlDataValidator(theForm)
{
	if (theForm.m.value == "0"  || theForm.d.value == "0"  || theForm.y.value == "0")
	{
		alert("You entered an invalid date, please correct your date of birth");
		if (theForm.m.value == "0")
			theForm.m.focus();
		else if (theForm.d.value == "0")
			theForm.d.focus();
		else
			theForm.y.focus();
		return false;
	}

	var dd = parseInt(theForm.d.value, 10);
	var mm = parseInt(theForm.m.value, 10);
	var yyyy = parseInt(theForm.y.value, 10);

	var dinm = new daysInMonth();
	if (dinm[mm] < dd || dd < 1)
	{
		alert("You entered an invalid date, please correct your date of birth");
		theForm.m.focus();
		return false;
	}

	var today = new Date();
	var year = today.getFullYear();	
	var mon  = today.getMonth() + 1;
	var day  = today.getDate();
	if (mm == 2 && dd == 29)
	{
		if (!(((yyyy % 4 == 0) && (yyyy % 100 != 0)) || (yyyy % 400 == 0)))
		{
			alert("The year " + yyyy + " is not a leap year.\nPlease check the date.");
			theForm.m.focus();
			return false;
		}
	}

	var age = year - yyyy;
	if ((mm > mon) || (mm == mon && day < dd))
		age --;

	if (age < 18)
	{
	    alert("The date of birth you entered indicates that you are less than 18 years of age. Please correct your date of birth or select another payment option");
		theForm.m.focus();
		return false;
	}

	theForm.dob.value = "" + theForm.y.value + theForm.m.value + theForm.d.value;

	if ((theForm.ssn.value == "") || (theForm.ssn.value.length != 4) || (!isNumber(theForm.ssn.value)))
	{
		alert("Please enter your last 4 digits of your social security number.");
		theForm.ssn.focus();
		return false;
	}
	
	if (!theForm.agreement.checked)
	{
		alert("Please check the agreement check box to complete your Bill Me Later order"); 
		theForm.agreement.focus();
		return false;
	}
	
	return true;
}

function isNumber(val)
{
	var validChars = "0123456789";
	var isNum = true;
    var aChar;

	var i;
   for (i = 0; i < val.length && isNum == true; i++) 
   { 
      aChar = val.charAt(i); 
      if (validChars.indexOf(aChar) == -1) 
         isNum = false;
   } 
   return isNum;
}

function swatch(isku)
{
		var url = "xt_orderform_addSwatch.asp?sku=" + isku;
		var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=yes,height=200,width=440,scrollbars=yes,top=0,left=0";

		wind = window.open ("","Swatch",opts);
		wind.focus();
		wind.location = url;
}
	  
function li(iSku, productSku, dept_id)
{
	var url = "swatch_popup.asp?swatchSku=" + iSku + "&productSku=" + productSku + "&dept_id=" + dept_id;
	var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=yes,height=530,width=575,scrollbars=yes,top=0,left=0";

	closer = window.open("", "LargePicture", opts);
	closer.focus();
	closer.location = url;
}

function postBack(iSku, productSku, tapeID, dept_id)
{
	var url = "swatch_popup.asp?swatchSku=" + iSku + "&productSku=" + productSku + "&tapeID=" + tapeID + "&dept_id=" + dept_id;
	var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=yes,height=530,width=575,scrollbars=yes,top=0,left=0";

	closer = window.open("", "LargePicture", opts);
	closer.focus();
	closer.location = url;
}

function pricemultiple(productSku)
{
	var url = "price_multiple.asp?sku=" + productSku;
	var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=no,height=460,width=475,scrollbars=no,top=0,left=0";

	closer = window.open("", "PriceMultiple", opts);
	closer.focus();
	closer.location = url;
}

function QQValidator(theForm)
{
	if (theForm.dept_id.selectedIndex < 1)
	{
		alert("Please select a product category.");
		theForm.dept_id.focus();
		return (false);
	}
}

function parseValue(strTapeID, dept_id)
{
	var objDropDown;
	if (document.getElementById)
	{
		objDropDown = document.getElementById("viewbyname");
	}
	else if (document.all)
	{
		objDropDown = document.all['viewbyname'];
	}
	
	var valuePair = objDropDown[objDropDown.selectedIndex].value;
	if (valuePair.toLowerCase() != 'noaction')
	{
		var individualValues = valuePair.split("|");
		postBack(individualValues[0], individualValues[1], strTapeID, dept_id);
		return true;
	}
	return false;
}

function parseTapeCloth(strSwatchSKU, strProductSKU, dept_id)
{
	var objDropDown
	if (document.getElementById)
	{
		objDropDown = document.getElementById("viewbytapes");
	}
	else if (document.all)
	{
		objDropDown = document.all['viewbytapes'];
	}
	
	var tapeValue = objDropDown[objDropDown.selectedIndex].value;
	if (tapeValue.toLowerCase() == 'noaction')
	{
		tapeValue = '';
	}
	postBack(strSwatchSKU, strProductSKU, tapeValue, dept_id);
}

function analink(a, tags)
{
	a.href += ((a.href.indexOf('?')>0)?"&":"?") + tags;
}

function clearDefault(el)
{
	if (el.defaultValue == el.value)
	{
		el.value = "";
	}
}

function checkPanelTrack(thisForm)
{
	if (thisForm.dept_id.value == '29000000')
	{
		if ((thisForm.pw.value) * 1 < 48)
		{
			var obj = thisForm.pw;
			for (var i = 0; i < obj.length; ++i)
			{
				if (obj[i].value == '48')
					obj.selectedIndex = i;
			}
		}
	}
	else
	{
		if ((thisForm.pw.value) * 1 == 48)
		{
			var obj = thisForm.pw;
			for (var i = 0; i < obj.length; ++i)
			{
				if (obj[i].value == '24')
					obj.selectedIndex = i;
			}
		}
	}
}

function infoWindow(url)
{
	var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=yes,height=530,width=575,scrollbars=yes,top=0,left=0";

 	closer = window.open("", "LargePicture", opts);
	closer.focus();
	closer.location = url;
}

function markBillingShippingEditable()
{
	markShippingEditable();
	markBillingEditable();
	document.PaymentForm.sfname.focus();
}

function markShippingEditable()
{
		document.getElementById("editMsg").innerHTML = "Edits made here will be saved to your account for future visits.";
		
		document.getElementById("sfname").readOnly = '';
		document.getElementById("sfname").className = "inputE";
		
		document.getElementById("slname").readOnly = '';
		document.getElementById("slname").className = "inputE";
		
		document.getElementById("sfname").readOnly = '';
		document.getElementById("sfname").className = "inputE";
		
		document.getElementById("sstreet").readOnly = '';
		document.getElementById("sstreet").className = "inputE";
		
		document.getElementById("sstreet2").readOnly = '';
		document.getElementById("sstreet2").className = "inputE";
		
		document.getElementById("scompany").readOnly = '';
		document.getElementById("scompany").className = "inputE";
		
		document.getElementById("scity").readOnly = '';
		document.getElementById("scity").className = "inputE";
		
		document.getElementById("sstate").readOnly = '';
		document.getElementById("sstate").className = "inputE";
		
		document.getElementById("szip").readOnly = '';
		document.getElementById("szip").className = "inputE";
		
		//document.getElementById("scountry").readOnly = '';
		//document.getElementById("scountry").className = "inputE";
		
		document.getElementById("sphone").readOnly = '';
		document.getElementById("sphone").className = "inputE";
		
		document.getElementById("sphone2").readOnly = '';
		document.getElementById("sphone2").className = "inputE";
		
		document.getElementById("semail").readOnly = '';
		document.getElementById("semail").className = "inputE";
		
	    document.PaymentForm.sfname.focus();
}
	
function markBillingEditable()
{
		document.getElementById("editMsg").innerHTML = "Edits made here will be saved to your account for future visits.";
				
		document.getElementById("bfname").readOnly = '';
		document.getElementById("bfname").className = "inputE";
		
		document.getElementById("blname").readOnly = '';
		document.getElementById("blname").className = "inputE";
		
		document.getElementById("bfname").readOnly = '';
		document.getElementById("bfname").className = "inputE";
		
		document.getElementById("bstreet").readOnly = '';
		document.getElementById("bstreet").className = "inputE";
		
		document.getElementById("bstreet2").readOnly = '';
		document.getElementById("bstreet2").className = "inputE";
		
		document.getElementById("bcompany").readOnly = '';
		document.getElementById("bcompany").className = "inputE";
		
		document.getElementById("bcity").readOnly = '';
		document.getElementById("bcity").className = "inputE";
		
		document.getElementById("bstate").readOnly = '';
		document.getElementById("bstate").className = "inputE";
		
		document.getElementById("bzip").readOnly = '';
		document.getElementById("bzip").className = "inputE";
		
		document.getElementById("bphone").readOnly = '';
		document.getElementById("bphone").className = "inputE";
		
		document.getElementById("bphone2").readOnly = '';
		document.getElementById("bphone2").className = "inputE";
		
		document.getElementById("bemail").readOnly = '';
		document.getElementById("bemail").className = "inputE";
		document.PaymentForm.bfname.focus();
}
	
function VerifyCallCenter(thisForm)
{
	if (!isEmailAddr(thisForm.user.value))
	{
		alert("Please enter your valid email address");
		thisForm.user.value = '';
		thisForm.user.focus();
		return false;
	}
	else if (thisForm.pswd.value == "" )
	{
		alert("Please enter your password");
		thisForm.pswd.focus();
		return false;
	}
	return true;
}

function sameAsShipping(thisForm)
{
	document.getElementById("bfname").value = document.getElementById("sfname").value;
	document.getElementById("blname").value = document.getElementById("slname").value;
	document.getElementById("bcompany").value = document.getElementById("scompany").value;
	document.getElementById("bstreet").value = document.getElementById("sstreet").value;
	document.getElementById("bcity").value = document.getElementById("scity").value;
	document.getElementById("bstate").value = document.getElementById("sstate").value;
	document.getElementById("bzip").value = document.getElementById("szip").value;
	document.getElementById("bphone").value = document.getElementById("sphone").value;
	document.getElementById("bphone2").value = document.getElementById("sphone2").value;
	document.getElementById("bemail").value = document.getElementById("semail").value;
}


function passReturningUserCheck(theForm, whichOne)
{
	// shipping check
	var whichAddr = "shipping";
	var isReturningUser = true;
	if (!addressValidator(theForm.ship_to_name, whichAddr, whichOne, "First Name", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_last_name, whichAddr, whichOne, "Last Name", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_street, whichAddr, whichOne, "street", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_city, whichAddr, whichOne, "city", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_state, whichAddr, whichOne, "state", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_zip, whichAddr, whichOne, "zip", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_phone, whichAddr, whichOne, "phone", isReturningUser))
		return false;
	if (!addressValidator(theForm.ship_to_phone2, whichAddr, whichOne, "phone2", isReturningUser))
		return false;
	// ship_to_email is optional for returning user
	if (theForm.ship_to_email.value != "")
	{
		if (!addressValidator(theForm.ship_to_email, whichAddr, whichOne, "email", isReturningUser))
			return false;
	}
	
	// billing check
	whichAddr = "billing";
	if (!addressValidator(theForm.bill_to_name, whichAddr, whichOne, "First Name", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_last_name, whichAddr, whichOne, "Last Name", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_street, whichAddr, whichOne, "street", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_city, whichAddr, whichOne, "city", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_state, whichAddr, whichOne, "state", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_zip, whichAddr, whichOne, "zip", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_phone, whichAddr, whichOne, "phone", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_phone2, whichAddr, whichOne, "phone2", isReturningUser))
		return false;
	if (!addressValidator(theForm.bill_to_email, whichAddr, whichOne, "email", isReturningUser))
		return false;
		
	return true;
}


function passNewUserCheck(theForm, whichOne)
{
	var whichAddr = "billing";
	var isReturningUser = false;
	var obj;

	// billing check
   if (theForm.bill_address_from[1].checked) 
   {	
   		if (!addressValidator(theForm.bill_to_name, whichAddr, whichOne, "First Name", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_to_last_name, whichAddr, whichOne, "Last Name", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_to_street, whichAddr, whichOne, "street", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_to_city, whichAddr, whichOne, "city", isReturningUser))
			return false;
		
		if (!addressValidator(theForm.bill_to_state, whichAddr, whichOne, "state", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_to_zip, whichAddr, whichOne, "zip", isReturningUser))
			return false;
		if (whichOne == "bml")
		{
			// because we don't have the ui for phone and email, we need to read it from shipping
			if (!addressValidator(theForm.bill_ship_to_phone, whichAddr, whichOne, "phone", isReturningUser))
				return false;
			if (!addressValidator(theForm.bill_ship_to_phone2, whichAddr, whichOne, "phone2", isReturningUser))
				return false;
			if (!addressValidator(theForm.bill_ship_to_email, whichAddr, whichOne, "email", isReturningUser))
				return false;
		}
	}

	// shipping check. Only required by bml
	whichAddr = "shipping";
	if (whichOne == "bml")
	{
		if (!addressValidator(theForm.bill_ship_to_name, whichAddr, whichOne, "First name", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_ship_to_last_name, whichAddr, whichOne, "Last name", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_ship_to_street, whichAddr, whichOne, "street", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_ship_to_city, whichAddr, whichOne, "city", isReturningUser))
			return false;
		/* no need for checking the state, state code is always get selected
		if (!addressValidator(theForm.bill_ship_to_state, whichAddr, whichOne, "state", isReturningUser))
			return false;
		*/
		if (!addressValidator(theForm.bill_ship_to_zip, whichAddr, whichOne, "zip", isReturningUser))
			return false;
		// because we don't have the ui for phone and email, we need to read it from shipping
		if (!addressValidator(theForm.bill_ship_to_phone, whichAddr, whichOne, "phone", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_ship_to_phone2, whichAddr, whichOne, "phone2", isReturningUser))
			return false;
		if (!addressValidator(theForm.bill_ship_to_email, whichAddr, whichOne, "email", isReturningUser))
			return false;
			
	}  // else if cc checkout, we don't need to checking the shipping address

	return true;
}

	

function addressValidator(obj, whichAddr, payType, whichField, isReturning)
{
	var message = "";
	obj.value = trim(obj.value);
	if (obj.value != "")
	{
		if (payType == "bml")  // extra check for bml
		{
			if (whichField == "First Name")
				message = nameValidatorForBml(obj.value, "First Name");
			else if (whichField == "Last Name")
				message = nameValidatorForBml(obj.value, "Last Name");
			else if (whichField == "street")
			{
				message = streetValidatorForBml(obj.value);
			} // no special checks for city and state, zip, email 
	
			else if (whichField == "phone" || whichField == "phone2")
			{
				if (isValidPhoneNumber(obj.value))
					message = phoneNumberValidatorForBml(obj.value, whichField);
				else
					message = "Please enter valid phone number";
			}
		} // end of extra check for bml

		// we just need regular check for state, zip, email for both payment type
		if (whichField == "state")
		{
			if (isReturning)
			{
				if (!isValidStateCode(obj.value))
					message = "Please enter two letters of valid state code";
			}
			else
			{ 
				if (obj.selectedIndex < 0)  // different ui than the returning user
					message = "Please select one of the \"State\" options.";
			} 
		}
		
		if (whichField == "zip")
		{
			if (!isValidZipCode(obj.value))
				message = "Please enter valid zip code.";
		}
		
		if (whichField == "email")
		{
			if (!isEmailAddr(obj.value))
				message = "Please enter valid email address.";
		}
	}
	else
	{
		message = "Please enter your valid " + whichAddr + " " + whichField + ".";
	}
	
	if (message != "")
	{
		if (isReturning)
		{
			if (whichAddr == "shipping")
				markShippingEditable();
			else
				markBillingEditable();
			alert(message);
			obj.focus();
		}
		else
		{
			if ( (whichAddr == "shipping") || 
			     ((whichAddr == "billing") && (whichField == "phone" || whichField == "phone2" || whichField == "email") ) )   
			{
					if (confirm("Your current information does not meet Bill Me Later\u00AE\'s requirements for processing your order.\n" + message + "\nTo continue with Bill Me Later\u00AE, click \"OK\" to update your information.\nClick \"Cancel\" to select another payment option."))
					{
						document.PaymentForm.action = "shipping.asp?errorCode=" + bmlErrorCode;
						document.PaymentForm.submit();
					}
					else
					{
						//document.PaymentForm.pmtype[0].checked = true;  should have none of the payment type checked
						document.PaymentForm.pmtype[1].checked = false;
					}
			}
			else
			{
				alert(message);
				obj.focus();
			}
		}
		return false;
	}
	else
		return true;
}


function nameValidatorForBml(name, whichName)
{
	var errorMessage = '';
     //Length check
     if (name.length < 2)
	 {
		errorMessage = "Empty or single character in " +  whichName + " is not acceptable to Bill Me Later\u00AE. Please write full name.";
	 	if (whichName == "First Name")
			bmlErrorCode = "1";
		else
			bmlErrorCode = "2";
	}
	
	 //Vowel Check
	 if (name.length >= 3)
     {
		if (name.search(/[aeiouy]/gi) == -1)       //trailing i after final slash is for case-insensitive search and g for global search
			errorMessage = whichName + " with three or more letters must contain a vowel - A, E, I, O, U, Y. Please correct the name.";
		if (whichName == "First Name")
			bmlErrorCode = "3";
		else
			bmlErrorCode = "4";
	 }
	 
	 //Special Characters Check
	 if (whichName == "First Name")
	 {
	 	if (name.search(/[\^\$\*\+\?`~!@#%(){}\[\]_=<>\/\\.']/g) != -1)   //dash - is allowed but not dot . and apostrophe ' in first name
		{
			 errorMessage = "First Name contains special characters that are not allowed. Please correct the name.";
			 bmlErrorCode = "5";
		}
	 }
	 else
	 {
	 	if (name.search(/[\^\$\*\+\?`~!@#%(){}\[\]_=<>\/\\-]/g) != -1)  //dash - is not allowed dot . and apostrophe are in last name
		{
			errorMessage = "Last Name contains special characters that are not allowed. Please correct the name.";
			bmlErrorCode = "6";
		}
	 }  
	                
	 //Special Words Check
	 if (whichName == "Last Name")
	 {
		 if ((name.toLowerCase().indexOf(" inc") >= 0)|| (name.toLowerCase().indexOf(" incorporated") >= 0)
      	     || (name.toLowerCase().indexOf(" corp") >= 0) || (name.toLowerCase().indexOf(" corporation") >= 0)
			 || (name.toLowerCase().indexOf(" llc") >= 0))
			 
		{
			errorMessage = "Last Name cannot contain words like Inc, Incorporation, Corp, Corporation, LLC. Please correct the name.";
			bmlErrorCode = "7";
		}
	 }
	  //Let's not put the check for first and last name combined with word "and"
	 
	 return errorMessage;
}


function streetValidatorForBml(street)
{

	var errorMessage = "";
	if (!(street.search(/[a-z]/gi) >= 0 && street.search(/[0-9]/) >= 0) )
	{
		 errorMessage = "Address is not correct. Please correct the address."; 
		 bmlErrorCode = "8";
	}
	return errorMessage;
}

function phoneNumberValidatorForBml(phone, whichField)
{

	var errorMessage = "";
	var areaCode = phone.substring(0, 3);
	var whichPhone = "";
	if (whichField == "phone")
		whichPhone = "Daytime Phone";
	else
		whichPhone = "Evening Phone";

	phone = phone.replace(/[-,(,), ]/g,""); // converting phone number to number only

	if (areaCode == "200" || areaCode == "900" || areaCode == "666")
	{
		errorMessage = "The area code in your " + whichPhone + " is invalid to Bill Me Later\u00AE.  Plase enter your valid phone number.";
		if (whichField == "phone")
			bmlErrorCode = "11";
		else
			bmlErrorCode = "12";
	}

	else if (areaCode == "800" || areaCode == "811" || areaCode == "822" || areaCode == "833" || areaCode == "844" || areaCode == "855"
	         || areaCode == "866" || areaCode == "877" || areaCode == "888" || areaCode == "899")
	{
		errorMessage = "The area code in your " + whichPhone + " can not be a toll free area code. Please enter your valid phone number.";
		if (whichField == "phone")
			bmlErrorCode = "13";
		else
			bmlErrorCode = "14";
	}
	else if (phone.substring(3, 6) == "555" || phone.substring(3, 4) == "1")
	{
		errorMessage = "The " + whichPhone + " you entered is not valid to Bill Me Later\u00AE. Please enter your valid phone number.";
		if (whichField == "phone")
			bmlErrorCode = "9";
		else
			bmlErrorCode = "10";
	}
	else
	{
		// check to see if the last 7 digits are identical
		var lastSevenDigits = phone.substring(3, 10);
		var identical = true;
		var firstChar = lastSevenDigits.charAt(0);
		var i;
		
		for (i = 1; i < lastSevenDigits.length; i++)
		{
			if (lastSevenDigits.charAt(i) != firstChar)
			{
				identical = false;
				break;
			}
		}
	
		 if (identical)
		 {
			errorMessage = "The " + whichPhone + " you entered is not valid to Bill Me Later\u00AE. Please enter your valid phone number.";
			if (whichField == "phone")
				bmlErrorCode = "9";
			else
				bmlErrorCode = "10";
		}
	}
	
	return errorMessage;
}


function verifyPaymentType(theForm)
{
	if (theForm.paymeth.value != "")
	{
		if (theForm.pmtype[0].checked)
		{
			if (confirm("You selected 'Other Payment Methods', if you like to use credit card to make payment, please click the Ok button to contiune your credit card checkout, otherwise click Cancel button to use the 'Other Payment Methods' to complete your transaction."))
					theForm.paymeth.value = "";
			else
				theForm.pmtype[0].checked = false;
			return false;
		}
		else if (theForm.pmtype[1].checked)
		{
			if (confirm("You selected 'Other Payment Methods', if you like to use Bill Me Later as your payment type, please click the Ok button to contiune your Bill Me Later checkout, otherwise click Cancel button to use the 'Other Payment Methods' to complete your transaction."))
				theForm.paymeth.value = "";
			else
				theForm.pmtype[1].checked = false;
			return false;
		}
	}
}


function LTrim( value ) 
{		
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim( value ) 
{
	return LTrim(RTrim(value));
}

function selectSpShip(theForm, txt)
{
	theForm.spShip.value = txt;
	theForm.action = "payment_returning_user.asp";
	theForm.submit();
}

function LogEmailSignup(thisForm)
{
	var strEmail = document.forms[thisForm].email.value;
	if (!isEmailAddr(strEmail))
	{
		alert("Please enter a valid e-mail address in the form: yourname@yourdomain.comm");
		document.forms[thisForm].email.focus();
	}
	else
	{
		var emailParams = 'email=' + strEmail + '&source=' + document.forms[thisForm].source.value;
		emailPost('xt_newsletter.asp', emailParams);
	}
}

function emailPost(url, parameters)
{
	var emailHttp = getEmailHttp();
	if (emailHttp == null)
		alert('Sorry we could not complete the resigtration.\nPlease try again.');
	
	emailHttp.onreadystatechange=function()
    {
      if(emailHttp.readyState == 4 || emailHttp.readyState == 'complete')
	  {
			switch(emailHttp.responseText)
			{
				case '0':
					alert('Sorry we could not complete the resigtration.\nPlease try again.');
					break;
				case '1':
					alert('Thank you for your submission!\nYou are now registered to receive our special offer newletters.');
					break;
				case '2':
					alert('You have already registered your e-mail address with us.');
			}
		}
	}
    emailHttp.open("POST", url,true);
	emailHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    emailHttp.setRequestHeader("Content-length", parameters.length);
    emailHttp.setRequestHeader("Connection", "close");
    emailHttp.send(parameters);
}

function getEmailHttp()
{
	var xmlHttp;
	try { // ff
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {  // ie
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {  
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browsr does not support AJAX");
				return null;
			}
		}
	}
	return xmlHttp;
}