//	check and submit form using ajax
//	dependance: prototype.js > 1.5.0, script.aculo.us
//	© mountain multi media (www.gomedia.ch)
//	v1.4 3.7.2008/mhm

var MMM_Ajax_Form=function(form,submitTo){
	this.init(form,submitTo);
};

DynamicForm=MMM_Ajax_Form;	//	renamed 3.7.2008 mhm

MMM_Ajax_Form.prototype.init=function(form,submitTo){
	this.dom=form;
	Element.extend(this.dom);
	this.obj=this;
	this.id=this.dom.id;
	this.submitTo=submitTo?submitTo:"ajax";

	pFormSet.append(this);

	this.dom.button_submit=this.dom.elements["adr.submit"]?this.dom.elements["adr.submit"]:this.dom.elements["submitButton"];
	if(this.dom.button_submit){this.dom.button_submit.onclick=function(){this.blur()}}

	this.dom.button_cancel=this.dom.elements["adr.cancel"]?this.dom.elements["adr.cancel"]:this.dom.elements["cancelButton"];
	if(this.dom.button_cancel){this.dom.button_cancel.onclick=function(){this.blur()}}

	pFormSet.initWaitIcon(this);
	pFormSet.initMessagePane(this);

	this.dom.onsubmit=function(){return pFormSet.doSubmit(this)}

	Form.reset(this.dom);
	Form.enable(this.dom);
	Form.focusFirstElement(this.dom);
}

MMM_Ajax_Form.prototype.fieldSwitch=function(switchOn,field_switchable){
	field_switchable=this.dom.elements[field_switchable];
	field_switchable.readOnly=switchOn?false:true;
	if(field_switchable.readOnly){field_switchable.value=""}
	else{field_switchable.focus()}
}

/////////////////////////////////////////////////////////////////

pFormSet = {
	pForms:new Object(),
	
	hideMessage:function(objID){
		new Effect.Fade(this.pForms[objID].messagePane);
		this.pForms[objID].messagePane.className=this.pForms[objID].messagePane.classNameDefault;
	},

	showMessage:function(form,serverResponseText,serverResponseState){
		Element.hide(form.img_wait);
		Effect.Appear(form.messagePane);
		$(form.messagePane).addClassName(serverResponseState);
		while(form.messagePane.firstChild){form.messagePane.removeChild(form.messagePane.firstChild);}
		form.messagePane.appendChild(document.createTextNode(serverResponseText));
		if(serverResponseState=="ok"){
			setTimeout("pFormSet.hideMessage('"+form.id+"')",10000);
			setTimeout("pFormSet.restart('"+form.id+"')",11000);
		}
		return true;
	},
	
	clearMessage:function(obj){
		nObj=obj.cloneNode(false);
		obj.parentNode.insertBefore(nObj,obj);
		obj.parentNode.removeChild(obj);
	},

	restart:function(objID){
		Element.show(this.pForms[objID].dom.button_submit);
		Element.show(this.pForms[objID].dom.button_cancel);
		Form.reset(this.pForms[objID].dom);
		Form.enable(this.pForms[objID].dom);
	},

	append:function(obj){
		this.pForms[obj.id]=obj;
	},
	
	initWaitIcon:function(obj){
		this.pForms[obj.id].img_wait=document.createElement("IMG");
		this.pForms[obj.id].img_wait.src="/i/icn/wait.gif";
		this.pForms[obj.id].img_wait.style.width="16px";this.pForms[obj.id].img_wait.style.height="16px";
		this.pForms[obj.id].img_wait.style.styleFloat="left";
	},
	
	initMessagePane:function(obj){
		this.pForms[obj.id].messagePane=document.createElement("P");
		this.pForms[obj.id].messagePane.classNameDefault="messagePane";
		this.pForms[obj.id].messagePane.className=this.pForms[obj.id].messagePane.classNameDefault;
		Element.hide(this.pForms[obj.id].messagePane);
		this.pForms[obj.id].dom.button_submit.parentNode.appendChild(this.pForms[obj.id].messagePane);
	},
	
	doSubmit:function(obj){
		if(fieldsOK(obj)){
			protectandserve(this.pForms[obj.id].dom);
			if(this.pForms[obj.id].submitTo=="ajax"){
				params=Form.serialize(this.pForms[obj.id].dom);
				this.submitWait(obj);
				this.pForms[obj.id].ajaxSuccess=this.ajaxSuccess.bind(this.pForms[obj.id]);
				this.pForms[obj.id].ajaxFailure=this.ajaxFailure.bind(this.pForms[obj.id]);
				pFormSet.current=this.pForms[obj.id];
				new Ajax.Request(this.pForms[obj.id].dom.action+"json/",{
					parameters:params,
					method:"post",
					onSuccess:this.ajaxSuccess,
					onFailure:this.ajaxFailure
				});
			}else{
				obj.target=this.pForms[obj.id].submitTo;
				obj.submit();
			}
		}else{
			deAlert((obj.elements["fcMsg"]?obj.elements["fcMsg"].value:"Bitte f(ue)llen Sie diese Felder aus:")+"\n\n"+formMessage)
		}
		return false;
	},

	submitWait:function(obj){
		Form.disable(this.pForms[obj.id].dom);
		this.pForms[obj.id].dom.button_submit.parentNode.insertBefore(this.pForms[obj.id].img_wait,this.pForms[obj.id].dom.button_submit);
		Element.show(this.pForms[obj.id].img_wait);
		if(this.pForms[obj.id].dom.button_submit){$(this.pForms[obj.id].dom.button_submit).hide();}
		if(this.pForms[obj.id].dom.button_cancel){$(this.pForms[obj.id].dom.button_cancel).hide();}
	},
	
	ajaxSuccess:function(req){
		eval(unescape(req.responseText));	//	GOSJSON
		pFormSet.showMessage(pFormSet.current,GOSJSON["messagetext"],GOSJSON["state"]);
	},
	
	ajaxFailure:function(req){
		eval(unescape(req.responseText));	//	GOSJSON
		pFormSet.showMessage(pFormSet.current,GOSJSON["messagetext"],GOSJSON["state"]);
	}
}

//////////////////////////////////////////////////////////////////

oldload=window.onload;

window.onload=function(){
	if(oldload){oldload()}
	$A(document.getElementsByTagName("form")).findAll(function(form){
		return(form.target.toLowerCase()=="pform");
	}).each(function(form){
		myForm=new MMM_Ajax_Form(form);
	});
}