﻿// This script is used for processing the state/province and country dropdowns
var sCardType;



/*******************************************/
/*******************************************/

function selectStateProvince_onChange(oStateProvince, oCountry) {
	var iIndex, sValue, sCountryCode, sCountryName;

	iIndex = oStateProvince.selectedIndex;

	if (iIndex > 0) {
		sValue = oStateProvince.options[iIndex].value;

		if (sValue == sOther) {
			sCountryName = oCountry.options[oCountry.selectedIndex].value;

			if ((sCountryName == sCanada) || (sCountryName == sUnitedStates)) {
				oCountry.selectedIndex = 0;
				oCountry.focus();
			}
		}
		else {
			sCountryCode = sValue.substring(sValue.indexOf("-") + 1);

			if (sCountryCode == sUnitedStatesCode) {
				oCountry.selectedIndex = iUnitedStatesIndex;
			}
			else {
				if (sCountryCode == sCanadaCode) {
					oCountry.selectedIndex = iCanadaIndex;
				}
			}
		}
	}
}

function selectCountry_onChange(oStateProvince, oCountry) {
	var iIndex, sValue, sStateProvince, sCountryCode;

	iIndex = oCountry.selectedIndex;
	sValue = oCountry.options[iIndex].value;

	if (iIndex > 0) {
		if ((sValue != sCanada) && (sValue != sUnitedStates)) {
			oStateProvince.selectedIndex = oStateProvince.options.length - 1;
		}
		else {
			sStateProvince = oStateProvince.options[oStateProvince.selectedIndex].value;

			if (sStateProvince == sOther) {
				oStateProvince.selectedIndex = 0;
				oStateProvince.focus();
			}
			else {
				sCountryCode = sStateProvince.substr(sStateProvince.indexOf("-") + 1);

				if (((sValue == sUnitedStates) && (sCountryCode != sUnitedStatesCode))
				  || ((sValue == sCanada) && (sCountryCode != sCanadaCode))) {
					oStateProvince.selectedIndex = 0;
					oStateProvince.focus();
				}
			}
		}
	}
}




/*******************************************/
/*******************************************/

function isValidFirstName() {
	if (isBlank(oFirstName.value)) {
		oFirstName.focus();
		oFirstName.select();

		alert("Please enter your first name.");

		return false;
	}

	return true;
}

function isValidLastName() {
	if (isBlank(oLastName.value)) {
		oLastName.focus();
		oLastName.select();

		alert("Please enter your last name.");

		return false;
	}

	return true;
}

function isValidJobTitle() {
	if (isBlank(oJobTitle.value)) {
		oJobTitle.focus();
		oJobTitle.select();

		alert("Please enter your job title.");

		return false;
	}

	return true;
}



/*******************************************/
/*******************************************/

function isValidFax(oFax, sLabel) {
	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (isBlank(oFax.value)) {
		oFax.focus();
		oFax.select();

		alert("Please enter your " + sLabel + "fax number.");

		return false;
	}

	return true;
}

function isValidHomeFax() {
	return isValidFax(oHomeFax, null);
}

function isValidBillingFax() {
	return isValidFax(oBillingFax, sCardType + " billing");
}

function isValidBusinessFax() {
	return isValidFax(oBusinessFax, "business");
}




/*******************************************/
/*******************************************/

function isValidPhone(oPhone, sLabel) {
	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (isBlank(oPhone.value)) {
		oPhone.focus();
		oPhone.select();

		alert("Please enter your " + sLabel + "phone number.");

		return false;
	}

	return true;
}

function isValidHomePhone() {
	return isValidPhone(oHomePhone, null);
}

function isValidBillingPhone() {
	return isValidPhone(oBillingPhone, sCardType + " billing");
}

function isValidBusinessPhone() {
	return isValidPhone(oBusinessPhone, "business");
}




/*******************************************/
/*******************************************/

function isValidEmailAddress(oEmail) {
	if (!isValidEmail(oEmail.value, true)) {
		oEmail.focus();
		oEmail.select();

		return false;
	}

	return true;
}

function isValidHomeEmail() {
	return isValidEmailAddress(oHomeEmail);
}

function isValidBillingEmail() {
	return isValidEmailAddress(oBillingEmail);
}

function isValidBusinessEmail() {
	return isValidEmailAddress(oBusinessEmail);
}




/*******************************************/
/*******************************************/

function isValidStreetAddress1(oStreetAddress1, sLabel) {
	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (isBlank(oStreetAddress1.value)) {
		oStreetAddress1.select();
		oStreetAddress1.focus();

		alert("Please enter your " + sLabel + "street address.");

		return false;
	}

	return true;
}

function isValidHomeStreetAddress1() {
	return isValidStreetAddress1(oHomeStreetAddress1, null);
}

