//--- 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)
				dataType = parameters[0];
				errorMessage = parameters[1].replace(/ \b/, ""); //--- Removes the first character, which should be a blank.
				
				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);
						break;
				}
								
			}
		}
	}

	//--- 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;
	}
	
	
	
	
	//--- validateType(fieldName) -------------------------------------------------------------
	//--- scope: public
	//--- description: validates the type entered by the user.
	//---              Valid types are: text, number, phone, email, select, radio, check.
	//-----------------------------------------------------------------------------------------
	FormValidator.prototype.validateType = function(field, fieldType)
	{
		//--- Data Type Erros and Focus take precedence over form validation errors.
		this.errorMessage = "--- System Error ---\n\nInvalid Data Type of Type ('" + fieldType + "')";
		this.fieldToFocusOn = field;
		return this.errorMessage;
	}
	
	
	
	
	//--- 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);
		
		if(validateEmptyString(txtObj.value) == true)
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
		else
		{
			//--- Validate whatever is contained within the field.
			if(validateCharacters(txtObj.value, "0123456789.") != true)
			{
				//--- String does not validate.
				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);
		
		if(validateEmptyString(txtObj.value) == true)
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
		else
		{
			//--- Validate whatever is contained within the field.
			if(validateCharacters(txtObj.value, "0123456789-() ") != true)
			{
				//--- String does not validate.
				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);
		
		if(validateEmptyString(txtObj.value) == true)
		{
			//--- String is empty!
			this.errorMessage += errorMessage + "\n";
			this.returnString += "0";
			
			if(this.fieldToFocusOn == "")
			{
				this.fieldToFocusOn = field;
			}
		}
		else
		{
			//--- Validate whatever is contained within the field.
			if(validateEmail(txtObj.value) != true)
			{
				//--- String does not validate.
				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 ---	
	
	//--- 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;
		}
	}
	
	
	
	
	
}