// -->

// 2007apr06	lpb	Modified subroutine checkInternationalIndicator to check for a zip code of 0 as invalid.  Customers
//					were able to enter 00000 as the zip, causing processing errors on the AS400.

<!-- Begin
function setShipping() {
	//setShipping for domestic accounts
	if(document.forms.form.billingType.value == 1){
		if(document.forms.form.useBilling.checked) {
			document.forms.form.custshipSalutation.value = document.forms.form.custSalutation.value;
			document.forms.form.custshipFirst.value = document.forms.form.custFirst.value;
			document.forms.form.custshipMiddle.value = document.forms.form.custMiddle.value;
			document.forms.form.custshipLast.value = document.forms.form.custLast.value;
			document.forms.form.custshipSuffix.value = document.forms.form.custSuffix.value;
			document.forms.form.custshipAddress1.value = document.forms.form.custAddress1.value;
			document.forms.form.custshipAddress2.value = document.forms.form.custAddress2.value;
			document.forms.form.custshipCity.value = document.forms.form.custCity.value;
			document.forms.form.custshipStateID.value = document.forms.form.custStateID.value;
			if((document.forms.form.custStateID.value  == "TN") || 
			   (document.forms.form.custStateID.value  == "GA") || 
			   (document.forms.form.custStateID.value  == "AL")) {
			   alert('Your bill-to state is TN, GA or AL.  Your ship-to state can not be TN, GA or AL.');
			   }
			
			document.forms.form.custshipZip.value = document.forms.form.custZip.value;
			document.forms.form.custshipAreaCode.value = document.forms.form.custAreaCode1.value;
			document.forms.form.custshipPhone.value = document.forms.form.custPhone1.value;
			document.forms.form.custshipExt.value = document.forms.form.custExt1.value;
		}
		else {
			document.forms.form.custshipSalutation.value = "";
			document.forms.form.custshipFirst.value =  "";
			document.forms.form.custshipMiddle.value =  "";
			document.forms.form.custshipLast.value =  "";
			document.forms.form.custshipSuffix.value =  "";
			document.forms.form.custshipAddress1.value = "";
			document.forms.form.custshipAddress2.value = "";
			document.forms.form.custshipCity.value = "";
			document.forms.form.custshipStateID.value = "";
			document.forms.form.custshipZip.value = "";
			document.forms.form.custshipAreaCode.value = "";
			document.forms.form.custshipPhone.value = "";
			document.forms.form.custshipExt.value = "";
		}
	}
	//setShipping for international or APO/FPO Accounts
	else if(document.forms.form.billingType.value == 2){
		if(document.forms.form.useBilling.checked) {
			document.forms.form.custshipSalutation.value = document.forms.form.custSalutation.value;
			document.forms.form.custshipFirst.value = document.forms.form.custFirst.value;
			document.forms.form.custshipMiddle.value = document.forms.form.custMiddle.value;
			document.forms.form.custshipLast.value = document.forms.form.custLast.value;
			document.forms.form.custshipSuffix.value = document.forms.form.custSuffix.value;
			document.forms.form.custshipAddress1.value = document.forms.form.custAddress1.value;
			document.forms.form.custshipAddress2.value = document.forms.form.custAddress2.value;
			document.forms.form.custshipAddress3.value = document.forms.form.custAddress3.value;
			document.forms.form.custshipCity.value = document.forms.form.custCity.value;
			document.forms.form.custshipCountryPostalCode.value = document.forms.form.custCountryPostalCode.value;
			document.forms.form.custshipCountryCode.value = document.forms.form.custCountryCode.value;
			document.forms.form.custshipCountryPhone.value = document.forms.form.custCountryPhone.value;
		}
		else {
			document.forms.form.custshipSalutation.value = "";
			document.forms.form.custshipFirst.value =  "";
			document.forms.form.custshipMiddle.value =  "";
			document.forms.form.custshipLast.value =  "";
			document.forms.form.custshipSuffix.value =  "";
			document.forms.form.custshipAddress1.value = "";
			document.forms.form.custshipAddress2.value = "";
			document.forms.form.custshipAddress3.value = "";
			document.forms.form.custshipCity.value = "";
			document.forms.form.custshipCountryPostalCode.value = "";
			document.forms.form.custshipCountryCode.value = "";
			document.forms.form.custshipCountryPhone.value = "";
		}
		
		
		}
}

function setCountry() {
	if(document.forms.form.useIntInd.checked == false) {
		document.forms.form.custshipCountryCode.value = "US";
		document.forms.form.custCountryCode.value = "US";
	}
}



