


/******************************************************************************************
         ajaxFreeForm.js
 ******************************************************************************************/

/*
	Place this line in where the EE FreeForm tag is to make it ajaxable.
	<script type="text/javascript" src="/path/to/ajaxFreeForm.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[method=post]").attr("id");
		if(ajaxFormID=="#") return
	}
	$(ajaxFormID).ajaxForm(
	  {  
		 beforeSubmit:function()
		 {
			if( typeof(ajaxBeforeSubmit) == "function" ){
				if( ajaxBeforeSubmit() == false ) return false;
			}

			if( $("#ajaxFormMsg").length == 0 ){
				$(ajaxFormID).find("input[type='submit']")
					.before("<div id='ajaxFormMsg' "
						+"style='color:#d00;font-weight:bold;"
						+"font-size:15px;padding-left:20px;'></div>");
			}
			$("#ajaxFormMsg").html("Sending...");
			$("#ajaxFormMsg").css({border:"1px solid #a00", backgroundColor:"#fdd"});
		 },
		  success:function(rtn) 
		  { 
			  dbg(rtn);
			  if(rtn=="success")
			  {
				   $(ajaxFormID).resetForm();
				   $("#ajaxFormMsg").html("Thank you for your comment.");
			   }else{
					rtn=$.stripHtmlRoots(rtn);
					var rtn = $(rtn).filter("#content").find("ul").html()  
					$("#ajaxFormMsg").html(     rtn    );
			   } //if
		  } //sucess function
	  }// options
	);  //ajaxForm

}); //ready


/*
 * Strips out <html> and <body> tags, so they dont conflict with the current page.
 */
$.stripHtmlRoots = function(data) {
    return data.replace(/<\/?html>/gi, '')
            .replace(/<\/?body>/gi, '')
            .replace(/\n/g, '');
};



