function validateNumber(FNumber,FAllowZero)
{
if (FAllowZero)
	var pattern = /\D/;
else
	var pattern = /\D|^0/;

if (pattern.test(FNumber.value))
{
	message = "This field accepts positive whole numbers only.\n\n"
	message += "Please try again.  Thank you."
	alert(message);
	FNumber.value = FNumber.defaultValue;
	FNumber.focus();
	return false;
}
else
	return true;
}

function validateDecimal(FNumber,FAllowNeg)
{
if (FAllowNeg)
{
	var pattern = /[^\d\-\.]|\-{2,}|\.{2,}/;
	PosText = "";
}
else
{
	var pattern = /[^\d\.]|\.{2,}/;
	PosText = "postive ";
}
if (pattern.test(FNumber.value))
{
	message = "This field accepts " + PosText + "numbers only.\n\n"
	message += "Please try again.  Thank you."
	alert(message);
	FNumber.value = FNumber.defaultValue;
	FNumber.focus();
	return false;
}
else
	return true;
}

function validateDate(FDate)
{
isValidDate = true;
if (FDate.value != "")
{
	if (isNaN(Date.parse(FDate.value)))
		isValidDate = false;
	else
	{
		dateArray = FDate.value.split("/");
		if (dateArray.length == 3)
		{
			if (dateArray[0].length > 2 || dateArray[1].length > 2 || dateArray[2].length > 4)
				isValidDate = false;

			monthVal = parseInt(dateArray[0],10);
			dayVal = parseInt(dateArray[1],10);
			yearVal = parseInt(dateArray[2],10);
			if (isNaN(monthVal) || isNaN(dayVal) || isNaN(yearVal))
				isValidDate = false;
			else if (monthVal < 1 || monthVal > 12)
				isValidDate = false;
			else if (dayVal < 1 || dayVal > 31)
				isValidDate = false;
			else if (yearVal < 1900 || yearVal >= 2078)
				isValidDate = false;
			else if (monthVal == 2)
			{
				if (yearVal % 4 == 0 && dayVal > 29)
					isValidDate = false;
				else if (yearVal % 4 != 0 && dayVal > 28)
					isValidDate = false;
			}
			else if ((monthVal == 4 || monthVal == 6 || monthVal == 9 || monthVal == 11) && dayVal == 31)
				isValidDate = false;
		}
		else
			isValidDate = false;
	}
}

if (isValidDate)
	return true;
else
{
	alert("You have entered an invalid date.\nPlease use the mm/dd/yyyy format.");
	FDate.value = FDate.defaultValue;
	FDate.focus();
	return false;
}
}

function validatePrice(FPrice)
{
if (validateDecimal(FPrice))
{
	FPrice.value = rectifyPrice(FPrice.value);
	return true;
}
else
	return false;
}

function rectifyPrice(FPrice)
{
FPrice = FPrice.toString();
if (FPrice == "")
	return "";
var PriceArray = FPrice.split(".");
if (PriceArray.length == 1)
	FPrice = PriceArray[0] + ".00";
else if (PriceArray[1].length == 0)
	FPrice = PriceArray[0] + ".00";
else if (PriceArray[1].length == 1)
	FPrice = PriceArray[0] + "." + PriceArray[1] + "0";
else
	FPrice = PriceArray[0] + "." + PriceArray[1].slice(0,2);
return FPrice;
}

function commaPrice(FPrice)
{
FPrice = rectifyPrice(FPrice);
if (FPrice == "")
	return "";

var PriceArray = FPrice.split(".");
priceString = "." + PriceArray[1];
for (i=PriceArray[0].length; i >= 0; i=i-3)
{
	if (i-3 <= 0)
		priceString = PriceArray[0].slice(0,i) + priceString;
	else
		priceString = "," + PriceArray[0].slice(i-3,i) + priceString;
}
return priceString;
}

function roundTwoDigits(FNumber)
{
FNumber = FNumber.toString();
var numberArray = FNumber.split(".");
if (numberArray.length == 1)
	return FNumber;
else if (numberArray[1].length < 3)
	return FNumber;

wholeNumber = numberArray[0];
tenthsDigit = parseInt(numberArray[1].charAt(0));
hundredthsDigit = parseInt(numberArray[1].charAt(1));
decisionDigit = parseInt(numberArray[1].charAt(2));
if (decisionDigit >= 5)
{
	hundredthsDigit++;
	if (hundredthsDigit == 10)
	{
		hundredthsDigit = 0;
		tenthsDigit++;
		if (tenthsDigit == 10)
		{
			tenthsDigit = 0;
			wholeNumber++;
		}
	}
}
return parseFloat(wholeNumber + "." + tenthsDigit + hundredthsDigit);
}