function verifyEmail1(addr) { 
    var atPos = addr.indexOf('@'); 
    var dotPos = addr.lastIndexOf('.') 
    if (atPos>=0 && dotPos>atPos) return true; 
    else alert("You entered an invalid email address."); 
    return false; 
} 
function validateCardInfo() {
if (form.cardType.value == "") {
	alert("Please select a card type");
	return false;}

if (form.expmo.value == "") {
	alert("Please specify the expiration month.");
	return false;}

if (form.expyr.value == "") {
	alert("Please specify the expiration year.");
	return false;}
	
return true;
}


function verifyEmail (emailStr) { 

var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|COM|NET|ORG|EDU|INT|MIL|GOV|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);

if (matchArray==null) {
alert("Email address seems incorrect (check @ and .'s)");
return false;
}

var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths Email username contains invalid characters.");
return false;
}
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths Email domain name contains invalid characters.");
return false;
}
}


if (user.match(userPat)==null) {
alert("The Email username doesn't seem to be valid.");
return false;
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("The Email destination IP address is invalid!");
return false;
}
}
return true;
}

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The Email domain name does not seem to be valid.");
return false;
}
}

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The Email address must end in a well-known domain or two letter " + "country.");
return false;
}

if (len<2) {
alert("The Email address is missing a hostname!");
return false;
}

return true;
}

function validateUserID() {
if (document.forms.form.custloginPassword.value.length < 6 || document.forms.form.custloginPassword.value.length > 15){
	alert("The password you entered was not of the required length.\nLength must be 6 to 15 characters.")
	return false;
}
if (document.forms.form.custloginPassword.value != document.forms.form.custloginPasswordConfirm.value) {
	alert("The passwords you entered did not match. Please try again.");
	return false;
}
if (document.forms.form.custloginUsername.value == "") {
	alert("You left your user name blank.");
	return false;
	}
	
return true;
}


function validateTempUser() {
	if (document.forms.form.custloginPassword.value != document.forms.form.custloginPasswordConfirm.value) {
	alert("The passwords you entered did not match. Please try again.");
	return false;
}
if(document.forms.form.custloginPassword.value.length > 0 || document.forms.form.custloginUsername.value.length > 0) {
		if (document.forms.form.custloginPassword.value.length < 6 || document.forms.form.custloginPassword.value.length > 15){
		alert("The password you entered was not of the required length.\nLength must be 6 to 15 characters.")
		return false;
	}
	if (document.forms.form.custloginUsername.value == "") {
	alert("You left your user name blank.");
	return false;
	}
	
}

	
	
}


function validatePassword() {
if (document.forms.form.custloginPassword.value != document.forms.form.custloginPasswordConfirm.value) {
	alert("The passwords you entered did not match. Please try again.");
	return false;
}
if (document.forms.form.custloginPassword.value.length < 6 || document.forms.form.custloginPassword.value.length > 15){
	alert("The password you entered was not of the required length.\nLength must be 6 to 15 characters.")
	return false;
}
	
return true;
}



//formElement = this(the object/form element being sent), maxLen = integer value
function checkLength(formElement,maxLen) {
if(formElement.value.length > maxLen) {
	alert("I'm sorry, this field will only accept up to " + maxLen + " characters.");
	formElement.value = formElement.value.substring(0,maxLen); 
	}
}



function checkEmailMatch() {
if (document.forms.form.custEmail.value != document.forms.form.custEmailMatch2.value) {
	alert("The 2nd email you entered did not match. Please check both and try again.");
	return false;
}
return true;
}
function checkSubEmailMatch() {
if (document.forms.form.emailSubEmailAddress.value != document.forms.form.emailsubEmailMatch2.value) {
	alert("The 2nd email you entered did not match. Please check both and try again.");
	return false;
}
return true;
}

function checkSubEmailMatch2() {
if (document.forms.form.custEmail.value != document.forms.form.custEmail2.value) {
	alert("The 2nd email you entered did not match. Please check both and try again.");
	return false;
}
return true;
}

function checkAvailEmailMatch() {
if (document.forms.form.backorderEmail.value != document.forms.form.backorderEmailMatch2.value) {
	alert("The 2nd email you entered did not match. Please check both and try again.");
	return false;
}
return true;
}
// End -->



// 090204:  Additional Credit Card Validation Functions:
//
// isValidExpDate(formField,fieldLabel,required)
//   -- checks for date in the format MM/YY or MM/YYYY against the current date
// isValidCreditCardNumber(formField,ccType,fieldLabel,required)
//   -- checks for valid credit card format using the Luhn check and known digits about various cards
//  -- original code pulled from http://javascript.about.com/library/scripts/blccvalid.htm and then modified 
// (ie, did not need exp date checking...

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the ' + fieldLabel + ' field.');
		formField.focus();
		result = false;
	}
	
	return result;
}


