//Copyright 2003 Mavice LLC

var defaultEmptyOK = false

var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."
var iPrefix = "This field must be a valid "
var iSuffix = ". Please reenter it now."
var iUSPhone = "10 digit U.S. phone number (like 415 555 1212)"
var iZIPCode = "5 or 9 digit U.S. ZIP Code (like 94043)"
var iSSN = "US Social Security Number (like 444 44 4444)"
var iDB = "D & B Number (like 44 444 4444)"
var iDistChan = "Distribution Channel (like 30)"
var iSalesOrg = "Sales Organization (like 1000)"
var iOrder = "Order Number"
var iPartner = "Partner Number"
var iEmail = "email address (like myname@mydomain.com)"
var iPassword = "password"
var iDate = "Date (like mm dd yyyy)"
var sEmail = "Email Address"

var whitespace = " \t\n\r"

function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

function isAlpha (c)
{
	if ((c >= "0") && (c <= "9")) { return true }
	if ((c >= "a") && (c <= "z")) { return true }
	if ((c >= "A") && (c <= "Z")) { return true }
	if (c == " ") { return true }
	return false
}

function isAlphaExt (c)
{
	if ((c >= "0") && (c <= "9")) { return true }
	if ((c >= "a") && (c <= "z")) { return true }
	if ((c >= "A") && (c <= "Z")) { return true }
	if (c == " ") { return true }
	if (c == ".") { return true }
	if (c == ",") { return true }
	if (c == "-") { return true }
	if (c == "#") { return true }
	if (c == "_") { return true }
	if (c == "?") { return true }	
	if (c == "(") { return true }
	if (c == ")") { return true }
	if (c == "\n") { return true }
	if (c == "\r") { return true }	
	return false
}

function isPhoneNumber(c)
{
	if ((c >= "0") && (c <= "9")) { return true }
	if (c == ".") { return true }
	if (c == "-") { return true }
	if (c == "(") { return true }
	if (c == ")") { return true }
	return false
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{
	var i
	
	if (isEmpty(s)) return true
	
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i)
		if (whitespace.indexOf(c) == -1) return false
	}
	return true
}

function TrimWhitespace(s)
	{
		var i = 0;
		var j = s.length-1;

		if (isEmpty(s)) 
			return '';

		while ((j >= 0) && (whitespace.indexOf(s.charAt(j)) != -1))
			j--;
		
		if (j == -1)
			return '';
				
		while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
			i++;
		 
		return s.substring (i, j+1)
	}


function isInteger (s)
{
	var i, c
	for (i = 0; i < s.length; i++)
	{
		c = s.charAt(i)
		if (!isDigit(c)) return false
	}
	return true
}

function warnEmpty (theField, s)
{
	theField.focus()
	alert(mPrefix + s + mSuffix)
	return false
}

function warnInvalid(theField, s)
{
	theField.focus()
	theField.select()
	alert(iPrefix + s + iSuffix)
	return false
}

function charInString (c, s)
{
	for (i = 0; i < s.length; i++)
	{
		if (s.charAt(i) == c) return true
	}
	return false
}
function stripInitial (s, c)
{
	var i = 0
	while ((i < s.length) && (s.charAt(i) == c))
		i++
	return s.substring (i, s.length)
}
function stripEndings (s, c)
{
	var i = s.length-1
	while ((i >= 0) && (s.charAt(i) == c))
		i--;
	return s.substring (0, i+1)
}
function stripCharsInBag (s, bag)
{
	var i
	var returnString = ""
	
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i)
		if (bag.indexOf(c) == -1) returnString += c
	}
	return returnString
}
function stripWhitespace (s)
{
	return stripCharsInBag (s, whitespace)
}
function isEmail (s)
{
	s = stripInitial(s, ' ');
	s = stripEndings(s, ' ');

	if (charInString(" ", s)) return false
	
	var i = 1
	while ((i < s.length) && (s.charAt(i) != "@"))	i++
	if ((i >= s.length) || (s.charAt(i) != "@")) return false 
	else i += 2
	while ((i < s.length) && (s.charAt(i) != "."))   i++
	if ((i >= s.length - 1) || (s.charAt(i) != ".")) return false 
	return true
}
function isCorrectNumber (theField, s){
	return (isInteger(theField.value) && theField.value.length == s)
}
function getRadioButtonValue (radio, count)
{
	if (count == 1)
	{	
		if (radio.checked)
			return radio.value
		else
			return -1
	}
	for (var i = 0; i < radio.length; i++)
		if (radio[i].checked)
			return radio[i].value
	return -1	
}
function isRadioSelected (radio, count)
{
	if (count == 1)
	{
		if (radio.checked)
			return true
		else
			return false
	}
	for (var i = 0; i < radio.length; i++)
		if (radio[i].checked)
			return true
	return false
}
function checkString (theField, s, emptyOK)
{
	theField.value = TrimWhitespace(theField.value)
	if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmpty (theField, s)
	var c
	for (var i = 0; i < theField.value.length; i++)
	{
		c = theField.value.charAt(i)
		if (!isAlpha(c)) { warnInvalid(theField, s); return false; }
	}
	return true
}