function isValidBillingStreetAddress1() {
	return isValidStreetAddress1(oBillingStreetAddress1, sCardType + " billing");
}

function isValidBusinessStreetAddress1() {
	return isValidStreetAddress1(oBusinessStreetAddress1, "business");
}




/*******************************************/
/*******************************************/

function isValidCity(oCity, sLabel) {
	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (isBlank(oCity.value)) {
		oCity.select();
		oCity.focus();

		alert("Please enter your " + sLabel + "city.");

		return false;
	}

	return true;
}

function isValidHomeCity() {
	return isValidCity(oHomeCity, null);
}

function isValidBillingCity() {
	return isValidCity(oBillingCity, sCardType + " billing");
}

function isValidBusinessCity() {
	return isValidCity(oBusinessCity, "business");
}




/*******************************************/
/*******************************************/

function isValidState(oState, sLabel) {
	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (oState.selectedIndex == 0) {
		oState.focus();

		alert("Please select your " + sLabel + "state.");

		return false;
	}

	return true;
}

function isValidHomeState() {
	return isValidState(oHomeState, null);
}

function isValidBillingState() {
	return isValidState(oBillingState, sCardType + " billing");
}

function isValidBusinessState() {
	return isValidState(oBusinessState, "business");
}




/*******************************************/
/*******************************************/

function isValidZipPostal(oCountry, oZipPostal, sLabel) {
	var bUSZip;

	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (oCountry) {
		if ((oCountry.selectedIndex == iCanadaIndex)
		  && (isBlank(oZipPostal.value))) {
			oZipPostal.focus();
			oZipPostal.select();

			alert("Please enter your " + sLabel + "postal code.");

			return false;
		}
	}

	if (oCountry) {
		bUSZip = oCountry.selectedIndex == iUnitedStatesIndex;
	}
	else {
		bUSZip = true;
	}

	if (bUSZip) {
		if (isBlank(oZipPostal.value)) {
			oZipPostal.focus();
			oZipPostal.select();

			alert("Please enter your " + sLabel + "zip code.");

			return false;
		}

		if (!isValidZip(oZipPostal.value)) {
			oZipPostal.focus();
			oZipPostal.select();

			alert("Please enter a valid " + sUnitedStatesCode + " zip code.");

			return false;
		}
	}

	return true;
}

function isValidHomeZipPostal() {
	return isValidZipPostal(oHomeCountry, oHomeZipPostal, null);
}

function isValidBillingZipPostal() {
	return isValidZipPostal(oBillingCountry, oBillingZipPostal, sCardType + " billing");
}

function isValidBusinessZipPostal() {
	return isValidZipPostal(oBusinessCountry, oBusinessZipPostal, "business");
}




/*******************************************/
/*******************************************/

function isValidStateProvinceCountry(oStateProvince, oCountry, sLabel) {
	var sValue, sCountryCode;

	if (sLabel == null) {
		sLabel = "";
	}
	else {
		if (sLabel.length > 0) {
			sLabel += " ";
		}
	}

	if (oStateProvince.selectedIndex == 0) {
		if (oCountry.selectedIndex == iUnitedStatesIndex) {
			oStateProvince.focus();

			alert("Please select your " + sLabel + "state.");

			return false;
		}

		if (oCountry.selectedIndex == iCanadaIndex) {
			oStateProvince.focus();

			alert("Please select your " + sLabel + "province.");

			return false;
		}

		oStateProvince.focus();

		alert("Please select your " + sLabel + "state or province.");

		return false;
	}
	else {
		sValue = oStateProvince.options[oStateProvince.selectedIndex].value;

		if (sValue == sOther) {
			if (oCountry.selectedIndex == 0) {
				oCountry.focus();

				alert("Please select your " + sLabel + "country.");

				return false;
			}
			else {
				if  ((oCountry.selectedIndex == iUnitedStatesIndex)
				  || (oCountry.selectedIndex == iCanadaIndex)) {
					oCountry.focus();

					alert("Please select a country other than United States or Canada when selectiong a " +
						sLabel + "state/province of \"Other\".") ;

					return false;
				}
			}
		}
		else {
			sCountryCode = sValue.substr(sValue.indexOf("-")+1);

			if (sCountryCode == sUnitedStatesCode) {
				if (oCountry.selectedIndex != iUnitedStatesIndex) {
					oCountry.focus();

					alert("Please select United States when selecting a " + sLabel + "state.");

					return false;
				}
			}
			else {
				if (oCountry.selectedIndex != iCanadaIndex) {
					oCountry.focus();

					alert("Please select Canada when selecting a " + sLabel + "province.");

					return false;
				}
			}
		}
	}

	return true;
}

