	// This file contains the data validation JavaScript functions
	// It is included in the HTML pages with forms that need these
	// data validation routines.

	// use forceEntry(elem)  to force entry in a text field.
	// use forceSelection(elem)  to force selection of a select field.
	// use isEmail(str, [emptyok])  to force a string as an email.
	// use isZipcode(str) to force a zipcode.



// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";


/****************************************************************/


function forceEntry(elem){
	if (isWhitespace(elem.value)){return false;}else {return true;};
}

/****************************************************************/
function forceSelection(elem){

	if (elem.selectedIndex < 0 || elem.options[elem.selectedIndex].value.length==0){
		return false;
	}
	return true;
}

/****************************************************************/

function forcePhone(elem){

	var str=elem.value;
	
	
	if (isWhitespace(str)){return false;};
	
	if (str.length!=14 && str.length != 12 && str.length!=10){return false;};
	
	if (str.length==14){			
		for(var i=0; i< str.length; i++){
			switch (i) {
			  case 0: 
				  if (str.charAt(i) != '('){return false;};
				  break;
			  case 1:
			  case 2:
			  case 3:
			    if (!(/\d/.test(str.charAt(i)))) {return false;};
			    break;
			  case 4:
				  if (str.charAt(i) != ')'){return false;};
			    break;
			  case 5:
				if (str.charAt(i) != '-'){return false;};
			    break;
			  case 6:
			  case 7:
			  case 8:
			    if (!(/\d/.test(str.charAt(i)))) {return false;};
			    break;
			  case 9:
				if (str.charAt(i) != '-'){return false;};
			    break;
			  case 10:
			  case 11:
			  case 12:
			  case 13: 
				if (!(/\d/.test(str.charAt(i)))) {return false;};
				break;
			}	
		}
		return true;
	}
	else if (str.length==12){
		for(var i=0; i< str.length; i++){
			switch (i) {
			  case 0: 
			  case 1:
			  case 2:
					if (!(/\d/.test(str.charAt(i)))) {return false;};
			  	    break;
			  case 3:
					if (str.charAt(i) != '-'){return false;};
					break;
			  case 4:
			  case 5:
			  case 6:
					if (!(/\d/.test(str.charAt(i)))) {return false;};
			  	    break;
			  case 7:
					if (str.charAt(i) != '-'){return false;};
					break;
			  case 8:
			  case 9:
			  case 10:
			  case 11:
				if (!(/\d/.test(str.charAt(i)))) {return false;};
				break;
			}	
		}
		return true;	
	
	}
	else if (str.length==10){
		for(var i=0; i< str.length; i++){
			if (!(/\d/.test(str.charAt(i)))) {return false;};
		}	
		return true;
	}

}

/****************************************************************/

// 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;
	}

/****************************************************************/


// 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 defaultEmptyOK;
       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;
    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) != ".")) return false;
    else return true;
}

		

/* 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;
}


/****************************************************************/

/* 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;
}

/****************************************************************/

function isEmpty(str){

	return (str==null || str.length==0);

}

/****************************************************************/

// 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 masks keypresses to force the field to be in the format
// (###)-###-####
// use by adding to the keypress event of a text box


function maskPhone (field) {
  switch (field.value.length) {
    case 0: 
	  field.value = field.value + '(';
      //event.keyCode = '('.charCodeAt();
      
      return /\d/.test(String.fromCharCode(event.keyCode));
      //return true;
      break;
    case 1:
    case 2:
    case 3:
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    case 4:
	  field.value = field.value + ')-';
      //event.keyCode = ')'.charCodeAt();
      return /\d/.test(String.fromCharCode(event.keyCode));
      //return true;
      break;
    case 5:
      event.keyCode = ' '.charCodeAt();
      return true;
      break;
    case 6:
    case 7:
    case 8:
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    case 9:
      event.keyCode = '-'.charCodeAt();
      return true;
      break;
    case 10:
    case 11:
    case 12:
    case 13: 
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    default:
      return false;
      break;
  }
}

