$(document).ready(function(){
	
	var options = {
		url: '/contactusSubmit',
		type: 'post',
		beforeSubmit: validateform,
		success: contactCompleted,
		dataType:'json'
	};
	
	
	$('#contactForm').submit(function() { 
		
		$(this).ajaxSubmit(options); 
		
		return false;
	
	});
	
	//Add 
	$("#map").hover(function(){
		$(this).attr("src", "/assets/contact/images/4hp_map_close.gif");
	},function(){
		$(this).attr("src", "/assets/contact/images/4hp_map.gif");
	});
	
});	
	
	
	/**
	 * Validate the form
	 */
	function validateform(formData, jqForm, options){

		var message = "'Checking details... ";
		
		//Alert user to validation
		blockFormCheck(message);
		
		
		//Collect all form vars
		var name = $('#contactForm input[@name=name]').fieldValue();
		var email = $('#contactForm input[@name=email]').fieldValue();
		var add1 = $('#contactForm input[@name=add1]').fieldValue();
		var add2 = $('#contactForm input[@name=add2]').fieldValue();
		var add3 = $('#contactForm input[@name=add3]').fieldValue();
		var city = $('#contactForm input[@name=city]').fieldValue();
		var postcode = $('#contactForm input[@name=postcode]').fieldValue();
		var company = $('#contactForm input[@name=company]').fieldValue();
		var dates = $('#contactForm input[@name=dates]').fieldValue();
		var guests = $('#contactForm input[@name=guests]').fieldValue();
		var tel = $('#contactForm input[@name=tel]').fieldValue();
		
		var error = false;
		
		//check email
		if(checkemail(email) == false){
			$("#email").css({background: "#ff9900", color:"#000000"});
			error = true;
		}else{
			$("#email").css({background: "#282020", color:"#cbbfaf"});
		}
		
		
		//check name is not emtpy
		if(notEmpty(name)== false){
			$("#name").css({background: "#ff9900", color:"#000000"});
			error = true;
		}else{
			$("#name").css({background: "#282020", color:"#cbbfaf"});
		}
		
		//check add is not emtpy
		if(notEmpty(add1)== false){
			$("#add1").css({background: "#ff9900", color:"#000000"});
			error = true;
		}else{
			$("#add1").css({background: "#282020", color:"#cbbfaf"});
		}
		
		//check add is not emtpy
		if(notEmpty(city)== false){
			$("#city").css({background: "#ff9900", color:"#000000"});
			error = true;
		}else{
			$("#city").css({background: "#282020", color:"#cbbfaf"});
		}
		
		//check add is not emtpy
		if(notEmpty(postcode)== false){
			$("#postcode").css({background: "#ff9900", color:"#000000"});
			error = true;
		}else{
			$("#postcode").css({background: "#282020", color:"#cbbfaf"});
		}
		
		
		//return
		if(error == true){
			$('#errorMsg').html("Please correct the fields");
			unblockUser();
			return false;
		}else{
			$('#errorMsg').empty();
			return true;
		}
	}
	
	
	
	/**
	 * Function to block the user interface
	 */
	function unblockUser(){
		$.unblockUI();
	}
	
	
	function contactCompleted(responseText, statusText){
	
		
		if(responseText.valid == "true"){
				$.blockUI( '<table cellpadding="0" cellspacing="0" border="0" width="280"><tr><td><img src="/assets/images/4hp_load_logo_3_36.gif" /><\/td><td>Thank you. We will be in contact soon.<\/td><\/tr><\/table>' );
				$('#errorMsg').empty();
				$('#contactForm').resetForm();
		}else{
				alert('ere');
				$.blockUI( '<table cellpadding="0" cellspacing="0" border="0" width="280"><tr><td><img src="/assets/images/4hp_load_logo_3_36.gif" /><\/td><td>There is a problem with the server, please try to re-submit the form<\/td><\/tr><\/table>' );
				
				$.each( responseText, function(i, n){
					   if(n == "invalid"){
					   		$("#"+i).css({background: "#ff9900", color:"#000000"});
					   }else{
					   		$("#"+i).css({background: "#282020", color:"#cbbfaf"});
					   }
				 });
		
				$('#errorMsg').html("Please correct the fields");
		}

		
		var unblock = function(){
			unblockUser();
		}
		
		setTimeout(unblock, 3500); 
	}
	
	
