/**
 * JS
 * sp-comments.js
 * 
 * @author msavary
 * Copyright 2010-2011 © USER STUDIO. All Rights Reserved.
 * 
 * Simple Publisher comment form validation and submission
 * 
 */


$(document).ready(function() {
    $.validator.setDefaults({
		submitHandler: function() { alert("Error : comment was not submitted."); }
	});
	$("form.commentingForm").validate({
		submitHandler: function(form) {
			$(form).find("input[type=submit]").fadeOut();
			$.ajax({
				type: "POST",
				url: "./lib/sp-comments/submit.ajax.php5",
				async: true,
				data: $(form).serialize(),
				dataType: "JSON",
				success: function(msg, status) {
			    	 var serverObj = $.parseJSON(msg);
			         if (serverObj.success != "true") {
					 	var n = new Date();           // Grab new copy of date
						var s = n.getTime();          // Grab current millisecond #
					    $(".commentingCaptcha").attr('src', './captcha_'+s+'.jpg'); //load new captcha
					    $(form).find("#ccaptcha").val(""); //erase only that one
						$(form).find("input[type=submit]").fadeIn();
					 } else {
						$(form).html("<h4><em class='congrats'>Your comment was submitted. Thank you&nbsp;!</em></h4>");
					 }
				}
			});
		}
	});
});
