function Redirect(page){
	document.location = page + location.search;
}

function FocusNext(obj) {
	if (obj.value.length == 4)
		for (var i=0;i<obj.form.elements.length;i++)
			if (obj.form.elements[i].name == obj.name) {
				obj.form.elements[i+1].focus();
				break;		
			}
	
	
}

function jumpNext(obj) {
	if (obj.form.CCTYPE.options[obj.form.CCTYPE.options.selectedIndex].value == 'ZC') {
		obj.form.check_num.focus();
		obj.form.rout_num.tabIndex = obj.form.check_num.tabIndex + 1;
		obj.form.acc_num.tabIndex = obj.form.rout_num.tabIndex + 1;
	} else {
		if (obj.form.check_num)	{
			obj.form.check_num.tabIndex = 701;
			obj.form.rout_num.tabIndex = 702;
			obj.form.acc_num.tabIndex = 703;
		}
		
		obj.form.cc1.focus();
		obj.form.cc1.tabIndex = obj.form.CCTYPE.tabIndex + 1;
		obj.form.cc2.tabIndex = obj.form.cc1.tabIndex + 1;
		obj.form.cc3.tabIndex = obj.form.cc2.tabIndex + 1;
		obj.form.cc4.tabIndex = obj.form.cc3.tabIndex + 1;
		obj.form.EXPMONTH.tabIndex = obj.form.cc4.tabIndex + 1;
		obj.form.EXPYEAR.tabIndex = obj.form.EXPMONTH.tabIndex + 1;
	}
}

function checkForCheckPayment(obj){
	if (obj.form.CCTYPE.options[obj.form.CCTYPE.options.selectedIndex].value == 'ZC') {
		alert("Skipping card information field -- Not necessary for check payments");
		obj.form.check_num.focus();
	}
}

function checkForCCPayment(obj){
	if (obj.form.CCTYPE.options[obj.form.CCTYPE.options.selectedIndex].value != 'ZC') {
		alert("Skipping check information field -- Not necessary for credit card payments");
		obj.form.cc1.focus();
	}
}

