<!--
	// This file contains the data validation JavaScript functions
	// It is included in the HTML pages with forms that need these
	// data validation routines.

// whitespace characters
var whitespace = " \t\n\r";
var oneMinute = 60 * 1000;
var oneHour = oneMinute * 60;
var oneDay = oneHour * 24;
var oneWeek = oneDay * 7;
//******************************************************** ********//
function dateInMS(oDate)
{
	var aDate = new Date();
	var str = new String(oDate.value);
	var newstr = new String();

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	var addOne = false;
	if (i == 2) addOne = true;

	newstr = str.substring(0,i);
	aDate.setMonth(newstr);
//	the month is 0 based but the day is 1 based. go figure
	aDate.setMonth(aDate.getMonth()-1);
	j = i+1;
	i = 0;

	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
		i++;

	newstr = str.substring(j,i+j);
	aDate.setDate(newstr);
	
	j = i+3;
	i = 0;

	if (addOne) j++;

	while (i+j < count)
		i++;

	newstr = str.substring(j,i+j);
	if (newstr.length == 2) {
		newstr = '20' + newstr;
	}
	aDate.setYear(newstr);
	return aDate.getTime();
}


/****************************************************************/

// PURPOSE:  Check to see if the string passed in is a valid time.
//	A valid time is defined as a string which is postfixed with either
//  "PM" or "AM".  Next it checks to see if there is a colon in the
//  string.  If there is, it makes sure that at least one digit preceeds
//  it and two proceed it.

	function IsTime(strTime)
	{
		var strTestTime = new String(strTime);
		strTestTime.toUpperCase();

		var bolTime = false;

		if (strTestTime.indexOf("PM",1) != -1 || strTestTime.indexOf("AM",1))
			bolTime = true;

		if (bolTime && strTestTime.indexOf(":",0) == 0)
			bolTime = false;

		var nColonPlace = strTestTime.indexOf(":",1);
		if (bolTime && ((parseInt(nColonPlace) + 5) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 4) > (strTestTime.length - 1)))
			bolTime = false;


		return bolTime;
	}

/****************************************************************/

function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/****************************************************************/

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail(s) {
	if (isEmpty(s)) {
		if (isEmail.arguments.length == 1)
			return false;
		else
			return (isEmail.arguments[1] == true);
	}

	// is s whitespace?
	if (isWhitespace(s))
		return false;

	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@"))
		i++;

	if ((i >= sLength) || (s.charAt(i) != "@"))
		return false;
	i += 2;

	// look for .
	while ((i < sLength) && (s.charAt(i) != "."))
		i++;

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != "."))
		return false;
	return true;
}

/****************************************************************/

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
		alert("You need to enter information for " + FieldName);
//		objField.focus();
		objField.select();
		return false;
	}

	return true;
}

// Checks to see if a required field is blank.  If it is, 
// false is returned
function TestEntry(objField)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
//		objField.focus();
		objField.select();
		return false;
	}
	return true;
}

	
/****************************************************************/

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceNumber(objField, FieldName)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
			alert(FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
//			objField.focus();
			return false;
		}

	return true;
}

/****************************************************************/

// Returns true if the string passed in is a valid money
//  (no alpha characters except a decimal place), 
//   else it displays an error message

function ForceMoney(objField, FieldName)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.')) {
			alert(FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
			objField.focus();
			return false;
		}

	return true;
}


/****************************************************************/

// Right trims the string...  Useful for SQL datatypes of CHAR

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

/****************************************************************/

/* PURPOSE:  Returns true if the string is a valid date number.
	A method is passed in (1 = month, 2 = day).  If the string is
	nonnumeric, false is passed back.  If the day in the date string
	is greater than 31, false is returned.  If the month is greater
	than 12, an error is returned.
*/

function isDateNumber(strNum,method)
{
	var str = new String(strNum);
	var i = 0;

	if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

	if (method == 2)
		if (parseInt(str) > 31)
			return false;
	if (method == 1)
		if (parseInt(str) > 12)
			return false;

	for (i = 0; i < str.length; i++)
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;


	return true;
}

