// This file is Copyright 2009 by Eric L. Wahl/Next Millennium Web, LLC. All rights reserved.

function ValidateBuyNow()
{
	var BPQ = document.form1.BlackPinkQty.value;
	var BAQ = document.form1.BlackAquaQty.value;
	var TotalItems = 0 * 0;
	if (0 != BPQ.length)
	{
		if (isNaN(BPQ))
		{
			alert("Please enter a numeric value for the Black/Pink quantity.");
			document.form1.BlackPinkQty.focus();
			return false;
		}
		if (0 > BPQ)
		{
			alert("The Black/Pink quantity cannot be negative.");
			document.form1.BlackPinkQty.focus();
			return false;
		}
		TotalItems += BPQ * 1;
	}
	if (0 != BAQ.length)
	{
		if (isNaN(BAQ))
		{
			alert("Please enter a numeric value for the Black/Aqua quantity.");
			document.form1.BlackAquaQty.focus();
			return false;
		}
		if (0 > BAQ)
		{
			alert("The Black/Aqua quantity cannot be negative.");
			document.form1.BlackAquaQty.focus();
			return false;
		}
		TotalItems += BAQ * 1;
	}
	if (0 == TotalItems)
	{
		alert("Please enter a quantity for at least one of the items.");
		document.form1.BlackPinkQty.focus();
		return false;
	}
	return true;
}

