	// 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";

	function isNumeric(str) {
	if (str.length == null || str.length == 0) return false;
	for (var i=0; i < str.length; i++) {
		var ch = str.substring(i, i+1);
		if ((ch < "0" || ch > "9") && ch != "." && ch != " " && ch != ",") {
			return false;
		}
	}
	return true;
}

	function isNum(passedVal) 
	{
	// Is this a number ?
	  if (passedVal == "") {
	    return false;
	  }
	
	  for (i=0; i<passedVal.length; i++) {
	    if (passedVal.charAt(i) < "0") {
		  return false
		}
		if (passedVal.charAt(i) > "9") {
		  return false;
		}
	  }
	  return true
	}
	
	function validatePrompt (Ctrl, PromptStr) 
	{
		alert (PromptStr)
		Ctrl.focus();
		return;
	}
	
	function testZipCode(form) {
	  var valid = true;
	  var Ctrl = form.zip;
	
	  if ((Ctrl.value.length!=5) || (!isNum(Ctrl.value)) || (Ctrl.value==0)) 
	  {
	    validatePrompt(Ctrl, "Enter a valid zip code");
	    valid=false;
	  }
	  
	  return valid;
	}

/****************************************************************/

// 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, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	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;
}

function Left(str, n)
 /***
         IN: str - the string we are LEFTing
             n - the number of characters we want to return

         RETVAL: n characters from the left side of the string
 ***/
 {
         if (n <= 0)     // Invalid bound, return blank string
                 return "";
         else if (n > String(str).length)   // Invalid bound, return
                 return str;                // entire string
         else // Valid bound, return appropriate substring
                 return String(str).substring(0,n);
 }


function validEmail(email) {
	// create a variable which contains the invalid characters for an email address
	invalidChars = " /:,;()[]{}^<>" ;

	// check for invalid characters within the email address
	for (i=0; i<invalidChars.length; i++) {
	    badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) != -1) {
			return false;
		}
	}
	
	left_four = Left(email,4);
	if (left_four.toLowerCase() == "www."){
		return false;
	}

	// check that an "@" exists in the email address
	atPos = email.indexOf("@",1);
	if (atPos == -1) {
		return false;
	}

	// check that only one "@" exists in the email address
	if (email.indexOf("@",atPos+1) != -1) {
		return false;
	}

	// verify that there is at least 1 character before the "@" in the address
	if (atPos < 1) {
		return false;
	}

	// verify that a "." exists after the "@"
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}

	// verify that there are at least 2 characters after the "." in the address
	if (periodPos+3 > email.length) {
		return false;
	}

	// verify that there are at least 2 characters in the domain name
	if (periodPos-atPos < 3) {
		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;
}
		
/****************************************************************/

// 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) < 1 || parseInt(str) > 31)
			return false;
	if (method == 1)
		if (parseInt(str) < 1 || parseInt(str) > 12)
			return false;
	
	if (method == 3)
		if (parseInt(str) < 1980 || parseInt(str) > 2999)
			return false;

	for (i = 0; i < str.length; i++)
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;


	return true;
}

function checkDate(string) {
		var valid = true
		var validchrs = "0123456789/"
		
		var temp;
		for (var i=0; i< string.length; i++) {
			temp = "" + string.substring(i, i+1);
			if (validchrs.indexOf(temp) == "-1") valid = false;
		}
		var parts = string.split("/")
		if (parts.length != 3) {
			valid = false; // must have 3 segments
		}
		else {
			if (parts[0]<1 || parts[0]>12) valid = false // month
			if (parts[1]<1 || parts[1]>31) valid = false // day
			if (parts[2].length<2 || parts[2].length==3 || parts[2].length>4) valid = false //year
			
			// months with 30 days 
			if (parts[0]==4 || parts[0]==6 || parts[0]==9 || parts[0]==11){ 
				if (parts[1]==31) valid = false 
			}
			// february, leap year 
			if (parts[0]==2){ 
				// feb 
				var g=parseInt(parts[2]/4) 
				if (isNaN(g)) { 
					valid = false 
				} 
				
				if (parts[1]>29) valid = false 
				if (parts[1]==29 && ((parts[2]/4)!=parseInt(parts[2]/4))) valid = false 
			}
		}
return valid;
}

/****************************************************************/

// 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 false;
		// if the field is empty, just 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) {
		//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++;

	//Test lenth of year portion of date
	if (i != 2 && i != 4) {
		//PromptErrorMsg(strDate,strField);
		return false;
	}

	//Test value if year is 4 digits
	if ( i == 4 && !isDateNumber(str.substring(j,i+j),3)) {
		//PromptErrorMsg(strDate,strField);
		return false;
	}

	//Test value if year is 2 digits
	if ( i == 2 && !isDateNumber(str.substring(j,i+j),4)) {
		//PromptErrorMsg(strDate,strField);
		return false;
	}

	return true;
}

/****************************************************************/

// 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);

	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;
}

//If Date1 is before Date2 it returns true
function compareDate(date1, date2) 
{
	var parts1 = date1.split("/");
	var parts2 = date2.split("/");
	var day1 = parseInt(eval(parts1[1]));
	var day2 = parseInt(eval(parts2[1]));
	var month1 = parseInt(eval(parts1[0]));
	var month2 = parseInt(eval(parts2[0]));
	var year1 = parseInt(eval(parts1[2]));
	var year2 = parseInt(eval(parts2[2]));
	//alert('Day1:' + day1 + '\n' + 'Day2:' + day2 + '\n' + 'Month1:' + month1 + '\n' + 'Month2:' + month2 + '\n' + 'year1:' + year1 + '\n' + 'year2:' + year2)
	
	if (year1 < 70)
	{
		year1 = 2000 + year1
	}
	if (year1 >= 70 && year1 <= 99) {
		year1 = 1900 + year1
	}
	if (year2 < 70)
	{
		year2 = 2000 + year2
	}
	if (year2>= 70 && year2 <= 99) {
		year2 = 1900 + year2
	}
	
	if (year1 > year2)
	{
		return false; 
	}
	else if (year1 < year2)
	{
		return true;
	}
	if (year1 == year2)
	{
		if (month1 > month2)
		{
			return false;
		}
		else if (month1 < month2)
		{
			return true;
		}
		if (month1 == month2)
		{
			if (day1 > day2)
			{
				return false;
			}
			else if (day1 <= day2)
			{
				return true;
			}
		}	
	}
}
//Returns selected value in the select box
function get_select_text(ctrl) {
	var nindex = ctrl.selectedIndex;
	var item = ctrl.options[nindex].value;
	return item;
}

function isValidPhone(string) {
	if ((string.length == null) || (string.length < 10)) return false;
	var num_count = 0;
	for (var i=0; i < string.length; i++) {
		var ch = string.substring(i, i+1);
		if (isNum(ch)) {
			num_count = num_count +1;
		}else if (ch == "-"){
		}else if (ch == " "){
		}else if (ch == "("){
		}else if (ch == ")"){
		}else{
			return false;
		}		
	}
	if (num_count < 10) {
		return false;			
	}
	return true;
}