$(function(){
	$('.error').hide();
	$('input.text-input').css({backgroundColor:"#FFFFFF"});
	$('input.text-input').focus(function(){
		$(this).css({backgroundColor:"#AFDFFF"});
	});
	
	$('input.text-input').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});

	$(".button").click(function(){
		// validate and process form
		// first hide any error messages
		$('.error').hide();
		errorOccurred=0;
		
		var name = $("input#name").val();
		if(name == ""){
			$("label#name_error").show();
			//$("input#name").focus();
			errorOccurred=1;
		}

		var email = $("input#email").val();
		if(email == ""){
			$("label#email_error").show();
			//$("input#email").focus();
			errorOccurred=1;
		}

		var phone = $("input#phone").val();
		if(phone == ""){
			$("label#phone_error").show();
			//$("input#phone").focus();
			errorOccurred=1;
		}

		var job = $("input#job").val();
		if(job == ""){
			$("label#job_error").show();
			//$("input#job").focus();
			errorOccurred=1;
		}

		var company = $("input#company").val();
		if(company == ""){
			$("label#company_error").show();
			//$("input#company").focus();
			errorOccurred=1;
		}

		var postcode = $("input#postcode").val();
		if(postcode == ""){
			$("label#post_error").show();
			//$("input#postcode").focus();
			errorOccurred=1;
		}

		var enquiry = $("textarea#enquiry").val();
		if(enquiry == ""){
			$("label#enquiry_error").show();
			//$("input#enquiry").focus();
			errorOccurred=1;
		}

		var employees = $("select#employees :selected").val();
		if(employees == ""){
			$("label#employees_error").show();
			//$("input#employees").focus();
			errorOccurred=1;
		}
		
		if (!errorOccurred) {
			$.post('./process.php',$('#contactForm').serialize(),function() {
					$('#contact_form').html("<div id='message'></div>");
					$('#message').html("<h2 >Contact Form has been successfully submitted.</h2>")
					.append("<p>Thank you for contacting us.<br />We will be in touch soon.</p>")
					.hide()
					.fadeIn(1500, function(){
						$('#message').append("<img id='checkmark' src='./img/check.png' />");
					});
			});
		}
		return false;
	});
});

runOnLoad(function(){
	$("input#name").select().focus();
});