function contactInit() {
	obs.init();
	contact.init();
}


/**
* Validate contact form data
* company field removed
*/
var contact = {
	error : false,		//Have input error
	
	//Form fields to validate. Bool indicates if REQUIRED
	fields : {
		name : true,
		email : true,
		phone : false,
		message : true
	},
	
	init : function() {
		var doField = function(e) {
			contact.validateField($E.getTarget(e));
		};
			
		//Field listeners
		for(f in contact.fields) {
			$E.on(f, 'blur', doField);
		}
		
		//Form listener
		$E.on('contactForm', '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';
			},
			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 = $(id.concat('Error')); //error msg element. eg. phoneError
			if(span) span.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) {
		
		$E.stopEvent(e);
		contact.error = false; //reset
		
		for(f in contact.fields) {
			contact.validateField($(f));
		}

		if(contact.error) {
			alert("Invalid data. View the form for details");
		}
		else $E.getTarget(e).submit(); //Submit the form.
	}
}

/**
* De-obscurify the email addresses
* mikehealy.com.au
*/
var obs = {
	
	init : function() {
		
		var contactTbl = $('contactTable');
		if(!contactTbl) contactTbl = null; //All spans
		
		var emails = $D.getElementsByClassName('obs', 'span', contactTbl);
		if(!emails) return;
		for(i=0; i<emails.length; i++) {
			obs.convert(emails[i]);	
		}
	},
	
	//Un-obscure email address element
	convert : function(el) {
		addy = el.innerHTML.replace(/\[ ?at ?\]/g, '@');
		addy = addy.replace(/ /g, '');
		addy = addy.replace(/\[ ?dot ?\]/g, '.');
		var link = document.createElement('a');
		if(el.title && el.title!='') {
			link.title = el.title;
			link.title = link.title.replace(/\[email\]/g, addy);
		}
		link.href = 'mailto:' + addy;
		link.innerHTML = addy;
		el.innerHTML = '';
		el.appendChild(link);
	}
};
$E.onDOMReady(contactInit);
