var sCollection = "";
var sStyle = "";
var sBrowser = "";
if (document.all) {
	sCollection = "all.";
	sStyle = ".style";
	sBrowser = "ie";
} else if (document.layers) {
	sBrowser = "n4";
} else if (document.getElementById) {
	sBrowser = "n6";
	sCollection = "getElementById(";
	sStyle = ").style";
}

function getObject(eName) {
	theObj = (typeof eName == "string") ? eval("document." + sCollection + eName + sStyle) : eName;
	return theObj;
}

// LUHN Formula for validation of credit card numbers.
function isCreditCard(cardNumber) { 
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;
	for( i = 0; i < cardNumber.length; ++i ) {
		ar[i] = parseInt(cardNumber.charAt(i));
	}
	for( i = ar.length -2; i >= 0; i-=2 ) {  // you have to start from the right, and work back.
		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
	}										 // if the double digit is > 9, add those individual digits together 
	for( i = 0; i < ar.length; ++i ) {
		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
	}
	return (((sum%10)==0)?true:false);	  	
}

function validateCreditCard(cardNumber,expMonth, expYear) {
	
		cardNumber = cardNumber.replace(/\ /g,''); //strip spaces from number
		if (arguments.length < 2) {
			alert("arguments length too short");
			return false;
		}
		if ((cardNumber.length < 15)) {
			alert("Credit card number is too short.");
			return false;
		}
		if (expMonth > 12) {
			alert("Month must be 1-12.");
			return false;
		}

		if (expMonth < 1) {
			alert("Month must be 1-12.");
			return false;
		}

		if (expYear < 1) {
			alert("Please select expired year.");
			return false;
		}
		var expYear = "20" + expYear;
		var cardType = "Visa"; // default to visa
		var cc = cardNumber.toString();
		var firstdig = cc.substring(0,1);
		var seconddig = cc.substring(1,2);
		var first4digs = cc.substring(0,4);
		//visa test
		if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4)) {
			cardType = "Visa";
		}
		//master card test
		if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) {
			cardType = "MasterCard";
		}
		//amex card test
		if ((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))) {
			cardType = "American Express";
		}
		//discover card test
		if ((cc.length == 16) && (first4digs == "6011")) {
			cardType = "Discover";
		}
		if (cardType == "") {
//					alert("no card type set");
			return false;
		}
// uncomment the switch statement below  to test with false card #'s.
/*

		switch (cardType) {
			case "Visa":
				cardNumber = "4111111111111111";
			break;
			case "MasterCard":
				cardNumber = "5555555555554444";
			break;
			case "American Express":
				cardNumber = "378282246310005";
			break;
			case "Discover":
				cardNumber = "6011111111111117";
			break;
			default:
			break;
		}
*/
		if (!isCreditCard(cardNumber)) {
			alert("The credit card entered is not valid.");
			return false;
		}
		return true;
}

function chkPhone(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0) {return;}
	}
	
	var len = field.value.length;	
	var value = field.value;
	var valid = "1234567890"
	var tmp = "";
	for (var x=0; x<len; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && x < 12) {tmp += value.charAt(x);}
	}
	field.value = tmp;
	
	if (len == field.maxLength) {
		field.form[(getIndex(field)+1) % field.form.length].focus();
	}
}

function chkFld(field, desc, minLen) {

	if (field.type == "text") {
		fieldVal = field.value;
		if (fieldVal.length < minLen) {
			alert("Please fill in the \"" + desc + "\" field.");
			field.focus();
			return false;}
	}

	if (field.type == "select-one") {

		if (field[field.selectedIndex].value == "#" || field[field.selectedIndex].value == "00" ) {
			alert("Please select a value in the \"" + desc + "\" field.");
			field.focus();
			return false;}
	}
	return true;
}

function chkEmail(field) {
  var str = field.value;        
  var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;

  if (field.value == "") {
    alert("Please enter your email address.");
    field.focus();
    return false;
  }
  if (re.test(str)) {
    return true;
  }
  alert("\"" + str + "\" is an invalid email address!");
  field.select();
  field.focus();
  return false;
}

function isNumeric(field, desc){
	var fieldVal = field.value;
	var len = fieldVal.length;
	
	var valid = "1234567890"
	var tmp = "";
	var bValid = true;
	for (var x=0; x<len; x++) {
		if (valid.indexOf(fieldVal.charAt(x)) == -1) {bValid = false;}
	}
	if (!bValid) {
		alert(desc + " must be numeric.");
		return false;
	} else {
		return true;
	}
}

