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

  $(".regformbutton").click(function() {
		// validate and process form
		// first hide any error messages    	$("#id option:selected")val()
    $('.errorg').hide();
    $('.errorprog').hide();	
	
	var regtitle = $('select.regtitle option:selected').val();
  if (regtitle == "") {
        $("label#regtitle_error").show();
        $("input[name='regtitle']").focus();
        return false;
      }
		
	  var regclientname = $("input#regclientname").val();
		if (regclientname == "") {
      $("label#regclientname_error").show();
      $("input#regclientname").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 regemail = $("input#regemail").val();
if (regemail == "") {
$("label#regemail_error").html('This field is required');
$("label#regemail_error").show();
$("input#regemail").focus();
return false;
}
if (!validateAddress(regemail)) {
$("label#regemail_error").html('This email is not valid');
$("label#regemail_error").show();
$("input#regemail").focus();
return false;
}
		var regworkphone = $("input#regworkphone").val();
		if (regworkphone == "") {
      $("label#regworkphone_error").show();
      $("input#regworkphone").focus();
      return false;
    }
	
	var regzip = $("input#regzip").val();
		if (regzip == "") {
      $("label#regzip_error").show();
      $("input#regzip").focus();
      return false;
    }
 var file = $("input#file").val();
var regphotographer = $("input[name='regphotographer']:checked").val();
 if (!regphotographer) {
       $("label#regphotographer").show();
       $("input[name='regphotographer']").focus();
       return false;
     }

	var regtemplates = $("input[name='regtemplates']:checked").val();
	var regprice = $("input[name='regprice']:checked").val();
		var software = $("input[name='software']:checked").val();
	var regwhere = $('select.regwhere option:selected').val();
  if (regwhere == "") {
        $("label#regwhere_error").show();
        $("input[name='regwhere']").focus();
        return false;
      }

	var regcompanyname = $("input#regcompanyname").val();
	var regwebsite = $("input#regwebsite").val();
	var regmobile = $("input#regmobile").val();
	var regaddress = $("input#regaddress").val();	
	var regaddress1 = $("input#regaddress1").val();
	var regwheredata = $("input#regwheredata").val();
    var regweddings = $("input[name='regweddings']:checked").val();	
    var regportraits = $("input[name='regportraits']:checked").val();	
    var regevents = $("input[name='regevents']:checked").val();
	var regadicinfo = $("textarea#regadicinfo").val();	
	var tango = $("input#tango").val();	
	var what = $("input#what").val();	
	
	var dataString = 'regtitle='+ regtitle +'&regclientname=' + regclientname + '&regcompanyname=' + regcompanyname + '&regwebsite=' + regwebsite + '&regemail=' + regemail + '&regworkphone=' + regworkphone+ '&regmobile=' + regmobile + '&regaddress=' + regaddress + '&regaddress1=' + regaddress1 + '&regzip=' + regzip + '&regphotographer=' + regphotographer + '&regwhere=' + regwhere + '&regwheredata=' + regwheredata + '&regweddings=' + regweddings + '&regportraits=' + regportraits + '&regevents=' + regevents + '&regadicinfo=' + regadicinfo + '&regtemplates=' + regtemplates +'&regprice=' + regprice + '&software=' + software + '&tango=' + tango;
		//alert (dataString);return false;
 
				$.ajax({
      type: "POST",
      url: "/registerSubmit.php",
      data: dataString,
      success: function() {
        $('#regform').html("<div id='regmessage'></div>");
        $('#regmessage').html("<h4>Thank you for registering with Booked Images.</h4><br /><p>We will review your application and send you your login details shortly.</p><br /><p>Registration applications are dealt with regularly throughout the working day but may be delayed during the weekends when our offices are closed.</p><br /><p>If you have an immediate enquiry please don\'t hesitate to call us on: 0118 976 2796 - or email:<a href='mailto:info@bookedimages.com'>info@bookedimages.com </a></p>")
        .hide()
        .fadeIn(1500, function() {
          $('#regmessage').append("<br>");
        });


      }
     });   
	 
	 
    return false;
	});
});


