<!--
// ---------------------------------------------------------------------------------
//     Function:  isEmpty
//
//   Created By:  Jeff Welch
//
//      Purpose:  Checks to see if an object is empty (strings, arrays, etc.).
//
//       Inputs:  strThisString -- field to be checked
//
//      Returns:  true if object is empty, false if it is not.
//
// Last Updated:  02/02/2001
// ---------------------------------------------------------------------------------- 
function isEmpty(objThisObject){
    return ((objThisObject == null) || (objThisObject.length == 0));
}

// ---------------------------------------------------------------------------------
//     Function:  isWhitespace
//
//   Created By:  Jeff Welch
//
//      Purpose:  Checks to see if a form element is blank.  If it is, alert the user.
//
//       Inputs:  strThisString -- field to be checked
//
//      Returns:  true if the string is only whitespace, false if it is not
//
// Last Updated:  02/02/2001
// ---------------------------------------------------------------------------------- 
function isWhitespace (strThisString){ 
  
    var intCharCounter;
    var strChar;
    var strWhiteSpaceChars;                      //list of whitespace characters
    
    strWhiteSpaceChars = " \t\n\r";
    
    // Is the string empty?
    if (isEmpty(strThisString)){ 
        return true;
    }

    // Search through string's characters to find a non-whitespace character.
    for (intCharCounter = 0; intCharCounter < strThisString.length; intCharCounter++){
    
        // Check that current character isn't whitespace.
	    strChar = strThisString.charAt(intCharCounter);
	    if (strWhiteSpaceChars.indexOf(strChar) == -1){
	        return false;
	    }
    }

    // Otherwise, it is white space.
    return true;
}



// ---------------------------------------------------------------------------------
//     Function:  ForceEntry
//
//   Created By:  Jeff Welch
//
//      Purpose:  Checks to see if a form element is blank.  If it is, alert the user.
//
//       Inputs:  objField     -- field to be checked
//                strFieldName -- name for the field that the user will understand 
//
//      Returns:  true if the field has a value, false if it does not
//
// Last Updated:  02/02/2001
// ---------------------------------------------------------------------------------- 
function ForceEntry(objField, strFieldName){
    var strFieldValue
    //get the value depending on the type of form element
    if( objField.type == "select-one" ){
        strFieldValue = objField.options[objField.options.selectedIndex].value;
    }
    //Radio buttons need the function: ForceEntry only when the user does not chose from the radio group and the value is " "
    else if(objField.type == "radio"){
		strFieldValue = " ";
	}
    else{
        strFieldValue = objField.value;
    }
	if ( isWhitespace(strFieldValue) || strFieldValue == "nothing" ) {
		alert( "You need to enter information for " + strFieldName );
		objField.focus();
		//objField.select();
		return false;
	}

	return true;
}	

// ---------------------------------------------------------------------------------
//     Function:  ForceDate
//
//      Purpose:  Checks to see if a form element is of a valid date form.
//                If it is not, alert the user. accepts: 
//                (m/d/yy)(m/dd/yy)(mm/d/yy)(mm/dd/yy)(mm/dd/yyyy)(m/d/yyyy)(mm/d/yyyy)
//                (m/dd/yyyy) where / can be -.  
//
//       Inputs:  objField      -- field to be checked
//                strFieldLabel -- label for the field that the user will understand 
//
//      Returns:  true if the field has a valid value, false if it does not
//
// Last Updated:  02/02/2001
// ---------------------------------------------------------------------------------- 
function ForceDate( objField, strFieldLabel )
{
	var strDate = new String(objField.value);
	var intCharCounter1;
	var intCharCounter2;
	var intCharMax;
	
    // if the field is empty, assume it is OK
	if (isWhitespace(strDate)) {
		return true;
	}

    
	intCharCounter1 = 0;
	intCharCounter2 = 0;	
	intCharMax = strDate.length; 
	
	while ((strDate.charAt(intCharCounter1) != "/" && strDate.charAt(intCharCounter1) != "-") && intCharCounter1 < intCharMax)
		intCharCounter1++;

	if (intCharCounter1 == intCharMax || intCharCounter1 > 2) {
	    alert("You have entered an invalid date for " + strFieldLabel + ".  Please make sure your date format is in MM/DD/YYYY format.");
	    objField.focus();	
		return false;
	}

	var addOne = false;
	if (intCharCounter1 == 2) addOne = true;

	if (!isDateNumber(strDate.substring(0,intCharCounter1),1)) {
	    alert("You have entered an invalid date for " + strFieldLabel + ".  Please make sure your date format is in MM/DD/YYYY format.");
	    objField.focus();	
		return false;
	}

	intCharCounter2 = intCharCounter1+1;
	intCharCounter1 = 0;

	while ((strDate.charAt(intCharCounter1+intCharCounter2) != "/" && strDate.charAt(intCharCounter2+intCharCounter1) != "-") && intCharCounter1+intCharCounter2 < intCharMax)
		intCharCounter1++;

	if (intCharCounter1+intCharCounter2 == intCharMax || intCharCounter1 > 2) {
	    alert("You have entered an invalid date for " + strFieldLabel + ".  Please make sure your date format is in MM/DD/YYYY format.");
	    objField.focus();	
		return false;
	}

	if (!isDateNumber(strDate.substring(intCharCounter2,intCharCounter1+intCharCounter2),2)) {
	    alert("You have entered an invalid date for " + strFieldLabel + ".  Please make sure your date format is in MM/DD/YYYY format.");
	    objField.focus();	
		return false;
	}

	intCharCounter2 = intCharCounter1+3;
	intCharCounter1 = 0;

	if (addOne) intCharCounter2++;

	while (intCharCounter1+intCharCounter2 < intCharMax)
		intCharCounter1++;


	if (intCharCounter1 != 2 && intCharCounter1 != 4) {
	    alert("You have entered an invalid date for " + strFieldLabel + ".  Please make sure your date format is in MM/DD/YYYY format.");
	    objField.focus();	
		return false;
	}

	if (!isDateNumber(strDate.substring(intCharCounter2,intCharCounter1+intCharCounter2),3)) {
	    alert("You have entered an invalid date for " + strFieldLabel + ".  Please make sure your date format is in MM/DD/YYYY format.");
	    objField.focus();	
		return false;
	}
	return true;
}

