// General function for pop-up window
var newWindow = null
	function makeNewWindow(x,y) {
		newWindow =  window.open(x,y,"status=1,menubar=1,toolbar=0,scrollbars=1,resizable=0,width=600,height=600")
		newWindow.focus()
}

// General function that returns false if "fieldname" is empty.
function checkfield(fieldname) {
	var fieldval = (fieldname.value.length == 0) ? false: true;
	return fieldval;
}

// General function that returns false if "Phone" contains "x".
function checkphonefield(fieldname) {
	var phonefieldval = (fieldname.value.indexOf("x",0) > -1) ? false: true;
	return phonefieldval;
}

// General function that returns false if "Fax" contains "x".
function checkfaxfield(fieldname) {
	var faxfieldval = (fieldname.value.indexOf("x",0) > -1) ? false: true;
	return faxfieldval;
}

// Function that checks for valid E-mail. Returns false if not valid.
function checkemail(fieldname) {
	if (!checkfield(fieldname)) {
		return false;
	}

	invalidchars = " /:,;"
	for (count = 0; count < invalidchars.length; count++) {
		badchar = invalidchars.charAt(count);
		if (fieldname.value.indexOf(badchar,0) > -1) {
			return false;
		}
	}

	atpos = fieldname.value.indexOf("@", 1);
	if (atpos == -1) {
		return false;
	}
	if (fieldname.value.indexOf("@", atpos + 1) > -1) {
		return false;
	}

	periodpos = fieldname.value.indexOf(".", atpos);
	if (periodpos == -1) {
		return false;
	}
	if (periodpos + 3 > fieldname.value.length) {
		return false;
	}
	return true;
}

// Function that compares confirmed e-mail addresses. Returns false if not valid.
function confirmfreereportemail(emailAddress1, emailAddress2) {
	if (emailAddress1.value != emailAddress2.value)
	{
        return false;
    } else {
        return true;
    }
}

