$(function() {
  $('.error').hide(); //hide any error graphics

  $(".button").click(function() { // validate and process form
    $('.error').hide();  //first hide any error graphics
		
	  var name = $("input#name").val(); 
		if (name == "") {
      $("label#nameError").show();
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#emailError").show();
      $("input#email").focus();
      return false;
    }
		var subject = $("input#subject").val();
		if (subject == "") {
      $("label#subjectError").show();
      $("input#subject").focus();
      return false;
    }
		var message = $("textarea#message").val();
		if (message == "") {
      $("label#messageError").show();
      $("textarea#message").focus();
      return false;
    }
		
		var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {  //upon successful form submission, fire this function
        $('#contactForm').html("<div id='formSidebar'></div>"); //adds div id="formSidebar" to div id="contactForm"
        $('#contactForm').html("<h3>Your message has been sent!</h3>") //adds heading3 to div id="contactForm"
        .append("<p>Thank you for your message.  We will be in touch soon!</p>") //when the above is finished, display this paragraph
        .hide()
        .fadeIn(1000, function() { //fade in the checkmark after 1000 milliseconds
          $('#formSidebar').append("<img id='checkmark' src='img/check.png' />"); //the checkmark...
			if ($("#contact").is(":hidden")) { //CALLBACK! when the the above condition is met, this line executes and checks to see if the div id="contact" is hidden
      			$("#contact").slideToggle("slow"); 
   	  		} else {
      			$("#contact").slideUp(6000); //slide the form up, but this time, does it slowly (6 seconds)
      		}
		  });
      }
     });
    return false;
	});
});