/****************************************************************/

// Displays an alert box with the passed in string...

function PromptErrorMsg(Field,strError)
{
	alert("You have entered an invalid date for " + strError + ".  Please make sure your date format is in M/D/Y format.");
	Field.focus();
}

/****************************************************************/

/* PURPOSE: Checks to see if the string is a valid date.  A valid
	date is defined as any of the following:

		MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,
		MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY
*/

function WeekdayDate(oDate,strField)
{
	var aDate = new Date();
	var str = new String(oDate.value);
	var newstr = new String();

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	var addOne = false;
	if (i == 2) addOne = true;

	if (!isDateNumber(str.substring(0,i),1)) {
		PromptErrorMsg(oDate,strField);
		return false;
	}

	newstr = str.substring(0,i);
	aDate.setMonth(newstr);
//	the month is 0 based but the day is 1 based. go figure
	aDate.setMonth(aDate.getMonth()-1);
	j = i+1;
	i = 0;

	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
		i++;

	if (!isDateNumber(str.substring(j,i+j),2)) {
		PromptErrorMsg(oDate,strField);
		return false;
	}

	newstr = str.substring(j,i+j);
	aDate.setDate(newstr);
	
	j = i+3;
	i = 0;

	if (addOne) j++;

	while (i+j < count)
		i++;

	if (!isDateNumber(str.substring(j,i+j),3)) {
		PromptErrorMsg(oDate,strField);
		return false;
	}

	newstr = str.substring(j,i+j);
	if (newstr.length == 2) {
		newstr = '20' + newstr;
	}
	aDate.setYear(newstr);
	
	if (aDate.getDay() == 0) {
		alert('Furniture cannot be assembled on Sunday ');
		return false;
	}
	if (aDate.getDay() == 6) {
		alert('Furniture cannot be assembled on Saturday ');
		return false;
	}
	return true;
}


function isDate(oDate)
{
	var str = new String(oDate.value);

	if (isWhitespace(str))
		return false;

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	if (i == count || i > 2)
		return false;

	var addOne = false;
	if (i == 2)
		addOne = true;

	if (!isDateNumber(str.substring(0,i),1))
		return false;

	j = i+1;
	i = 0;

	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
		i++;

	if (i+j == count || i > 2)
		return false;

	if (!isDateNumber(str.substring(j,i+j),2))
		return false;

	j = i+3;
	i = 0;

	if (addOne)
		j++;

	while (i+j < count)
		i++;

	if (i != 2 && i != 4)
		return false;

	if (!isDateNumber(str.substring(j,i+j),3))
		return false;

	return true;
}


function CheckNumber(oNumber, sField, fRequired)
{
	if (oNumber.value.length == 0 && !fRequired)
		return true;

	if (!isNaN(parseInt(oNumber.value)) && parseInt(oNumber.value) > 0)
		return true;

	alert('Please enter a valid ' + sField + '.');
	oNumber.focus();
	oNumber.select();
	return false;
}


function GetDate(oDate) {
	if (isDate(oDate)) {
		var ary = oDate.value.split('/');
		if (ary.length == 0)
			ary = oDate.value.split('-');
		if (ary.length == 3) {
			if (ary[0].length == 0 || ary[0].length > 2 || isNaN(parseInt(ary[0])))
				return null;
			if (ary[1].length == 0 || ary[1].length > 2 || isNaN(parseInt(ary[1])))
				return null;
			if (ary[2].length != 2 && ary[2].length != 4 || isNaN(parseInt(ary[2])))
				return null;
			var nYear = parseInt(ary[2], 10) + (ary[2].length == 2 ? 2000 : 0);
			var nMonth = parseInt(ary[0], 10) - 1;
			var nDay = parseInt(ary[1], 10);
			return new Date(nYear, nMonth, nDay);
		}
	}
}


function CheckFuture(oDateCtl) {
	var oDate = GetDate(oDateCtl);
	var oNow = new Date();
	var oToday = new Date(oNow.getFullYear(), oNow.getMonth(), oNow.getDate());
	if (oDate >= oToday)
		return true;

	alert("Please enter today's date or a date in the future.");
	oDateCtl.focus();
	oDateCtl.select();
	return false;
}


