/****************************************************************
**	File:		formCheck.js
**	
**	Purpose:	To allow the programmer to validate a form through 
**					the use of client-side scripting.
**
**	Author:		Matt Finholt
**	Date:		8/15/01
**
**	Objects:	formWatcher(formID, highliteClass, message)
**					methods:	getErrors()
**								addElement(formField, type, compareType, compareValue, id, origColor, txtLabel, message)
**								compare(elementObject)
**								formOK()
**
**		`		formElement(message, formField, type, compareType, compareValue, id, origColor, txtLabel)
**
**
**
**
*****************************************************************/


function formCheck(highliteClass)
	{
	this.oElements = new Array();		//init new variables & array
	this.oHClass = highliteClass;
	this.msgErrors = "";
	//this.oFormID = formID;		
	
	/***************************************************************
	**	This function allows the user to obtain a long string containing
	**		the errors separated by a comma.
	***************************************************************/
	this.getErrors = function()
		{
		return this.msgErrors;
		}//end function
	
	
	/***************************************************************
	**	This function allows the user to add any form element that they 
	**		would like to validate to this object.
	***************************************************************/
	this.addElement = function(formField, type, compareType, compareValue, id, origColor, txtLabel, message)
		{
		
		//create a new formElement object and add it to the array of elements
		this.oElements[this.oElements.length] = new formElement(message, formField, type, compareType, compareValue, id, origColor, txtLabel);
		}//end function
		
		
	/***************************************************************
	**	This method takes the information that is stored in the formElement 
	**		object and checks it agains the information found in the 
	**		form field.
	***************************************************************/	
	this.compare = function(elementObject)
		{
		var isValid = true;
				
		if(elementObject.eFormField.value != "")
			{
			switch(elementObject.eType)  //check for the type of form object to validate
				{
				case 'text':	// if the form object is a text field... use this
				case 'Text':
					switch(elementObject.eCompType)  //check which type of form text object to validate
						{
						case 'email':
						case 'Email':
						elementObject.eFormField.value = Trim(elementObject.eFormField.value) //remove leading and trailing spaces
							//check to see if the user has entered a valid email address		
							if((elementObject.eFormField.value.indexOf('@',0)==-1)||(elementObject.eFormField.value.indexOf('.',0)==-1)||(elementObject.eFormField.value.length<=6))
								{	isValid = false;	}
							else
								{
//jh09/29/08								var tempArray = null;
//jh09/29/08								tempArray = elementObject.eFormField.value.split(".");	//split the string where there is a period
//jh09/29/08								if((tempArray[1] == null)||(tempArray[1].length < 2))	//check lengths of returned array elements
//jh09/29/08									{	isValid = false;	}

								apos=elementObject.eFormField.value.indexOf("@");
								dotpos=elementObject.eFormField.value.lastIndexOf(".");
								if (apos<1||dotpos-apos<2) 
									{	isValid = false;	}
								else
									{
										if(elementObject.eFormField.value.indexOf(' ',0)!=-1) //check for space in the email address
											{	isValid = false;	}
									}

								}//end else
							break;
						
						case 'number':	//check the validity of the number
						case 'Number':
							if(isInteger(elementObject.eFormField.value)) //check if the string is a valid integer
								{
								if(!eval(elementObject.eFormField.value + "" + elementObject.eCompValue))  // check validity of the number
									{	isValid = false;	}
								}//end if
							else
								{	isValid = false;	}									
							break;					
						
						case 'string':	//return false if length is less than specified length...
						case 'String':	
							if(!eval(elementObject.eFormField.value.length + "" + elementObject.eCompValue))	//compare length of string
								{	isValid = false;	}
							break;					
						
						default:		//return false if there is nothing in the form field (default option) 
							if(formObject.value.length == 0)
								{	isValid = false;	}
							break;
						}//end switch
					break;
				
				case 'select':	//if form object is a select box, do this check
				case 'Select':
					var count = 0;	//init count to 0
					
					for (var i=0; i<elementObject.eFormField.length; i++)	//loop through number of elements in select box
						{
						//alert(elementObject.eFormField[i].selected + " - " + elementObject.eFormField[i].value);
						if ((elementObject.eFormField[i].selected) && (elementObject.eFormField[i].value != ""))
					    	{	count++;	}
						}//end for
					  
					if(count == 0)
						{	isValid = false;	}
					break;
				
				case 'radio':
				case 'Radio':					
					var count = 0;					
					var object = elementObject.eFormField
					
					for(var j=0; j < elementObject.eFormField.length; j++)	//loop through array of radio buttons
						{
						if(elementObject.eFormField[j].checked)		//see if button checked
							{	count++;	}
						}//end for loop
					if(count == 0)					//if none checked, give error
						{	isValid = false;	}						
					break;
					
				default:	//this is to keep the programmer in check... to make sure this isn't too difficult for them =).
					alert("You must assign a type to your Form Element. ie. Text, Select, Radio, Checkbox");
					break;
				}//end swich statement
			}//end if
		else
			{	isValid = false;	}
		
		return isValid;
		}//end compare function
		
	/***************************************************************
	**	This method simply alerts the errors for the page user
	***************************************************************/		
	/*
	this.alertMessage = function()
		{
		alert(this.oMessage + "\n" + this.msgErrors);
		}//end alert function
	*/
		
	/***************************************************************
	**	This function is called upon by the user to check if all the
	**		previously instantiated form elements contain valid information.
	**		It cycles through the form elements and calls upon onother method
	**		to check that particular elements validity.
	***************************************************************/		
	this.formOK = function()
		{			
		this.msgErrors = "";	//reset the errors string
		
		var isValid = true;  	//init variables
		var validityCheck = false;
		var object = null;
				
		//loop through elements and call method to check validity
		for(var i=0; i<this.oElements.length; i++)
			{
			validityCheck = this.compare(this.oElements[i]); //call method to check validity
			
			if(!validityCheck)	//if the form element is not valid - do this
				{	
				isValid = false;
				
				if(((TheBrowser.isIE) || (TheBrowser.isNS6)) && this.oElements[i].eTxtLabelName != "")	//change the color of the textlabel
					{
					object = document.getElementById(this.oElements[i].eTxtLabelName);
					object.className = this.oHClass;	
					}
				
				this.msgErrors +=  "," + this.oElements[i].eMessage;  //add to the error msg
				}
			else		
				{
				if(((TheBrowser.isIE) || (TheBrowser.isNS6)) && this.oElements[i].eTxtLabelName != "")	//if the element is valid, change the color back to origional
					{	
					object = document.getElementById(this.oElements[i].eTxtLabelName);
					object.className = this.oElements[i].eOrigColorClass;
					}				
				}//end else
			}//end for loop
		
		if(this.msgErrors != "")	//if there are errors, truncate the string by cutting off the first comma
			{
			this.msgErrors = this.msgErrors.slice(1, this.msgErrors.length);
			}//end if
			
		//return validity statement - true/false
		return isValid;
		}//end formOK function
		
	}//end object 
	
	
