//--- validateForm(form) ------------------------------------------------------------------
//--- scope: public
//--- description: validates all fields marked for validation with a hidden field that
//---              starts witha v_ (v_txtFirstName).
//-----------------------------------------------------------------------------------------	
validateForm = function(form)
{
	var validationCode = "";
	var parameters = new Array(2);
	var fieldValidationResult = "";
	var i=0;

	var validate = new FormValidator(form);


	//--- Run through all fields in the form that were marked for validation.
	for(i=0;i<form.elements.length;i++)
	{
		//--- Validate only the fileds that were marked.
		if(form.elements[i].type == "hidden")
		{
			//--- Check each hidden field for the validate code, v_.
			validationCode = form.elements[i].id.substring(0,2);
			
			if(validationCode == "v_")
			{
				fieldToValidate = form.elements[i].id.substring(2, form.elements[i].id.length);
				
				parameters = form.elements[i].value.split(",");				
				// This was used when the fields were typed in with single quotes. I.E. ('txtField', 'Error Message')
				//dataType = parameters[0].replace(/'/g, "");
				//errorMessage = parameters[1].replace(/'/g, "");
				
				//--- Now fields are passed in as (txtField, Error Message)
//*** All the catch and Try code probably need to be done in a better way! ***
				try 
				{
					dataType = parameters[0];
					errorMessage = parameters[1].replace(/ \b/, ""); //--- Removes the first character, which should be a blank.
				}
				catch(error)
				{
					//--- Error!  They either forgot a comma or typed an extra comma.  End the program.
					alert("--- System Error ---\n\nInvalid Syntax!\n(value=\"" + form.elements[i].value + "\")");
					document.getElementById(fieldToValidate).focus();
					return false;
				}
				
				try
				{
					switch(dataType)
					{
						case "text":
							//--- check textfield
							validate.validateText(fieldToValidate, errorMessage);
							break;
						case "number":
							validate.validateNumber(fieldToValidate, errorMessage);
							break;
						case "phone":
							validate.validatePhone(fieldToValidate, errorMessage);
							break;
						case "email":
							validate.validateEmail(fieldToValidate, errorMessage);
							break;
						case "select":
							validate.validateSelect(fieldToValidate, errorMessage);
							break;
						case "radio":
							validate.validateRadio(fieldToValidate, errorMessage);
							break;
						case "check":
							validate.validateCheck(fieldToValidate, errorMessage);
							break;
						default:
							//--- ERROR
							validate.validateType(fieldToValidate, dataType);
							alert(validate.getErrorMessage());
							document.getElementById(validate.getFieldToFocusOn()).focus();
							return false;
							break;
					}
				}
				catch (error)
				{
					alert("--- System Error---\n\nYour field names do not match!\n" + form.elements[i-1].id + " != " + form.elements[i].id);
					return false;
				}
								
			}
		}
	}

	//--- Display and return.
	if(validate.validate() == true)
	{
		return true;
	}
	else
	{
		alert(validate.getErrorMessage());
		document.getElementById(validate.getFieldToFocusOn()).focus();
		return false;
	}
}

//--- 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;
			}
		}
	}
	
	
	
	
	//--- 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;
		}
	}
	
	
	
	
	
}
