////////////////////////////////////////////////////////////////////////////////////////////////////////
////
////	JQUERY FORM VALIDATOR
////
////	A plug-in script that handles the validation of a form
////
////	USAGE:
////
////	Include this file into the page and make sure that the form(s) has/have the following:
////	- The form(s) to be validated must have the class validForm
////	- A submit button with the class of validSubmitButton that is disabled (ensures that only users with
////	  javascript can use the form).
////	- There is a noscript tag that includes a description of needing javascript
////	- All required fields to have the class validFieldRequired (as well as any others they may need)
////	- If select boxes or radio buttons are to be considered not selected, they must have the value
////	  [NULL]. EG if a select box has an option that is considered not selected, its value must be [NULL]
////	- All fields that must have a valid email address must have the class validFieldEmail
////	- All fields that must only be numbers must have the class validFieldNumber 
////	- All fields that must be web addresses (optional http://, subdomain, domain and top-level domain)
////	  must have the class validFieldWeb
////	- The fields have a title with their description (that will be alerted to the user if left empty)
////	- There must be a style called invalidFormItem which styles incorrect items
////
////	The validateForm function calls customValidation(errorStr) to allow users to add any custom validation
////
////////////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function()
{
	$(".validSubmitButton").attr("disabled",false);
	$(".validForm").submit(validateForm);
});


// Enter any additional form validation here.  This function is passed the
// error string after checking base required and types of fields.
// This function should return true if the elements are validated and
// and error string if not valid.
function customValidation(errorStr)
{
	return true;	
}


// This function performs the base form validation and calls customValidation.
// If no errors are found, it allows the form to submit, else it cancels
function validateForm()
{
	// Disable the button until the validator is checked
	$(".validSubmitButton").attr("disabled",true);
	
	// Remove the invalidField class from all elements
	$(".invalidFormItem").removeClass("invalidFormItem");
	
	// Set the error field to empty
	var errorStr = "";
	var invalidItems = [];
	var validForm = true;

	// Make sure all required fields have a value (that is not empty or [NULL])
	$(".validFieldRequired").each(function()
	{
		var value = $(this).attr("value");
		var title = $(this).attr("title");
		
		// If the value is appropriate, return
		if (value != "" && value != "[NULL]") { return true; }
		
		// Add the title to the error string
		errorStr += "\n" + title + " is required";
		validForm = false;
		invalidItems.push($(this));
	});
	
	
	// Make sure all email type fields are properly formed
	$(".validFieldEmail").each(function()
	{
		var value = $(this).attr("value");
		var title = $(this).attr("title");
		
		// Use RegEx to test for the email format and return if it is
		regEx = /^\s*[a-z\d_]+(\.[a-z\d_]+)*@[a-z\d\-]{1,255}\.[a-z]{2,6}\s*(\.[a-z]{2,6}\s*)?(\.[a-z]{2,6}\s*)?$/;
		if(regEx.test(value)) { return true; }
		
		// Add the title to the error string
		errorStr += "\n" + title + " must be a proper email address";
		validForm = false;
		invalidItems.push($(this));
	});
	
	
	// Make sure all number type fields are properly formed
	$(".validFieldNumber").each(function()
	{
		var value = $(this).attr("value");
		var title = $(this).attr("title");
		
		// Use RegEx to test for the number format and return if it is
		regEx = /^(\s*\d+\s*)+$/;
		if(regEx.test(value)) { return true; }
		
		// Add the title to the error string
		//errorStr += "\n" + title + " must be numbers or the following: + ( ) #";
		errorStr += "\n" + title + " is required and must be numbers";
		validForm = false;
		invalidItems.push($(this));
	});
	
	
	// Make sure all web type fields are properly formed
	$(".validFieldWeb").each(function()
	{
		var value = $(this).attr("value");
		var title = $(this).attr("title");
		
		// Use RegEx to test for the web format and return if it is
		regEx = /^\s*(http\:\/\/)?([a-z\d\-]{1,63}]\.)*[a-z\d\-]{1,255}\.[a-z]{2,6}\s*(\.[a-z]{2,6}\s*)?(\.[a-z]{2,6}\s*)?$/;
		if(regEx.test(value)) { return true; }
		
		// Add the title to the error string
		errorStr += "\n" + title + " must be formatted as a web address";
		validForm = false;
		invalidItems.push($(this));
	});
	
	//Make sure the captcha is correct
	$(".validFieldCICaptcha").each(function()
	{
		var value = $(this).attr("value");
		var title = $(this).attr("title");
		var action = $(".validForm").attr("action");
		
		$.ajax({
			type: 'GET',
			url: action,
			async: false,
			data: "ajax_captcha="+value,
			success: function(data) {
				if(data !== '') {
					validForm = false;
					errorStr += "\nThe code you entered does not match the numeric code in the image";
					invalidItems.push($(this));
				}
			}
		});
	});
	
	// Run a custom validator and add any custom error to the error string
	customError = customValidation(errorStr);
	if (customError != true) { validForm = false; errorStr += customError; }
	
	
	// If the form is valid, enable the submit button and continue
	if (validForm) { return true; }
	
	//// If the form is not valid, this code will run
	
	// Apply an invalid style to incorrect items
	jQuery.each(invalidItems, function()
	{
		this.addClass("invalidFormItem");
	});
	
	alert("There was a problem with submitting the form: \n\r" + errorStr);
	$(".validSubmitButton").attr("disabled",false);
	
	return false;
}