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

  $(".submitbutton").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
	var errorcount = 0;	
	
	var firstname = $("input#firstname").val();
	if (firstname == "") {
      $("label#firstname_error").show();
      $("input#firstname").focus();
	  errorcount++
    }
	
	var lastname = $("input#lastname").val();
	if (lastname == "") {
      $("label#lastname_error").show();
      $("input#lastname").focus();
	  errorcount++
    }

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var email = $("input#email").val();
	if(reg.test(email) == false) {
	$("label#email_error").show();
    $("input#email").focus();
	errorcount++	
   }
   
	var comment = $("textarea#comment").val();
	if (comment == "") {
      $("label#comment_error").show();
      $("textarea#comment").focus();
	  errorcount++
    }
	if (errorcount >0){
      return false;}
	  

		
		
		
		
		var dataString = 'firstname='+ firstname + '&lastname='+ lastname + '&email=' + email + '&comment=' + comment;
		//alert (dataString);return false;
		$('.loading').show();
		$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
	});
});

