// This is a grouping of validation utility functions.

//////////////////////////  
// Returns chars from left of string
// (used to get valid chars of input name) 
function leftString (InString, num)  
{
	OutString=InString.substring (0, num);
	return (OutString);
}
// End of leftstring utility
/////////////////////////

//////////////////////////  
// Returns chars from right of string
// (used to get year from date string) 
function rightString (InString, num)  
{
	OutString=InString.substring (InString.length-num, InString.length);
	return (OutString);
}
// End of rightstring utility
/////////////////////////

//////////////////////////  
// Tests to see if input is blank. 
function isblank(s)
{
    for(var i = 0; i < s.length; i++) 
    {
        var c = s.charAt(i);
        if ((c != ' ')) return false;
    }
    return true;
}
// End of isblank utility
/////////////////////////

///////////////////////
// Test to see if input contains disallowed chars
function allowNotInString (InString, RefString)
{
	for (Count=0; Count < InString.length; Count++)  
	{
		TempChar = InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)!=-1)  
			return (false);
	}
	return (true);
}
// End of char utility
/////////////////////////

///////////////////////
// Test to see if input is numeric

function isNumberString (InString)  
{
        if(InString.length==0) 
                return (false);
        RefString="1234567890";
        for (Count=0; Count < InString.length; Count++)  
        {
                TempChar= InString.substring (Count, Count+1);
                if (RefString.indexOf (TempChar, 0)==-1)  
                        return (false);
        }
        return (true);
}
// End of numeric utility
/////////////////////////

////////////////////////
// Test to see if the field is a floating point value 

function isFloatingPoint (InString)
{
        if(InString.length==0)
                return (false);
        TempChar= InString.substring (InString.length-3, InString.length-2);
	  if (TempChar != ".")
         	    return(false);
        RefString="1234567890.";
        for (Count=0; Count < InString.length; Count++)
        {
                TempChar= InString.substring (Count, Count+1);
                if (RefString.indexOf (TempChar, 0)==-1)
                        return (false);
        }
        return (true);
}
// End of float utility
/////////////////////////


///////////////////////
// Test to see if number is within range

function isWithinRange (InString, RangeMin, RangeMax)  
{
	if ((InString == null) || (InString == "")) 
		return (false) 
	if((InString>=RangeMin) && (InString<=RangeMax))
		return (true);
	else
		return (false);
}
// End of withing range utility
/////////////////////////

///////////////////////
// Test to see if input is USzip
function isUSZip (InString)  
{
	if (InString.length==0) 
		return (false);
	if ((InString.length!=5) && (InString.length!=10))
		return (false);
	RefString="1234567890-";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1) 
			return (false);
	}
	return (true);
}

// End of US zip utility
/////////////////////////


function isValidEmail (InString)
{
    if (InString.length==0)
        	return (false);
	if ( (InString.indexOf('@') == -1) || (InString.indexOf('@') != InString.lastIndexOf('@')) )
			return (false);
	if (InString.indexOf('.') == -1) 
			return (false);
	if ( (InString.lastIndexOf('.')) < (InString.indexOf('@')) )
			return (false); 

    return (true);
}

// End of Email utility
/////////////////////////


///////////////////////
// Test to see if input is Date-appropriate
// and spit out mmddyyyy string
function isDate(InString)
{
	var TempString = "";
	var TempString2 = "";
	var Year = "";
	var Month = "";
	var Day = "";
	if (InString.length!=10)
		return ("wrongFormat");
		
	if (InString.substring(2,3) != "/")
		return ("wrongFormat");
	if (InString.substring(5,6) != "/")
		return ("wrongFormat");
		
	for (Count=0; Count <InString.length; Count++) 
	{
		DateChar = InString.substring(Count, Count+1);
		if ((DateChar=="/") || (DateChar==",") || (DateChar==".") || (DateChar=="-"))
			TempString +=""
		else
			TempString += DateChar
	}
	// Make sure string is number
	if (isNaN(TempString))
	{
		return("notNumber");
	}
	// Make sure string is correct length
	if (TempString.length != 8)
	{
		return("wrongFormat");
	}
	// Set vars for year and month
	Year = rightString(TempString, 4);
	Month = leftString(TempString, 2);
	TempString2 = leftString(TempString, 4);
	Day = rightString(TempString2, 2);

	// Make sure the month is valid
	if (Month >= 13)
	{
		return("wrongMonth");	
	}	
	
	// Check the day for months that need 31
	if ((Month == "01") || (Month == "03") || ( Month == "05")
		|| (Month == "07") || (Month == "08") || (Month == "10") || (Month == "12"))
	{
		if (Day <= 31)
		 	return(TempString);
		else
			return("invalidDate");
	}

	// Check the day for months that need 30
	if ((Month == "04") || (Month == "06") || ( Month == "09")
		|| (Month == "11"))
	{
		if (Day <= 30)
		 	return(TempString);
		else
			return("invalidDate");
	}

	// Deal with February and leap year
	if (Month == "02")
	{
		if (Year % 4 == 0)
		{
			if(Day <= 29)
				return(TempString);
			else
				return("invalidDate");
		}
		if (Year % 4 != 0)
		{
			if(Day <= 28)
				return(TempString);
			else
				return("invalidDate");
		}
	}

	return (TempString);
}
//End of Date checker
/////////////////////////

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Begin the main  object constructor and object array
// Here's the array constructor
var listOfTypes = new Array();
// Here's the object constructor (content page calls this)
function fieldType(Name, DataType, Mandatory, PrettyName, CharsNot)
{
	this.name = Name;
	this.type = DataType;
	this.mandatory = Mandatory;
	this.prettyname = PrettyName;
	this.charsnot = CharsNot;
	listOfTypes[count] = this;
	count++;
}
// End of object constructor
///////////////////////////////////////