// ---------------------------------------------------------------------------------
//     Function:  isDateNumber
//
//      Purpose:  Checks to see if a string is a valid date number.  The method 
//                indicates which part of the date the string is intended to be.
//                1=Month, 2=Day.  If the string is nonnumeric, it fails.  If the 
//                day is greater than 31 it fails.  If the month is greater than 12,
//                it fails.  If the number is <= 0 it fails.    
//
//       Inputs:  objField      -- field to be checked
//                strFieldLabel -- label for the field that the user will understand 
//
//      Returns:  true if the field has a valid value, false if it does not
//
// Last Updated:  02/02/2001
// ---------------------------------------------------------------------------------- 

function isDateNumber(strNum,method)
{
	var str = new String(strNum);
	var i = 0;

    //if the string is not a number > 0, exit
	if (isNaN(parseInt(str)) || parseInt(str) < 0){
	    return false;
	}

    //day cannot be greater than 31
	if (method == 2)
		if (parseInt(str) > 31)
			return false;
	//month cannot be greater than 12
	if (method == 1)
		if (parseInt(str) > 12)
			return false;
    //make sure each character is an integer
	for (i = 0; i < str.length; i++)
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;


	return true;
}

// ---------------------------------------------------------------------------------
//     Function:  ForceNumber
//
//      Purpose:  Checks to see if a field's value is numeric 
//
//       Inputs:  objField      -- field to be checked
//                strFieldLabel -- label for the field that the user will understand 
//
//      Returns:  true if the field has a valid value, false if it does not
//
// Last Updated:  02/02/2001
// ---------------------------------------------------------------------------------- 

