<!-- hide from non-JS browsers
// filename: val.js
// Referenced by edit pages for form validation functions
// checkDecimals, checkDate, checkMonth, checkYear
// v 5.0

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=
//  new functions added:
//		validate(strFormName, arrFields):	validates forms using an array of validation functions and parameters
//
//		valCheckLength (obj, emptyOK, sLength)	
//		valCheckEmail (obj, emptyOK, nullParam)
//		valCheckDate(obj, emptyOK, nullParam )
//		valCheckDecimals(obj, emptyOK, zeroOK)
//		valCheckWhitespace (obj, emptyOK, sAlert)
//		valCheckInteger(obj, emptyOK, negOK)


//define which files should be excluded from upload - checkExtension(thefield)
extArray = new Array(".exe", ".vbs", ".bat", ".js");


// main validation function

function validate(strFormName, arrFields) {
	var strValidate;
	var valid=true;
	var n=0;

	for(n=0; n<arrFields.length; n++)
	{
		if(arrFields[n][1]=="valCustom") {

			strValidate= "if (!"+arrFields[n][3]+") {valid=false; }";
		}
		else {
			strValidate= "if (!"+arrFields[n][1]+"(document."+strFormName+"."+arrFields[n][0]+", "+arrFields[n][2]+", arrFields["+n+"][3])) {valid=false;  }";
		}

		//alert(strValidate);
		eval(strValidate);
		if (valid==false) return false;
	}
	
}

function valCheckLength(obj, emptyOK, sLength) {
	if ((isEmpty(obj.value)) && (emptyOK==false)) {
			alert("This field may not be empty.");
			obj.focus();
			return false;
	}
	if (CheckLength(obj, sLength)) {
			return true;
	}
	else {
			return false;
	}
}

function valCheckEmail (obj, emptyOK, nullParam) {
	  if(checkEmail(obj,emptyOK))	{
		  return true;
	  }
	  else	{
		  return false;
	  }
}

function valCheckDate(obj, emptyOK, nullParam) {
	  if(checkDate(obj,emptyOK))	{
		  return true;
	  }
	  else	{
		  return false;
	  }
}

function valCheckDecimals(obj, emptyOK, zeroOK) {
	  if ((isEmpty(obj.value)) && (emptyOK==false)) {
			alert("This field may not be empty.");
			obj.focus();
			return false;
	  }
	  if(checkDecimals(obj, zeroOK, emptyOK))	{
		  return true;
	  }
	  else	{
		  return false;					
	  }
	}

function valCheckWhitespace (obj, emptyOK, sAlert)	{
   if (isWhitespace(obj.value)) {
		alert(sAlert);
		obj.focus();
		return false;
	}
	else	{
		return true;
	}
}


function valCheckInteger (obj, emptyOK, negOK)		{
	if ((isEmpty(obj.value)) && (emptyOK==false))		{
			alert("This field may not be empty.");
			obj.focus();
			return false;
	}
	else if ((!isNonnegativeInteger(obj.value)) && (negOK==false)) {
		alert("You must enter a nonnegative integer.");
		obj.focus();
		return false;
	}
	else if (!isInteger(obj.value)) {
		alert("You must enter an integer.");
		obj.focus();
		return false;
	}
	else	{
		return true;
	}

}

// ********

function CheckLength(sField, sLength) {
	var maxlength = sLength;
		if (sField.value.length > maxlength) {
		alert("You have exceeded the maximum length size, " + sLength + " characters, for this field.  Please edit the field if necessary and re-submit the form.");
			sField.select();
			sField.focus();
			return false;
			}
	else {
		return true;
	}
}

function parseSelectBox(theform, selectvalue, value1, value2) {
	// get two values from select
	var thestring;
	thestring = selectvalue[selectvalue.selectedIndex].value;
	var thenum = thestring.indexOf(";;value2;;");	
	eval("document." + theform + "." + value1 + ".value = thestring.substring(0,thenum)");
	eval("document." + theform + "." + value2 + ".value = thestring.substring((thenum + 10),thestring.length)");	
	//alert(eval("document." + theform + "." + value1 + ".value = thestring.substring(0,thenum)"));
	//alert(eval("document." + theform + "." + value2 + ".value = thestring.substring((thenum + 10),thestring.length)"));

	}


function filterNum(str) {
	re = /^\$|,/g;
	// remove "$" and ","
	return str.replace(re, "");
}

var defaultEmptyOK = true


