var firstError = null;
var errorstring = '';
var validForm = true;
var reqFields = {
	'title' : 'your title',
	'firstname' : 'your firstname',
	'surname' : 'your surname',
	'work_phone' : 'your work phone number',
	'home_phone' : 'your home phone number',
	'cell_phone' : 'your cell phone number',
	'email' : 'your email address',
	'message' : 'your message'
};

function validate() {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms['frm_contact'].elements;
	for (var key in reqFields) {
		if (!x[key].value)
			writeError(x[key],'Please provide ' + reqFields[key]);
	}
	if (x['email'].value.indexOf('@') == -1)
		writeError(x['email'], 'Email address is not valid!');
	if (!validForm)
		alert('Please correct the following:\n\n' + errorstring);
	if (firstError)
		firstError.focus();
	return validForm;
}

function writeError(obj, message) {
	validForm = false;
	if (obj.hasError) return;
	errorstring += '* ' + message + '\n';
	obj.hasError = true;
	if (!firstError) firstError = obj;
}

$(document).ready(function() {
	$.get("token.php",function(txt){
		$(".secure").append('<input type="hidden" name="ts" value="' + txt + '" />');
	});
	document.forms['frm_contact'].onsubmit = function () {
		return validate();
	}
});