function isValueNumeric(text, desc){
	var fieldVal = text;
	var len = fieldVal.length;
	
	var valid = "1234567890"
	var tmp = "";
	var bValid = true;
	for (var x=0; x<len; x++) {
		if (valid.indexOf(fieldVal.charAt(x)) == -1) {bValid = false;}
	}
	if (!bValid) {
		return false;
	} else {
		return true;
	}
}


function isZipOrPhone(field, desc){
	var fieldVal = field.value;
	var len = fieldVal.length;	

	var valid = "1234567890-"
	var tmp = "";
	var bValid = true;
	for (var x=0; x<len; x++) {
		if (valid.indexOf(fieldVal.charAt(x)) == -1) {bValid = false;}
	}
	if (!bValid) {
		alert(desc + " must be numeric.");
		return false;
	} else {
		return true;
	}
}


function IsNumericBLN(objField) {
// Validates numeric input.
//  May 7, 2000 - C. Wilson

	// INITIALIZE procedure-scope variables.
	var blnNumeric = true
	
	// SCAN string for non-numeric characters.
	for (var i = 0; i < objField.value.length; i++) {
	
		var strDigit = objField.value.charAt(i)
	
		if (strDigit < "0" || strDigit > "9") {	//non-numeric character has been found.
			blnNumeric = false
			break
		}
	}
	
	// ASSIGN return value.
	return blnNumeric
	
}



function validateUserSignUp1(cntl) { 	
	with(cntl) {
	

	if (!chkFld(billToFName, "Billing First Name", 1)) {return false;}
	if (!chkFld(billToLName, "Billing Last Name", 1)) {return false;}
	if (!chkEmail(billToEmail)) {return false;}

	if (!chkFld(billToStreet1, "Billing Address 1", 1)) {return false;}
	if (!chkFld(billToCity, "Billing City", 1)) {return false;}
	//if (billToCountry.value == 'US' || billToCountry.value == 'CA') 
	if (!chkFld(billToState, "Billing State", 1)) {return false;}	
	if (!chkFld(billToPostal, "Billing Postal Code", 5)) {return false;}
	if (!isZipOrPhone(billToPostal, "Postal Code")) {return false;}



					// VALIDATE Daytime phone number.
					if ((!IsNumericBLN(billToDayPhoneAC) || (billToDayPhoneAC.value.length != 3) || 
						!IsNumericBLN(billToDayPhonePRE) || (billToDayPhonePRE.value.length != 3) || 
						!IsNumericBLN(billToDayPhoneSUF) || (billToDayPhoneSUF.value.length != 4))) {
						alert("Please enter a valid Daytime Phone Number.");
						billToDayPhoneAC.select()
						billToDayPhoneAC.focus()
						return false
					}

// VALIDATE day time telephone extension.
					if (!IsNumericBLN(billToDayPhoneEXT) && (billToDayPhoneEXT.value.length <= 10)) {
						alert("Please enter a valid numeric Daytime Phone Extension.");
						billToDayPhoneEXT.select()
						billToDayPhoneEXT.focus()
						return false
					}


					// VALIDATE evening phone number.
					if ((!IsNumericBLN(billToEveningPhoneAC) || (billToEveningPhoneAC.value.length != 3) || 
						!IsNumericBLN(billToEveningPhonePRE) || (billToEveningPhonePRE.value.length != 3) || 
						!IsNumericBLN(billToEveningPhoneSUF) || (billToEveningPhoneSUF.value.length != 4)) && 
						!((billToEveningPhoneAC.value.length < 1) && (billToEveningPhonePRE.value.length < 1) && (billToEveningPhoneSUF.value.length < 1))) {
						alert("Please enter a valid Eveningtime Phone Number.");
						billToEveningPhoneAC.select()
						billToEveningPhoneAC.focus()
						return false
					}

// VALIDATE evening telephone extension.
					if (!IsNumericBLN(billToEveningPhoneEXT) && (billToEveningPhoneEXT.value.length <= 10)) {
						alert("Please enter a valid numeric Eveningtime Phone Extension.");
						billToEveningPhoneEXT.select()
						billToEveningPhoneEXT.focus()
						return false
					}
	
	//shipping

	if (!chkFld(shipToFName, "shiping First Name", 1)) {return false;}
	if (!chkFld(shipToLName, "shiping Last Name", 1)) {return false;}
	
	if (!chkFld(shipToStreet1, "shiping Address 1", 1)) {return false;}
	if (!chkFld(shipToCity, "shiping City", 1)) {return false;}
	//if (shipToCountry.value == 'US' || shipToCountry.value == 'CA') 
	if (!chkFld(shipToState, "shiping State", 1)) {return false;}	
	if (!chkFld(shipToPostal, "shiping Postal Code", 5)) {return false;}
	if (!isZipOrPhone(shipToPostal, "Postal Code")) {return false;}



					// VALIDATE Daytime phone number.
					if ((!IsNumericBLN(shipToDayPhoneAC) || (shipToDayPhoneAC.value.length != 3) || 
						!IsNumericBLN(shipToDayPhonePRE) || (shipToDayPhonePRE.value.length != 3) || 
						!IsNumericBLN(shipToDayPhoneSUF) || (shipToDayPhoneSUF.value.length != 4))) {
						alert("Please enter a valid Daytime Phone Number.");
						shipToDayPhoneAC.select()
						shipToDayPhoneAC.focus()
						return false
					}

// VALIDATE day time telephone extension.
					if (!IsNumericBLN(shipToDayPhoneEXT) && (shipToDayPhoneEXT.value.length <= 10)) {
						alert("Please enter a valid numeric Daytime Phone Extension.");
						shipToDayPhoneEXT.select()
						shipToDayPhoneEXT.focus()
						return false
					}


					// VALIDATE evening phone number.
					if ((!IsNumericBLN(shipToEveningPhoneAC) || (shipToEveningPhoneAC.value.length != 3) || 
						!IsNumericBLN(shipToEveningPhonePRE) || (shipToEveningPhonePRE.value.length != 3) || 
						!IsNumericBLN(shipToEveningPhoneSUF) || (shipToEveningPhoneSUF.value.length != 4)) && 
						!((shipToEveningPhoneAC.value.length < 1) && (shipToEveningPhonePRE.value.length < 1) && (shipToEveningPhoneSUF.value.length < 1))) {
						alert("Please enter a valid Eveningtime Phone Number.");
						shipToEveningPhoneAC.select()
						shipToEveningPhoneAC.focus()
						return false
					}

// VALIDATE evening telephone extension.
					if (!IsNumericBLN(shipToEveningPhoneEXT) && (shipToEveningPhoneEXT.value.length <= 10)) {
						alert("Please enter a valid numeric Eveningtime Phone Extension.");
						shipToEveningPhoneEXT.select()
						shipToEveningPhoneEXT.focus()
						return false
					}
	
	}
	return true;
}



