/**
* Validate contact form data
* jQuery port from original YUI version
* @author Mike Healy www.mikehealy.com.au
* @version 0.8 2009-02-12
*/
var contact = {
	error : false,		//Have input error
	
	//Form fields to validate. Bool indicates if REQUIRED
	fields : {
		name : true,
		company : false,
		email : true,
		phone : true,
		message : true
	},
	
	init : function() {
		
		var doField = function(ev) {
			contact.validateField(ev.target);
		};
		
		//Field listeners
		for(f in contact.fields) {
			$('#'.concat(f)).bind('blur', doField);
		}
		
		//Form submit listener
		$('#contactForm').bind("submit", contact.validateAll);
	},
	
	/*
	* Validation methods for each individual fields
	* Method (property) name corresponds to form name/ID
	* Sets contact.error, and writes error MSG to page
	*/
	validateField : function(el) {
		
		//Test if empty form value is allowable
		var notPassedOk = function(field, value) {
			if(!contact.fields[field] && value == '') return true; //Not required, and not supplied. OK.
			return false; //Must continue with validation
		};
		
		//Validation Funcs for each field
		//Return string error message
		var sets = {
			name : function(value) {
				if(notPassedOk('name', value)) return;
				if(value == '') return 'Name is required';
				if(value.length < 3) return 'Too short. 3 chars min';
				if(value.length > 30) return 'Too long (' + value.length + ' chars). 30 max';
				if(value.search(/[0-9]/) != -1) return 'Cannot contain numbers';
			},
			company : function(value) {
				if(notPassedOk('company', value)) return;
				if(value.length < 3) return 'Too short. 3 chars min';
				if(value.length > 45) return 'Too long (' + value.length + ' chars). 45 max';
			},
			email : function(value) {
				if(notPassedOk('email', value)) return;
				if(value == '') return 'Email is required';
				if(value.search(/^[a-zA-Z0-9\.,\_\<\>\-]+@[a-zA-Z0-9\.,\_\<\>\-]+\..+$/) == -1) return 'Invalid email address';
			},
			phone : function(value) {
				if(notPassedOk('phone', value)) return;
				value = value.replace(/[^0-9 \(\)]/g, '');
				$('phone').value = value;
				value = value.replace(/[^0-9]/g, '');
				if(value.length < 10) return 'Too short. Please include area code';
				if(value.length > 13) return 'Too long, 13 chars max';
			},
			message : function(value) {
				if(notPassedOk('message', value)) return;
				if(value.length < 1) return 'Please enter a message';
			}
		};
		
		//Write error message to span
		function writeError (id, error) {
			if(error) contact.error = true;
			else error = '&nbsp;'; //for innerHTML of error span
			
			var span = $('#'.concat(id).concat('Error')); //error msg element. eg. phoneError
			if(span) span[0].innerHTML = error;
		};
		
		//Validate this ELEMENT
		var name = el.name;
		writeError(el.id, sets[name](el.value));
	},
	
	
	//Validate WHOLE form on submit
	//Each field
	validateAll : function(e) {
		
		contact.error = false; //reset
		
		for(f in contact.fields) {
			var selectorString = '#'.concat(f);
			contact.validateField($(selectorString)[0]); //pass element reference to validate func.
		}

		if(contact.error) {
			alert("Invalid data. see the form for details");
			return false;
		}
		//allow form submission
	}
}

$(document).ready(contact.init);
