$(document).ready(function() {

	//if submit button is clicked
	$('#submit').click(function () {

		//Get the data from all the fields
		var name = $('input[name=name]');
		var email = $('input[name=email]');
		var telephone = $('input[name=telephone]');
		var website = $('input[name=website]');
		var type = $('select[name=type]');
		var comment = $('textarea[name=message]');

		//Simple validation to make sure user entered something
		//If error found, add highlight class to the text field
		if (name.val()=='Your full name' || name.val()=='') {
			name.addClass('highlight');
			return false;
		} else name.removeClass('highlight');

		if (email.val()=='Your email address' || email.val()=='') {
			email.addClass('highlight');
			return false;
		} else email.removeClass('highlight');

		if (telephone.val()=='Your telephone number' || telephone.val()=='') {
			telephone.addClass('highlight');
			return false;
		} else telephone.removeClass('highlight');

		if (website.val()=='Your website address' || website.val()=='') {
			website.addClass('highlight');
			return false;
		} else website.removeClass('highlight');

		if (comment.val()=='Your message' || comment.val()=='') {
			comment.addClass('highlight');
			return false;
		} else comment.removeClass('highlight');

		//organize the data properly
		var data = 'name=' + name.val() + '&type=' + type.val() + '&website=' + website.val() + '&telephone=' + telephone.val() + '&email=' + email.val() + '&message='  + encodeURIComponent(comment.val());

		//disabled all the text fields
		$('.text').attr('disabled','true');

		//show the loading sign
		$('.loading').show();

		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/site/modules/ajax_contact/controller.ajax_contact.module.php",

			//GET method is used
			type: "get",

			//pass the data
			data: data,

			//Do not cache the page
			cache: false,

			//success
			success: function (html) {
				//if process.php returned 1/true (send mail success)
				if (html) {
					//hide the form
					$('.ajaxform').fadeOut('slow', function() {
					   $('.done').fadeIn('slow');
					});
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');
			}
		});

		//cancel the submit button default behaviours
		return false;
	});
});
