//	check and submit form using ajax
//	dependance: prototype.js 1.5.0, script.aculo.us
//	© mountain multi media (www.gomedia.ch)
//	v1.1 31.8.2007/mh

var DynamicForm=function(form){
	this.init(form);
};

DynamicForm.prototype.init=function(form){
	this.dom=form;
	this.obj=this;
	this.id=this.dom.id;
	pFormSet.append(this);

	this.dom.button_submit=this.dom.elements["submitButton"];
	this.dom.button_submit.onclick=function(){this.blur()}

	this.dom.button_cancel=this.dom.elements["cancelButton"];
	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);
}

DynamicForm.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);
		Element.addClassName(form.messagePane,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);
			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{
			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);
		Element.hide(this.pForms[obj.id].dom.button_submit);
		Element.hide(this.pForms[obj.id].dom.button_cancel);
	},
	
	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"]);
	}
}

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

Event.observe(window,"load",function(){
	$A(document.getElementsByTagName("form")).findAll(function(form){
		return(form.target.toLowerCase()=="pform");
	}).each(function(form){
		myForm=new DynamicForm(form);
	});
});