<!--
// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";

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 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;
}

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function validateEntry(objField, FieldName){
	var strField = new String(document.getElementById(objField).value);
	if (isWhitespace(strField)) {
		return ""+"You need to enter information for " + FieldName;
	}

	return true;
}

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message
function validateNumber(objField, FieldName){
	var strField = new String(document.getElementById(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') {
			return ""+FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.";
		}
	}
	
	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: 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 validateDate(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 validateLength(objField, max, fieldName){
	if (!isValidLength(new String( document.getElementById(objField).value ), 0, max)){
		return "Length of " + fieldName + " must be less than 1024 characters'"
	}
	return true;
}

function isValidLength(string, min, max) {
   if (string.length < min || string.length > max) return false;
   else return true;
}

errors = new Array();

function checkField(res){
	if (res != true){
		errors[ errors.length ] = res;
	}
}

function processForm(){
	validateForm();
	if ( errors.length == 0 ){
		//saveViewState();
		return true;	
	}
	
	var errorStr = new String();
	for ( i = 0; i < errors.length; i++){
		errorStr += errors[i];
		errorStr += "\n";
	}
	alert(errorStr);
	return false;	
}
// -->
