//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($){
	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 class="wdpajax-loading">Procesando tu comentario...</p>');
			//Extract action URL from commentform
			var formurl=commentform.attr('action');
			//Post Form with data
			$.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" >Detente, estas comentando muy seguido, intenta comentar en unos minutos.</p>');
					}
					else{
						if(textStatus=='timeout')
							infodiv.html('<p class="wdpajax-error" >Error en el servidor, por favor intentalo de nuevo.</p>');
						else
							infodiv.html('<p class="wdpajax-error" >Error desconocido</p>');
					}
				},
				success: function(data, textStatus){
					if(data=="success")
						infodiv.html('<p class="wdpajax-success" >Su comentario ha sido enviado y se encuentra pendiente de moderaci&oacute;n.</p>');
					else
						infodiv.html('<p class="wdpajax-error" >Error al intentar ingresar tu comentario, por favor intentalo de nuevo en unos minutos.</p>');
					commentform.find('textarea[name=comment]').val('');
				}
			});
			/*$.post(formurl, formdata, function(data, textStatus){
				if(textStatus=='success'){
					if(data=='success')
						infodiv.html('<p class="wdpajax-success" >Gracias por tu comentario.</p>');
					else{
						infodiv.html('<p class="wdpajax-error" >Tu comentario ha sido categorizado como spam.</p>');
					}
					commentform.find('textarea[name=comment]').val('');
				}
				else if(textStatus=='error'){
					infodiv.html('<p class="wdpajax-error" >Error del servidor, intentalo de nuevo.</p>');
				}
				else if(textStatus=='timeout'){
					infodiv.html('<p class="wdpajax-error" >Se supero el tiempo de espera, intenta comentar en unos minutos.</p>');
				}
			});**/
		}
	});
});