$(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 message = $('textarea[name=message]');
		
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {
			name.removeClass('noHighlight');
			name.addClass('highlight');
			return false; 
		} else name.removeClass('highlight');
			name.addClass('noHighlight');
		
		if ((email.val()=='') || (email.val().indexOf("@")==-1) || (email.val().indexOf(".")==-1)) {
			email.removeClass('noHighlight');
			email.addClass('highlight');
			return false;
		} else email.removeClass('highlight');
			email.addClass('noHighlight');
		
		if (message.val()=='') {
			message.removeClass('noHighlight');
			message.addClass('highlight');
			return false; 
		} else message.removeClass('highlight');
		message.addClass('noHighlight');
		
		//organize the data properly
		var data = 'name=' + name.val() + '&email=' + email.val() + '&message='  + encodeURIComponent(message.val());
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "includes/contact.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==1) {					
					//hide the form
					$('#contactForm').fadeOut('fast');					
					
					//show the success message
					$('#formMessage').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;
	});	
});	