function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function isValidCreditCardNumber(formField,ccType,fieldLabel,required)
{
	var result = true;
 	var ccNum = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
 
  	if (result && (formField.value.length>0))
 	{ 
 		if (!allDigits(ccNum))
 		{
 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}	

		if (result)
 		{ 
 			
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				alert('Please enter a valid ' + ccType + ' number in the ' + fieldLabel + ' field.\nIf the number appears correct, confirm that the\ncorrect card type has been selected.');
				formField.focus();
				result = false;
			}	
		} 

	} 
	
	return result;
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var 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)
    result = false;
    
  return result;
}



function GetRadioValue(rArray)
{
	for (var i=0;i<rArray.length;i++)
	{
		if (rArray[i].checked)
			return rArray[i].value;
	}
	
	return null;
}


function validateCCNum(cardType,cardNum)
{
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MC":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

function validCCForm(ccTypeField,ccNumField,ccExpMo, ccExpYr)
{
	if(validateCardInfo()) {
	var result = isValidCreditCardNumber(ccNumField,ccTypeField.value,"card number",true);
		if(result && isValidExpDate(ccExpMo, ccExpYr)){
			document.getElementById('orderSubmitButton').disabled=true;
			document.getElementById("form").submit();
		}
		else
			result = false;
	return result;
	}
	else
	return false;
}

function isValidExpDate(ccExpMo, ccExpYr){
	var curDate = new Date();
	var currMo = curDate.getMonth() + 1;
	var currYr = curDate.getYear();
	var result = true;

	if (ccExpYr.value < currYr ){
		result = false;
		alert('Please enter a valid credit card expiration date.');
		}
	else if (ccExpYr.value == currYr && ccExpMo.value < currMo){
		result = false;
		alert('Please enter a valid credit card expiration date.');
		}
	return result;
	
}

var int = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var misc = ' .\''

function isValid(parm,val) {
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}
function validateCharsAndMisc(formField, fieldLabel){
	if(!isValid(formField.value,lwr+upr+misc)){
		alert('Please enter a valid ' + fieldLabel + ' (no numeric values or symbols)');
		return false;
		}
	return true;
};

function validateCharsIntandMisc(formField, fieldLabel){
	if(!isValid(formField.value,lwr+upr+misc+int)){
		alert('Please enter a valid ' + fieldLabel);
		return false;
		}
	return true;
};
function validatePhoneArea(formField,fieldLabel)
{
     // Check for correct area code
     var reArea = /(^\d{3}$)/;
	 if (formField.value.length > 0)
     if (!reArea.test(formField.value)) {
		alert('Please enter a valid ' + fieldLabel + ' in the format xxx');
          return false;
     }
return true;
}

function validatePhoneNum(formField,fieldLabel)
{
     // Check for correct phone number
     var reNum = /(^\d{3}-\d{4}$)/;
	 if (formField.value.length > 0)
     if (!reNum.test(formField.value)) {
		alert('Please enter a valid ' + fieldLabel + ' in the format xxx-xxxx');
          return false;
     }
return true;
}
function validateZip(formField,fieldLabel)
{
     // Check for correct phone number
     var reNum = /(^\d{5}$)/;
	if(formField.value.length == 0){
		alert('Please enter a valid ' + fieldLabel + ' in the format xxxxx');
          return false;
		}
	if (formField.value.length > 0)
     if (!reNum.test(formField.value)) {
		alert('Please enter a valid ' + fieldLabel + ' in the format xxxxx');
          return false;
     }
return true;
}

function validateSelect(formField, fieldLabel){
	 if (formField.value.length == 0){
		alert('Please select a ' + fieldLabel + ' from the list provided');
		return false;
		}
	else 
		return true;
	}
function checkIntlInd(){
	if(!document.form.viewIntlInfo.checked){
			document.form.viewIntlInfo.focus();
			alert("Please review the Natchezss International Order Policy.");
			return false;
		}
	return true;
	}
function validateEmails(formField1,formField2){
	if(formField1.value != formField2.value){
		alert("The email addresses you entered do not match.");
		return false;
		}
	else if(!verifyEmail(formField1.value)){
		//alert("The email address you entered does not appear to be a valid email address.");
		return false;
		}
	return true;
	}
function validateUserInfo(form){
	//validateUserInfo for domestic accounts
	if(form.billingType.value == 1 && form.shippingType.value == 1){
		if(	!validateCharsAndMisc(form.custFirst, ' Billing First Name')||
			!validateCharsAndMisc(form.custLast, ' Billing Last Name')||
			!validateCharsAndMisc(form.custCity, ' Billing City') || 
			!validateSelect(form.custStateID, 'Billing State') ||
			!validateZip(form.custZip, ' Billing Zip Code')||
			!validatePhoneArea(form.custAreaCode1, ' Billing Primary Area Code')||
			!validatePhoneNum(form.custPhone1, ' Billing Primary Phone Number')||
			!validateCharsIntandMisc(form.custExt1, ' Billing Primary Phone Ext.')||
			!validatePhoneArea(form.custAreaCode2, ' Billing Secondary Area Code')||
			!validatePhoneNum(form.custPhone2, ' Billing Secondary Phone Number')||
			!validateCharsIntandMisc(form.custExt2, ' Billing Secondary Phone Ext.')||
			!validateEmails(form.custEmail, form.custEmailMatch2)||
 			!validateCharsAndMisc(form.custshipCity, ' Shipping City')||
			!validateCharsAndMisc(form.custshipFirst, ' Shipping First Name')||
			!validateCharsAndMisc(form.custshipLast, ' Shipping Last Name')||
			!validateSelect(form.custshipStateID, 'Shipping State') ||
			!validateZip(form.custshipZip, ' Shipping Zip Code')||
			!validatePhoneArea(form.custshipAreaCode, ' Shipping Area Code')||
			!validatePhoneNum(form.custshipPhone, ' Shipping Phone Number')||
			!validateCharsIntandMisc(form.custshipExt, ' Shipping Phone Ext.')
			)
			return false;
		else 
			return true;
	}
	//validateUserInfo for international accounts
	else if (form.billingType.value == 2 && form.shippingType.value == 2){
		if( !checkIntlInd()||
			!validateCharsAndMisc(form.custFirst, ' Billing First Name')||
			!validateCharsAndMisc(form.custLast, ' Billing Last Name')||
			!validateCharsAndMisc(form.custCity, ' Billing City') || 
			!validateSelect(form.custCountryCode, 'Billing Country')||
			!validateEmails(form.custEmail, form.custEmailMatch2)||
			!validateCharsAndMisc(form.custshipFirst, ' Shipping First Name')||
			!validateCharsAndMisc(form.custshipLast, ' Shipping Last Name')||
			!validateCharsAndMisc(form.custshipCity, ' Shipping City')||
			!validateSelect(form.custshipCountryCode, 'Shipping Country')
			)
			return false;
		else 
			return true;
		}
	//domestic shipping, int'l billing
	else if (form.billingType.value == 2 && form.shippingType.value == 1){
		if(  !checkIntlInd()||
			!validateCharsAndMisc(form.custFirst, ' Billing First Name')||
			!validateCharsAndMisc(form.custLast, ' Billing Last Name')||
			!validateCharsAndMisc(form.custCity, ' Billing City') || 
			!validateSelect(form.custCountryCode, 'Billing Country')||
			!validateEmails(form.custEmail, form.custEmailMatch2)||
 			!validateCharsAndMisc(form.custshipCity, ' Shipping City')||
			!validateCharsAndMisc(form.custshipFirst, ' Shipping First Name')||
			!validateCharsAndMisc(form.custshipLast, ' Shipping Last Name')||
			!validateCharsAndMisc(form.custshipCity, ' Shipping City') || 
			!validateSelect(form.custshipStateID, 'Shipping State') ||
			!validateZip(form.custshipZip, ' Shipping Zip Code')||
			!validatePhoneArea(form.custshipAreaCode, ' Shipping Area Code')||
			!validatePhoneNum(form.custshipPhone, ' Shipping Phone Number')||
			!validateCharsIntandMisc(form.custshipExt, ' Shipping Phone Ext.')
			)
			return false;
		else 
			return true;
		}
	//domestic billing, int'l shipping
	else if (form.billingType.value == 1 && form.shippingType.value == 2){
		if(  !checkIntlInd()||
			!validateCharsAndMisc(form.custFirst, ' Billing First Name')||
			!validateCharsAndMisc(form.custLast, ' Billing Last Name')||
			!validateCharsAndMisc(form.custCity, ' Billing City') || 
			!validateSelect(form.custStateID, 'Billing State') ||
			!validateZip(form.custZip, ' Billing Zip Code')||
			!validatePhoneArea(form.custAreaCode1, ' Billing Primary Area Code')||
			!validatePhoneNum(form.custPhone1, ' Billing Primary Phone Number')||
			!validateCharsIntandMisc(form.custExt1, ' Billing Primary Phone Ext.')||
			!validatePhoneArea(form.custAreaCode2, ' Billing Secondary Area Code')||
			!validatePhoneNum(form.custPhone2, ' Billing Secondary Phone Number')||
			!validateCharsIntandMisc(form.custExt2, ' Billing Secondary Phone Ext.')||
			!validateEmails(form.custEmail, form.custEmailMatch2)||
 			!validateCharsAndMisc(form.custshipFirst, ' Shipping First Name')||
			!validateCharsAndMisc(form.custshipLast, ' Shipping Last Name')||
			!validateCharsAndMisc(form.custshipCity, ' Shipping City')||
			!validateSelect(form.custshipCountryCode, 'Shipping Country')
			)
			return false;
		else 
			return true;
		}
}

