function submitCreditCard()
{
	with(document.creditCardPage)
	{
		if(shipTotal.value == "$0.00" && shipMethod.value == "UPS")
			alert("Please select a shipping method.");
		else if(cardType.value == "None Selected")
			alert("Please select a credit card type.");
		else if(cardHolderName.value == "")
			alert("Please enter the credit card holder's name.");
		else if(cardNumber.value == "")
			alert("Please enter the credit card number");
		else if((cardType.value == "American Express" && cardNumber.value.length != 15) || isNaN(cardNumber.value))
			alert("Please enter a 15 digit numerical credit card number.");
		else if((cardNumber.value.length != 16 || isNaN(cardNumber.value)) && cardType.value != "American Express")
			alert("Please enter a 16 digit numerical credit card number.");
		else if(isExpired(parseInt(expirationMonth.value), parseInt(expirationYear.value)))
			alert("The expiration date on the credit card has passed.");
		else if(cvv.value.length != 3 || isNaN(cvv.value))
			alert("Please enter a 3-digit CVV Security Code.");
		else
			submit();
	}
}

function isExpired(month, year)
{
	var cal = new Date();
	//alert("CARD: " + month + ", NOW: " + (cal.getMonth()+1));
	
	if(year < cal.getFullYear())
		return true;
	else if((cal.getFullYear() == year) && (month < cal.getMonth()+1))
		return true;
	else
		return false;

}

function phoneFormat(phoneNumber)
{
	var number = "";
	ValidChars = "0123456789";
			
	for(i = 0; i < phoneNumber.length; i++)
	{
		if (ValidChars.indexOf(phoneNumber.charAt(i)) != -1) 
			number += phoneNumber.charAt(i);
	}

	return "(" + number.substring(0,3) + ") " + number.substring(3, 6) + "-" + number.substring(6,10)
}

function isPhoneError(phoneNumber, who)
{
	var ValidChars = "0123456789().- "; 

	for(i = 0; i < phoneNumber.length; i++)
	{
		if (ValidChars.indexOf(phoneNumber.charAt(i)) == -1)
		{
			alert(who + " number \"" + phoneNumber + "\" has invalid characters.");
			return true;
		}
	}

	var number = "";
	ValidChars = "0123456789";
			
	for(i = 0; i < phoneNumber.length; i++)
	{
		if (ValidChars.indexOf(phoneNumber.charAt(i)) != -1) 
			number += phoneNumber.charAt(i);
	}
			
	if(number.length != 10)
	{
		alert(who + " number \"" + phoneNumber + "\" requires 10 numerical digits.");
		return true;
	}

	return false;
}

function noShipMethodSelected()
{
	for(var i = 0; i < document.checkout.shipRadio.length; i++)
		if(document.checkout.shipRadio[i].checked)
			return false;
	return true;

}

function emailNotValid(email)
{
	var reg = new RegExp("((\\w+)[.])*(\\w+)[@]((\\w+)[.])*(\\w+)[.](\\w{2,})");
	output = reg.exec(email);

	if(output != null)
	if(output[0] == email)
		return false;
	return true;
	
	//for(i=0;i<output.length;i++)
	//	alert(output[i]);
}

function checkoutSubmit()
{
	with(document.checkout)
	{
		if(custName.value == "")
			alert("Please enter the customer's full name.");
		else if(custAddress.value == "")
			alert("Please enter the customer's street address.");
		else if(custCity.value == "")
			alert("Please enter the customer's city/town.");
		else if(custState.value == "OTHER" && custProvince.value == "")
			alert("Please enter the customer's Non-US/CA/AU province.");
		else if(custZip.value == "")
			alert("Please enter the customer's zip code.");
		else if(isNaN(custZip.value))
			alert("Please ensure that the customer zip code uses only numeric digits.")
		else if(custZip.value.length != 5 && custZip.value.length != 9)
			alert("The customer's zip code must be either 5 or 9 digits long.");
		else if(custEmail.value == "")
			alert("Please enter the customer's email address.");
		else if(emailNotValid(custEmail.value))
			alert("The customer's email address is not formatted properly.");
		else if(custPhone.value == "")
			alert("Please enter the customer's phone number.");
		else if(isPhoneError(custPhone.value, "Customer's Phone"))
			return;
		else if(isPhoneError(shipPhone.value, "Receiver's Phone"))
			return;		
		else if(shipName.value == "")
			alert("Please enter the recipient's full name.");
		else if(shipAddress.value == "")
			alert("Please enter the recipient's street address.");
		else if(shipCity.value == "")
			alert("Please enter the recipient's city/town.");
		else if(shipState.value == "OTHER" && shipProvince.value == "")
			alert("Please enter the recipient's Non-US/CA/AU province.");
		else if(shipZip.value == "")
			alert("Please enter the recipient's zip code.");
		else if(isNaN(shipZip.value))
			alert("Please ensure that the recipient zip code uses only numeric digits.")
		else if(shipZip.value.length != 5 && shipZip.value.length != 9)
			alert("The recipient's zip code must be either 5 or 9 digits long.");
		else if(shipEmail.value == "")
			alert("Please enter the recipient's email address.");
		else if(emailNotValid(shipEmail.value))
			alert("The recipient's email address is not formatted properly.");
		else if(shipPhone.value == "")
			alert("Please enter the recipient's phone number.");
		else if(isPhoneError(shipPhone.value))
			return;
		else if(isPhoneError(shipPhone.value))
			return;	
		
		else if(noShipMethodSelected())
			alert("Please select a shipping method.");
		else
		{
			custPhone.value = phoneFormat(custPhone.value);
			shipPhone.value = phoneFormat(shipPhone.value);
			submit();
		}
		
	}
}