function ValidateSandH()
{
	// First make sure all required fields actually have a value.
	if (0 == document.form1.SH_FirstName.value.length)
	{
		alert("Please enter your first name.");
		document.form1.SH_FirstName.focus();
		return false;
	}
	if (0 == document.form1.SH_LastName.value.length)
	{
		alert("Please enter your last name.");
		document.form1.SH_LastName.focus();
		return false;
	}
	if (0 == document.form1.SH_Address1.value.length)
	{
		alert("Please enter your address.");
		document.form1.SH_Address1.focus();
		return false;
	}
	if (0 == document.form1.SH_City.value.length)
	{
		alert("Please enter your city.");
		document.form1.SH_City.focus();
		return false;
	}
	if (0 == document.form1.SH_PostCode.value.length)
	{
		alert("Please enter your zip/post code.");
		document.form1.SH_PostCode.focus();
		return false;
	}

	// Basic zip/postal code validation for the countries we support.
	switch (document.form1.SH_Country.value)
	{
		case 'United States':
			if (!(document.form1.SH_PostCode.value.match(/^(\d{5}|\d{5}-\d{4})$/)))
			{
				alert("Please enter a valid zip code.");
				document.form1.SH_PostCode.focus();
				return false;
			}
			break;
		case 'Canada':
			if (!(document.form1.SH_PostCode.value.match(/^[ABCEGHJ-NPRSTVXYabceghj-nprstvxy]\d[A-Za-z] \d[A-Za-z]\d$/)))
			{
				alert("Please enter a valid postal code.");
				document.form1.SH_PostCode.focus();
				return false;
			}
			break;
		case 'Great Britain':
			if (!(document.form1.SH_PostCode.value.match(/^[A-Za-z]{1,2}\d[A-Za-z\d]?\s?\d[ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$/)))
			{
				alert("Please enter a valid postal code.");
				document.form1.SH_PostCode.focus();
				return false;
			}
			break;
	}
	return true;
}

function ValidateBilling()
{
	// First make sure all required fields actually have a value.
	if (0 == document.form1.BL_FirstName.value.length)
	{
		alert("Please enter your first name.");
		document.form1.BL_FirstName.focus();
		return false;
	}
	if (0 == document.form1.BL_LastName.value.length)
	{
		alert("Please enter your last name.");
		document.form1.BL_LastName.focus();
		return false;
	}
	if (0 == document.form1.BL_Address1.value.length)
	{
		alert("Please enter your address.");
		document.form1.BL_Address1.focus();
		return false;
	}
	if (0 == document.form1.BL_City.value.length)
	{
		alert("Please enter your city.");
		document.form1.BL_City.focus();
		return false;
	}
	if (0 == document.form1.BL_PostCode.value.length)
	{
		alert("Please enter your zip/post code.");
		document.form1.BL_PostCode.focus();
		return false;
	}
	if (0 == document.form1.BL_Phone.value.length)
	{
		alert("Please enter your phone number.");
		document.form1.BL_Phone.focus();
		return false;
	}
	if (0 == document.form1.BL_EMail.value.length)
	{
		alert("Please enter your e-mail address.");
		document.form1.BL_EMail.focus();
		return false;
	}
	if (0 == document.form1.BL_CCNum.value.length)
	{
		alert("Please enter your credit card number.");
		document.form1.BL_CCNum.focus();
		return false;
	}
	if (3 > document.form1.BL_CCCSC.value.length)
	{
		alert("Please enter your credit card security code.");
		document.form1.BL_CCCSC.focus();
		return false;
	}
	if (2 > document.form1.BL_CCExpMo.value.length)
	{
		alert("Please enter your credit card expiration month.");
		document.form1.BL_CCExpMo.focus();
		return false;
	}
	if (2 > document.form1.BL_CCExpYr.value.length)
	{
		alert("Please enter your credit card expiration year.");
		document.form1.BL_CCExpYr.focus();
		return false;
	}

	// Basic zip/postal code and phone number validation for the countries we support.
	switch (document.form1.BL_Country.value)
	{
		case 'United States':
			if (!(document.form1.BL_PostCode.value.match(/^(\d{5}|\d{5}-\d{4})$/)))
			{
				alert("Please enter a valid zip code.");
				document.form1.BL_PostCode.focus();
				return false;
			}
			var $TempPhone = document.form1.BL_Phone.value.replace(/\D/g, "");
			if (!($TempPhone.match(/^[2-9]\d{9,}$/)))
			{
				alert("Please enter a valid phone number.");
				document.form1.BL_Phone.focus();
				return false;
			}
			break;
		case 'Canada':
			if (!(document.form1.BL_PostCode.value.match(/^[ABCEGHJ-NPRSTVXYabceghj-nprstvxy]\d[A-Za-z] \d[A-Za-z]\d$/)))
			{
				alert("Please enter a valid postal code.");
				document.form1.BL_PostCode.focus();
				return false;
			}
			var $TempPhone = document.form1.BL_Phone.value.replace(/\D/g, "");
			if (!($TempPhone.match(/^[2-9]\d{9,}$/)))
			{
				alert("Please enter a valid phone number.");
				document.form1.BL_Phone.focus();
				return false;
			}
			break;
		case 'Great Britain':
			if (!(document.form1.BL_PostCode.value.match(/^[A-Za-z]{1,2}\d[A-Za-z\d]?\s?\d[ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$/)))
			{
				alert("Please enter a valid postal code.");
				document.form1.BL_PostCode.focus();
				return false;
			}
			var $TempPhone = document.form1.BL_Phone.value.replace(/\D/g, "");
			if (!($TempPhone.match(/^[0]\d{8,}$/)))
			{
				alert("Please enter a valid phone number.");
				document.form1.BL_Phone.focus();
				return false;
			}
			break;
	}

	// Basic e-mail address validity checking. (Probably matches about 99.9% of all in-use e-mail addresses, as well as allowing some invalid ones too.)
	if (!(document.form1.BL_EMail.value.match(/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/)))
	{
		alert("Please enter a valid e-mail address.");
		document.form1.BL_EMail.focus();
		return false;
	}

	// CC number validity checking.
	if (!IsValidCreditCard(document.form1.BL_CCType.options[document.form1.BL_CCType.selectedIndex].value, document.form1.BL_CCNum.value))
	{
		alert("Please enter a valid credit card type and number.");
		document.form1.BL_CCNum.focus();
		return false;
	}

	// CC expiration date validity checking.
	var CCMonth = parseInt(document.form1.BL_CCExpMo.value, 10);
	var CCYear = parseInt(document.form1.BL_CCExpYr.value, 10);
	if (12 < CCMonth)
	{
		alert("Please enter a valid expiration date that is in the future.");
		document.form1.BL_CCExpMo.focus();
		return false;
	}
	// Just to make things a little easier, we're going to compare the current date to the first day of the month *following* the entered cc exp date.
	if (12 == CCMonth)
	{
		CCMonth = 0;	// Remember, the month is 0 based!
		CCYear++;
	}
	CCYear += 2000;			// This should be fine for the next 80+ years or so. Have to figure out something new by then. :)
	var CCExp = new Date(CCYear, CCMonth, 1);
	var Now = new Date();
	if (CCExp <= Now)
	{
		alert("Please enter a valid expiration date that is in the future.");
		document.form1.BL_CCExpMo.focus();
		return false;
	}

	// Wow, I think we're finally done!
	return true;
}

// This function not only calculates and displays the subtotal, but also prevents non-numeric values from being entered.
function ShowSubtotal()
{
	var BPQ = document.form1.BlackPinkQty.value;
	var BAQ = document.form1.BlackAquaQty.value;
	var TotalItems = (BPQ * 1) + (BAQ * 1);
	var TotalPrice = TotalItems * 29.99;
	document.getElementById("Subtotal").innerHTML = "$" + TotalPrice.toFixed(2);
}

function LimitText(Field, NumChars)
{
	if (Field.value.length > NumChars) Field.value = Field.value.substr(0, NumChars);
}

function LimitTextNumeric(Field, NumChars)
{
	if (Field.value.length > NumChars)
	{
		Field.value = Field.value.substr(0, NumChars).replace(/\D/g, "");
	}
	else
	{
		Field.value = Field.value.replace(/\D/g, "");
	}
}

function IsValidCreditCard(CCType, CCNum)
{
	// Do the regular expression checking first.
	var MyRegEx;
	switch (CCType.toUpperCase())
	{
		case "VISA":
			// Visa: length 16, prefix 4.
			MyRegEx = /^4\d{15}$/;
			break;

		case "MC":
			// Mastercard: length 16, prefix 51-55.
			MyRegEx = /^5[1-5]\d{14}$/;
			break;

		case "DISCOVER":
			// Discover: length 16, prefix 6011.
			MyRegEx = /^6011\d{12}$/;
			break;

		case "AMEX":
			// American Express: length 15, prefix 34 or 37.
			MyRegEx = /^3[4,7]\d{13}$/;
			break;

		case "DINERS":
			// Diners: length 14, prefix 30, 36, or 38.
			MyRegEx = /^3[0,6,8]\d{12}$/;
			break;

		default:
			// Something we don't recognize.
			return false;
	}
	// Remove all non-numeric characters from the cc number. (Just to be sure)
	CCNum = CCNum.replace(/\D/g, "");
	if (!MyRegEx.test(CCNum))
	{
		return false;
	}

	// Checksum ("Mod 10")
	// Add even digits in even length strings or odd digits in odd length strings.
	var Checksum = 0;
	var Start = 2-(CCNum.length % 2);
	var End = CCNum.length;
	for (var i=Start; i<=End; i+=2)
	{
		Checksum += parseInt(CCNum.charAt(i-1));
	}
	// Analyze the odd digits in even length strings or the even digits in odd length strings.
	for (var i=(CCNum.length % 2) + 1; i<End; i+=2)
	{
		var Digit = parseInt(CCNum.charAt(i-1)) * 2;
		if (Digit < 10)
		{
			Checksum += Digit;
		}
		else
		{
			Checksum += (Digit-9);
		}
	}
	if (0 == (Checksum % 10))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function UpdateTotal(ShipField)
{
	ShipCost = 1.0 * GetShipCost(ShipField.value);
	if (0 < Tax)
	{
		Tax = .09 * (ShipCost + Subtotal);
	}
	var TotalPrice = ShipCost + Tax + Subtotal;
	document.getElementById("Tax").innerHTML = Tax.toFixed(2);
	document.getElementById("Total").innerHTML = TotalPrice.toFixed(2);
}

function SwitchShippingCountry(strCountry)
{
	switch (strCountry)
	{
		case 'United States':
			document.getElementById('SH_UsStateRow').style.display = '';
			document.getElementById('SH_CaProvinceRow').style.display = 'none';
			document.getElementById('SH_PcLabel').innerText = 'Zip Code';
			document.getElementById('SH_State_US').name = 'SH_State';
			document.getElementById('SH_State_CA').name = 'SH_State_ND';
			break;
		case 'Canada':
			document.getElementById('SH_UsStateRow').style.display = 'none';
			document.getElementById('SH_CaProvinceRow').style.display = '';
			document.getElementById('SH_PcLabel').innerText = 'Postal Code';
			document.getElementById('SH_State_US').name = 'SH_State_ND';
			document.getElementById('SH_State_CA').name = 'SH_State';
			break;
		case 'Great Britain':
			document.getElementById('SH_UsStateRow').style.display = 'none';
			document.getElementById('SH_CaProvinceRow').style.display = 'none';
			document.getElementById('SH_PcLabel').innerText = 'Postal Code';
			document.getElementById('SH_State_US').name = 'SH_State_ND';
			document.getElementById('SH_State_CA').name = 'SH_State_ND';
			break;
	}
}

function SwitchBillingCountry(strCountry)
{
	switch (strCountry)
	{
		case 'United States':
			document.getElementById('BL_UsStateRow').style.display = '';
			document.getElementById('BL_CaProvinceRow').style.display = 'none';
			document.getElementById('BL_PcLabel').innerText = 'Zip Code';
			document.getElementById('BL_State_US').name = 'SH_State';
			document.getElementById('BL_State_CA').name = 'SH_State_ND';
			break;
		case 'Canada':
			document.getElementById('BL_UsStateRow').style.display = 'none';
			document.getElementById('BL_CaProvinceRow').style.display = '';
			document.getElementById('BL_PcLabel').innerText = 'Postal Code';
			document.getElementById('BL_State_US').name = 'SH_State_ND';
			document.getElementById('BL_State_CA').name = 'SH_State';
			break;
		case 'Great Britain':
			document.getElementById('BL_UsStateRow').style.display = 'none';
			document.getElementById('BL_CaProvinceRow').style.display = 'none';
			document.getElementById('BL_PcLabel').innerText = 'Postal Code';
			document.getElementById('BL_State_US').name = 'SH_State_ND';
			document.getElementById('BL_State_CA').name = 'SH_State_ND';
			break;
	}
}