// JS functions for newsletter plugin

jQuery(document).ready(function(){ // When the dom is ready
	
	initPlugin(); // Get the ball rolling
	
}); // End dom ready 





function initPlugin() {

	// Attach handler to form submit
	jQuery("#nl_signup").submit(submitEntry);

}





function submitEntry() {

	var theemail = jQuery("#nl_email_address").val(); // Grab the submitted email
	
	jQuery("#nl_form_holder").html("submitting your email address..."); // show some feedback
	
	// submit the ajax request
	jQuery.post("/wp-admin/admin-ajax.php", {theemail:theemail, action:"nl_submit"},
	function(response)
	{
      
	  // Try to strip out the pesky 0 and -1 that get takced on to the end of the return string.
	  var lastTwoChars = response.substring((response.length-2),response.length),
	  parsedResponse = "";
	  
	  if(lastTwoChars == "-1") {
	  	parsedResponse = response.substring(0, (response.length-2));
	  }
	  
	  if(lastTwoChars.substring(1) == "0") {
	  	parsedResponse = response.substring(0, (response.length-1));
	  }
	  
	  if(parsedResponse == "") {
	  	parsedResponse = response;
	  }

	  jQuery("#nl_form_holder").html(parsedResponse); // show the response
	  
	});
	
	return false; // prevent the form from submitting

}