$(document).ready(function(){
  // write the comment form internal fragment into the form
  $.ajax({
    url: '/commentForm/fragment.html',
    success: function(fragment){
      $("#commentForm").html(fragment);
      $.ajax({
	// append the token as a hidden element
	url: '/commentForm/token.php',
	success: function(token){
	  $("#commentForm").append(
	    '<input type="hidden" name="ts" value="'+token+'"/>'
	  );
        }
      });
    }
  });

  jQuery.validator.messages.required = "";
    $("#commentForm").validate({
      invalidHandler: function(e, validator) {
	var errors = validator.numberOfInvalids();
	if (errors) {
	  alert("Please fill out all required fields.");
	}
      },
      onkeyup: false,
      submitHandler: function(form) {
	form.method = 'POST';
	form.action = '/commentForm/submit.php';
	form.submit();
      },
      messages: {
	email: {
	  required: "",
	  email: "",//"bad address",
	}
      },
    });
});


