//WDP AJAX Comments
//Ajaxify WordPress Commenting - Web Developer Plus
//Visit Tutorial: http://webdeveloperplus.com/wordpress/new-wordpress-plugin-wdp-ajax-comments/
//Plugin: http://wordpress.org/extend/plugins/wdp-ajax-comments/
//AJAX Comments JS Code
//Version: 1.2

jQuery('document').ready(function(jQuery){
	var commentform=jQuery('form[action$=wp-comments-post.php]');
/*	commentform.prepend('<div id="wdpajax-info" ></div>');*/
	var infodiv=jQuery('#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('<div class="wdpajax-loading"></div>');
			//Extract action URL from commentform
			var formurl=commentform.attr('action');
			//Post Form with data
			jQuery.ajax({
				type: 'post',
				url: formurl,
				data: formdata,
				dataType: 'html',
				error: function(xhr, textStatus, errorThrown){
					if(xhr.status==500){
						var response=xhr.responseText;
						var text=response.split('<p>')[1].split('</p>')[0];
						infodiv.html('<p class="wdpajax-error" >'+text+'</p>');
					}
					else if(xhr.status==403){
						infodiv.html('<p class="wdpajax-error" >تملهل!! انت تنشر التعليق بسرعة</p>');
					}
					else{
						if(textStatus=='timeout')
							infodiv.html('<p class="wdpajax-error" >خطأ في السيرفر. حاول مرة أخرى</p>');
						else
							infodiv.html('<p class="wdpajax-error" >خطأ غير معروف</p>');
					}
				},
				success: function(data, textStatus){
					if(data=="success")
						infodiv.html('<p class="wdpajax-success" >شكرا على تعليك. نحن نقدر ردك</p>');
					else
						infodiv.html('<p class="wdpajax-error" >خطأ في اظافة التعليق</p>');
					commentform.find('textarea[name=comment]').val('');
				}
			});
		}
	});
});