

/*
    Place this line in where the EE Comment Form tag is to make it ajaxable.
    <script type="text/javascript" src="/path/to/ajaxCommentForm.js" ></script>

    If you have more then one form on the page, you must specify by putting
    these lines as well:
    <script type="text/javascript">
        var ajaxFormID="#formIDHere"
    </script>

    This script assumes that the return will be the text: "succes"
    or if an error, will have a <div id="content"> around the error message

*/

$(function(){
if(typeof ajaxFormID == "undefined"){
	//var ajaxFormID="#"+$("form").attr("id");
	var ajaxFormID="#comment_form";
	if(ajaxFormID=="#") return
}
$(ajaxFormID).ajaxForm(
  {  
     beforeSubmit:function()
     {
		if( $("#ajaxFormMsg").length == 0 ){
			$(ajaxFormID).find("input[type='submit']")
				.before("<div id='ajaxFormMsg' "
					+"style='color:#d00;font-weight:bold;"
					+"font-size:15px;padding:10px;'></div>");
		}
        $("#ajaxFormMsg").html("Sending...");
        $("#ajaxFormMsg").css({border:"1px solid #a00", backgroundColor:"#fdd"});

		//make sure theres some content in the message box
		var $ta = $(ajaxFormID).find("textarea");
		if( $ta.length > 0 ){
			if ( $ta.attr("value") == null || $ta.attr("value").length < 4 ){
				$("#ajaxFormMsg").html("Error: Your message is too short.");		
				return false;
			}
		}//if
     },//beforeSubmit
      success:function(rtn) 
      { 
//alert(rtn);
//console.debug(rtn);	
		if(rtn.match(/<title>error/i)){
			rtn=$.stripHtmlRoots(rtn);
			$("#ajaxFormMsg").html(   
				$(rtn).filter("#content").html()
   			);
		}else if (rtn.match(/<title>.*accept/i)){
			$("#ajaxFormMsg").html("Your comment was received and awaits moderation. Thank you.");
		}else{
			$("#ajaxFormMsg").html("Re-submission Detected. To combat spam, this page must be refreshed. "	
				+"<sm>Please copy your message so you don't lose it, refresh this page, "
				+"then re-submit your comment. Thank you.");
		}
               /*
                rtn=$.stripHtmlRoots(rtn);
                var rtn = $(rtn).filter("#content").find("ul").html()  
                $("#ajaxFormMsg").html(     rtn    );
				*/
      } //sucess function
  }// options
);  //ajaxForm

}); //ready