function validateEmail(FEmail)
{
FEmail.value = FEmail.value.replace(/\s/g,"");

var pattern = /^[\w-_]+(\.[\w-_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,6})$/;
if (FEmail.value == "")
	return true;
else if (pattern.test(FEmail.value))
	return true;
else
{
	message = "You have entered an invalid e-mail address.\n\n";
	message += "Please try again.  Thank you.";
	alert(message);
	FEmail.value = FEmail.defaultValue;
	FEmail.focus();
	return false;
}
}

function changeState(FState,FCountry)
{
stateValue = selectValue(FState);
firstSemicolonPos = stateValue.indexOf(";");
lastSemicolonPos = stateValue.lastIndexOf(";");
if (firstSemicolonPos != -1)
{
	if (firstSemicolonPos == lastSemicolonPos)
		setSelectBox(FCountry,stateValue.slice(firstSemicolonPos+1));
	else
		setSelectBox(FCountry,stateValue.slice(firstSemicolonPos+1,lastSemicolonPos));
}
}

function changeCountry(FCountry,FState)
{
countryValue = selectValue(FCountry);
firstSemicolonPos = countryValue.indexOf(";");
lastSemicolonPos = countryValue.lastIndexOf(";");
if (firstSemicolonPos != -1)
{
	if (firstSemicolonPos == lastSemicolonPos)
		setSelectBox(FState,countryValue.slice(firstSemicolonPos+1));
	else
		setSelectBox(FState,countryValue.slice(firstSemicolonPos+1,lastSemicolonPos));
}
}

function validateState(FState,FCountry)
{
stateValue = selectValue(FState);
stateFirstSemicolonPos = stateValue.indexOf(";");
stateLastSemicolonPos = stateValue.lastIndexOf(";");
countryValue = selectValue(FCountry);
countryFirstSemicolonPos = countryValue.indexOf(";");
countryLastSemicolonPos = countryValue.lastIndexOf(";");

if (stateFirstSemicolonPos == -1)
	stateCountry = "";
{
	if (stateFirstSemicolonPos == stateLastSemicolonPos)
		stateCountry = stateValue.slice(stateFirstSemicolonPos+1);
	else
		stateCountry = stateValue.slice(stateFirstSemicolonPos+1,stateLastSemicolonPos);
}

if (countryFirstSemicolonPos == -1)
	countryState = "";
{
	if (countryFirstSemicolonPos == countryLastSemicolonPos)
		countryState = countryValue.slice(countryFirstSemicolonPos+1);
	else
		countryState = countryValue.slice(countryFirstSemicolonPos+1,countryLastSemicolonPos);
}

if ((stateCountry == "") && (countryState == ""))
	return false;
else if (stateCountry == selectText(FCountry))
	return true;
else if (countryState == selectText(FState))
	return true;
else
	return false;
}

function setSelectBox(FSelectBox,FText)
{
for (Fi=0; Fi < FSelectBox.length; Fi++)
{
	if (FSelectBox.options[Fi].text == FText)
	{
		FSelectBox.selectedIndex = Fi;
		break;
	}
}
}

function selectText(FSelectBox)
{
return FSelectBox.options[FSelectBox.selectedIndex].text;
}

function selectValue(FSelectBox)
{
return FSelectBox.options[FSelectBox.selectedIndex].value;
}

function refreshCart()
{
if (parent.frames[0].name == "NexternalCart")
	cartFrame = parent.frames[0];
else if (parent.frames[1].name == "NexternalCart")
	cartFrame = parent.frames[1];
cartFrame.location.replace("cart.asp?RecheckCoupon=True&Count=" + Math.random() + "&OverrideJava=True#Bottom");
}

function autoChange(FIsPopulated,FRadioCheckbox)
{
if ((FRadioCheckbox != null) && (FIsPopulated.value != ""))
	FRadioCheckbox.checked = true;
}

