$(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 name = $("input#realname").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#realname").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#no_email_error").show();
      $("input#email").focus();
      return false;
    }
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if(!emailReg.test(jQuery.trim($("input#email").val()))) {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var address1 = $("input#address1").val();
    var address2 = $("input#address2").val();
    var city = $("input#city").val();
    var county = $("input#county").val();
    var postcode = $("input#postcode").val();
    var country = $("input#country").val();
    var mobile = $("input#mobile").val();
    var phone = $("input#telephone").val();
    var enquiry = $("textarea#enquiry").val();
    
		
		var dataString = '&name='+ name + '&email=' + email + '&phone=' + phone  + '&mobile=' + mobile + '&enquiry=' + enquiry + '&address1=' + address1 + '&address2=' + address2 + '&city=' + city + '&county=' + county + '&postcode=' + postcode + '&country=' + country;
		//alert(dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "./mail/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>")
        .append("<img id='checkmark' src='./assets/check.png' alt='success' />")
        .append("<p><a href='./contact.php'>go back</a></p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message');
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#realname").select().focus();
});