function FormatDate(oDate) {
	if (isDate(oDate)) {
		var ary = oDate.value.split('/');
		if (ary.length == 0)
			ary = oDate.value.split('-');
		if (ary.length == 3) {
			if (ary[0].length == 0 || ary[0].length > 2 || isNaN(parseInt(ary[0])))
				return;
			if (ary[1].length == 0 || ary[1].length > 2 || isNaN(parseInt(ary[1])))
				return;
			if (ary[2].length != 2 && ary[2].length != 4 || isNaN(parseInt(ary[2])))
				return;
			var sDate = (ary[0].length == 1 ? '0' + ary[0] : ary[0]);
			sDate += '/' + (ary[1].length == 1 ? '0' + ary[1] : ary[1]);
			sDate += '/' + (ary[2].length == 2 ? (parseInt(ary[2]) < 30 ? '20' : '19') + ary[2] : ary[2]);
			oDate.value = sDate;
		}
	}
}


function CheckDate(oDate, fRequired)
{
	if (oDate.value.length == 0 && !fRequired)
		return true;

	if (isDate(oDate)) {
		FormatDate(oDate);
		return true;
	}

	alert('Please enter a valid date in MM/DD/YYYY format.');
	oDate.focus();
	oDate.select();
	return false;
}


function ForceDate(oDate,strField)
{
	if (isDate(oDate))
		return true;

	PromptErrorMsg(oDate,strField);
	return false;
}

/****************************************************************/

// This function determines if the string passed in is a valid
// US zip code.  It accepts either ##### or #####-####.  If the
// string is valid, it returns true, else false.

function isZipcode(strZip)
{
	var s = new String(strZip.value);

	if (s.length != 5 && s.length != 10)
		// inappropriate length
		return false;


	for (var i=0; i < s.length; i++)
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
			return false;

	return true;
}

/****************************************************************/

// This function ensures that a field is less than or equal to the
// Length passed in.  You must call this function with the element
// name in your form (for example: "ForceLength(document.forms[0].txtElement)"
// as opposed to "ForceLength(document.forms[0].txtElement.value)"
// If the field's value is too large, an error message is displayed
// and false is returned, else true is returned.

function ForceLength(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length > nLength) {
		alert(strWarning);
		return false;
	} else
		return true;
}

function getObject(sID) {
	return document.getElementById ? document.getElementById(sID) : document.all ? document.all[sID] : null;
}

function checkText(sID, sField, cch) {
	var oText = getObject(sID);
	if (!oText || oText.value.length >= cch)
		return true;
	if (oText.value.length == 0)
		alert('Please enter a value for the ' + sField + ' field.');
	else
		alert('Please enter a valid value for the ' + sField + ' field.');
	oText.focus();
	return false;
}

function checkEmail(sID, sField) {
	var oText = getObject(sID);
	if (!oText || isEmail(oText.value))
		return true;
	if (oText.value.length == 0)
		alert('Please enter a value for the ' + sField + ' field.');
	else
		alert('Please enter a valid value for the ' + sField + ' field.');
	oText.focus();
	return false;
}

function checkEmailOrBlank(sID, sField) {
	var oText = getObject(sID);
	if (!oText || oText.value.length == 0 || isEmail(oText.value))
		return true;
	alert('Please enter a valid value for the ' + sField + ' field.');
	oText.focus();
	return false;
}

function checkSelect(sID, sField) {
	var oSelect = getObject(sID);
	if (!oSelect || oSelect.selectedIndex > 0)
		return true;
	alert('Please select a value for the ' + sField + ' field.');
	oSelect.focus();
	return false;
}

function checkRadio(sID, sField) {
	var oCheck0 = getObject(sID + '0'), oCheck1 = getObject(sID + '1');
	if (!oCheck0 || !oCheck1 || oCheck0.checked || oCheck1.checked)
		return true;
	alert('Please choose Yes or No for the ' + sField + ' field.');
	oCheck1.focus();
	return false;
}

// -->