function checkEmpty (theField, s, emptyOK)
{
	theField.value = TrimWhitespace(theField.value)
	if (checkEmpty.arguments.length == 2) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmpty (theField, s)
	return true
}

function checkCheckBox(theField,s)
{
	if (!theField.checked)
	{warnEmpty(theField, s); return false;}
	return true

}	

function checkNumber (theField, s, emptyOK)
{
	theField.value = TrimWhitespace(theField.value)
	if (checkNumber.arguments.length == 2) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmpty (theField, s)
	var c
	for (var i = 0; i < theField.value.length; i++)
	{
		c = theField.value.charAt(i)
		if (!isPhoneNumber(c)) { warnInvalid(theField, s); return false; }
	}
	return true
}

function checkStringExt (theField, s, emptyOK)
{
	if (checkStringExt.arguments.length == 2) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmpty (theField, s)
	var c
	for (var i = 0; i < theField.value.length; i++)
	{
		c = theField.value.charAt(i)
		if (!isAlphaExt(c)) { warnInvalid(theField, s); return false; }
	}
	return true
}

function checkNumThree (aField, aLen, bField, bLen, cField, cLen, msg, emptyOK)
{
	aField.value = TrimWhitespace(aField.value)
	bField.value = TrimWhitespace(bField.value)
	cField.value = TrimWhitespace(cField.value)	
	if (checkNumThree.arguments.length == 5) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(aField.value) && isEmpty(bField.value) && isEmpty(cField.value))) return true
	if (isCorrectNumber(aField, aLen) && isCorrectNumber(bField, bLen) && isCorrectNumber(cField, cLen)) return true
	return warnInvalid (aField, msg)
}

function checkNumLength (theField, length, fieldname, emptyOK)
{
	if (checkNumLength.arguments.length == 3) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isCorrectNumber(theField, length)) return true
	return warnInvalid (theField, fieldname)
}

function checkQuantity (theField, emptyOK)
{
    if (checkQuantity.arguments.length == 1) emptyOK = defaultEmptyOK
     if ((emptyOK == true) && (isEmpty(theField.value))) return true
     if (isInteger(theField.value)) return true

     alert('Please enter a positive integer')
     theField.select()
     theField.focus()
     return false
}


function checkEmail (theField, emptyOK)
{
	if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))
		return warnEmpty (theField, sEmail)
	else if (!isEmail(theField.value, false))

	return warnInvalid (theField, iEmail)
	else return true
}

function checkPassword (theField, theConfirmField)
{
	if (theField.value.length == 0 && theConfirmField.value.length == 0) 
		return true
	else
		if (theField.value.length >= 8 && theField.value.length <= 20)
			if (theField.value != theConfirmField.value)
				alert('Your Password and Password Confirm fields are not the same.  Please re-enter your Password now.')
			else
				return true
		else
			alert('Your Password must be between 8 and 20 characters long.  Please re-enter your Password now.')
			
}

function checkVerifyEmail (theField, theConfirmField)
{
	if (theField.value != theConfirmField.value)
		alert('Your Email and Confirm Email fields are not the same.  Please re-enter your Email now.')
	else
		return true
		
			
}