function isValidHomeStateProvinceCountry() {
	return isValidStateProvinceCountry(oHomeStateProvince, oHomeCountry, null);
}

function isValidBillingStateProvinceCountry() {
	return isValidStateProvinceCountry(oBillingStateProvince, oBillingCountry, sCardType + " billing");
}

function isValidBusinessStateProvinceCountry() {
	return isValidStateProvinceCountry(oBusinessStateProvince, oBusinessCountry, "business");
}




/*******************************************/
/*******************************************/

function isValidCardType() {
	sCardType = "";

	if (oCardType.selectedIndex == 0) {
		oCardType.focus();

		alert("Please select the credit card type.");

		return false;
	}

	sCardType = oCardType.options[oCardType.selectedIndex].value;

	return true;
}


function isValidCardName() {
	if (isBlank(oCardName.value)) {
		oCardName.focus();
		oCardName.select();

		alert("Please enter the name as it appears on the " + sCardType + " card.");

		return false;
	}

	return true;
}


function isValidCardNumber() {
	if (isBlank(oCardNumber.value)) {
		oCardNumber.focus();
		oCardNumber.select();

		alert("Please enter the " + sCardType + " card number.");

		return false;
	}

	if (!isValidCreditCard(oCardNumber.value)) {
		oCardNumber.focus();
		oCardNumber.select();

		alert("Please enter a valid " + sCardType + " number with no dashes or spaces.");

		return false;
	}

	return true;
}


function isValidCardExpiration() {
	var todaysDate = new Date();

	if (oExpMonth.selectedIndex == 0) {
		oExpMonth.focus();

		alert("Please select the " + sCardType + " expiration month.");

		return false;
	}

	if (oExpYear.selectedIndex == 0) {
		oExpYear.focus();

		alert("Please select the " + sCardType + " expiration year.");

		return false;
	}

	if (parseInt(oExpYear.options[oExpYear.selectedIndex].value) == todaysDate.getFullYear()) {
		var sExpMonth = oExpMonth.options[oExpMonth.selectedIndex].value;

		if (sExpMonth.substr(0,1) == "0") {
			sExpMonth = sExpMonth.substr(1,1);
		}

		if (parseInt(sExpMonth) < (todaysDate.getMonth() + 1)) {
			oForm.selectExpMonth.focus();

			alert("Your " + sCardType + " card expiration date cannot occur in the past.");

			return false;
		}
	}

	return true;
}




function isValidCardSIC() {
	if (!isValidNumber(oCardSIC.value)) {
		oCardSIC.focus();
		oCardSIC.select();

		alert("Please enter the 3-4 digit number from the back of the " + sCardType + " card.");

		return false;
	}
	else {
		var iCardSIC;

		iCardSIC = parseInt(oCardSIC.value);

		if  (iCardSIC > 9999
		  || iCardSIC < 100) {
			oCardSIC.focus();
			oCardSIC.select();

			alert("Please enter the 3-4 digit number from the back of the " + sCardType + " card.");

			return false;
		}
	}

	return true;
}


function showCardSICHelp() {
	window.open('/utils/SiteSICHelp.htm','SICHelpWindow','width=625,height=670,scrollbars=no');
}



/*******************************************/
/*******************************************/



function setBillingToShipping(oCheckbox) {
	if (!oCheckbox.checked) {
		return;
	}

	if (oBillingCity
	  &&  oHomeCity) {
		oHomeCity.value = oBillingCity.value;
	}

	if (oBillingStreetAddress1
	  &&  oHomeStreetAddress1) {
		oHomeStreetAddress1.value = oBillingStreetAddress1.value;
	}

	if (oBillingStreetAddress2
	  &&  oHomeStreetAddress2) {
		oHomeStreetAddress2.value = oBillingStreetAddress2.value;
	}

	if (oBillingCity
	  &&  oHomeCity) {
		oHomeCity.value = oBillingCity.value;
	}

	if (oBillingState
	  &&  oHomeState) {
		oHomeState.selectedIndex = oBillingState.selectedIndex;
	}

	if (oBillingStateProvince
	  &&  oHomeStateProvince) {
		oHomeStateProvince.selectedIndex = oBillingStateProvince.selectedIndex;
	}

	if (oBillingCountry
	  &&  oHomeCountry) {
		oHomeCountry.selectedIndex = oBillingCountry.selectedIndex;
	}

	if (oBillingZipPostal
	  &&  oHomeZipPostal) {
		oHomeZipPostal.value = oBillingZipPostal.value;
	}

	if (oBillingEmail
	  &&  oHomeEmail) {
		oHomeEmail.value = oBillingEmail.value;
	}

	if (oBillingPhone
	  &&  oHomePhone) {
		oHomePhone.value = oBillingPhone.value;
	}
}