// Ajax Comment Posting
// WordPress plugin
// version 1.3
// author: regua
// http://regua.biz

$(document).ready(function($){

// If you want to use Google Analytics with ACP, fill in your
// GA tracker code in between the quotation marks below.
// The code should look like UA-XXXXXX-XX, with Xs being numbers.
// Make sure the line ends with a semi-colon.
  var google_analytics = "";
// To disable the use of Google Analytics with ACP, either remove the line
// above or change its value to "".

// -------------------------------------
// DO NOT EDIT BELOW THIS LINE IF YOU DON'T KNOW WHAT YOU'RE DOING
// -------------------------------------


// Google Analytics code
  if(google_analytics && google_analytics != "") {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");  
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function(){  
      var pageTracker = _gat._getTracker(google_analytics);  
      pageTracker._initData();  
      pageTracker._trackPageview();  
    });  
  }
	   
	$('#commentform').after('<div id="error"></div>');
	$('#submit').after('<img src="/wp-content/plugins/ajax-comment-posting/loading.gif" id="loading" alt="'+loading+'" />');
	$('#loading').hide();
	var form = $('#commentform');
	var err = $('#error');
	
	// WP Ajax Edit Comments hook
	if (window.AjaxEditComments) {
   	AjaxEditComments.init();
	} // end if
	
    form.submit(function(evt) { 
    
  if(form.find('#author')[0]) {
      if(form.find('#author').val() == '') {
		   err.html('<span class="error">'+enter_name+'</span>');
		   return false;
	   } // end if
		if(form.find('#email').val() == '') {
			err.html('<span class="error">'+enter_email+'</span>');
			return false;
		} // end if
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(form.find('#email').val())) {
			err.html('<span class="error">'+enter_valid+'</span>');
			if (evt.preventDefault) {evt.preventDefault();}
      return false;
		} // end if
	} // end if
	
	if(form.find('#comment').val() == '') {
		err.html('<span class="error">'+enter_comment+'</span>');
		return false;
	} // end if
	
    $(this).ajaxSubmit({
						   
		beforeSubmit: function() {
			$('#loading').show();
			$('#submit').attr('disabled','disabled');
		}, // end beforeSubmit

		error: function(request){
				err.empty();
				if (request.responseText.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
					var data = request.responseText.match(/<p>(.*)<\/p>/);
					err.html('<span class="error">'+ data[1] +'</span>');
				} else {
					var data = request.responseText;
					err.html('<span class="error">'+ data[1] +'</span>');
				}
				$('#loading').hide();
				$('#submit').removeAttr("disabled");
				return false;
		}, // end error()

        success: function(data) {
			try {
                var response = $("<ol>"+data+"</ol>");
					 
					 if ($(document).find('.commentlist')[0]) {
							$('.commentlist').append(response.find('.commentlist li:last'));
					 } else {
							$('#respond').before(response.find('.commentlist'));
					 } // end if
					 if ($(document).find('#comments')[0]) {
					 		$('#comments').html(response.find('#comments'));
					 } else {
					 		$('.commentlist').before(response.find('#comments'));
					 } // end if
					 
					 err.empty();
					 form.remove(); // REMOVE THIS IF YOU DON'T WANT THE FORM TO DISAPPEAR
					 $('#respond h3').hide();
					 err.html('<span class="success">Thank you for leaving a comment on VPSI\'s blog, we appreciate your contribution and encourage you to check back often! If you haven\'t already done so, subscribe to our <a href=\"http://www.vpsiinc.com/subscription.htm\" target=\"_blank\">free e-newsletter</a>.</span>');
					 $('#submit').removeAttr("disabled");
           $('#loading').hide();	
                
            } catch (e) {
                $('#loading').hide();
				        $('#submit').removeAttr("disabled");
                 alert(error+'\n\n'+e);
            } // end try
            
            // WP Ajax Edit Comments hook
				if (window.AjaxEditComments) {
   				AjaxEditComments.init();
				} // end if
						   
			} // end success()
			
		}); // end ajaxSubmit()
		
        return false; 
		
	}); // end form.submit()
}); // end document.ready()
										