function shippingCost()
{
	with(document.creditCardPage)
	{
		var inString;
	
		for(i = 0; i < upsSelect.length; i++)
			if(upsSelect[i].checked)
				inString = upsSelect[i].value;
		var price = inString.substring(inString.lastIndexOf('&')+1, inString.length);	
		document.creditCardPage.shipTotal.value = "$" + price.substring(0, price.length - 2) + "." + price.substring(price.length-2, price.length);
		var outString = "" + (parseInt(price) + parseInt(document.creditCardPage.linePrice.value));
		document.creditCardPage.grandTotal.value = "$" + outString.substring(0, outString.length - 2) + "." + outString.substring(outString.length-2, outString.length);
		document.creditCardPage.upsMethodBox.value = inString.substring(inString.indexOf('&')+1, inString.lastIndexOf('&'));
	}
}

function cardTypeSelect()
{
	document.creditCardPage.cardTypeConfirm.value = document.creditCardPage.cardType.value
}

function cardNumberSelect()
{
	

	var number = document.creditCardPage.cardNumber.value;
	var i = Math.min(12, number.length);
	var out = "";
	
	while(i-- > 0)
		out += "x";
	
	if(number.length > 12)
		out += number.substring(12, Math.min(number.length, 16));
		
	document.creditCardPage.cardNumberConfirm.value = out;
}


function copyCat()
{
	with(document.checkout)
	{
		if(shipTo[0].checked)
		{
			shipName.value = custName.value;
			shipBusiness.value = custBusiness.value;
			shipAddress.value = custAddress.value;
			shipCity.value = custCity.value;
			shipState.value = custState.value;
			shipProvince.value = custProvince.value;
			shipCountry.value = custCountry.value;
			shipZip.value = custZip.value;
			shipEmail.value = custEmail.value;
			shipPhone.value = custPhone.value;
		}
	}
}

function enableShipping()
{
	with(document.checkout)
	{
		if(shipTo[0].checked)
		{
			var color = "#ddd";
			var check = true;
			copyCat();
		}
		else
		{
			var color = "#fff";
			var check = false;
		}
		
			shipName.style.backgroundColor=color;
			shipBusiness.style.backgroundColor=color;
			shipName.readOnly=check;
			shipBusiness.readOnly=check;
			shipAddress.style.backgroundColor=color;
			shipAddress.readOnly=check;
			shipCity.style.backgroundColor=color;
			shipCity.readOnly=check;
			shipState.style.backgroundColor=color;
			shipState.disabled=check;
			shipProvince.style.backgroundColor=color;
			shipProvince.readOnly=check;
			shipZip.style.backgroundColor=color;
			shipZip.readOnly=check;
			shipCountry.style.backgroundColor=color;
			shipCountry.disabled=check;
			shipEmail.style.backgroundColor=color;
			shipEmail.readOnly=check;
			shipPhone.style.backgroundColor=color;
			shipPhone.readOnly=check;
			
	}
}

function searchBar()
{	
	with(document.searchForm)
	{
		if(searchText.value == "Enter Search Text Here")
		{
			searchText.style.color='black';
			searchText.value = "";
		}
	}
}

function searchBarBlur()
{
	with(document.searchForm)
	{
		if(searchText.value == "")
		{
			searchText.style.color='gray';
			searchText.value = "Enter Search Text Here";
		}
	}
}

function addToCart()
{
	with(document.addItem)
	{
		if(isNaN(qty.value))
			alert("Please enter a numerical value for the quantity of " +
					"this item to place into the shopping cart.");
		else if(qty.value < 1)
			alert("Please enter a quantity of at least 1 to proceed.");
		else if(parseInt(qty.value) > 20)
		{
			if(confirm("Are you sure you with to add " + qty.value + " of this item " +
					"to your shopping cart?"))
					submit();
		}		
		else
		{
			qty.value = Math.ceil(parseFloat(qty.value));
			submit();
		}
	}

}

function updateCartFunction()
{
	with(document.cartForm)
	{
		submit();
	}
}

function emptyCartFunction()
{
	if(confirm("Are you sure you want to remove all items from your cart?"))
	{
		document.cartForm.cartEmpty.value = "yes";
		document.cartForm.submit();
	}
}




