// Title:			Form Validation
// File:			formValidation.js
// Purpose:			Validates a form
// Author:			Troy Martin - Scripted Motion, LLC
// Last Updated:	04/06/06

//-----------------------------------------------------------------------
// Purpose: Outputs an error message to the specified element
// In: 	element - The element that failed validation
//		errorElementID - The errorElement that should take the error
//		errors - An errors array
// Returns: N/A
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function outputError(element, errorElementID, errors)
{
	var errorElement = document.getElementById(errorElementID);
	if(errorElement != null)
	{
		element.className = "inputError";
		errorElement.innerHTML = errors[0];
	}
}

//-----------------------------------------------------------------------
// Purpose: Resets the appearance of an element
// In: 	element - The element that needs to be reset
//		errorElementID - The errorElement that needs to be reset
// Returns: N/A
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function resetElement(element, errorElementID)
{
	// Remove any error message in the error element
	var errorElement = document.getElementById(errorElementID);
	if(errorElement != null && errorElement.innerHTML != "")
	{	
		errorElement.innerHTML = "";
	}
	// Reset the input field to it's normal state
	//element.className = "inputNormal";
}

//-----------------------------------------------------------------------
// Purpose: Determines the type of validation that needs to be done
//          and calls the appropriate function.
// In: 	element - The element that needs to be validated
//		type - The type of validation to be performed. (i.e. email)
//		errorContainerID - The id of the errorElement 
// Returns: N/A
// Precondition: Has to have access to the validation functions
//               found in validationFunctions.js
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateElement(element, type, errorElementID, required)
{
	// Variable Declarations
	var errors = new Array();
	var result = true;
	var value = element.value;
	
	// Remove leading / trailing whitespace from value
	value = trim(value);

	// If the field is not required, and it is == "", return true. 
	// Otherwise, it has to pass the validation functions...none of which
	// accept an empty string
	if(required == false)
	{
		if(value == "undefined" || value == "")
		{
			resetElement(element, errorElementID);
			return true;
		}
	}
		
	// Select the appropriate validation function and validate value
	switch(type)
	{
		case "name":
			result = validateName(value, errors);
			break;		
		case "email":
			result = validateEmailAddress(value, errors);
			break;
		case "phone":
			result = validateNorthAmericanPhoneNumber(value, errors);
			break;
		case "company":
			result = validateCompany(value, errors);
			break;
		case "message":
			result = validateMessage(value, errors);
			break;
		default:
			alert("Unknown validation type");
			return false;
	}	
	
	// Check the result of the validation function
	if(result == false)
	{
		outputError(element, errorElementID, errors);
		return false;
	}
	else
	{
		resetElement(element, errorElementID);
		return true;
	}	
}

//-----------------------------------------------------------------------
// Purpose: Validates the form prior to submission
// In: 	N/A
// Returns: True if the form passes validation. False if not.
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateOnSubmit()
{
	errors = new Array();
	var errorsEncountered = false;

	// Name validation
	if(validateElement(document.contactForm.name, "name", "nameErrorContainer", true) == false)
	{	
		errorsEncountered = true;
	}
	// Email validation
	if(validateElement(document.contactForm.emailAddress, "email", "emailAddressErrorContainer", true) == false)
	{	
		errorsEncountered = true;
	}
	// Phone validation
	if(validateElement(document.contactForm.phone, "phone", "phoneErrorContainer", false) == false)
	{	
		errorsEncountered = true;
	}
	// Company validation
	if(validateElement(document.contactForm.company, "company", "companyErrorContainer", false) == false)
	{	
		errorsEncountered = true;
	}
	// Message validation
	if(validateElement(document.contactForm.message, "message", "messageErrorContainer", true) == false)
	{	
		errorsEncountered = true;
	}
	if(errorsEncountered == true)
	{
		return false;
	}
	return true;
}