function checkDate(mField, dField, yField, emptyOK)
{

	var month = mField.value; 
	var day = dField.value;
	var year = yField.value;

	if (month < 1 || month > 12) return warnInvalid (mField, iDate)
	if (day < 1 || day > 31) return warnInvalid (mField, iDate)
	if ((month==4 || month==6 || month==9 || month==11) && day==31) return warnInvalid (mField, iDate)
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) return warnInvalid (mField, iDate)
	}
	if (year.length < 4) return warnInvalid (mField, iDate)
	return true;  
}

function checkDateRange(sMonth,sDay,sYear,eMonth,eDay,eYear, rangeDays)
{
	startDate = new Date(sYear.value, sMonth.value - 1,sDay.value);
	endDate =  new Date(eYear.value,eMonth.value - 1,eDay.value);
	var startDateSeconds = startDate.getTime();
	var endDateSeconds = endDate.getTime();

	difference = endDateSeconds - startDateSeconds;

	if (difference >= 0)
	{
		// 8640000 = number of milliseconds in a day
		if (Math.floor(difference / 86400000) > rangeDays){
			alert('The time between your Start and End Dates must be less than ' + rangeDays + '. Please reenter your Start and End Dates now.')
			return false
		}
	}else{
		alert('Your Start Date must be before your End Date.  Please reenter your Start and End Dates now.')
		return false
	}
	return true;
}

function CheckCartName(form, index)
{
	var hName = "form.hidCartName"
	var temp
	
	for( var j = 0; j < index; j++)
	{
		temp = eval(hName + j)	
		if (temp.value == form.txtOrderName.value) {
			alert('Your Order Form name must be unique.  Please reenter it now.')
			return false;
		}
	}
	return true;
}




function checkSelection(selField, s)
{
	if (selField.options[selField.selectedIndex].value == '') 
	{
		alert('You did not choose a value in the ' + s + ' selection box.  Please enter one now.')
		return false;
	}
	return true;
}

// Methods to be used in no focus should be set

function warnInvalidNF(theField, s)
{
	alert(iPrefix + s + iSuffix)
	return false
}
function warnEmptyNF (theField, s)
{
	alert(mPrefix + s + mSuffix)
	return false
}
function checkNumberNF (theField, s, emptyOK)
{
	theField.value = TrimWhitespace(theField.value)
	if (checkNumberNF.arguments.length == 2) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmptyNF (theField, s)
	var c
	for (var i = 0; i < theField.value.length; i++)
	{
		c = theField.value.charAt(i)
		if (!isDigit(c)) { warnInvalidNF(theField, s); return false; }
	}
	return true
}
function checkNumLengthNF (theField, length, fieldname, emptyOK)
{
	if (checkNumLengthNF.arguments.length == 3) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isCorrectNumber(theField, length)) return true
	return warnInvalidNF (theField, fieldname)
}
function checkStringExtNF (theField, s, emptyOK)
{
	if (checkStringExtNF.arguments.length == 2) emptyOK = defaultEmptyOK
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmptyNF (theField, s)
	var c
	for (var i = 0; i < theField.value.length; i++)
	{
		c = theField.value.charAt(i)
		if (!isAlphaExt(c)) { warnInvalidNF(theField, s); return false; }
	}
	return true
}

function MoveCursor(curItem, Size, nextItem){
	if (curItem.value.length > Size-1){
		nextItem.focus();
	}
}

function checkZip(theField, s, emptyOK){
	theField.value = TrimWhitespace(theField.value);
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmpty (theField, s);
	var tmp = theField;
	tmp.value = theField.value.replace('-','');
	if(isCorrectNumber(tmp, 5) || isCorrectNumber(tmp, 9)) return true;
	return warnInvalid(theField, s);
}

function checkCode(theField, code, s, emptyOK){
	theField.value = TrimWhitespace(theField.value);
	if ((emptyOK == true) && (isEmpty(theField.value))) return true
	if (isWhitespace(theField.value))	return warnEmptyNF (theField, s);
	if(theField.value == code) return true;
	return warnInvalid(theField, s);
}