	// This file contains the data validation JavaScript functions
	// It is included in the HTML pages with forms that need these
	// data validation routines.


// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";



/****************************************************************/

// 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;
}

/****************************************************************/

/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "&#39;");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "&#39;");
	return new_s;
}

/****************************************************************/

function makeSafe (i)
{
	i.value = sqlSafe (i.value);
}

/****************************************************************/

// 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 (objField)
{   
var s = new String(objField.value)
/*
re = /[^A-Za-z0-9_\.@-]/;
if(s.search(re) != -1){
	alert('Email Address contains illegal characters.  You may only use letters, numbers, periods, @ and underscores.');
	objField.focus();
	return false;
}
*/  
    // is s whitespace?
    if (isWhitespace(s)){
	alert('Please enter an Email Address');
	objField.focus();
	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) != "@")) {
	alert(s + ' is not a valid Email Address');
	objField.focus();
	return false;
    } else {
	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) != ".")) {
	alert(s + ' is not a valid Email Address');
	objField.focus();
	return false;
    } else { 
	return true;
    }
}

/****************************************************************/

// Deals with the date... makes sure it's not blank

function isDash(objField, FieldName) 
{
    var strField = new String(objField.value);
    if (strField == '-') {
    alert("You need to select the " + FieldName);
    objField.focus();
    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 select statement is -1 (not entered)

function isChosen(objField, FieldName)
{
	//alert(objField.selectedIndex);
	var strField = new String(objField.value);
	if ((strField == '-1') || (objField.selectedIndex == 0)) {
		alert("You need to select an " + FieldName);
		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 str = new String(objField.value);
for (i = 0; i < str.length; i++){
		if ((str.charAt(i) < '0' || str.charAt(i) > '9') && (str.charAt(i) != '.') && (str.charAt(i) != '$') && (str.charAt(i) != ',')) {
			alert(FieldName + ' contains illegal characters.  You may only enter numbers.');
			objField.focus();
			return false;
		}
	}
	return true;
/*	var s = new String(objField.value);
	\re = /[^0-9]/;
	if(s.search(re) != -1){
		alert(FieldName + ' contains illegal characters.  You may only enter numbers.');
		objField.focus();
		return false;
	} else {
		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) != '.') && (strField.charAt(i) != '$') && (strField.charAt(i) != ',')) {
			alert(FieldName + " must be a valid numeric entry.  Please use only dollar signs, decimals and numeric characters.");
			objField.focus();
			return false;
		}

	return true;
}
*/
function forceMoney(objField, FieldName)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;
	var strArray = strField.split(".");
	if(strArray.length > 2){
		alert(FieldName + " must be a valid numeric entry.  Please use only numeric characters and a single decimal.");
		return false;
	} else if(strArray.length == 2){
		if(checkNumeric(strArray[0]) && checkNumeric(strArray[1])){
			return true;
		} else {
			alert(FieldName + " must be a valid numeric entry.  Please use only numeric characters and a single decimal.");
			objField.focus();
			return false;
		}
	} else {
		if(checkNumeric(strArray[0])){
			return true;
		} else {
			alert(FieldName + " must be a valid numeric entry.  Please use only numeric characters and a single decimal.");
			objField.focus();
			return false;
		}			
	}
	return true;
}
function checkNumeric(str){
	for (i = 0; i < str.length; i++){
		if ((str.charAt(i) < '0' || str.charAt(i) > '9') && (str.charAt(i) != '.') && (str.charAt(i) != '$') && (str.charAt(i) != ',')) {
			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) != '.') && (strField.charAt(i) != '$')) {
			alert(FieldName + " must be a valid numeric entry.  Please use only dollar signs, decimals and numeric characters.");
			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 forceDate(strDate,strField)
{
	var str = new String(strDate.value);

	if (isWhitespace(str)) {
		return true;
		// if the field is empty, just return true...
	}

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	if (i == count || i > 2) {
		PromptErrorMsg(strDate,strField);
		return false;
	}

	var addOne = false;
	if (i == 2) addOne = true;

	if (!isDateNumber(str.substring(0,i),1)) {
		PromptErrorMsg(strDate,strField);
		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) {
		PromptErrorMsg(strDate,strField);
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),2)) {
		PromptErrorMsg(strDate,strField);
		return false;
	}

	j = i+3;
	i = 0;

	if (addOne) j++;

	while (i+j < count)
		i++;


	if (i != 2 && i != 4) {
		PromptErrorMsg(strDate,strField);
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),3)) {
		PromptErrorMsg(strDate,strField);
		return false;
	}

	return true;
}

