$(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 comment = $('textarea[name=msg]');
		var phone = $('input[name=phone]');		
		var tok1 = $('input[name=token1]');
		var tok2 = $('input[name=token2]');		
		var error = 0;

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

if (email.val()=='') { 
email.addClass('hightlight'); 
error=1;
} else{
	var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	if(filter.test(email.val())){
		email.removeClass('hightlight'); 
	}else{
		email.addClass('hightlight');
		error=1;
	}

}

if (phone.val()=='') { 
phone.addClass('hightlight'); 
error=1;
} else{
	var filter1 = /^[0-9+ ()-]*$/;
	if(filter1.test(phone.val())){
		phone.removeClass('hightlight'); 
	}else{
		phone.addClass('hightlight');
		error=1;
	}

}

if (comment.val()=='') { 
comment.addClass('hightlight'); 
error=1;
} else comment.removeClass('hightlight'); 

if (error) return false;	


		//organize the data properly
		var data = {name : name.val(), email : email.val(), msg : comment.val(), phone : phone.val(), token1 : tok1.val(), token2 : tok2.val()};
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "sendmsg.php",	
			
			type: "POST",

			//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=='Thank you! We have received your message.') {					
					//hide the form
					$('.form').fadeOut('fast', function() {
									$('.done').fadeIn('fast');
								});					
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	
