$(document).ready(function() {
	$('#enquiry-form-button').click(function(e) {
		e.preventDefault();
		
		if( $('#fullname').val() == '' )
		{
			alert('Please enter your Full Name');
			$('#fullname').focus();
			return false;
		}
		else if( $('#emailaddr').val() == '' || !validEmail($('#emailaddr').val()) )
		{
			alert('Please enter a valid Email Address');
			$('#emailaddr').focus();
			return false;
		}
		else if( $('#yourenquiry').val() == '' )
		{
			alert('Please enter your enquiry');
			$('#yourenquiry').focus();
			return false;
		}
		
		//submit the form
		postEnquiryData({
			name: $('#fullname').val(),
			email: $('#emailaddr').val(),
			enquiry: $('#yourenquiry').val(),
			enquiry_form_newspage_submitted: $('#enquiry-form-newspage-submitted').val()
		});
		
		//update messages
		displaySending2();
		
	});
});

function postEnquiryData(data)
{
	$.post("/ajax/enquiry", data, delayDisplayComplete2);
}

function displaySending2()
{
	$("table.enquiry-form-table").html('<tr><td style="height: 100px; text-align: center;">Sending your details ...</td></tr>');
}

function delayDisplayComplete2() {
	setTimeout("displayComplete2()", 1000);
}

function displayComplete2() {
	$("table.enquiry-form-table").html('<tr><td style="height: 100px; text-align: center;">Your details have been submitted.</td></tr>');
	//setTimeout("closePanel()", 4000);
}

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

	//pageTracker._trackEvent('contact_form', 'submitted');
	
	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 style="text-align:center">Thank You. Your enquiry has been sent successfully.</h1>');
	$("#getintouchtext").html('CONTACT US. CLICK HERE');
	//pageTracker._trackEvent('contact_form', 'complete');
	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');
		//if( pageTracker == undefined && pageTracker != null ) { pageTracker._trackEvent('contact_form', 'opened'); }
	} else {
		$("#getintouchtext").html('CONTACT US. CLICK HERE');
		$("#contactform").slideUp("slow");
		setTimeout('$("#contactformframe").hide();', 700);
		//if( pageTracker == undefined && pageTracker != null ) { pageTracker._trackEvent('contact_form', 'closed'); }
	}
}
