/* Information Request form validation. Hacked by CoryH on 2010-01-26 - used GregS's Newsletter Subscription validation from Bingham. */
var formValidation = new function() {
	/**
	 * @method isValidEmail - Returns true if email is valid.
	 * Regex is from http://www.javascriptkit.com/script/script2/acheck.shtml.
	 * @author gregs 2009-09-22
	 */
	function isValidEmail(email) {
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		return filter.test(email);
	};
	
	return {
		
		/**
		 * @method validateSubscribeForm - Validations subscription form.
		 * Fires alert() with error message if form is not valid.
		 * 
		 * @param {Form} form - The form DOM element to look in for field values.
		 * 
		 * @return {Boolean} true if form is valid. False if not.
		 * @author gregs 2009-09-22
		 */
		validateSubscribeForm : function(form) {
		var email = form.email.value || "";
		var firstName = form.firstName.value || "";
		var lastName = form.lastName.value || "";
		
		var errors = [];

		if (!firstName.length) {
			errors.push("First name is required.");
		}
		if (!lastName.length) {
			errors.push("Last name is required.");
		}
		if (!email.length || !isValidEmail(email)) {
			errors.push("Valid email address is required.");
		}			
		if (!errors.length) {
			return true;
		};
		
		var errorMessage = "Information Request could not be submitted due to the following errors:";
		errorMessage += "\n\n - " + errors.join("\n - ");
		
		alert(errorMessage);
		
		return false;			
	},
	validateUnsubscribeForm : function(form) {
		var email = form.emailUnsubscribe.value || "";
		
		var errors = [];

		if (!email.length || !isValidEmail(email)) {
			errors.push("Valid email address is required.");
		}			
		if (!errors.length) {
			return true;
		};
		
		var errorMessage = "Information Request could not be submitted due to the following errors:";
		errorMessage += "\n\n - " + errors.join("\n - ");
		
		alert(errorMessage);
		
		return false;			
	},
	};
};
