// Set Global Variables
var theSpaces = / /g;
var theInvalids = /["';]/g;
var theChars = /\D/g;
var theNums = /\d/g;
var theRemote;

// Add Item
function addItem(theItemID, theItemSize, theItemColor, theItemQuantity) {
var theForm = document.EasyCart;
if (theItemID) {
	theForm.ItemID.value = theItemID;
	}
if (theItemSize) {
	theForm.ItemSize.value = theItemSize;
	}
if (theItemColor) {
	theForm.ItemColor.value = theItemColor;
	}
if (theItemQuantity) {
	theForm.ItemQuantity.value = theItemQuantity;
	}
}

// Check Quantity
function checkQuantity(theField) {
var theChars = /\D/g;
	theField.value = theField.value.replace(theChars, "");
if (theField.value == "" || theField.value == 0) {
	theField.value = 1;
	}
	theField.value = parseInt(theField.value);
}

// Update Cart
function updateCart(theAction, thePage, theID) {
var theSubmit = true;
var theForm = document.EasyCart;
	theForm.Action.value = theAction;
if (typeof theID != "undefined") {
	theForm.ItemID.value = theID;
	}
if (theAction == "Remove") {
	if (!confirm("Are you sure you wish to Remove this Item from the Cart?")) {
		theSubmit = false;
		}
	}
if (theAction == "Empty") {
	if (!confirm("Are you sure you wish to Empty the Cart?")) {
		theSubmit = false;
		}
	}
if (theAction == "Checkout") {
	if (theForm.ShippingLocationCountry.selectedIndex == 0) {
		alert("You must select a Shipping Country.");
		theForm.ShippingLocationCountry.focus();
		theSubmit = false;
		}
	if (theForm.ShippingLocationCountry[theForm.ShippingLocationCountry.selectedIndex].value == "United States") {
		if (theForm.ShippingLocationState.selectedIndex == 0) {
			alert("You must select a Shipping State.");
			theForm.ShippingLocationState.focus();
			theSubmit = false;
			}
		}
	}
if (theSubmit == true) {
	theForm.action = thePage;
	theForm.submit();
	}
}

// Set Country
function setCountry(theForm, theSection) {
var theCountry = theSection + "Country";
var theState = theSection + "State";
var theProvince = theSection + "Province";
if (theForm[theCountry][theForm[theCountry].selectedIndex].value == "United States") {
	theForm[theState].disabled = false;
	document.getElementById(theState).style.display = "";
	document.getElementById(theProvince).style.display = "none";
	theForm[theProvince].disabled = true;
	theForm[theProvince].value = "";
	}
else {
	document.getElementById(theState).style.display = "none";
	theForm[theState].disabled = true;
	theForm[theProvince].disabled = false;
	document.getElementById(theProvince).style.display = "";
	theForm[theState].selectedIndex = 0;
	}
if (theSection == "Shipping") {
	theForm.ShippingLocationCountry.selectedIndex = theForm.ShippingCountry.selectedIndex;
	updateCart('Shipping', 'Checkout_Action.asp');
	theForm.ShippingLocationState.selectedIndex = theForm.ShippingState.selectedIndex;
	updateCart('Shipping', 'Checkout_Action.asp');
	}
}

// Set Billing Address
function setBillingAddress(theForm) {
if (!theForm.BillToSame.checked) {
	document.getElementById("BillingAddress").style.display = "";
	document.getElementById("BillingSpacer").style.display = "";
	document.getElementById("BillingFirstName").style.display = "";
	document.getElementById("BillingLastName").style.display = "";
	document.getElementById("BillingCompany").style.display = "";
	document.getElementById("BillingAddress1").style.display = "";
	document.getElementById("BillingAddress2").style.display = "";
	document.getElementById("BillingCity").style.display = "";
	document.getElementById("BillingCountry").style.display = "";
	document.getElementById("BillingProvince").style.display = "";
	document.getElementById("BillingZip").style.display = "";
	theForm.BillingCountry.selectedIndex = theForm.ShippingCountry.selectedIndex;
	theForm.BillingState.selectedIndex = theForm.ShippingState.selectedIndex;
	theForm.BillingProvince.value = theForm.ShippingProvince.value;
	if (theForm.BillingCountry[theForm.BillingCountry.selectedIndex].value == "United States") {
		theForm.BillingState.disabled = false;
		document.getElementById("BillingState").style.display = "";
		document.getElementById("BillingProvince").style.display = "none";
		}
	else {
		theForm.BillingProvince.disabled = false;
		document.getElementById("BillingState").style.display = "none";
		document.getElementById("BillingProvince").style.display = "";
		}
		theForm.BillingFirstName.focus();
	}
else {
	document.getElementById("BillingAddress").style.display = "none";
	document.getElementById("BillingSpacer").style.display = "none";
	document.getElementById("BillingFirstName").style.display = "none";
	document.getElementById("BillingLastName").style.display = "none";
	document.getElementById("BillingCompany").style.display = "none";
	document.getElementById("BillingAddress1").style.display = "none";
	document.getElementById("BillingAddress2").style.display = "none";
	document.getElementById("BillingCity").style.display = "none";
	document.getElementById("BillingCountry").style.display = "none";
	document.getElementById("BillingState").style.display = "none";
	document.getElementById("BillingProvince").style.display = "none";
	document.getElementById("BillingZip").style.display = "none";
	theForm.BillingFirstName.value = "";
	theForm.BillingLastName.value = ""
	theForm.BillingCompany.value = "";
	theForm.BillingAddress1.value = "";
	theForm.BillingAddress2.value = "";
	theForm.BillingCity.value = "";
	theForm.BillingCountry.selectedIndex = 0;
	theForm.BillingState.selectedIndex = 0;
	theForm.BillingProvince.value = "";
	theForm.BillingZip.value = "";
	if (theForm.BillingPhone.value == "") {
		theForm.BillingPhone.focus();
		}
	else if (theForm.BillingEmail.value == "") {
		theForm.BillingEmail.focus();
		}
	else {
		theForm.NameOnCard.focus();
		}
	}
}

// Check Zip
function checkZip(theField) {
	theField.value = theField.value.replace(theChars, "");
var theLength = theField.value.length;
var theDash = theField.value.indexOf("-");
if (theLength == 5) {
	return true;
	}
if (theLength == 9) {
	var Part1 = theField.value.substr(0,5);
	var Part2 = theField.value.substr(5,4);
	theField.value = Part1 + "-" + Part2;
	return true;
	}
}

// Check Phone
function checkPhone(theField) {
	theField.value = theField.value.replace(theChars, "");
if (!isNaN(theField.value) && theField.value.length == 10) {
	var Part1 = theField.value.substr(0,3);
	var Part2 = theField.value.substr(3,3);
	var Part3 = theField.value.substr(6,4);
	theField.value = Part1 + "-" + Part2 + "-" + Part3;
	return true;
	}
}

// Check Email
function checkEmail(theField) {
var theInvalidChars = /[^A-Za-z0-9._@-]/g;
	theField.value = theField.value.replace(theInvalidChars, "");
var theLength = theField.value.length;
var theAt = theField.value.indexOf("@");
var thePeriod = theField.value.lastIndexOf(".");
if (!(theLength < 7 || theAt < 2 || (thePeriod > theLength - 2 || thePeriod < theLength - 4) || theAt > thePeriod - 2)) {
	return true;
	}
}

// Check Credit
function checkCredit(theType, theNumber) {
	theNumber.value = theNumber.value.replace(theChars, "");
var theCard = theType.options[theType.selectedIndex].value;
var theValue = theNumber.value;
if (theCard == "V") {
	if ((theValue.length == 13 || theValue.length == 16) && (theValue.substr(0,1) == "4")) {
		return true;
		}
	}
else if (theCard == "M") {
	if ((theValue.length == 16) && (theValue.substr(0,2) >= "51" && theValue.substr(0,2) <= "55")) {
		return true;
		}
	}
else if (theCard == "A") {
	if ((theValue.length == 15) && (theValue.substr(0,2) == "34" || theValue.substr(0,2) == "37")) {
		return true;
		}
	}
else if (theCard == "D") {
	if ((theValue.length == 16) && (theValue.substr(0,4) == "6011")) {
		return true;
		}
	}
}

// Format Currency
function formatCurrency(theValue) {
var theValue = "" + Math.round(eval(theValue) * Math.pow(10,2))
while (theValue.length <= 2) {
	theValue = "0" + theValue;
	}
var theDecimal = theValue.length - 2;
var theDollars = theValue.substring(0,theDecimal)
var theCents = theValue.substring(theDecimal,theValue.length);
var theCommas = /(-?\d+)(\d{3})/
while (theCommas.test(theDollars)) {
	theDollars = theDollars.replace(theCommas, "$1,$2")
	}
	return "$" + theDollars + "." + theCents;
}

// Disable Buttons
function disableButtons(theForm) {
	theForm.KeepShopping.disabled = true;
	theForm.MakeChanges.disabled = true;
	theForm.EmptyCart.disabled = true;
	theForm.ConfirmOrder.disabled = true;
	theForm.Submit.disabled = true;
}

// Show Content
function showContent(theID, theImage, theTitle, theWidth, theHeight) {
if (document.getElementById) {
	var theClientWidth = document.body.clientWidth;
	var theClientHeight = document.body.clientHeight;
	var theCenterWidth = (theClientWidth - theWidth) / 2;
	var theCenterHeight = (theClientHeight - theHeight) / 2;
	if (theID.length < 1) {
		return;
		}
		document.getElementById(theID).style.width = theWidth;
		document.getElementById(theID).style.height = theHeight;
		document.getElementById(theID).style.display = "block";
	}
else {
	makeRemote('Image.asp?Image=' + theImage + '&Width=' + theWidth + '&Height=' + theHeight + '&Title=' + escape(theTitle), theWidth + 22, theHeight + 45, ',Resizable=yes')
	}
}

// Hide Content
function hideContent(theID) {
if (document.getElementById) {
	if (theID.length < 1) {
		return;
		}
		document.getElementById(theID).style.display = "none";
	}
}

// Show Error
function showError(theID, theClass, theError) {
if (document.getElementById || document.all) {
	if (document.getElementById) {
		document.getElementById(theID).className = theClass;
		document.getElementById(theID).innerHTML = theError;
		}
	else if (document.all) {
		document.all[theID].className = theClass;
		document.all[theID].innerHTML = theError;
		}
	return false;
	}
else {
	alert(theError);
	return false;
	}
}

// Make Remote
function makeRemote(theURL, theWidth, theHeight, theOptions) {
var theCenterWidth = (window.screen.width - theWidth) / 2;
var theCenterHeight = (window.screen.height - theHeight) / 2;
if (theRemote) {
	theRemote.close();
	}
	theRemote = window.open(theURL, "Remote", "Width=" + theWidth + ",Height=" + theHeight + "," + theOptions);
	theRemote.moveTo(theCenterWidth, theCenterHeight - 50);
	theRemote.focus();
}

// Remove Invalids
function removeInvalids(theField) {
	theField.value = theField.value.replace(theInvalids, "");
	return theField.value;
}

// Give Focus
function giveFocus(theForm, theField) {
	document[theForm][theField].focus();
}