//--- formValidator.js ---
// This file handles basic form validation.  (blank fields, e-mail, and phone).

//--- function validateForm(form) -------------------------------------------------------------
//--- Description: Basic form validation function.  It souldn't really be included with the 
//--- same file as the class, but this was done for ease of use with other developers.
//---------------------------------------------------------------------------------------------
function validateForm(form)
{
	var myTest = new FormValidator();
	myTest.validateForm(form);
	
	//--- Check to see if we submit the form or display an error ------------------------------
	if(myTest.getReturnValue() == false)
	{
		//--- Check to see if this was caused by the form not validating or by user error.
		if(myTest.getSystemError() != "")
		{
			alert(myTest.getSystemError());
		}
		else
		{
			alert(myTest.getErrorMessage());
			form(myTest.getFieldToFocusOn()).focus();
		}
		
		return false;
	}
	else
	{
		return true;
	}
}

//--- Class Follows ---------------------------------------------------------------------------
function FormValidator()
{
	//--- Class Propetires
	this.errorMessage = "Please fill in the following\n\n";
	this.fieldToFocusOn = "";
	this.returnValue = new Boolean;
	this.errorString = new String;
	this.systemError = new String;
	
	
	
	
	//--- Set and Get Data Methods
	FormValidator.prototype.setFieldToFocusOn = function(fieldToFocusOn)
	{
		this.fieldToFocusOn = fieldToFocusOn;
	}
	
	FormValidator.prototype.getFieldToFocusOn = function()
	{
		return(this.fieldToFocusOn);
	}
	
	FormValidator.prototype.setErrorMessage = function(errorMessage)
	{
		this.errorMessage += errorMessage + "\n";
	}
	
	FormValidator.prototype.getErrorMessage = function()
	{
		return(this.errorMessage);
	}
	
	FormValidator.prototype.setReturnValue = function(returnValue)
	{
		this.returnValue = returnValue;
	}
	
	FormValidator.prototype.getReturnValue = function()
	{
		return(this.returnValue);
	}
	
	FormValidator.prototype.setErrorString = function(error)
	{
		this.errorString = error;
	}
	
	FormValidator.prototype.getErrorString = function()
	{
		//--- If this returns a -1 then it validates.
		return(this.errorString.indexOf(1));
	}
	
	FormValidator.prototype.setSystemError = function(error)
	{
		this.systemError = error;
	}
	
	FormValidator.prototype.getSystemError = function()
	{
		return(this.systemError);
	}
	
	
	
	
	//--- Public Methods
	
	//--- validateForm(form) ------------------------------------------------------------------
	//--- scope: public
	//--- description: validates all fields marked for validation with a hidden field that
	//---              starts witha v_ (v_txtFirstName).
	//-----------------------------------------------------------------------------------------	
	FormValidator.prototype.validateForm = function(form)
	{
		var validationCode = "";
		var parameters = new Array(2);
		var fieldValidationResult = "";
		var i=0;

		//--- 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(",");
					
					//here we test every field.					
					//--- Determine if the field validates or not and which field to focus on if it does not.					
					fieldValidationResult = validateField(form.elements[i-1].value, parameters[0]);
					this.errorString += fieldValidationResult;
					
					if(fieldValidationResult == 1)
					{
						this.errorMessage += removeQuotes(parameters[1]) + "\n";
						
						if(this.fieldToFocusOn == "")
						{
							
							this.fieldToFocusOn = fieldToValidate;
						}
					}
					
				}
			}
		}

		//--- Display and return.
		if(this.errorString.indexOf(1) >= 0)
		{
			this.returnValue = false;
		}
		else
		{
			this.returnValue = true;	
		}
		return;
	}
	
	
	
	
	
	//--- Private Functions ---
	
	//--- validateField(fieldValue, fieldType) ------------------------------------------------
	//--- scope: private
	//--- description: validates the data according to the fieldType.
	//-----------------------------------------------------------------------------------------	
	function validateField(fieldValue, fieldType)
	{	
		var errorCode = "";
		
		//--- Always check to see if the field is empty first.
		if(fieldValue == "")
		{
			//--- false
			errorCode = "1";
		}
		else
		{
			switch(removeQuotes(fieldType))
			{
				case "":
					//--- Nothing left to do.  Data exist, It validates.
					//--- true
					errorCode = "0";
					
					break;
					
					
				case "email": //--- Check to see if data is in email format.	
					
					//--- Data exist, now check to see if it is e-mail address.	
					if(validateEmail(fieldValue) != true)
					{
						//--- false
						errorCode = "1";
					}
					else
					{
						//--- true
						errorCode = "0";
					}
					
					break;
				
				
				case "phone": //--- Check to see if data is in phone format.
					
					//--- Data exist, now check to see if it is a phone number.
					if(validateCharacters(fieldValue, "0123456789-() ") != true)
					{
						//--- false
						errorCode = "1";
					}
					else
					{
						//--- true
						errorCode = "0";
					}
					
					break; 
								
					
				case "numeric": //--- Check to see if data is numbers only.
				
					//--- Data exist, now we check to see if it numbers only.
					if(validateCharacters(fieldValue, "0123456789") != true)
					{
						//--- false
						errorCode = "1";
					}
					else
					{
						//--- true
						errorCode = "0";
					}
					
					break;
					
					
				default:
					//--- error 
					//--- unrecognized data type.
					//errorCode = "2";
					//NOT SURE HOW TO DO THIS PART
					alert("ERROR\n\nInvalid datatype - " + removeQuotes(fieldType));
					errorCode = "1";
					break;
			}
		}
		
		return(errorCode);
	}




	//--- 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;
	}
	
	
	
	
	//--- removeQuotes(string) ----------------------------------------------------------------
	//--- scope: private
	//--- description: Removes excess quotes and spaces from strings.
	//-----------------------------------------------------------------------------------------
	function removeQuotes(string)
	{
		return(string.substring(string.indexOf("'") + 1, string.lastIndexOf("'")));
	}
	
	
	
	//--- 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;
		}
	}
	
}