$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var surname = $("input#surname").val();
		if (surname == "") {
      $("label#surname_error").show();
      $("input#surname").focus();
      return false;
    }
		var forname = $("input#forname").val();
		if (forname == "") {
      $("label#forname_error").show();
      $("input#forname").focus();
      return false;
    }
			var position = $("input#position").val();
		if (position == "") {
      $("label#position_error").show();
      $("input#position").focus();
      return false;
    }
			var address = $("input#address").val();
		if (address == "") {
      $("label#address_error").show();
      $("input#address").focus();
      return false;
    }
			var postcode = $("input#postcode").val();
		if (postcode == "") {
      $("label#postcode_error").show();
      $("input#postcode").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
		var tests = $("input#tests").val();
		if (tests == "") {
      $("label#tests_error").show();
      $("input#tests").focus();
      return false;
    }
		var testdate = $("input#testdate").val();
		if (testdate == "") {
      $("label#testdate_error").show();
      $("input#testdate").focus();
      return false;
    }
		var company = $("input#company").val();
		if (company == "") {
      $("label#company_error").show();
      $("input#company").focus();
      return false;
    }
		var otherinfo = $("input#otherinfo").val();
		if (otherinfo == "") {
      $("label#otherinfo_error").show();
      $("input#otherinfo").focus();
      return false;
    }

		
		var dataString = 'surname='+ surname + '&forname=' + forname + '&position=' + position + '&address=' + address + '&postcode=' + postcode + '&email=' + email + '&phone=' + phone + '&tests=' + tests + '&testdate=' + testdate + '&company=' + company + '&otherinfo=' + otherinfo;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(0, function() {
          $('#message').append("<img id='checkmark' src='images/tick.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