function checkDecimals(obj, zeroOK, emptyOK) {
		decallowed = 2;  // how many decimals are allowed?
		if (checkDecimals.arguments.length == 1) {
			zeroOK = true;
			emptyOK = defaultEmptyOK
		}
		if (checkDecimals.arguments.length == 2) emptyOK = defaultEmptyOK;
		var fieldName = obj;
		var fieldValue = obj.value;
		// use to strip $,
		//var fieldValue = filterNum(obj.value);
		if (emptyOK){
			if (isEmpty(fieldValue)) return true;
		}
		
//		allow blanks or not?
//		if (isNaN(fieldValue) || fieldValue == "") {
		if (isNaN(fieldValue)) {
			alert("That does not appear to be a valid number.  Please try again.");
			fieldName.select();
			fieldName.focus();
			return false;
		}
		if (zeroOK){
			if (fieldValue <0)	{
				alert ("The number cannot be negative.  Please try again.");
				fieldName.select();
				fieldName.focus();
				return false;
			}
		}
		else{
			if (fieldValue <=0)	{
				alert ("The number cannot be zero or negative.  Please try again.");
				fieldName.select();
				fieldName.focus();
				return false;
			}
		}
		if (fieldValue.indexOf('.') == -1) fieldValue += ".";
		dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
		if (dectext.length > decallowed) {
					alert ("Please enter a number with up to " + decallowed + " decimal places.  Please try again.");
					fieldName.select();
					fieldName.focus();
					return false;
		}
		
		//alert ("That number validated successfully.");
		return true;
						
	}



function checkDate(objName, emptyOK) {
	var datefield = objName;
	//alert(datefield);
	if (checkDate.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == false) && (isEmpty(datefield.value))) {
		datefield.select();
	alert("You must enter a date.  Please try again.");
	datefield.focus();
      return false;
}
	if (chkdate(objName) == false) {
		datefield.select();
		alert("That date is invalid.  Please try again.");
		datefield.focus();
		//if (is.ie4up) {
		//	event.returnValue = false;
		//}
		//else {
		  return false;
		//  }
	}
	else {
		return true;
	}
}


function chkdate(objName) {
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	//alert(strDate);
	if (strDate.length < 1) {
		return true;
	}

	var d = datefield.value;
	d = new Date(d);
	if (isNaN(d)) {
		return false;
   }
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length == 2) {
	strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
	err = 2;
	return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
	for (i = 0;i<12;i++) {
	if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
	intMonth = i+1;
	strMonth = strMonthArray[i];
	i = 12;
	   }
	}
	if (isNaN(intMonth)) {
	err = 3;
	return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
	err = 4;
	return false;
	}
	if (intMonth>12 || intMonth<1) {
	err = 5;
	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
	err = 6;
	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
	err = 7;
	return false;
	}
	if (intMonth == 2) {
	if (intday < 1) {
	err = 8;
	return false;
	}
	if (LeapYear(intYear) == true) {
	if (intday > 29) {
	err = 9;
	return false;
	}
	}
	else {
	if (intday > 28) {
	err = 10;
	return false;
	}
	}
	}
	if (strDatestyle == "US") {
	//long date
	//datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	datefield.value = intMonth + "/" + intday + "/" + strYear;
	}
	else {
	datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
}

function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}



// Check that string theField.value is a valid Month.
//
function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) {
		alert("You must enter a valid month (1 - 12). Please try again.");
		theField.select();
		theField.focus();
		return false;
	}
    else return true;
}

// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}



// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}


// Check that string theField.value is a valid Year.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkYear (theField, emptyOK){
	if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	var thisDay = new Date();
	var thisYear = thisDay.getFullYear();
	var startYear = thisYear - 1;
	var endYear = thisYear + 1;
	var lmsg = "The year must be " + startYear + ", " + thisYear + ", or " + endYear + ". Please try again.";
    if (!isYear(theField.value, false)) {
		alert(lmsg);
		theField.select();
		theField.focus();
		return false;
		}
    else {
		var fieldValue = theField.value;
		if (fieldValue.length == 2) fieldValue = "20" + fieldValue; 
		if (parseInt(fieldValue) < startYear || parseInt(fieldValue) > endYear) {
			alert(lmsg);
			theField.select();
			theField.focus();
			return false;
		}
		else {
			theField.value = fieldValue;
			return true;
		}
	}
}

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}



// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


// whitespace characters
var whitespace = " \t\n\r";

// 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;
}

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkEmail (theField, emptyOK) {
//var E_field = theField;
	  if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
		if ((emptyOK == true) && (isEmpty(theField.value))) return true;
		else if (!isEmail(theField.value, false))
		//E_field.select();
		alert("That email is invalid.  Please try again.");
		//E_field.focus();
	   else 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;
}




//Limits the files that we can select
    function checkExtension(thefield)      {

		var file = thefield.value;
		while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();

		//Loop through the array of allowed extensions
		for (var i = 0; i < extArray.length; i++) {
				if (extArray[i] == ext) { 
					 alert("You may not upload files with an extension of " + ext);     
					return (false); 
				}
		}

		return (true);
}
         

//  End -->