function isCC(ccno) {
  if (ccno.length > 16)
    return (false);
  sum = 0; mul = 1; l = ccno.length;
  for (i = 0; i < l; i++) {
    digit = ccno.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
} 

function isVisa(ccno)
{
  if (((ccno.length == 16) || (ccno.length == 13)) &&
      (ccno.substring(0,1) == 4))
    return isCC(ccno);
  return (false);
} 

function isMasterCard(ccno)
{
  firstdig = ccno.substring(0,1);
  seconddig = ccno.substring(1,2);
  if ((ccno.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
    return isCC(ccno);
  return (false);
}

function isAmericanExpress(ccno)
{
  firstdig = ccno.substring(0,1);
  seconddig = ccno.substring(1,2);
  if ((ccno.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCC(ccno);
  return (false);
}  

function isDiscover(ccno) {
  if ((ccno.substring(0,4) == '6011') && (ccno.length == 16)) 
	return isCC(ccno);
  return (false);

}

function isEmail(eobj) 
{

var str=eobj.value;
var filter=/^.+@.+\..{2,4}$/;

return (filter.test(str));


/*
  if ((eobj.value.length < 6 || eobj.value.length > 50 || eobj.value.indexOf('@') < 1 || eobj.value.indexOf('.') < 1 || eobj.value.indexOf('@.') > 0))
	{
	  return (false);
	}
//  else 
//	  if (!syntax_check(eobj,1))
//	{
//	  return (false);
//	}
//  else
	return (true);
*/
}


function check_same(field) {

	if (field.form.shipping_id.options) {
		if (field.form.shipping_id.options.selectedIndex > 3) {
			field.form.shipping_id.options.selectedIndex = 0;
		}
 	}


}



function check_confirm(obj1,obj2){
	if (obj1.value != obj2.value)
		return (false);	
	return (true);
}

function on_billing_change(field) {
	if (field.form.update_billing)
		if (field.form.update_billing.value != "1") {
			alert("The billing information is being modified. It will be saved after your submission.")
			field.form.update_billing.value = "1";
		}
}

function on_shipping_combo_change(thatobj)
{
	var s = thatobj.options.selectedIndex;

	if (s == 0)
		clear_ship_form(thatobj.form)
		

	if (s == 1 || s == 2 || s == 3) {
		alert("The option you selected from the list does not have any function.")
		thatobj.selectedIndex = 0;
		on_shipping_combo_change(thatobj);
	}

	if (s > 3)
	{
		thatobj.form.submission.value = "refresh";
		thatobj.form.action = "checkout_step_1.asp" + location.search;
		thatobj.form.onsubmit = null;
		thatobj.form.submit();
	}
	
}

function clear_ship_form(form_name){
	form_name.NAMETOSHIP.value = '';
	form_name.LASTTOSHIP.value = '';
	form_name.MITOSHIP.value = '';
	form_name.COMPANYTOSHIP.value = '';
	form_name.PHONETOSHIP.value = '';
	form_name.FAXTOSHIP.value = '';
	form_name.EMAILTOSHIP.value = '';
	form_name.ADDRESSTOSHIP.value = '';
	form_name.CITYTOSHIP.value = '';
	form_name.ZIPTOSHIP.value = '';
	set_combo(form_name.STATETOSHIP,0);
	set_combo(form_name.COUNTRYTOSHIP,39);
}

function enable_disable_ship_form(form_name, s) {
	form_name.NAMETOSHIP.disabled = s;
	form_name.LASTTOSHIP.disabled = s;
	form_name.MITOSHIP.disabled = s;
	form_name.COMPANYTOSHIP.disabled = s;
	form_name.PHONETOSHIP.disabled = s;
	form_name.FAXTOSHIP.disabled = s;
	form_name.EMAILTOSHIP.disabled = s;
	form_name.ADDRESSTOSHIP.disabled = s;
	form_name.CITYTOSHIP.disabled = s;
	form_name.ZIPTOSHIP.disabled = s;
	form_name.STATETOSHIP.disabled = s;
	form_name.COUNTRYTOSHIP.disabled = s;
}

function copy_billing_to_shipping(form_name) {
	form_name.NAMETOSHIP.value = form_name.NAME.value;
	form_name.LASTTOSHIP.value = form_name.LAST.value;
	form_name.MITOSHIP.value = form_name.MI.value;
	form_name.COMPANYTOSHIP.value = form_name.COMPANY.value;
	form_name.PHONETOSHIP.value = form_name.PHONE.value;
	form_name.FAXTOSHIP.value = form_name.FAX.value;
	form_name.EMAILTOSHIP.value = form_name.EMAIL.value;
	form_name.ADDRESSTOSHIP.value = form_name.ADDRESS.value;
	form_name.CITYTOSHIP.value = form_name.CITY.value;
	form_name.ZIPTOSHIP.value = form_name.ZIP.value;
	set_combo(form_name.STATETOSHIP, form_name.STATE.selectedIndex);
	set_combo(form_name.COUNTRYTOSHIP, form_name.COUNTRY.selectedIndex);
}




function sameAsBilling(cb){
	if (cb.checked)
		copy_billing_to_shipping(cb.form);

	enable_disable_ship_form(cb.form, cb.checked);

}


function set_combo(comboname,option) {
	comboname.options[option].selected = true;
}

function syntax_check(obj,syntax) {
  var checkOK = new Array();
	checkOK[0] = " 0123456789-()"
	checkOK[1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
	checkOK[2] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.#/\,";
	checkOK[3] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
	checkOK[4] = "0123456789";
	checkOK[5] = "0123456789x";  // For hidden cc fields


  var checkStr = obj.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK[syntax].length;  j++)
	  if (ch == checkOK[syntax].charAt(j))
		break;
	if (j == checkOK[syntax].length)
	{
	  allValid = false;
	  break;
	}
	allNum += ch;
  }
  if (!allValid) return (false) 
	else return (true);
}

function Form_Validator(theForm)
{
	var gocc	= false;
	var today	= new Date();
	month		= today.getMonth() + 1;
	year		= today.getFullYear();
	if (year > 2000)
		year-=2000;		

	
	if (theForm.EMAIL_CONFIRM) {
		if (theForm.EMAIL_CONFIRM.value != '' || theForm.PASSWORD.value != '' || theForm.PASSWORD_CONFIRM.value != '') {

			if (!check_confirm(theForm.EMAIL_CONFIRM,theForm.EMAIL)) {
				alert("The confirmation email does not match to your billing email")
				theForm.EMAIL_CONFIRM.focus();
				return (false);
			}
	
			if (theForm.PASSWORD.value.length > 5 && theForm.PASSWORD.value.length < 21) {
				if (!check_confirm(theForm.PASSWORD,theForm.PASSWORD_CONFIRM)) {
					alert("The passwords you entered are not the same, please reenter your passwords")
					theForm.PASSWORD.focus();
					return (false);
				}
			} else 
				{
					alert("Please provide a 6-20 characters long password.")
					theForm.PASSWORD.focus();
					return (false);
				}
		}
	}
	
	
  if (theForm['NAME']) {
  
	if (theForm['NAME'].value.length < 2)
	{
	  alert("Please enter at least 2 characters for the First Name on the Billing Info Form");
	  theForm['NAME'].focus();
	  return (false);
	}

	if (theForm['NAME'].value.length > 20)
	{
	  alert("We are sorry, we can't store more than 20 characters for the First Name field on the Billing Info Form");
	  theForm['NAME'].focus();
	  return (false);
	}
  
	if (theForm.LAST.value.length < 2)
	{
	  alert("Please enter at least 2 characters for the Last Name field on the Billing Info Form");
	  theForm.LAST.focus();
	  return (false);
	}

	if (theForm.LAST.value.length > 20)
	{
	  alert("We are sorry, we can't store more than 20 characters for the Last Name field on the Billing Info Form");
	  theForm.LAST.focus();
	  return (false);
	}

	if (theForm.PHONE.value.length < 10)
	{
	  alert("Please enter at least 10 digits for the Phone field on the Billing Info Form");
	  theForm.PHONE.focus();
	  return (false);
	}

	if (theForm.PHONE.value.length > 20)
	{
	  alert("We are sorry, we can't store more than 20 digits for the Phone field on the Billing Info Form");
	  theForm.PHONE.focus();
	  return (false);
	}

	if (theForm.ADDRESS.value.length < 5)
	{
	  alert("Please enter your Street Address for your Billing Info");
	  theForm.ADDRESS.focus();
	  return (false);
	}

	if (theForm.ADDRESS.value.length > 60)
	{
	  alert("We are sorry, we can't store more than 60 characters for the Street Address field on the Billing Info Form");
	  theForm.ADDRESS.focus();
	  return (false);
	}

	if ( (theForm.COUNTRY.options[theForm.COUNTRY.options.selectedIndex].value == "US" || theForm.COUNTRY.options[theForm.COUNTRY.options.selectedIndex].value == "CA") && theForm.STATE.options[theForm.STATE.options.selectedIndex].value == "XX")
	{
	  alert("Please select a state for your Billing Info");
	  theForm.STATE.focus();
	  return (false);
	}
  
	if (theForm.CITY.value.length < 3)
	{
	  alert("Please enter your City for your Billing Info");
	  theForm.CITY.focus();
	  return (false);
	}

	if (theForm.CITY.value.length > 30)
	{
	  alert("We are sorry, we can't store more than 30 characters for the Billing City field");
	  theForm.CITY.focus();
	  return (false);
	}

	if (theForm.ZIP.value.length < 5)
	{
	  if (theForm.COUNTRY.options[theForm.COUNTRY.options.selectedIndex].value != "US" && theForm.ZIP.value.length == 4)
	  {
	    //Some countries have 4-digit postal code, that's ok
	  } else {
	    alert("Too few digits/letters for the ZIP/Postal Code in your Billing Info");
	    theForm.ZIP.focus();
	    return (false);
	  }
	}

	if (theForm.ZIP.value.length > 10)
	{
	  alert("We are sorry, we can't store more than 10 digits for the Zip Code field on the Billing Info Form");
	  theForm.ZIP.focus();
	  return (false);
	}

}

if (theForm.EMAIL) {
  if (!isEmail(theForm.EMAIL))
  {
	alert("Please provide a valid e-mail address on your Billing Info Form (valid address example: yourname@yourwebsite.com)");
    theForm.EMAIL.focus();
    return (false);
  }
}



	if (theForm.NAMETOSHIP) {
		var ship_info = theForm.NAMETOSHIP.value + theForm.LASTTOSHIP.value + theForm.EMAILTOSHIP.value + theForm.PHONETOSHIP.value + theForm.ADDRESSTOSHIP.value + theForm.CITYTOSHIP.value + theForm.STATETOSHIP.options[theForm.STATETOSHIP.options.selectedIndex].value + theForm.COUNTRYTOSHIP.options[theForm.COUNTRYTOSHIP.options.selectedIndex].value + theForm.ZIPTOSHIP.value;
		if (ship_info.replace(' ','') != 'XXUS') {
			theForm.shipping_changed.value = '0';
		   if (theForm.NAMETOSHIP.value.length < 2 || theForm.NAMETOSHIP.value.length > 20)
			{
			  alert("Please enter your first name for the shipping (2-20 characters long)");
			  theForm.NAMETOSHIP.focus();
			  return (false);
			}

			if (theForm.LASTTOSHIP.value.length < 2 || theForm.LASTTOSHIP.value.length > 20)
			{
			  alert("Please enter your last name for the shipping (2-20 characters long)");
			  theForm.LASTTOSHIP.focus();
			  return (false);
			}

			if (theForm.EMAILTOSHIP) {
			  if (!isEmail(theForm.EMAILTOSHIP))
			  {
				alert("Please provide a valid email address for the shipping (valid address example: yourname@yourwebsite.com)");
				theForm.EMAILTOSHIP.focus();
				return (false);
			  }
			}
		  
			if (theForm.PHONETOSHIP.value.length < 10 || theForm.PHONETOSHIP.value.length > 20)
			{
			  alert("Please enter your phone number (10-20 characters long)");
			  theForm.PHONETOSHIP.focus();
			  return (false);
			}

			if (theForm.ADDRESSTOSHIP.value.length < 5 || theForm.ADDRESSTOSHIP.value.length > 60)
			{
			  alert("Please enter your street address for the shipping (5-60 characters long)");
			  theForm.ADDRESSTOSHIP.focus();
			  return (false);
			}

			if ( (theForm.COUNTRYTOSHIP.options[theForm.COUNTRYTOSHIP.options.selectedIndex].value == "US" || theForm.COUNTRYTOSHIP.options[theForm.COUNTRYTOSHIP.options.selectedIndex].value == "CA") && theForm.STATETOSHIP.options[theForm.STATETOSHIP.options.selectedIndex].value == "XX")
			{
			  alert("Please select a state for the shipping");
			  theForm.STATETOSHIP.focus();
			  return (false);
			}
		  
			if (theForm.CITYTOSHIP.value.length < 3 || theForm.CITYTOSHIP.value.length > 30)
			{
			  alert("Please enter your city for the shipping (3-30 characters long)");
			  theForm.CITYTOSHIP.focus();
			  return (false);
			}

			if (theForm.ZIPTOSHIP.value.length < 5 || theForm.ZIPTOSHIP.value.length > 10)
			{

			  if (theForm.COUNTRYTOSHIP.options[theForm.COUNTRYTOSHIP.options.selectedIndex].value != "US" && theForm.ZIPTOSHIP.value.length == 4)
			  {
			    //Some countries have 4-digit postal code, that's ok
			  } else {
			    alert("Please check your ZIP/postal code in your Shipping info.");
			    theForm.ZIPTOSHIP.focus();
			    return (false);
			  }

			}
		}
	}


  
	if (theForm.CARDNUM) { // Is there a cardum field on the form?
		if (theForm.CCTYPE.options[theForm.CCTYPE.options.selectedIndex].value == 'IN') { // Is the payment type Invoice?
// Following is disabled because it doesn't apply to invoice 12/26/2006
//			if (theForm.check_num.value.length < 1 || theForm.rout_num.value.length != 9 || theForm.acc_num.value.length < 5)
//			  {
//				alert("The check information you entered seems invalid. Please reenter the check number, routing number, and account number as advised.");
//				theForm.CCTYPE.focus();
//				return (false);
//			  }
		} else { // Payment type is Credit Card
			if (theForm.cc1)
				theForm.CARDNUM.value = theForm.cc1.value +''+theForm.cc2.value +''+ theForm.cc3.value +''+ theForm.cc4.value;
	  
			if (theForm.update_cc)
				if (theForm.update_cc.value == '1') // CC is being updated (from myAccount.asp possibly) cc verification will be done
					gocc = true;
				else
					gocc = false;
			else 
				gocc = true;


			if (gocc)
			{

				if (!syntax_check(theForm.CARDNUM,5))
				  {
					alert("Please enter only numbers for the \"Credit Card Number\" field.");
					theForm.CARDNUM.focus();
					return (false);
				  }
		
				if (!((theForm.CCTYPE.options[theForm.CCTYPE.options.selectedIndex].value == 'VS' && isVisa(theForm.CARDNUM.value) && (theForm.cvv.value.length == 3)) || (theForm.CCTYPE.options[theForm.CCTYPE.options.selectedIndex].value == 'MC' && isMasterCard(theForm.CARDNUM.value) && (theForm.cvv.value.length == 3)) || (theForm.CCTYPE.options[theForm.CCTYPE.options.selectedIndex].value == 'AX' && isAmericanExpress(theForm.CARDNUM.value) && (theForm.cvv.value.length == 4)) || (theForm.CCTYPE.options[theForm.CCTYPE.options.selectedIndex].value == 'DS' && isDiscover(theForm.CARDNUM.value) && (theForm.cvv.value.length == 3))))
				  {
					alert("The numbers you entered seems invalid for the " + theForm.CCTYPE.options[theForm.CCTYPE.options.selectedIndex].text + ". \nPlease verify the following:\nCredit Card Type, Account Number, and Security Code.");
					theForm.CCTYPE.focus();
					return (false);
				  }

				if (theForm.EXPYEAR.options.selectedIndex == 0 || theForm.EXPMONTH.options.selectedIndex == 0)
				  {
					alert('Expiration Date cannot be left blank. Please choose the month and the year as written on your credit card. e.g. 05/09 is May 2009');
					theForm.EXPMONTH.focus();
					return (false);
				  }
			
				if ((theForm.EXPYEAR.options[theForm.EXPYEAR.options.selectedIndex].value < year) || (theForm.EXPYEAR.options[theForm.EXPYEAR.options.selectedIndex].value <= year && theForm.EXPMONTH.options[theForm.EXPMONTH.options.selectedIndex].value < month))
				  {
					alert('According to your computer\'s calendar ('+today.toLocaleString()+') your credit card is already expired.');
					theForm.EXPYEAR.focus();
					return (false);
				  }
			} // Go cc
		} // end if cctype (payment type)
	}	// end if cardnum
	
  return (true);

} // end function form_validator