// This checks the mailing list subscription activation form. It calls the various functions to check for validity.
function checkListservSubscriptionActivationForm(f) {
	var firstNameval = checkfield(f.firstName);
	var lastNameval = checkfield(f.lastName);
	var emailAddress1val = checkemail(f.emailAddress1);
	var emailAddress2val = checkemail(f.emailAddress2);
	var emailAddressval = confirmfreereportemail(f.emailAddress1, f.emailAddress2);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your Mailing List Subscription form:\n\n";

	if (!emailAddressval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need you to confirm your E-mail Address properly!\n';
		focusfield = f.emailAddress2;
	}
	
	if (!emailAddress2val) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address confirmation in proper format!\n';
		focusfield = f.emailAddress2;
	}

	if (!emailAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address in proper format!\n';
		focusfield = f.emailAddress1;
	}
	
	if (!lastNameval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Last Name!\n';
		focusfield = f.lastName;
	}
	
	if (!firstNameval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your First Name!\n';
		focusfield = f.firstName;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// This checks the mailing list subscription update form. It calls the various functions to check for validity.
function checkListservSubscriptionUpdateForm(f) {
	var emailAddress1val = checkemail(f.emailAddress1);
	var emailAddress2val = checkemail(f.emailAddress2);
	var emailAddressval = confirmfreereportemail(f.emailAddress1, f.emailAddress2);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your Mailing List Subscription form:\n\n";

	if (!emailAddressval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need you to confirm your E-mail Address properly!\n';
		focusfield = f.emailAddress2;
	}
	
	if (!emailAddress2val) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address confirmation in proper format!\n';
		focusfield = f.emailAddress2;
	}

	if (!emailAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address in proper format!\n';
		focusfield = f.emailAddress1;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// This checks the mailing list subscription removal form. It calls the various functions to check for validity.
function checkListservSubscriptionUnsubscribeForm(f) {
	var emailAddress1val = checkemail(f.emailAddress1);
	var emailAddress2val = checkemail(f.emailAddress2);
	var emailAddressval = confirmfreereportemail(f.emailAddress1, f.emailAddress2);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your Mailing List Subscription Removal form:\n\n";

	if (!emailAddressval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need you to confirm your E-mail Address properly!\n';
		focusfield = f.emailAddress2;
	}
	
	if (!emailAddress2val) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address confirmation in proper format!\n';
		focusfield = f.emailAddress2;
	}

	if (!emailAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address in proper format!\n';
		focusfield = f.emailAddress1;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// Function that checks for checked item from basket. Returns false if not valid.
function checkProductID(ProductID) { 
	var ckbx_arr=document.getElementsByName('ProductID');
	var ckbx_arr_ln=ckbx_arr.length;
	for(var i=0;i<ckbx_arr_ln;i++) 
	{
	if(ckbx_arr[i].checked)
		return true;
	}
	return false; 
}

// This checks the basket for checked item for removal from basket by Product ID. It calls the various functions to check for validity.
function checkbasket(f) {
	var ProductIDval = checkProductID(f.ProductID);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to remove an item from your shopping cart:\n\n";
	
	if (!ProductIDval) {
		formvalid = false;
		errormsg = errormsg + '  - You need to check the box that corresponds to the item that you wish to remove.\n';
		focusfield = f.remove;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// Function that checks if other billing province or state. Returns false if not valid.
function checkbillstateother(BillState, BillStateOther) {
	if ( BillState.options[BillState.selectedIndex].value == "Other") {
		if ( checkfield(BillStateOther) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks for valid billing province or state. Returns false if not valid.
function checkbillstate(BillState) {
	if ( BillState.options[BillState.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks if other shipping province or state. Returns false if not valid.
function checkshipstateother(ShipState, ShipStateOther) {
	if ( ShipState.options[ShipState.selectedIndex].value == "Other") {
		if ( checkfield(ShipStateOther) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks for valid shipping province or state. Returns false if not valid.
function checkshipstate(ShipState) {
	if ( ShipState.options[ShipState.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for valid credit card type. Returns false if not valid.
function checkCardTypeList(CardTypeList) {
	if ( CardTypeList.options[CardTypeList.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

function checkMod10(CCNumber) {  // v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = CCNumber.value.length;  // The length of the submitted cc number
var iCCN = parseInt(CCNumber);  // integer of CCNumber
var sCCN = CCNumber.value.toString();  // string of CCNumber
sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the CCNumber is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length 
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // CCNumber is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN) % 10;  // right most digit
      calc = parseInt(calc);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from CCNumber
      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }                                               
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
// change alert to on-page display or other indication as needed.
if (bResult = false)
	{
        return false;
    } else {
        return true;
    }
}

// Function that checks for valid credit card expiration month. Returns false if not valid.
function checkCCExpMonth(CCExpMonth) {
	if ( CCExpMonth.options[CCExpMonth.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for valid credit card expiration year. Returns false if not valid.
function checkCCExpYear(CCExpYear) {
	if ( CCExpYear.options[CCExpYear.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for checked order agreement. Returns false if not valid.
function checkOrderAgreement(ConfirmOrderAgreement) { 
  if (ConfirmOrderAgreement.checked != true)    {
	return false;
    } else {
        return true;
    }
}

// This checks the search our site form. It calls the various functions to check for validity.
function checkSearchForm(f) {
	var SearchTermval = checkfield(f.searchTerm);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your search form:\n\n";

	if (!SearchTermval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your keyword(s) to search our site!\n';
		focusfield = f.searchTerm;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

//  General function that returns false if u.s. zip code is invalid.
function checkUSZipCode(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
		//alert("Please enter your 5 digit or 5 digit+4 zip code.");
			return false;
		}
	for (var i=0; i < field.length; i++) {
	temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
		//alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
	if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
		//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
   		}
	}
return true;
}

// This checks the u.s. store location search form. It calls the various functions to check for validity.
function checkUSStoreLocatorForm(f) {
	var UserZipCodeval = checkUSZipCode(f.UserZipCode);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following error was found when attempting to submit your store location search form:\n\n";
	
	if (!UserZipCodeval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Valid U.S. Zip Code!\n';
		focusfield = f.UserZipCode;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

//  General function that returns false if canada postal code is invalid.
function checkPostalCode(UserPostalCode) {
	if (UserPostalCode.value.length == 6 && UserPostalCode.value.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) {
		return true;
	} else if (UserPostalCode.value.length == 7 && UserPostalCode.value.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) {	
		return true;
	} else {
		return false;
	}
}

// This checks the canadian store location search form. It calls the various functions to check for validity.
function checkPostalCodeSearchForm(f) {
	var UserPostalCodeval = checkPostalCode(f.UserPostalCode);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following error was found when attempting to submit your store location search form:\n\n";
	
	if (!UserPostalCodeval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Valid Canadian Postal Code!\n';
		focusfield = f.UserPostalCode;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// This checks the part search form by part number. It calls the various functions to check for validity.
function checkSearchByPartNumForm(f) {
	var SearchTermsval = checkfield(f.SearchTerms);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following error was found when attempting to submit your part search by part number or keyword:\n\n";

	if (!SearchTermsval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Part Number or Keyword to search our Product Catalog!\n';
		focusfield = f.SearchTerms;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// Function that checks for brand name selection. Returns false if not valid.
function checkBrandName(BrandName) {
	if ( BrandName.options[BrandName.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// This checks the part search form by brand and item description. It calls the various functions to check for validity.
function checkSearchByBrandModelForm(f) {
	var BrandNameval = checkBrandName(f.BrandName);
	var SearchTermsval = checkfield(f.SearchTerms);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your part search by brand and item description:\n\n";

	if (!SearchTermsval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Item Description to search our Product Catalog!\n';
		focusfield = f.SearchTerms;
	}
	
	if (!BrandNameval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Brand Name to search our Product Catalog!\n';
		focusfield = f.BrandName;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// Function that checks for checked parts search support. Returns false if not valid.
function checkSupportStatus(SupportStatus, EmailAddress) { 
  if (SupportStatus.checked)    {
		if ( checkemail(EmailAddress) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// This checks the part search form by part number with e-mail support. It calls the various functions to check for validity.
function checkPartNumSearchForm(f) {
	var SearchTermsval = checkfield(f.SearchTerms);
	var SupportStatusval = checkSupportStatus(f.SupportStatus, f.EmailAddress);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your part search by part number or keyword:\n\n";

	if (!SupportStatusval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address if you check the box for support!\n';
		focusfield = f.EmailAddress;
	}

	if (!SearchTermsval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Part Number or Keyword to search our Product Catalog!\n';
		focusfield = f.SearchTerms;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// This checks the part search form by brand and item description with e-mail support. It calls the various functions to check for validity.
function checkModelNumSearchForm(f) {
	var BrandNameval = checkBrandName(f.BrandName);
	var SearchTermsval = checkfield(f.SearchTerms);
	var SupportStatusval = checkSupportStatus(f.SupportStatus, f.EmailAddress);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your part search by brand and item description:\n\n";

	if (!SupportStatusval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your E-mail Address if you check the box for support!\n';
		focusfield = f.EmailAddress;
	}

	if (!SearchTermsval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Item Description to search our Product Catalog!\n';
		focusfield = f.SearchTerms;
	}
	
	if (!BrandNameval) {
		formvalid = false;
		errormsg = errormsg + '  - Whoops, we need your Brand Name to search our Product Catalog!\n';
		focusfield = f.BrandName;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// Function that compares confirmed billing e-mail addresses. Returns false if not valid.
function confirmbillemail(BillEmail1, BillEmail2) {
	if (BillEmail1.value != BillEmail2.value)
	{
        return false;
    } else {
        return true;
    }
}

// This checks the order checkout form. It calls the various functions to check for validity.
function checkorderform(f) {
	var BillFirstval = checkfield(f.BillFirst);
	var BillLastval = checkfield(f.BillLast);
	var BillAddress1val = checkfield(f.BillAddress1);
	var BillCityval = checkfield(f.BillCity);
	var BillStateOtherval = checkbillstateother(f.BillState, f.BillStateOther);
	var BillStateval = checkbillstate(f.BillState);
	var BillZipval = checkfield(f.BillZip);
	var BillPhoneval = checkphonefield(f.BillPhone);
	var BillEmail1val = checkemail(f.BillEmail1);
	var BillEmail2val = checkemail(f.BillEmail2);
	var BillEmailval = confirmbillemail(f.BillEmail1, f.BillEmail2);
	var ShipFirstval = checkfield(f.ShipFirst);
	var ShipLastval = checkfield(f.ShipLast);
	var ShipAddress1val = checkfield(f.ShipAddress1);
	var ShipCityval = checkfield(f.ShipCity);
	var ShipStateOtherval = checkshipstateother(f.ShipState, f.ShipStateOther);
	var ShipStateval = checkshipstate(f.ShipState);
	var ShipZipval = checkfield(f.ShipZip);
	var ShipPhoneval = checkphonefield(f.ShipPhone);
	var ShipEmailval = checkemail(f.ShipEmail);
	var CardTypeListval = checkCardTypeList(f.CardTypeList);
	var CCNumberval = checkfield(f.CCNumber);
	var Mod10val = checkMod10(f.CCNumber);
	var CCExpMonthval = checkCCExpMonth(f.CCExpMonth);
	var CCExpYearval = checkCCExpYear(f.CCExpYear);
	var OrderAgreementval = checkOrderAgreement(f.ConfirmOrderAgreement);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your payment form:\n\n";
	
	if (!OrderAgreementval) {
		formvalid = false;
		errormsg = errormsg + '  - You must check the box to confirm that you have read and agreed to the Order Agreement.\n';
		focusfield = f.ConfirmOrderAgreement;
	}

	if (!CCExpYearval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number Expiration Year must be selected.\n';
		focusfield = f.CCExpYear;
	}

	if (!CCExpMonthval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number Expiration Month must be selected.\n';
		focusfield = f.CCExpMonth;
	}

	if (!Mod10val) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number must be valid.\n';
		focusfield = f.CCNumber;
	}

	if (!CCNumberval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number must be filled in.\n';
		focusfield = f.CCNumber;
	}

	if (!CardTypeListval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Type must be selected.\n';
		focusfield = f.CardTypeList;
	}
	
	if (!ShipEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping E-mail Address must be filled in.\n';
		focusfield = f.ShipEmail;
	}

	if (!ShipPhoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Phone Number must be filled in.\n';
		focusfield = f.ShipPhone;
	}

	if (!ShipZipval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Zip Code must be filled in.\n';
		focusfield = f.ShipZip;
	}

	if (!ShipStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping State must be selected.\n';
		focusfield = f.ShipState;
	}
	
	if (!ShipStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Shipping State or Province must be filled in if other.\n';
		focusfield = f.ShipStateOther;
	}
	
	if (!ShipCityval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping City must be filled in.\n';
		focusfield = f.ShipCity;
	}

	if (!ShipAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Street Address must be filled in.\n';
		focusfield = f.ShipAddress1;
	}

	if (!ShipLastval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Last Name must be filled in.\n';
		focusfield = f.ShipLast;
	}

	if (!ShipFirstval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping First Name must be filled in.\n';
		focusfield = f.ShipFirst;
	}

	if (!BillEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing E-mail Address must be confirmed properly and match.\n';
		focusfield = f.BillEmail2;
	}
	
	if (!BillEmail2val) {
		formvalid = false;
		errormsg = errormsg + '  - Billing E-mail Address must be confirmed.\n';
		focusfield = f.BillEmail2;
	}
	
	if (!BillEmail1val) {
		formvalid = false;
		errormsg = errormsg + '  - Billing E-mail Address must be filled in.\n';
		focusfield = f.BillEmail1;
	}
	
	if (!BillPhoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Phone Number must be filled in.\n';
		focusfield = f.BillPhone;
	}

	if (!BillZipval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Zip Code must be filled in.\n';
		focusfield = f.BillZip;
	}

	if (!BillStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing State must be selected.\n';
		focusfield = f.BillState;
	}
	
	if (!BillStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Billing State or Province must be filled in if other.\n';
		focusfield = f.BillStateOther;
	}
	
	if (!BillCityval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing City must be filled in.\n';
		focusfield = f.BillCity;
	}

	if (!BillAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Street Address must be filled in.\n';
		focusfield = f.BillAddress1;
	}

	if (!BillLastval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Last Name must be filled in.\n';
		focusfield = f.BillLast;
	}

	if (!BillFirstval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing First Name must be filled in.\n';
		focusfield = f.BillFirst;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

var ShipFirst = "";
var ShipLast = "";
var ShipAddress1 = "";
var ShipAddress2 = "";
var ShipCity = "";
var ShipState = "";
var ShipStateIndex = 0;
var ShipStateOther = "";
var ShipCountryIndex = 0;
var ShipZip = "";
var ShipPhone = "";
var ShipEmail = "";
var ShipConfirm = 0;

function InitSaveVariables(form) {
ShipFirst = form.ShipFirst.value;
ShipLast = form.ShipLast.value;
ShipAddress1 = form.ShipAddress1.value;
ShipAddress2 = form.ShipAddress2.value;
ShipCity = form.ShipCity.value;
ShipZip = form.ShipZip.value;
ShipStateIndex = form.ShipState.selectedIndex;
ShipState = form.ShipState[ShipStateIndex].value;
ShipStateOther = form.ShipStateOther.value;
ShipCountry = form.ShipState[ShipCountryIndex].value;
ShipPhone = form.ShipPhone.value;
ShipEmail = form.ShipEmail.value;
ShipConfirm = form.ShipConfirm.checked;
}

function ShipToBillPerson(form) {
if (form.copyBillToShip.checked) {
InitSaveVariables(form);
form.ShipFirst.value = form.BillFirst.value;
form.ShipLast.value = form.BillLast.value;
form.ShipAddress1.value = form.BillAddress1.value;
form.ShipAddress2.value = form.BillAddress2.value;
form.ShipCity.value = form.BillCity.value;
form.ShipZip.value = form.BillZip.value;
form.ShipState.selectedIndex = form.BillState.selectedIndex;
form.ShipStateOther.value = form.BillStateOther.value;
form.ShipCountry.selectedIndex = form.BillCountry.selectedIndex;
form.ShipPhone.value = form.BillPhone.value;
form.ShipEmail.value = form.BillEmail1.value;
form.ShipConfirm.checked = form.BillConfirm.checked;
}
else {
form.ShipFirst.value = ShipFirst;
form.ShipLast.value = ShipLast;
form.ShipAddress1.value = ShipAddress1;
form.ShipAddress2.value = ShipAddress2;
form.ShipCity.value = ShipCity;
form.ShipZip.value = ShipZip;
form.ShipState.selectedIndex = ShipStateIndex;
form.ShipStateOther.value = ShipStateOther;
form.ShipCountry.selectedIndex = ShipCountryIndex;
form.ShipPhone.value = ShipPhone;
form.ShipEmail.value = ShipEmail;
form.ShipConfirm.checked = ShipConfirm;
   }
}

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// April 20, 2005: Fixed the removeSelectedOptions() function to 
//                 correctly handle single selects
// June 12, 2003: Modified up and down functions to support more than
//                one selected option
/*
DESCRIPTION: These are general functions to deal with and manipulate
select boxes. Also see the OptionTransfer library to more easily 
handle transferring options between two lists

COMPATABILITY: These are fairly basic functions - they should work on
all browsers that support Javascript.
*/


// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
//  This is a general function used by the select functions below, to
//  avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			var selected1=true;
			var selected2=false;
			}
		else if (which == "unselect") {
			var selected1=false;
			var selected2=true;
			}
		else {
			return;
			}
		var re = new RegExp(regex);
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			if (re.test(obj.options[i].text)) {
				obj.options[i].selected = selected1;
				}
			else {
				if (only == true) {
					obj.options[i].selected = selected2;
					}
				}
			}
		}
	}
		
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
	}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
	}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
//  This function Unselects all options that match the regular expression
//  passed in. 
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
	
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
			}
		}
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				to.options[index] = new Option( o.text, o.value, false, false);
				}
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
		}
	}

// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
//  Copy all options from one select box to another, instead of
//  removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		copySelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		copySelectedOptions(from,to,arguments[2]);
		}
	}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) { 
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
		}
	else {
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 
				from.options[i] = null; 
				} 
			}
		}
	from.selectedIndex = -1; 
	} 




// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}

// -------------------------------------------------------------------
// Select all options from select menu
// document.form represents the name of the form
// -------------------------------------------------------------------
function categoriesList() {
var form = document.RearrangeCategoriesForm;
for (var i=0; i<form.upDownList.length; i++) {
form.upDownList.options[i].selected = true;
}
return true;
}

function subCategoriesList() {
var form = document.RearrangeSubcategoriesForm;
for (var i=0; i<form.upDownList.length; i++) {
form.upDownList.options[i].selected = true;
}
return true;
}

function productsList() {
var form = document.RearrangeProductsForm;
for (var i=0; i<form.upDownList.length; i++) {
form.upDownList.options[i].selected = true;
}
return true;
}