function validateNoSpace(FInput)
{
if (FInput.value == "")
	return true;
else if (FInput.value.indexOf(" ") != -1)
{
	message = "This field does not accept spaces.\n\n";
	message += "Please try again.  Thank you.";
	alert(message);
	FInput.value = FInput.defaultValue;
	FInput.focus();
	return false;
}
else
	return true;
}

function getClientDate()
{
dateObj = new Date();
curMonth = dateObj.getMonth()+1;
curDay = dateObj.getDate();
curYear = dateObj.getYear();
if (curYear < 1000)
	curYear += 1900;
curHours = dateObj.getHours();
curMinutes = dateObj.getMinutes();
curSeconds = dateObj.getSeconds();

curDate = curMonth + "/" + curDay + "/" + curYear + " " + curHours + ":" + curMinutes + ":" + curSeconds;
return curDate;
}

function isWhitespace(FValue)
{
var pattern = /^\s*$/;
if (pattern.test(FValue))
	return true;
else
	return false;
}

function isPOBox(FAddressLine1,FAddressLine2)
{
address1Chars = FAddressLine1.value.replace(/\W/g,"");

var poBoxTest = /^pobox|postofficebox/i;
if (poBoxTest.test(address1Chars))
{
	if (FAddressLine2.value == "")
		return true;
	else
	{
		address2Chars = FAddressLine2.value.replace(/\W/g,"");
		if (poBoxTest.test(address2Chars))
			return true;
		else
			return false;
	}
}
else
	return false;
}

function enforceMaximum(FNumber,FAllowZero,FType,FMaximum)
{
switch (FType)
{
case "Number":
	validation = validateNumber(FNumber,FAllowZero);
	break;
case "Decimal":
	validation = validateDecimal(FNumber,false);
	break;
case "Price":
	validation = validatePrice(FNumber,false);
	break;
}
if (validation)
{
	if (FNumber.value > FMaximum)
	{
		alert("Ths value may not exceed " + FMaximum + ".\n\nPlease try again.  Thank you.");
		FNumber.value = FNumber.defaultValue;
		FNumber.focus();
		return false;
	}
	else
		return true;
}
else
	return false;
}


function resetSelectBox(FSelectBox)
{
for (Fi=0; Fi < FSelectBox.length; Fi++)
{
	if (FSelectBox.options[Fi].defaultSelected)
	{
		FSelectBox.selectedIndex = Fi;
		break;
	}
}
}

function promptNew(FSelect,FName,FFindMatch,FDeleteNone,FMaxLength)
{
if (selectValue(FSelect) == 0 && selectValue(FSelect) != "")
{
	EnteredName = prompt("Please enter the new " + FName + ":","");
	if (EnteredName == null)
	{
		resetSelectBox(FSelect);
		return false;
	}
	else if (isWhitespace(EnteredName))
	{
		alert(FName + " cannot be blank.\n\nPlease try again.  Thank you.");
		return promptNew(FSelect,FName,FFindMatch,FMaxLength);
	}
	else if (EnteredName.length > FMaxLength)
	{
		alert(FName + " exceeds the " + FMaxLength + " character limit.\n\nPlease try again.  Thank you.");
		return promptNew(FSelect,FName,FFindMatch,FMaxLength);
	}
	else
	{
		matchFound = false;
		if (FFindMatch)
		{
			for (i=0; i < FSelect.length; i++)
			{
				if (FSelect.options[i].text == EnteredName)
				{
					FSelect.selectedIndex = i;
					matchFound = true;
					break;
				}
			}
		}
		if (!matchFound)
		{
			FSelect.options[FSelect.selectedIndex].value = EnteredName;
			FSelect.options[FSelect.selectedIndex].text = EnteredName;
			if (FDeleteNone && FSelect.options[0].value == "")
				FSelect.options[0] = null;
		}
		return true;
	}
}
else
	return true;
}

function browserName()
{
browserAgent = navigator.userAgent;
var patternNetscape = /netscape/i;
var patternOpera = /opera/i;
var patternSafari = /safari/i;

if (patternNetscape.test(browserAgent))
	return "Netscape";
else if (patternOpera.test(browserAgent))
	return "Opera";
else if (patternSafari.test(browserAgent))
	return "Safari";
else
	return "Other";
}

function persistOpera(FURL)
{
if (browserName() == "Opera")
{
	if (parent.frames[0].name == "NexternalProduct")
		productFrame = parent.frames[0];
	else
		productFrame = parent.frames[1];
	productFrame.document.location = FURL;
	return false;
}
else
	return true;
}
