$(function() {
  $('.error').hide();

  $("#submit_res").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	 var name = $("input#name").val();
		if (name == "") {
   $("input#name").focus();
   return false;
  }
		var phone = $("input#phone").val();
		if (phone == "") {
   var focus_on = "input#phone";
   var error_phone = true;
  } else {
   var error_phone = false;
		}
		var email = $("input#email").val();
		if ( (email == "") || (email.indexOf(".") > 2) == false || (email.indexOf("@") > 0) == false ) {
   var focus_on = "input#email";
   var error_email = true;
  } else {
   var error_email = false;
		}
		
		// email OR phone
		if ( error_email == true && error_phone == true ) {
			$(focus_on).focus();
   return false;
		}
		
		var antispam = $("input#antispam").val();
		var answer = $("input#xyz").val() / 3;
		if (antispam != answer) {
   $("input#antispam").focus();
   return false;
  }
		
		// collect the other varibales
		var people = $("input#people").val();
		var date = $("input#date").val();
		var time = $("input#time").val();
		var comments = $("#comments").val();
		
		// more for each item
		//
		//
		//
		
		// only do this if not return false from above tests
		var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone + '&people=' + people + '&date=' + date + '&time=' + time + '&comments=' + comments + '&sec=21';
		
		$('#submit_res').removeAttr('disabled');
		
		$('#doing').html('<img src=\"images/loading2.gif\" />');
		$.ajax({
   type: "POST",
    url: "includes/emailprocess.php",
				data: dataString,
				success: function() {
						$('#contact_form').html("<div id='message'></div>");
						$('#message').html("<img id='checkmark' src='images/check.png' />")
						.hide()
						.fadeIn(500, function() {
								$('#message').append("<h2>Reservation Submitted!</h2><p>We will be in touch soon.</p>")
						});
				}
			});
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

