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

  $(".contactformbutton").click(function() {
		// validate and process form
		// first hide any error messages    	$("#id option:selected")val()
    $('.error').hide();
    $('.errorpro').hide();
	
	var title = $('select.title option:selected').val();
  if (title == "") {
        $("label#title_error").show();
        $("input[name='title']").focus();
        return false;
      }
		
	  var clientname = $("input#clientname").val();
		if (clientname == "") {
      $("label#clientname_error").show();
      $("input#clientname").focus();
      return false;
    }
	
function validateAddress(address) {
var pattern = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

return pattern.test(address);
}

var email = $("input#email").val();
if (email == "") {
$("label#email_error").html('This field is required');
$("label#email_error").show();
$("input#email").focus();
return false;
}
if (!validateAddress(email)) {
$("label#email_error").html('This email is not valid');
$("label#email_error").show();
$("input#email").focus();
return false;
}
		var workphone = $("input#workphone").val();
		if (workphone == "") {
      $("label#workphone_error").show();
      $("input#workphone").focus();
      return false;
    }
	
	var zip = $("input#zip").val();
		if (zip == "") {
      $("label#zip_error").show();
      $("input#zip").focus();
      return false;
    }
	
var photographer = $("input[name='photographer']:checked").val();
 if (!photographer) {
       $("label#photographer_error").show();
       $("input[name='photographer']").focus();
       return false;
     }

	

	var where = $('select.where option:selected').val();
  if (where == "") {
        $("label#where_error").show();
        $("input[name='where']").focus();
        return false;
      }

	var companyname = $("input#companyname").val();
	var website = $("input#website").val();
	var mobile = $("input#mobile").val();
	var address = $("input#address").val();	
	var wheredata = $("input#wheredata").val();	
    var weddings = $("input[name='weddings']:checked").val();	
    var portraits = $("input[name='portraits']:checked").val();	
    var events = $("input[name='events']:checked").val();
	var adicinfo = $("textarea#adicinfo").val();	
	var tango = $("input#tango").val();	
	
	var dataString = 'title='+ title +'&clientname=' + clientname + '&companyname=' + companyname + '&website=' + website + '&email=' + email + '&workphone=' + workphone+ '&mobile=' + mobile + '&address=' + address + '&zip=' + zip+ '&photographer=' + photographer + '&where=' + where + '&wheredata=' + wheredata + '&weddings=' + weddings + '&portraits=' + portraits + '&events=' + events + '&adicinfo=' + adicinfo + '&tango=' + tango + '&portraits=' + portraits;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "/contactSubmit.php",
      data: dataString,
      success: function() {
        $('#contactform').html("<div id='message'></div>");
        $('#message').html("<p><strong>Thank you for completing our contact form. </STRONG><p><p>One of our team will be in touch shortly.  If your enquiry is urgent, please call us on: 0118 976 2796, or email: <a href='mailto:info@bookedimages.com'>info@bookedimages.com</a></p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<br>");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("form#contactform").select().focus();
});

