
mtLibAddEvent(function(){
	oMTLib.ajaxifyForms();
}, "load");


oMTLib.ajaxifyForms = function(oXML){
	var aForms = document.getElementsByTagName("form");
	for(var i=0; i<aForms.length; i++){
		if(aForms[i].className.match(/\bajax\b/gi)){
			aForms[i].__ajaxSubmit = function(){
				var a = this.getElementsByTagName("*");
				a[a.length - 1].value = "processing form....";
				var sURL = this.action.split("#")[0];
				sURL += (sURL.indexOf('?') != -1)? "&" : "?";
				var aQuery = [];
				for(var i=0; i<this.elements.length;i++){
					var e = this.elements[i];
					if(e.name != ""){ 
						switch(e.nodeName){
							case 'INPUT':
								if((e.type.match(/(submit|image|cancel|reset)/)) || (e.type.match(/(checkbox|radio)/) && !e.checked)){continue;};
								aQuery[aQuery.length] = (e.name)+'='+escape(e.value);
								break;
							case 'TEXTAREA':
								aQuery[aQuery.length] = (e.name)+'='+escape(e.value);
								break;
							case 'SELECT':
								aQuery[aQuery.length] = (e.name)+'='+escape(e.options[e.selectedIndex].value);
								break;
						};
					};
				};
				Ajax.request({uri:sURL, callback:oMTLib.__ajaxifyFormsSubmitted, scope:oMTLib, post:aQuery.join("&")});
			};
			aForms[i].oOriginalSubmit = aForms[i].onsubmit;
			if(typeof aForms[i].onsubmit != 'function'){
				aForms[i].onsubmit = aForms[i].__ajaxSubmit();
			}else{
				aForms[i].onsubmit = function(){
					if(this.oOriginalSubmit() != false){
						oMTLib._oForm = this;
						this.__ajaxSubmit();
					};
					return false;
				};
			};
		};
	};
};
oMTLib.__ajaxifyFormsSubmitted = function(oXML){
	oXML = null;
	var o = new oMTLib.animation(oMTLib._oForm);
	oMTLib._oForm.style.width = oMTLib._oForm.offsetWidth + "px";
	oMTLib._oForm.style.height = oMTLib._oForm.offsetHeight + "px";
	oMTLib._oForm.style.overflow = "hidden";
	o.animation("height", "px", parseInt(o.obj.style.height), 30, 500);
	o.onanimationendheight = function(){
		this.obj.innerHTML = "Thank you. Form submitted successfully";
		this.highlightFade(800, "color", oMTLib.utils.colourBlend("fff", "666", 12));
	};
	oMTLib._oForm = null;
}; 