function ForceNumber(objField, strFieldLabel)
{
	var strField = new String(objField.value);
	var strCharCounter;
	
	//if the string is blank, it is valid.  
	if (isWhitespace(strFieldLabel)){
	    return true;
	}

    //for each character in the string, make sure it is an integer
	for (strCharCounter = 0; strCharCounter < strField.length; strCharCounter++)
		if (strField.charAt(strCharCounter) < '0' || strField.charAt(strCharCounter) > '9') {
			alert(strFieldLabel + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
			objField.focus();
			objField.select();
			return false;
		}

	return true;
}

// ---------------------------------------------------------------------------------
// ForceLength
//
//   Created By:  Jeff Welch
//
//      Purpose:  Ensures that the length of the entry is equal to the length required
//
//       Inputs:  objField -- the field in question
//                intLength -- max length of field
//                strFieldLabel -- label for the field that the user will understand
//
//      Returns:  Nothing
//
// Last Updated:  02/22/2001
// ---------------------------------------------------------------------------------- 

function ForceLength(objField, intMaxLength, intMinLength, strFieldLabel){
	var strField = new String(GetValue(objField));
	var blnIsValid
	//check for too long	
    if( intMaxLength != "" ){
	    if( strField.length > intMaxLength ) {
			alert(strFieldLabel + " cannot be longer than " + intMaxLength + " chararcters in length.  It is currently " + strField.length + " characters long.");
			objField.focus();
			objField.select();
			return false;
		}
    }
    if( intMinLength != "" ){
	    if( strField.length < intMinLength ){
			alert(strFieldLabel + " must be longer than " + intMinLength + " chararcters in length.  It is currently " + strField.length + " characters long.");
			objField.focus();
			objField.select();
			return false;	
	    }
	}
	
	//if all is good
	return true;
}
// ---------------------------------------------------------------------------------
// GetValue
//
//   Created By:  Jeff Welch
//
//      Purpose:  gets the value for an object
//
//       Inputs:  strObject -- the object with value
//
//      Returns:  value of the object
//
// Last Updated:  02/22/2001
// ---------------------------------------------------------------------------------- 

function GetValue(objObject){
    
    if( objObject.type == "select-one" ){
        return objObject.options[objField.options.selectedIndex].value;    
    }
    else {
        return objObject.value;
    }        
}
// ---------------------------------------------------------------------------------
// BatchValidate
//
//   Created By:  Jeff Welch
//
//      Purpose:  validates a set of form elements
//
//       Inputs:  blnValidate -- do we do validation?
//
//                objForm -- the form to validate
//
//                strForceEntryFields -- a list of required field names and their labels
//                of the form: "field1,lable1;field2,label2;..."
//
//                strForceLengthFields -- a list of field names that must be a certain length, 
//                their max lengths, their min lengths, and their labels
//                of the form: "field1,max1,min1,label1;field2,min2,max2,lable2;...
//
//				  strForceNumberFields -- a list of field names that must be numbers, their
//                values, and their labels, of the form: "field1,lable1;field2,label2;..."
//                
//                strForceDateFields -- a list of field names that must be dates, their values
//                and their labels of the form:  "field1,lable1;field2,label2;..."
//
//      Returns:  true or false
//
// Last Updated:  08/14/2001
// ---------------------------------------------------------------------------------- 

function BatchValidate(objForm, blnValidate, strForceEntryFields, strForceLengthFields, strForceNumberFields, strForceDateFields)
{
	var arrForceEntryFields;
	var arrForceLengthFields;
	var arrForceNumberFields;
	var arrForceDateFields;
	
	var arrForceEntryArguments;	
	var arrForceLengthArguments;	
	var arrForceNumberArguments;	
	var arrForceDateArguments;
	
	var intFieldCounter;
	var blnIsValid = true;
	
	if( blnValidate == false )
	{
		return true;
	}
	
	//parse the forced entry fields	
	if( strForceEntryFields != "" )
	{

		//for each of the fields, verify that the user put an entry into it
		arrForceEntryFields = strForceEntryFields.split(";")
		for( intFieldCounter = 0; intFieldCounter < arrForceEntryFields.length; intFieldCounter++ )
		{			
			arrForceEntryArguments = arrForceEntryFields[intFieldCounter].split(",")
			blnIsValid = ForceEntry(objForm.elements[arrForceEntryArguments[0]], arrForceEntryArguments[1])
			if( blnIsValid == false )
			{
				return blnIsValid
			}
		}
	}
	
	//parse the forced length fields
	if( strForceLengthFields != "" && blnIsValid == true )
	{
		//for each of the fields, verify that the user did not go over the max limit
		//for text but did go higher than the minimum
		arrForceLengthFields = strForceLengthFields.split(";")
		for( intFieldCounter = 0; intFieldCounter < arrForceLengthFields.length; intFieldCounter++ )
		{
			arrForceLengthArguments = arrForceLengthFields[intFieldCounter].split(",")
			blnIsValid = ForceLength(objForm.elements[arrForceLengthArguments[0]], arrForceLengthArguments[1], arrForceLengthArguments[2], arrForceLengthArguments[3]);
			
			if( blnIsValid == false )
			{
				return blnIsValid
			}			
		}
	}	
	//parse the forced number fields
	if( strForceNumberFields != "" && blnIsValid == true )
	{
		//for each of the fields, verify that the user put in a valid number
		arrForceNumberFields = strForceNumberFields.split(";")
		for( intFieldCounter = 0; intFieldCounter < arrForceNumberFields.length; intFieldCounter++ )
		{
			arrForceNumberArguments = arrForceNumberFields[intFieldCounter].split(",")
			blnIsValid = ForceNumber(objForm.elements[arrForceNumberArguments[0]] , arrForceNumberArguments[1]);
			
			if( blnIsValid == false )
			{
				return blnIsValid
			}			
		}		
	}
	
	//parse the forced date fields
	if( strForceDateFields != "" && blnIsValid == true )
	{
		//for each of the fields, verify that the user put in a valid date
		arrForceDateFields = strForceDateFields.split(";")
		for( intFieldCounter = 0; intFieldCounter < arrForceDateFields.length; intFieldCounter++ )
		{
			arrForceDateArguments = arrForceDateFields[intFieldCounter].split(",")
			blnIsValid = ForceDate(objForm.elements[arrForceDateArguments[0]] , arrForceDateArguments[1]);
			
			if( blnIsValid == false )
			{
				return blnIsValid
			}			
		}		
	}	
	return blnIsValid	        
}


// -->