/****************************************************************/
// Check if a credit card has expired
function isValidExpirationDate(objFocus, objFieldMonth, objFieldYear, TodayMonth, TodayYear){
 
	var Month = new Number(objFieldMonth.value);
	var Year = new Number(objFieldYear.value);
	if(Year > TodayYear){
		return true;
	} else {
		if(Year == TodayYear){
			if(Month >= TodayMonth){
				return true;
			} else {
				objFocus.focus();
				alert('Credit Card Expired');
				return false;
			}
		} else {
				objFocus.focus();
				alert('Credit Card Expired');
				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(objField, strFieldName)
{
	var s = new String(objField.value);

	if (s.length != 5 && s.length != 10) {
		alert(strFieldName + ' does not contain a valid Zip Code.');
		objField.focus();
		objField.select();
		// inappropriate length
		return false;
	}

	for (var i=0; i < s.length; i++) {
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-'){
			alert(strFieldName + ' does not contain a valid Zip Code.');
			objField.focus();
			objField.select();
			return false;
		}
	}
	return true;

}

/****************************************************************/

// This function ensures that a field is greater 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 small, 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);
		objField.focus();
		objField.select();
		return false;
	} else
		return true;
}


/****************************************************************/
// Checks two fields to see if they are the same.  If so, returns
// true.  If not, returns false and displays an alert with strWarning
// for the text.

function isSame(objField1, objField2, FieldName1, FieldName2)
{
	if(objField1.value == objField2.value) {
		return true;
	} else {
		alert(FieldName1 + ' does not match ' + FieldName2);
		objField1.focus();
		objField1.select();
		return false;
	}
} 

/****************************************************************/
// Checks an array of radio buttons to see if one of them is
// checked.  If at least one is checked, it returns true. Otherwise
// it alerts the user and returns false.

function isRadioChecked(objField, FieldName, itemNumber) {
	var ischecked = false;
	if (objField != null) {
		if (objField.checked){
			ischecked = true;
		} else {
			for (var i = 0; i < objField.length; i++) {
				if(objField[i].checked){
					ischecked = true;
				}
			}
		}
	}
	if (! ischecked) {
//		if (itemNumber != 0) {
			if(objField[itemNumber] != null){
				objField[itemNumber].focus();
			} else {
				objField.focus();
			}
//		} else {
//			objField.focus();
//		}
		alert('Please select the ' + FieldName);
	}
	return ischecked;
}

function isRadioCheckedNoPrompt(objField, FieldName) {
	var ischecked = false;
	if(typeof(objField.length) != 'undefined'){
		for (var i = 0; i < objField.length; i++) {
			if(objField[i].checked){
				ischecked = true;
			}
		}
		if (! ischecked ) {
			objField[0].focus();
			alert('Please select the ' + FieldName);
		}
	} else {
		if(objField.status){
			ischecked = true;
		}
		if (! ischecked ) {
			objField.focus();
			alert('Please select the ' + FieldName);
		}
	}
	return ischecked;
}

/****************************************************************/
// Checks a checkbox to see if it is checked.  

function isCheckboxChecked(objField, FieldName){
	var ischecked = false;
	if (objField.checked){
		ischecked = true;
	} else {
		objField.focus();
		alert('Please select the ' + FieldName);
	}
		return ischecked;
}

/****************************************************************/
// Give focus to the first element of the first form on the page

function focusForm(n){
	for(i=0;i < this.document.forms[n].elements.length; i++){
		var objRef = this.document.forms[n].elements[i];
		if(objRef.type == 'text') {
			objRef.focus();
			objRef.select();
			return true;
		} 
	}
	return false;
}

/****************************************************************/
// Make sure the field contains only legal characters for a username.
/*
function isUserName(objField){
	var s = new String(objField.value);
	re = /\W/;
	if(s.search(re) != -1){
		alert('Login Name contains illegal characters.  You may only use letters, numbers and underscores.');
		objField.focus();
		return false;
	} else {
		return true;
	}
}
*/
/****************************************************************/
// Skip to the next field if s is n long
 
function nextField(s, n, objNext){
	if(s.length == n){
		objNext.focus();
		return true;
	} else {
		return false;
	}
}

/****************************************************************/
// Check to see if a select box is changed from the first selection

function isSelectChanged(objSelect, FieldName){
	if(objSelect.selectedIndex == 0){
		alert('Please select a ' + FieldName);
		objSelect.focus();
		return false;
	} else {
		return true;
	}
}

/****************************************************************/
// Copy one field to another. duh.

function copyField(objField1, objField2)
{
	if(objField2.value == "") {
	    objField2.value = objField1.value;
		return true;
	} else {
		return false;
	}
}

/****************************************************************/
// Concatenate two fields with a string in between, then copy to 
// a third field.

function concatField(objField1, objField2, objField3, s)
{
	objField3.value = objField1.value + s + objField2.value;
	return true;
}

/****************************************************************/
// Get the value of a checked radio button
/*
function getRadioValue(objField){
	for (var i = 0; i < objField.length; i++) {
		if(objField[i].checked){
			return objField[i].value;
		}
	}
}
*/
