// Set Global Variables
var theSpaces = / /g;
var theChars = /\D/g;
var theNums = /\d/g;
var theRemote;

// Add Item
function addItem(theForm, theQuantity, theItem, theSize, theColor, thePrice, theConfirm) {
if (theQuantity) {
	theForm.Quantity.value = theQuantity;
	}
if (theItem) {
	theForm.Item.value = theItem;
	}
if (theSize) {
	theForm.Size.value = theSize;
	}
if (theColor) {
	theForm.Color.value = theColor;
	}
if (thePrice) {
	theForm.Price.value = thePrice;
	}
if (theConfirm == "y") {
	if(!confirm("Do you wish to Add the following Item to your Cart.\n\n" + theItem + "\n" + formatCurrency(thePrice))) {
		return false;
		}
	}
}

// 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 (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 = "inline";
	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 = "inline";
	theForm[theState].selectedIndex = 0;
	}
}

// Set Shipping Address
function setShippingAddress(theForm) {
if (theForm.ShipToDifferent.checked) {
	document.getElementById("ShippingAddress").style.display = "";
	document.getElementById("ShippingSpacer").style.display = "";
	document.getElementById("ShippingFirstName").style.display = "";
	document.getElementById("ShippingLastName").style.display = "";
	document.getElementById("ShippingCompany").style.display = "";
	document.getElementById("ShippingAddress1").style.display = "";
	document.getElementById("ShippingAddress2").style.display = "";
	document.getElementById("ShippingCity").style.display = "";
	document.getElementById("ShippingCountry").style.display = "";
	document.getElementById("ShippingProvince").style.display = "";
	document.getElementById("ShippingZip").style.display = "";
	document.getElementById("ShippingPhone").style.display = "";
	theForm.ShippingCountry.selectedIndex = 1;
	if (theForm.ShippingCountry[theForm.ShippingCountry.selectedIndex].value == "United States") {
		theForm.ShippingFirstName.focus();
		theForm.ShippingState.disabled = false;
		document.getElementById("ShippingState").style.display = "";
		document.getElementById("ShippingProvince").style.display = "none";
		}
		theForm.ShippingFirstName.focus();
	}
else {
	document.getElementById("ShippingAddress").style.display = "none";
	document.getElementById("ShippingSpacer").style.display = "none";
	document.getElementById("ShippingFirstName").style.display = "none";
	document.getElementById("ShippingLastName").style.display = "none";
	document.getElementById("ShippingCompany").style.display = "none";
	document.getElementById("ShippingAddress1").style.display = "none";
	document.getElementById("ShippingAddress2").style.display = "none";
	document.getElementById("ShippingCity").style.display = "none";
	document.getElementById("ShippingCountry").style.display = "none";
	document.getElementById("ShippingState").style.display = "none";
	document.getElementById("ShippingProvince").style.display = "none";
	document.getElementById("ShippingZip").style.display = "none";
	document.getElementById("ShippingPhone").style.display = "none";
	theForm.ShippingFirstName.value = "";
	theForm.ShippingLastName.value = ""
	theForm.ShippingCompany.value = "";
	theForm.ShippingAddress1.value = "";
	theForm.ShippingAddress2.value = "";
	theForm.ShippingCity.value = "";
	theForm.ShippingCountry.selectedIndex = 0;
	theForm.ShippingState.selectedIndex = 0;
	theForm.ShippingProvince.value = "";
	theForm.ShippingZip.value = "";
	theForm.ShippingPhone.value = "";
	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 < 8 || theAt < 2 || (thePeriod > theLength - 3 || thePeriod < theLength - 5) || theAt > thePeriod - 3)) {
	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.UpdateTotal.disabled = true;
	theForm.EmptyCart.disabled = true;
	theForm.ConfirmOrder.disabled = true;
	theForm.Submit.disabled = true;
}

// 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();
}

// Show Processing Message
function showProcessingMessage(theObject, theWidth, theHeight) {
alert(theWidth);
var theObjectPosition = findPosition(theObject);
alert("Success!");
var theObjectLeft = theObjectPosition(0);
var theObjectTop = theObjectPosition(1);
alert(theObjectLeft);
alert(theObjectTop);
}

// Find Position
function findPosition(theObject) {
var theObjectTemp = document.getElementById(theObject);
var theObjectLeft = theObjectTemp.offsetLeft;
alert(document.getElementById(theObject).offsetLeft);
return false;
var theObjectTop = theObjectTemp.offsetTop;
if (theObjectTemp.offsetParent) {
	while (theObjectTemp = theObjectTemp.offsetParent) {
		theObjectLeft += theObjectTemp.offsetLeft;
		theObjectTop += theObjectTemp.offsetTop;
		}
	}
	return [theObjectLeft, theObjectTop];
}

// Give Focus
function giveFocus(theForm, theField) {
	eval("document.forms[" + theForm + "]." + theField + ".focus()");
}