$(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#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
	  var lname = $("input#lname").val();
		if (lname == "") {
      $("label#lname_error").show();
      $("input#lname").focus();
      return false;
    }
	
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if (!email.match(emailExp)){
		$("label#email_error2").show();
      $("input#email").focus();
      return false;
	}
	
	
		var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
	

	
  var services = $("input#services").val();
		if (services == "") {
      $("label#services_error").show();
      $("input#services").focus();
      return false;
    }
		
 var enquiry = $("textarea#enquiry").val();
		if (enquiry == "") {
      $("label#enquiry_error").show();
      $("input#enquiry").focus();
      return false;
    }

		
		var dataString = 'name='+ name + '&lname='+ lname + '&email=' + email + '&phone=' + phone + '&services='+ services + '&enquiry='+ enquiry;
		//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>Thank you, we have received your enquiry!</h2>")
        .append("<p>A member of the Kedron Dental Centre team will be in touch with you soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

