$.fn.setFocus = function(e){
	if (document.getElementById(e)) document.getElementById(e).focus();
	return this;
};

$.fn.ajaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		var params = {
			onErrAlert : false
		};
    $(this)
    .find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
    .filter(":enabled")
    .each(function() {
      params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
    });
    $("body").addClass("curWait");
    
		$.post(this.getAttribute("action") + "?type=xml", params, function(xml){
			$("body").removeClass("curWait");
			strError = "Unable to submit form. Please try again later.";
			oFocus = null;
			$("data", xml).each(function() {
				strRedirect = this.getAttribute("redirecturl");
				strError = this.getAttribute("error");
				strMessage = this.getAttribute("message");
				strEval = this.getAttribute("eval");
				oFocus = this.getAttribute("focus");
			});
			
			if(strEval != null)
			  eval(strEval);
			
			if (strError != null && strError.length > 0) {
				if (params.onErrAlert){
					alert("The following errors were encountered:\n" + strError);
				} else {
					$("div.formErrors").html("<h3>Error<\/h3><ul>" + strError.replace(/(\t)(.+)/g, "<li>$2<\/li>") + "<\/ul>").filter(":hidden").fadeIn("normal");
				}
				if (oFocus) $("#" + oFocus).get(0).focus();
			} else if(strMessage != null && strMessage.length > 0){
				message("Message",strMessage);
			} else if (strRedirect != null && strRedirect.length > 0) {
				window.location = strRedirect;
			} 
		});
		return false;
	});
	
	return this;
}