////////////////////////////////////////////////////////////
// Begin the form validation

function verify(f, formAction) {
    var msg = "";
    var typeFields = "";
    var mandatoryFields = "";
    var TempString = "";
    var dateField = "";
    var nameField = "";
    var radioCheck = "";
    var radioError = "";
    var fieldType = "";
    var passWordStore = "";

    // First determine if single-button or double, and if
    // double, then set hidden fields to passed in values
    if (formAction != "submit") {
		f.elements.formAction.value = formAction;
    }
		
    // Loop through form elements only once
    for(i = 0; i < f.elements.length; i++) {
    	var formElement = f.elements[i];

	// Loop through list of element types
	// and compare against element name

	for (c = 0; c < count; c++) {
		var typeElement = listOfTypes[c];
		// Take substring of formelement and assign to variable
		TempString = formElement.name;
		TempString2 = typeElement.name;
		nameField = TempString.substring(0,TempString2.length);
		if (nameField == typeElement.name) {
			//  If the field is part of list
			//  Verify numeric fields
			//  Non-mandatory
			if ((typeElement.type == 1) && (formElement.value != "") && (typeElement.mandatory == "F")
				&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
				
				if (!isNumberString(formElement.value)) {
					typeFields += "Atleast one of the \"" + typeElement.prettyname + "\" field is empty or is not a whole number";
					typeFields += ".\n";
				}
			}	

			//	Mandatory
			if ((typeElement.type == 1) && (typeElement.mandatory == "T")
				&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)
				&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
				
				if (formElement.value != "") {
					if (!isNumberString(formElement.value)) {
						typeFields += "Atleast one of the \"" + typeElement.prettyname + "\" field is empty or is not a whole number";
						typeFields += ".\n";
					}
				} else {
                			mandatoryFields += "     " + typeElement.prettyname;
                			mandatoryFields += ".\n";					
				}
			}
	
			//  If the field is part of list
			//  Verify US zip fields
			//  Non-mandatory
				
			if ((typeElement.type == 2) && (formElement.value != "") && (typeElement.mandatory == "F")
				&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
			
				if (!isUSZip(formElement.value)) {
					typeFields += "- The \"" + typeElement.prettyname + "\" field ";
					typeFields += "must contain a valid Postal Code (5 numbers)";
					typeFields += ".\n";
				}
			}	

			if ((typeElement.type == 2) && (typeElement.mandatory == "T")
				&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)  
				&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
			
				if (formElement.value != "") {
					if (!isUSZip(formElement.value)) {
						typeFields += "- The \"" + typeElement.prettyname + "\" field";
						typeFields += "must contain a valid Postal Code (5 numbers)";
						typeFields += ".\n";
					}
				} else {
                			mandatoryFields += "     " + typeElement.prettyname;
                			mandatoryFields += ".\n";					
				}					
			}

			//  If the field is part of list
			//  Verify character list
			//  Non-mandatory
				
			if ((typeElement.type == 3)  && (formElement.value != "") 
				&& (typeElement.mandatory == "F")
				&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
			
				if (!allowNotInString(formElement.value, typeElement.charsnot)) {
					typeFields += "- The \"" + typeElement.prettyname + "\" field";
					typeFields += " cannot contain the following characters: \n";
					typeFields += "     " + typeElement.charsnot;
					typeFields += "\n";
				}
			}	

			//  If the field is part of list
			//  Verify character list
			//  Mandatory
				
			if ((typeElement.type == 3)  && (typeElement.mandatory == "T")
				&& (typeFields.indexOf(typeElement.prettyname) == -1)  
				&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)) {
			
				if (formElement.value != "") {
					if (allowNotInString(formElement.value, typeElement.charsnot) == false) {
						typeFields += "- The \"" + typeElement.prettyname + "\" field";
						typeFields += "cannot contain the following characters: \n";
						typeFields += "     " + typeElement.charsnot;
						typeFields += ".\n";
					}
				} else {
                			mandatoryFields += "     " + typeElement.prettyname;
                			mandatoryFields += ".\n";					
				}					
			}	

			//  If the field is part of list
			//  Verify Appropriate date string 
			//  non-mandatory
			if ((typeElement.type == 4)  && (formElement.value != "") 
				&& (typeElement.mandatory == "F")
				&& (typeFields.indexOf(typeElement.prettyname) == -1)) {   
			
				dateField = isDate(formElement.value);
					
				if (dateField == "wrongFormat") {
					typeFields += "- The \"" + typeElement.prettyname + "\" field";
					typeFields += " must be of the form mm/dd/yyyy.\n";
				}
					
				if (dateField == "wrongMonth") {
					typeFields += "- Please specify a valid month for ";
					typeFields += typeElement.prettyname + ".\n";
				}

				if (dateField == "invalidDate") {
					typeFields += "- Please specify a valid date for ";
					typeFields += typeElement.prettyname + ".\n";
				}

				if (dateField == "notNumber") {
					typeFields += "- The \"" + typeElement.prettyname + "\" field";
					typeFields += " must contain only numbers and separators.\n";
				}
						
				if ((dateField != "wrongFormat") 
					&& (dateField != "notNumber")
					&& (dateField != "wrongMonth")
					&& (dateField != "invalidDate")) {
                  			// do not modify form data
						//formElement.value = isDate(formElement.value);
					}				
				}
				
				//  If the field is part of list
				//  Verify Appropriate date string 
				//  Mandatory
				if ((typeElement.type == 4)  && (typeElement.mandatory == "T")
					&& (typeFields.indexOf(typeElement.prettyname) == -1)  
					&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)) {
				
					if (formElement.value != "") {
						dateField = isDate(formElement.value);
					
						if (dateField == "wrongFormat") {
							typeFields += "- The \"" + typeElement.prettyname + "\" field";
							typeFields += " must be of the form mm/dd/yyyy.\n";
						}
						
						if (dateField == "wrongMonth") {
							typeFields += "- Please specify a valid month for ";
							typeFields += typeElement.prettyname + ".\n";
						}

						if (dateField == "invalidDate") {
							typeFields += "- Please specify a valid date for ";
							typeFields += typeElement.prettyname + ".\n";
						}

						if (dateField == "notNumber") {
							typeFields += "- The \"" + typeElement.prettyname + "\" field";
							typeFields += " must contain only numbers and separators.\n";
						}
						
						if ((dateField != "wrongFormat") 
							&& (dateField != "notNumber")
							&& (dateField != "wrongMonth")
							&& (dateField != "invalidDate")) {
                     					// do not modify form data
								//formElement.value = isDate(formElement.value);
						}
					} else {
                				mandatoryFields += "     " + typeElement.prettyname;
                				mandatoryFields += ".\n";					
					}					
				}	
				

				//	If the field is part of list and is Select menu,
				//  verify that it has been selected.
				
				if ((typeElement.type == 5) && (typeElement.mandatory == "T")) {
					if((formElement.selectedIndex == 0) || (formElement.selectedIndex == null)) {
                				mandatoryFields += "     " + typeElement.prettyname;
                				mandatoryFields += ".\n";					
					}
				}	

				//  If the field is part of list and of unspecified type
				//  verify that it contains information.
				if ((typeElement.type == 6) && ((formElement.value == "") || (formElement.value == null))
					&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)) {
                				mandatoryFields += "     " + typeElement.prettyname;
                				mandatoryFields += ".\n";					
				}
				

				//  If the field is part of list and is radio
				//  verify that it belongs to a group specified by chars
				//  First set the checked var to not checked before entering loop
				if ((typeElement.type == 7) && (typeElement.charsnot != "")) {
					for (Count = 0; Count < f.elements.length; Count++) {
						if(formElement.checked) {
							radioCheck = "true";
							break;
						}				
					}
					radioError = "- A " + typeElement.prettyname + " must be selected.\n";
				}

				//	If the field password and mandatory
				if ((typeElement.type == 8) && (typeElement.mandatory == "T")
					&& (typeFields.indexOf(typeElement.prettyname) == -1)  
					&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)) {
				
					if (formElement.value == "") {
 	               			mandatoryFields += "     " + typeElement.prettyname;
                				mandatoryFields += ".\n";					
					}
					if ((formElement.value != "") && (formElement.value.length < 6)) {
						typeFields += "- The \"" + typeElement.prettyname + "\" field";
						typeFields += " must be longer than 5 characters.\n";
					}
					if ((formElement.value.length >= 6) && (passWordStore == "")) {
						passWordStore = formElement.value;
					}
					if ((formElement.value.length >= 6) && (passWordStore != "")) {
						if(formElement.value != passWordStore) {
							typeFields += "- The \"" + typeElement.prettyname + "\" fields";
							typeFields += " do not match. Please try again.\n";
						}
					}
				}

				//	If the field password and non-mandatory
				if ((typeElement.type == 8) && (typeElement.mandatory == "F")
					&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
				
					if ((formElement.value != "") && (formElement.value.length < 6)) {
						typeFields += "- The \"" + typeElement.prettyname + "\" field";
						typeFields += " must be longer than 5 characters.\n";
					}
					if ((formElement.value.length >= 6) && (passWordStore == "")) {
						passWordStore = formElement.value;
					}
					if ((formElement.value.length >= 6) && (passWordStore != "")) {
						if(formElement.value != passWordStore) {
						typeFields += "- The \"" + typeElement.prettyname + "\" fields";
						typeFields += " do not match. Please try again.\n";
					}
				}
			}

               	//  If the field is part of list
                	//  Verify email fields
                	//  Non-mandatory

                	if ((typeElement.type == 9) && (formElement.value != "") && (typeElement.mandatory == "F")
                    	&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
                 
				if (!isValidEmail(formElement.value)) {
                        	typeFields += "- A valid \"" + typeElement.prettyname + "\" field ";
                        	typeFields += " must have '@' and '.'";
                        	typeFields += ".\n";
                    	}
                	}

                	if ((typeElement.type == 9) && (typeElement.mandatory == "T")
                    	&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)
                    	&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
                    
				if (formElement.value != "") {
                        	if (!isValidEmail(formElement.value)) {
                            		typeFields += "- A Valid \"" + typeElement.prettyname + "\" field";
                            		typeFields += " must have '@' and '.'";
                            		typeFields += ".\n";
                        	}
                    	} else {
                        	mandatoryFields += "     " + typeElement.prettyname;
                        	mandatoryFields += ".\n";
                    	}
                	}

                	//  If the field is part of list
                	//  Verify floating point fields
                	//  Non-mandatory
                	if ((typeElement.type == 10) && (formElement.value != "") && (typeElement.mandatory == "F")
                    	&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
                  
				if (!isFloatingPoint (formElement.value)) {
                        	typeFields += "Atleast one of the \"" + typeElement.prettyname + "\" field is empty or is not a floating point number"; 
                        	typeFields += ".\n";
                    	}
                	}

                	//  Mandatory
                	if ((typeElement.type == 10) && (typeElement.mandatory == "T")
                   	&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)
                    	&& (typeFields.indexOf(typeElement.prettyname) == -1)) {
                  
				if (formElement.value != "") {
                        	if (!isFloatingPoint (formElement.value)) {
                            		typeFields += "Atleast one of the \"" + typeElement.prettyname + "\" field is empty or is not a floating point number";
                            		typeFields += ".\n";
                        	}
                    	} else {
                        	mandatoryFields += "     " + typeElement.prettyname;
                        	mandatoryFields += ".\n";
                    	}
			}

			//	If the field username and mandatory
			if ((typeElement.type == 11) && (typeElement.mandatory == "T")
				&& (typeFields.indexOf(typeElement.prettyname) == -1)  
				&& (mandatoryFields.indexOf(typeElement.prettyname) == -1)) {
			
				if (formElement.value == "") {
 	               		mandatoryFields += "     " + typeElement.prettyname;
                			mandatoryFields += ".\n";					
				}

				if ((formElement.value != "") && (formElement.value.length < 6)) {
					typeFields += "- The \"" + typeElement.prettyname + "\" field";
					typeFields += " must be longer than 5 characters.\n";
				}
			}

		}
	}
}
    // Now, if there were any errors, then display the messages, and
    // return false to prevent the form from being submitted. Otherwise
    // return true.
    if (!mandatoryFields && !typeFields && (!radioError || radioCheck)) f.submit();
    msg += "Please correct the following error(s) and re-submit.\n";
    msg += "____________________________\n\n";
    if (mandatoryFields) 
    {
        msg += "- These required field(s) are empty:\n" 
                + mandatoryFields + "\n";
    }
	if (typeFields)
	{
		msg += typeFields + "\n";
	}
	if (!radioCheck && radioError)
	{
		msg += radioError + "\n";
	}
    if(mandatoryFields || typeFields || (radioError && !radioCheck))
    {
    	alert(msg);
    }
}
// End of form validator
///////////////////////////////////////

//////////////////////////////////////
// This is the valid list of types and their 
// numeric equivalent
var numeric = "1";
var zip = "2";
var chars = "3";
var date = "4";
var select = "5";
var mandatoryOnly = "6";
var radio = "7";
var passwd = "8";
var email = "9";
var floatingPoint = "10";
var username = "11";
var	array = new Object();
var count = 0;
//////////////////////////////////////
