/*  PrototypeWrap, version 1.0
 *  Collection of wrappers for Prototype JavaScript framework, version 1.5.1
 *  (c) 2007 Mac McCahey
 *
 *  Floogy web site: http://www.floogy.com/
 *  Prototype web site: http://www.prototypejs.org/
 *
/*--------------------------------------------------------------------------*/

/*
Wrapper for Ajax.Request suitable for form submission. Triggers
handler function (h) if responseText is blank or begins with 'return '
else dies while alerting any other output (std or custom errors)
*/
function AjaxSubmit (f,h) {
	f = $(f); // http://dev.rubyonrails.org/ticket/8525
	new Ajax.Request (
		f.action, 
		{
			method: f.method,
			parameters: f.serialize(true),
			evalScripts: true,
			onLoading: function(r) { f.disable(); },
			onSuccess: function(r) { 
				f.enable();
				//alert (r.responseText); // good debug alert
				if (r.responseText.strip().length>0 && r.responseText.substr(0,7)!='return ') { 
					alert (r.responseText.stripTags().strip()); // error: responseText is set and doesn't begin with 'return '
				} else {
					eval(h)(r.responseText.substr(7,r.responseText.length)); // success: post responseText to handler function
				}
			},
			onFailure: function(r) { f.enable(); alert ('Server-side failure. Please try again.'); },
			onException: function(r) { f.enable(); alert ('Client-side failure. Please try again.'); },
			on404: function(r) { f.enable(); alert ('Page not found. Report to webmaster.'); }
		}
	);
	return false;
}