/***************************************************************
**
**   The formElement object holds all the information for the
**		individualy added form elements that the user would
**		like to validate.
**
***************************************************************/

function formElement(message, formField, type, compareType, compareValue, id, origColor, txtLabel)
	{
	this.eMessage = message;			//init variables to hold the formElement object information
	this.eFormField = formField;
	this.eCompType = compareType;
	this.eCompValue = compareValue; 
	this.eElementID = id;
	this.eOrigColorClass = origColor;
	this.eTxtLabelName = txtLabel;	
	this.eType = type;
	}//end object
	

/***************************************************************
**
**   This is a method to check the validity of an integer...
**
***************************************************************/	
	
function isInteger(numStr)
 	{
	// skip leading + or -
	if ((numStr.charAt(0) == "-") || (numStr.charAt(0) == "+"))
		{	var i = 1;	}
	else
		{	var i = 0;	}
	
	// Search through string's chars one by one until we find a 
	// non-numeric char, then return false; if we don't, return true
	for (i; i<numStr.length; i++)
		{   
	  	// Check that current character is number
	  	var character = numStr.charAt(i);
	  	if (!((character >= "0") && (character <= "9"))) 
	  		{	return false;	}
	  	}//end for
	
	// return true if all characters are numbers
	return true;
	}
	
//LTrim(string) : Returns a copy of a string without leading spaces.
//==================================================================
        function LTrim(str)
        /***
                PURPOSE: Remove leading blanks from our string.
                IN: str - the string we want to LTrim

                RETVAL: An LTrimmed string!
        ***/
        {
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(0)) != -1) {
                    // We have a string with leading blank(s)...

                    var j=0, i = s.length;

                    // Iterate from the far left of string until we
                    // don't have any more whitespace...
                    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                        j++;


                    // Get the substring from the first non-whitespace
                    // character to the end of the string...
                    s = s.substring(j, i);
                }

                return s;
        }

//RTrim(string) : Returns a copy of a string without trailing spaces.
//==================================================================
        function RTrim(str)
        /***
                PURPOSE: Remove trailing blanks from our string.
                IN: str - the string we want to RTrim

                RETVAL: An RTrimmed string!
        ***/
        {
                // We don't want to trip JUST spaces, but also tabs,
                // line feeds, etc.  Add anything else you want to
                // "trim" here in Whitespace
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
                    // We have a string with trailing blank(s)...

                    var i = s.length - 1;       // Get length of string

                    // Iterate from the far right of string until we
                    // don't have any more whitespace...
                    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                        i--;


                    // Get the substring from the front of the string to
                    // where the last non-whitespace character is...
                    s = s.substring(0, i+1);
                }

                return s;
        }	
//	Trim(string) : Returns a copy of a string without leading or
//               trailing spaces
//=============================================================
        function Trim(str)
        /***
                PURPOSE: Remove trailing and leading blanks from our string.
                IN: str - the string we want to Trim

                RETVAL: A Trimmed string!
        ***/
        {
                return RTrim(LTrim(str));
        }


