// 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 error = "";
    var stripped = fieldname.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fieldname.value == "") {
        return false;
    } else if (isNaN(parseInt(stripped))) {
        return false;
    } else if (!(stripped.length == 10)) {
        return false;
    }
    return true;
}

// General function that returns false if "Fax" contains "x".
function checkfaxfield(fieldname) {
	var error = "";
    var stripped = fieldname.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fieldname.value == "") {
        return false;
    } else if (isNaN(parseInt(stripped))) {
        return false;
    } else if (!(stripped.length == 10)) {
        return false;
    }
    return true;
}

// 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;
}

// 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;
	}
}

// Function that checks for valid date in yyyy-mm-dd format. Returns false if not valid.
function checkdate(input){
var validformat=/^\d{4}\-\d{2}\-\d{2}$/ //Basic check for format validity
var returnval=false
	if (!validformat.test(input.value))
		return returnval
	else { //Detailed check for valid date ranges
	var yearfield=input.value.split("-")[0]
	var monthfield=input.value.split("-")[1]
	var dayfield=input.value.split("-")[2]
	var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getFullYear()!=yearfield)||(dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield))
			return returnval
		else
		returnval=true
		}
		if (returnval==false) input.select()
	return returnval
}

// Function that checks at least 1 solution category is checked. Returns false if not valid.
function checkCategoryRequested(CategoryRequested) { 
	var ckbx_arr=document.getElementsByName('CategoryRequested');
	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; 
}

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

