function submitform() {
	// form validation
	var error = false;

	if (document.contact.name.value.length<1) {
		$("#contactname").html('<img src="/images/public/alert.gif" />');
		error=true;
	}
	if (!document.contact.type.selectedIndex) {
		$("#contacttype").html('<img src="/images/public/alert.gif" />');
		error=true;
	}
	if (!document.contact.how.selectedIndex) {
		$("#contacthow").html('<img src="/images/public/alert.gif" />');
		error=true;
	}
	if (!validEmail(document.contact.email.value)) {
		$("#contactemail").html('<img src="/images/public/alert.gif" />');
		error=true;
	}
	if (document.contact.spam_entered.value!=document.contact.spam_actual.value) {
		$("#antispam").html('<img src="/images/public/alert.gif" />');
		error=true;
	}
	
	if (!error) {
		postData();
		displaySending();
	}
	
	return true;
	
}

function displaySending() {
	$("#contactform").html('<h1 style="text-align:center">Sending Enquiry</h1><p align="center"><img src="/images/public/loadingAnimation.gif" /></p><br /><br />');
}

function postData() {
	$.post("/process-form.php", {
		name:document.contact.name.value,
		company:document.contact.company.value,
		type:document.contact.type.value,
		town:document.contact.town.value,
		email:document.contact.email.value,
		phone:document.contact.phone.value,
		website:document.contact.website.value,
		how:document.contact.how.selectedIndex,
		interest_consultancy:document.contact.interest_consultancy.checked,
		interest_solutions:document.contact.interest_solutions.checked,
		interest_training_and_adoption:document.contact.interest_training_and_adoption.checked,
		interest_support:document.contact.interest_support.checked,
		interest_other:document.contact.interest_other.checked,
		spam_actual:document.contact.spam_actual.checked,
		spam_entered:document.contact.spam_entered.checked,
		comments:document.contact.comments.value
	}, delayDisplayComplete);
}

function delayDisplayComplete() {
	setTimeout("displayComplete()", 1000);
}

function displayComplete() {
	$("#contactform").html('<h1>Thank You. Your enquiry has been sent successfully.</h1>');
	$("#getintouchtext").html('CONTACT US. CLICK HERE');
	setTimeout("closePanel()", 4000);
}

function closePanel() {
	$("#contactform").slideUp("slow");
	$("#getintouchtext").html('CONTACT US. CLICK HERE');
}

function checkvalid(ref, name) {
	switch (name) {
		case "contactname":
			if (ref.value.length>=1) $("#contactname").html('<img src="/images/public/tick.gif" />');
			else $("#contactname").html('<img src="/images/public/alert.gif" />');
			break;
		case "contacttype":
			if (ref.selectedIndex) $("#contacttype").html('<img src="/images/public/tick.gif" />');
			else $("#contacttype").html('<img src="/images/public/alert.gif" />');
			break;
		case "contactemail":
			if (validEmail(ref.value)) $("#contactemail").html('<img src="/images/public/tick.gif" />');
			else $("#contactemail").html('<img src="/images/public/alert.gif" />');
			break;
		case "contacthow":
			if (ref.selectedIndex) $("#contacthow").html('<img src="/images/public/tick.gif" />');
			else $("#contacthow").html('<img src="/images/public/alert.gif" />');
			break;
		case "contactspam":
			if (document.contact.spam_entered.value==document.contact.spam_actual.value) $("#antispam").html('<img src="/images/public/tick.gif" />');
			else $("#antispam").html('<img src="/images/public/alert.gif" />');
			break;
			
	}
}

function validEmail(s) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(s) == false) {
		return false;
	} else {
		return true;
	}
}

function toggleContactForm() {
	if ($("#contactform:first").is(":hidden")) {
		$("#contactform").html($("#contactformframe"));
		$("#contactformframe").show();
		$("#contactform").slideDown("slow");
		$("#getintouchtext").html('CLOSE ENQUIRY FORM');
	} else {
		$("#getintouchtext").html('CONTACT US. CLICK HERE');
		$("#contactform").slideUp("slow");
		setTimeout('$("#contactformframe").hide();', 700);
	}
}