function validateUserSignUp2(cntl) { 	
	with(cntl) {
    ccNumber  = sCardNumber.value
	ccNumber = ccNumber.replace(/\ /g,''); //strip spaces from number
	ccNumber = ccNumber.replace(/-/g,''); //strip spaces from number	
	sCardNumber.value = ccNumber;
	
	var cardLastFourDigit = sCardNumber.value.substring(sCardNumber.value.length-4, sCardNumber.value.length);
	if (!chkFld(sCardNumber, "Credit Card", 1)) {return false;}
	if (!isValueNumeric(cardLastFourDigit)) {alert("Credit Card must be numeric.");return false;}	
	
	if (isValueNumeric(sCardNumber.value)) {
		strMonth = sCardExpMonth.value;
		strYear = sCardExpYear.value;
		strCreditCard = sCardNumber.value;	
		if(!validateCreditCard(strCreditCard,strMonth,strYear)){return false;}
	}	
	
	}
	return true;
}


function Sync(cntl){
	with(cntl) {
		shipToFName.value = billToFName.value;
		shipToLName.value = billToLName.value;

		shipToCompany.value = billToCompany.value;
		shipToStreet1.value = billToStreet1.value;
		shipToStreet2.value = billToStreet2.value;
		shipToCity.value = billToCity.value;
		shipToState.value = billToState.value;
		shipToPostal.value = billToPostal.value;
		shipToPostalExt.value = billToPostalExt.value;
		shipToDayPhoneAC.value = billToDayPhoneAC.value;
		shipToDayPhonePRE.value = billToDayPhonePRE.value;
		shipToDayPhoneSUF.value = billToDayPhoneSUF.value;
		shipToDayPhoneEXT.value = billToDayPhoneEXT.value;
		shipToEveningPhoneAC.value = billToEveningPhoneAC.value;
		shipToEveningPhonePRE.value = billToEveningPhonePRE.value;
		shipToEveningPhoneSUF.value = billToEveningPhoneSUF.value;
		shipToEveningPhoneEXT.value = billToEveningPhoneEXT.value;
		
	}
}

function SameShipping(field, cntl) {
	with(cntl) {
		Sync(cntl);
		if (field.checked)
			syncIt = setInterval("Sync(document.userInfo)",50);
		else if (syncIt)
			clearInterval(syncIt);
		/**/
	}
}


function resetForm(cntl) {
	eval('document.' + cntl +'.reset()');	
}