// Function that checks for valid customer contact country. Returns false if not valid.
function checkcontactcountry(country) {
	if ( country.options[country.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that compares confirmed captcha variable. Returns false if not valid.
function confirmCaptchaVars(hashReferences, captchaPostedVariable) {
	if (hashReferences.value != captchaPostedVariable.value)
	{
        return false;
    } else {
        return true;
    }
}

// This checks the contact us form. It calls the various functions to check for validity.
function checkContactUsForm(f) {
	var CategoryRequestedval = checkCategoryRequested(f.CategoryRequested);
	var ContactFirstval = checkfield(f.ContactFirst);
	var ContactLastval = checkfield(f.ContactLast);
//	var ContactAddress1val = checkfield(f.ContactAddress1);
//	var ContactCityval = checkfield(f.ContactCity);
//	var ContactStateval = checkcontactstate(f.state);
//	var ContactZipval = checkfield(f.ContactZip);
//	var ContactCountryval = checkcontactcountry(f.country);
	var ContactEmailval = checkemail(f.ContactEmail);
	var CaptchaVarsval = confirmCaptchaVars(f.hashReferences, f.captchaPostedVariable);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your contact form:\n\n";

	if (!CaptchaVarsval) {
		formvalid = false;
		errormsg = errormsg + '  - The same, case-sensitive characters from the image must be be filled in.\n';
		focusfield = f.captchaPostedVariable;
	}

	if (!ContactEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Your E-mail Address must be filled in.\n';
		focusfield = f.ContactEmail;
	}

//	if (!ContactCountryval) {
//		formvalid = false;
//		errormsg = errormsg + '  - Your Country must be selected.\n';
//		focusfield = f.country;
//	}

//	if (!ContactZipval) {
//		formvalid = false;
//		errormsg = errormsg + '  - Your Postal or Zip Code must be filled in.\n';
//		focusfield = f.ContactZip;
//	}

//	if (!ContactStateval) {
//		formvalid = false;
//		errormsg = errormsg + '  - Your Province or State must be selected.\n';
//		focusfield = f.state;
//	}
	
//	if (!ContactCityval) {
//		formvalid = false;
//		errormsg = errormsg + '  - Your City must be filled in.\n';
//		focusfield = f.ContactCity;
//	}
	
//	if (!ContactAddress1val) {
//		formvalid = false;
//		errormsg = errormsg + '  - Your Street Address must be filled in.\n';
//		focusfield = f.ContactAddress1;
//	}

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

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

	if (!CategoryRequestedval) {
		formvalid = false;
		errormsg = errormsg + '  - At least one product category must be checked.\n';
		focusfield = f.CategoryRequested[0];
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// This checks the request a quote form. It calls the various functions to check for validity.
function checkRequestAQuoteForm(f) {
	var ContactFirstval = checkfield(f.ContactFirst);
	var ContactLastval = checkfield(f.ContactLast);
	var ContactEmailval = checkemail(f.ContactEmail);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your request for a quote form:\n\n";

	if (!ContactEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Your E-mail Address must be filled in.\n';
		focusfield = f.ContactEmail;
	}

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

	if (!ContactFirstval) {
		formvalid = false;
		errormsg = errormsg + '  - Your First Name must be filled in.\n';
		focusfield = f.ContactFirst;
	}
	
	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;
    }
}

// Function that compares confirmed e-mail addresses. Returns false if not valid.
function confirmemailaddress(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 = confirmemailaddress(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 = confirmemailaddress(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 = confirmemailaddress(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.
// [Replaces checkProductID() function - JG]
function checkCartItemID(CartItemID) { 
	var ckbx_arr=document.getElementsByName('CartItemID');
	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 CartItemID. It calls the various functions to check for validity.
function checkbasket(f) {
	var CartItemIDval = checkCartItemID(f.CartItemID);
	
	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 (!CartItemIDval) {
		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 state or province. 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 state or province. 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 state or province. 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 billing country. Returns false if not valid.
function checkbillcountry(BillCountry) {
	if ( BillCountry.options[BillCountry.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

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

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

// Function that checks for valid shipping country. Returns false if not valid.
function checkshipcountry(ShipCountry) {
	if ( ShipCountry.options[ShipCountry.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

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

// 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;
    }
}

// Basic function that checks for valid credit card number.
function checkcreditcard(fieldname) {
	var error = "";
    var stripped = fieldname.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fieldname.value == "") {
        return false;
    } else if (isNaN(parseInt(stripped))) {
        return false;
    } else if ( fieldname.value.length < 15 ) {
        return false;
    } else if ( fieldname.value.length > 19 ) {
        return false;
    }
    return true;
}

// Basic function that checks for valid credit card security code.
function checksecuritycode(fieldname) {
	var error = "";
    var stripped = fieldname.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fieldname.value == "") {
        return false;
    } else if (isNaN(parseInt(stripped))) {
        return false;
    } else if ( fieldname.value.length < 3 ) {
        return false;
    } else if ( fieldname.value.length > 4 ) {
        return false;
    }
    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 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 BillStateval = checkbillstate(f.BillState);
	var BillStateOtherval = checkbillstateother(f.BillState, f.BillStateOther);
	var BillZipval = checkPostalCodeBill(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 ShipStateval = checkshipstate(f.ShipState);
	var ShipStateOtherval = checkshipstateother(f.ShipState, f.ShipStateOther);
	var ShipZipval = checkPostalCodeShip(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 CCSecCodeval = checkfield(f.CCSecCode);
	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 Terms and Conditions.\n';
		focusfield = f.ConfirmOrderAgreement;
	}
	
	if (!CCExpYearval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Expiration Year must be selected.\n';
		focusfield = f.CCExpYear;
	}
	
	if (!CCExpMonthval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Expiration Month must be selected.\n';
		focusfield = f.CCExpMonth;
	}
	
	if (!CCSecCodeval) {
		formvalid = false;
		errormsg = errormsg + '  - Security Code from the back of your Credit Card Number must be filled in.\n';
		focusfield = f.CCSecCode;
	}
	
	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 Postal Code must be filled in.\n';
		focusfield = f.ShipZip;
	}
	
	if (!ShipStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Shipping Province must be filled in if other.\n';
		focusfield = f.ShipStateOther;
	}

	if (!ShipStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Province must be selected.\n';
		focusfield = f.ShipState;
	}
	
	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 Postal Code must be filled in.\n';
		focusfield = f.BillZip;
	}
	
	if (!BillStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Billing Province must be filled in if other.\n';
		focusfield = f.BillStateOther;
	}

	if (!BillStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Province must be selected.\n';
		focusfield = f.BillState;
	}
	
	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 pageSubTopicsList() {
var form = document.RearrangePageSubTopicsForm;
for (var i=0; i<form.upDownList.length; i++) {
form.upDownList.options[i].selected = true;
}
return true;
}

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

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

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

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

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 productSizesList() {
var form = document.RearrangeProductSizesForm;
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;
}

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

