//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('<div class="wdpajax-loading">送信しています...</div>');
			//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('<div>')[1].split('</div>')[0];
						infodiv.html('<div class="wdpajax-error" >'+text+'</div>');
					}
					else if(xhr.status==403){
						infodiv.html('<div class="wdpajax-error" >Stop!! You are posting comments too quickly.</div>');
					}
					else{
						if(textStatus=='timeout')
							infodiv.html('<div class="wdpajax-error" >タイムアウトエラーとなりました。再度お試しください。</div>');
						else
							infodiv.html('<div class="wdpajax-error" >エラーが発生しました。</div>');
					}
				},
				success: function(data, textStatus){
					if(data=="success")
						infodiv.html('<div class="wdpajax-success" >コメントありがとうございます！すぐに反映されますので、お待ち下さい。</div>');
					else
						infodiv.html('<div class="wdpajax-error" >フォームエラーが発生しました。</div>');
					commentform.find('textarea[name=comment]').val('');
				}
			});
			/*$.post(formurl, formdata, function(data, textStatus){
				if(textStatus=='success'){
					if(data=='success')
						infodiv.html('<div class="wdpajax-success" >Thanks for your comment. We appreciate your response.</div>');
					else{
						infodiv.html('<div class="wdpajax-error" >Comment marked as spam.</div>');
					}
					commentform.find('textarea[name=comment]').val('');
				}
				else if(textStatus=='error'){
					infodiv.html('<div class="wdpajax-error" >Some error</div>');
				}
				else if(textStatus=='timeout'){
					infodiv.html('<div class="wdpajax-error" >Server Time out. Try submitting your comment again.</div>');
				}
			});**/
		}
	});
});
