jQuery(function($){
	var commentform=$('form[action$=wp-comments-post.php]');
	commentform.prepend('<div id="wdpajax-info" ></div>');
	var infodiv=$('#wdpajax-info');
	commentform.validate({
		submitHandler: function(form){
			//serialize and store form data in a variable
			var formdata=commentform.serialize();
			//Add a status message
			infodiv.html('<p>Processing...</p>');
			//Extract action URL from commentform
			var formurl=commentform.attr('action');
			//Post Form with data
			$.ajax({
				type: 'post',
				url: formurl,
				data: formdata,
				error: function(XMLHttpRequest, textStatus, errorThrown){
					infodiv.html('<p class="wdpajax-error" >You might have left one of the fields blank.</p>');
				},
				success: function(data, textStatus){
					if(data!="error") {
						var respondDiv = $("#respond");
						if(respondDiv.parent().is(".comment")) {
							var commentDiv = respondDiv.parent();
							var children = commentDiv.children("ul.children");
							if(children.size() > 0) {
								children.append(data);
							} else {
								respondDiv.before("<ul class=\"children\">" + data + "</ul>");	
							}
						} else {
							var commentList = $("ol.commentlist");
							if(commentList.size() > 0) {
								commentList.append(data);
							} else {
								respondDiv.before("<ol class=\"commentlist\">" + data + "</ol>");
							}
						}
						infodiv.html("");
					}
					else
						infodiv.html('<p class="wdpajax-error" >Error in processing your form.</p>');
					commentform.find('textarea[name=comment]').val('');
				}
			});
		}
	});
});
