//--- Class Follows ---------------------------------------------------------------------------
function FormValidator(form)
{
	//--- Class Propetires
	this.form = form;
	this.errorMessage = "Please fill in the following\n\n";
	this.fieldToFocusOn = "";
	this.returnString = new Boolean;
	this.errorString = new String;
	this.systemError = new String;
	
	
	
	
	//--- Public Methods
	//--- validate() --------------------------------------------------------------------------
	//--- scope: public
	//--- description: Returns true or false depending on if the form is valid.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validate = function()
	{
		//--- If returnString contains even 1 zero 0, then we do not submit the form.
		if(validateCharacters(this.returnString, "0") == false)
		{
			return false;
		}
		else
		{
			return true;	
		}
		
	}
	
	
	
	
	//--- getErrorMessage() -------------------------------------------------------------------
	//--- scope: public
	//--- description: returns the error message.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.getErrorMessage = function()
	{
		return this.errorMessage;
	}
	
	
	
	
	//--- getFieldToFocusOn() --------------------------------------------------------------------------
	//--- scope: public
	//--- description: returns the field that is in error.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.getFieldToFocusOn = function()
	{
		return this.fieldToFocusOn;
	}
	
	
	
	
	//--- validateText(fieldName, errorMessage) -----------------------------------------------
	//--- scope: public
	//--- description: validates a textfield to ensure it is not empty.
	//-----------------------------------------------------------------------------------------	
	FormValidator.prototype.validateText = function(field, errorMessage)
	{
		var txtObj = new Object();
		
		txtObj = eval("this.form." + field);
		
		if(validateEmptyString(txtObj.value) == true)
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
		
	}
	
	
	
	
	//--- validateNumber(fieldName, errorMessage) ---------------------------------------------
	//--- scope: public
	//--- description: validates a textbox to verifty it contains only numbers.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateNumber = function(field, errorMessage)
	{
		var txtObj = new Object();
		
		txtObj = eval("this.form." + field);
		
		//--- check for empty string, valid characters.
		if((validateEmptyString(txtObj.value) == true) || (validateCharacters(txtObj.value, "0123456789.") != true))
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}
	
	
	
	
	//--- validateRange(fieldName, minValue, maxValue) ----------------------------------------
	//--- scope: public
	//--- description: validates a textbox to verifty it contents is within a specific range.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateRange = function(field, errorMessage, minValue, maxValue)
	{
		var txtObj = new Object();
		
		txtObj = eval("this.form." + field);
		
		//--- check for empty string, valid characters, valid range.
		if((validateEmptyString(txtObj.value) == true) || (validateCharacters(txtObj.value, "0123456789.") != true) || (validateRange(txtObj.value, minValue, maxValue) != true))
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}
	
	
	
	
	
	//--- validateLength(fieldName, errorMessage) ---------------------------------------------
	//--- scope: public
	//--- description: validates a textbox to verifty it contains a specific length.
	//--- 			   This function is identical to validateRange, except the filed length
	//---			   is passed to the private function instead of the filed value.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateLength = function(field, errorMessage, minValue, maxValue)
	{
		var txtObj = new Object();
		
		txtObj = eval("this.form." + field);
		
		//--- check for empty string, valid characters, valid range.
		if((validateEmptyString(txtObj.value) == true) || (validateRange(txtObj.value.length, minValue, maxValue) != true))
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}




	//--- validatePhone(fieldName, errorMessage) ----------------------------------------------
	//--- scope: public
	//--- description: validates a textbox to verifty it contains only numbers.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validatePhone = function(field, errorMessage)
	{
		var txtObj = new Object();
		
		txtObj = eval("this.form." + field);
		
		//--- check for empty string, valid characters, valid range.
		if((validateEmptyString(txtObj.value) == true) || (validateCharacters(txtObj.value, "0123456789-() ") != true) || (validateRange(txtObj.value.length, 10, 16) != true))
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}
	
	
	
	
	
	//--- validateEmail(fieldName, errorMessage) ----------------------------------------------
	//--- scope: public
	//--- description: validates a textbox to verifty it contains an email address.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateEmail = function(field, errorMessage)
	{
		var txtObj = new Object();
		
		txtObj = eval("this.form." + field);
		
		//--- check for empty string, valid email.
		if((validateEmptyString(txtObj.value) == true) || (validateEmail(txtObj.value) != true))
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}
	
	
	
	
	
	//--- validateSelect(fieldName, errorMessage) ---------------------------------------------
	//--- scope: public
	//--- description: validates a selectbox to ensure something other then the first entry 
	//---			   is selected.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateSelect = function(field, errorMessage)
	{
		var selObj = new Object();
		
		selObj = eval("this.form." + field);
		
		//--- check to make sure the first item is not selected.
		if(selObj.selectedIndex == 0)
		{
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";

			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}
	
	
	

	//--- validateCheck(fieldName, errorMessage) ---------------------------------------------
	//--- scope: public
	//--- description: validates a checkbox to ensure it is checked. 
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateCheck = function(field, errorMessage)
	{
		var chkObj = new Object();
		
		chkObj = eval("this.form." + field);
		
		//--- check to make sure the item is selected.
		if(chkObj.checked != true)
		{
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";

			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
	}
	
	
	
	
	//--- validateRadio(fieldName, errorMessage) ----------------------------------------------
	//--- scope: public
	//--- description: validates a radiobutton to ensure at least one item is checked. 
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateRadio = function(field, errorMessage)
	{
		var rdoObj = new Object();
		var errorString = new String();
		
		rdoObj = eval("this.form." + field);
		
		//--- Cycle through the radio options.
		for(i=0;i<rdoObj.length;i++)
		{
			if(rdoObj[i].checked == true)
			{
				errorString += "1";
			}
			else
			{
				errorString += "0";
			}
		}
		
		//--- Check to see if the errorString is all 0's.  If so, that means nothing was checked.
		if(validateCharacters(errorString, "0") == true)
		{
			//--- No item was checked.
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
		
	}
	
	
	
	

	
	
	//--- Private Functions ---	
	
	//--- validateRange(stringToValidate, minLength, maxLength) -------------------------------
	//--- scope: private
	//--- Description: Checks a string to see if its length is within the minimum and maximum
	//---			   allowable length.
	//-----------------------------------------------------------------------------------------
	function validateRange(valueToValidate, minValue, maxValue)
	{
		if(maxValue == null)
		{
			//--- Only check the minium length.
			if(valueToValidate >= minValue)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if((valueToValidate >= minValue) && (valueToValidate <= maxValue))
			{
				//--- The number of characters that were entered are with the allowable range.
				return true;	
			}
			else
			{
				return false;	
			}
		}
	}
	
	
	
	
	
	//--- validateEmptyString(stringToValidate) -----------------------------------------------
	//--- scope: private
	//--- Description: Checks a string to see if it is empty or begins with blank spaces.
	//-----------------------------------------------------------------------------------------
	function validateEmptyString(stringToValidate)
	{
		//--- check for an empty string or a string that begins with spaces.
		if((stringToValidate == "") ||  stringToValidate.indexOf(" ") == 0)
		{
			//--- validateEmptyString = true because the string is empty or starts with 0s.
			return true;
		}
		else
		{
			return false;
		}	
	}




	//--- validateCharacters(stringToValidate, chaactersToCheckFor ----------------------------
	//  scope: private
	//  Description: Pass in a string you wish to validate as the first parameter,
	//               and the allowed characters as the second parameter.
	//-----------------------------------------------------------------------------------------
	function validateCharacters(stringToValidate, charactersToCheckFor)
	{
		for(i=0;i<stringToValidate.length;i++)
		{
			if(charactersToCheckFor.indexOf(stringToValidate.charAt(i).toLowerCase()) == -1)
			{	
				//--- The -1 means that the number in (stringToValidate) was not found in (charactersToCheckFor).
				return false;
			}
		}
		
		//--- If we make it out of the for loop without having returned false, we know its safe to return true;
		return true;
	}
	
	
	
	
	//--- validateEmail(string) ---------------------------------------------------------------
	//--- scope: private
	//--- description: validates the data to ensure it is in a proper email format.
	//-----------------------------------------------------------------------------------------
	function validateEmail(email)
	{
		if((email.indexOf("@") == -1) || (email.indexOf(".") == -1) || (email.indexOf(" ") != -1))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	
	
	
	
}