$(function() {
  $('.error').hide();
  $('input.textinput').css({backgroundColor:"#ffffff"});
  $('input.textinput').focus(function(){
    $(this).css({backgroundColor:"#e5e5e5"});
  });
  $('input.textinput').blur(function(){
    $(this).css({backgroundColor:"#ffffff"});
  });

  $(".formbutton2").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var cbname = $("input#cbname").val();
		if (cbname == "") {
      $("label#name_error").show();
      $("input#cbname").focus();
      return false;
    }
	
	var cbtel = $("input#cbtel").val();
		if (cbtel == "") {
      $("label#tel_error").show();
      $("input#cbtel").focus();
      return false;
    }
	var cbtime = $("select#cbtime").val();
		if (cbtime == "") {
      $("label#time_error").show();
      $("select#cbtime").focus();
      return false;
    }
		var cbemail = $("input#cbemail").val();
		if (cbemail == "") {
      $("label#email_error").show();
      $("input#cbemail").focus();
      return false;
    }
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(cbemail))) {
      $("label#email_error2").show();
      $("input#cbemail").focus();
      return false;
    }
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		if (cbemail.match(illegalChars)) {
      $("label#email_error2").show();
      $("input#cbemail").focus();
      return false;
    }
		
		var dataString = 'cbname='+ cbname + '&cbtel=' + cbtel + '&cbtime=' + cbtime + '&cbemail=' + cbemail;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "/inc/callback_send.php",
      data: dataString,
      success: function() {
        $('#callback_form').html("<div id='message'></div>");
        $('#message').html("<h2>Thank you for your interest</h2>")
        .append("<p>We will call you back at the time selected.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message');
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#cbname").select().focus();
});
