//ver.: 08.02.29
FormValidator.SEPARATOR='|';
FormValidator.DEFAULT_FADEVALUE='40';		//áttetszőség %-aban
FormValidator.DEFAULT_AUTODESCRIPTION='1';	//ha mást nem mondunk az automatikusan input leírás aktív
FormValidator.DEBUG='0';					//az eredmény frame látható ha 1

function FormValidator(data){	

	this.name=data['name'];					//ürlap neve, !kötelező megadni!
	this.form=document.forms[this.name];
	this.form.validator=this;
	this.form.target=this.name + "FormProcessUnit";
	if (typeof(data['url'])!='undefined') this.form.action=data['url'];		//feldolgozó egység url-je, ha nincs megadva az adott form action paramétere helyetessíti
	
	if (typeof(data['successURL'])!='undefined') this.successURL=data['successURL'];	//ha meg van adva sikeres kérés esetén erre az url linkeli az oldalt
	else this.successURL=null;
	if (typeof(data['successObj'])!='undefined') this.successObj=data['successObj'];		//siker esetén eme objektumra hívja meg successFunc függvényt
	else this.successObj=null;
	if (typeof(data['successFunc'])!='undefined') this.successFunc=data['successFunc'];		//siker esetén ez a függvény hívódik meg, az esetleges megjegyzést megkapja paraméternek
	else this.successFunc=null;
	if (typeof(data['successAlert'])!='undefined') this.successAlert=data['successAlert'];		//siker esetén riaszt
	else this.successAlert=null;
	
	if (typeof(data['failureObj'])!='undefined') this.failureObj=data['failureObj'];		//siker esetén eme objektumra hívja meg failureFunc függvényt
	else this.failureObj=null;
	if (typeof(data['failureFunc'])!='undefined') this.failureFunc=data['failureFunc'];		//siker esetén ez a függvény hívódik meg, az esetleges megjegyzést megkapja paraméternek
	else this.failureFunc=null;
	if (typeof(data['failureAlert'])!='undefined') this.failureAlert=data['failureAlert'];		//siker esetén riaszt
	else this.failureAlert=null;
	
	if (typeof(data['method'])!='undefined') this.form.method=data['method'];	//a kérés módja (get/post), ha nincs megadva akkor az űrlapban megadott lesz az érvényes
	if (typeof(data['loadingImgID'])!='undefined')
		this.loadingImgID=data['loadingImgID'];					//töltést jelző kép ID-je
	else this.loadingImgID=null;
	if (typeof(data['fadeableObjIDs'])!='undefined') 
		this.fadeableObjIDs=data['fadeableObjIDs'].split("|");	//elhalványuló objektumok ID-je "|" jellel elválasztva
	else this.fadeableObjIDs=new Array();
	if (typeof(data['autoDescription'])!='undefined') 			//automatikus input leírásnak a megjelenítése (ha van) 0 vagy 1 értéke lehet
		this.autoDescription=data['fadeableObjIDs'];
	else this.autoDescription=FormValidator.DEFAULT_AUTODESCRIPTION;
	this.curShowedDescription="";

	this.errorObjects=new Array();	//hibás kitöltés esetén itt raktározodnak, azok az objok melyek hibásak
	this.errorText="";
	
	this.loading=0;
	this.reqTimeStart=null;
	this.reqTimeEnd=null;
	
	//iframe inicializáció
	
	this.preSubmitProcedure=function(){
		this.showEffect();
		var d=new Date();
		this.reqTimeStart=d.getTime();
		this.reqTimeEnd==null;
		this.loading=1;
	}
	
	this.responseHandler = function(){
		if (this.loading==0) return;
		//Régi hibák törlése:
		var obj,d=new Date();
		this.hideEffect(); 
		this.reqTimeEnd=d.getTime();
		this.loading=0;
		
		this.errorText="";
		while (this.errorObjects.length>0){				
			obj=this.errorObjects.pop()
			obj.innerHTML='';	
		}
		//Eredmény feldolgozása
		var responseText=eval(this.name + "FormProcessUnit").document.body.innerHTML;
		if (responseText.length==0) return;
		res=responseText.split(FormValidator.SEPARATOR);
		// - TRUE: ha az űrlap feldolgozása sikeres
		if (res[0]==1){	
			var comment='';
			if (res.length>1) comment=res[1];
			if (this.successAlert!=null) 
				alert(this.successAlert);
			if (this.successObj==null && this.successFunc!=null)
				this.successFunc(comment);
			else if (this.resultObj!=null && this.resultFunc!=null) 
				this.successFunc.call(this.successObj,comment);
			else;
			if (this.successURL){
				if (res.length>1 && res[1].length>0){
					if (this.successURL.indexOf('?')>-1) comment="&comment=" + res[1];
					else comment="?comment=" + res[1];
				}
				document.location.href=this.successURL + comment;
			}
		}
		// - FALSE: ha az űrlap feldolgozása sikertelen
		else if (res[0]==0){	
			var i,obj,name, comment='';
			this.errorObjects=new Array();
			for(i=1; i+2<res.length; i+=3){
				obj=document.getElementById(res[i] + "_error");
				if (obj){
					obj.innerHTML=res[i+2];
					this.errorObjects.push(obj);
				}
				else if (res[i]=='')
					alert(res[i+2]);
				//hibák rögzítése egy stringben
				if (res[i]!=''){
					if (res[i+1]!='') name=res[i+1];
					this.errorText+='A(z) ' + name + ' mező hibás:\n - ' + res[i+2] + '\n';
				}
			}
			if (this.failureAlert!=null) 
				alert(this.failureAlert);
			if (this.failureObj==null && this.failureFunc!=null)
				this.failureFunc();
			else if (this.resultObj!=null && this.resultFunc!=null) 
				this.failureFunc.call(this.failureObj);
			else;
		}
		return;
	}
	
	this.isLoading = function(){
		return this.loading;
	}
	
	this.showEffect = function(){
		var obj,i;
		if (this.loadingImgID!=null){
			obj=document.getElementById(this.loadingImgID);
			obj.style.display="block";
		}
		for (i=0;i<this.fadeableObjIDs.length;i++){
			FormValidator.fade(this.fadeableObjIDs[i],FormValidator.DEFAULT_FADEVALUE);
		}
	}
	
	this.hideEffect = function(){
		var obj,i;
		if (this.loadingImgID!=null){
			obj=document.getElementById(this.loadingImgID);
			obj.style.display="none";
		}
		for (i=0;i<this.fadeableObjIDs.length;i++){
			FormValidator.fade(this.fadeableObjIDs[i],100);
		}
	}
	
	this.getLoadingTime=function(){
		if (this.isLoading() || this.reqTimeStart==null || this.reqTimeEnd==null) return null;
		return this.reqTimeEnd-this.reqTimeStart;
	}
	
	this.alertError=function(){
		if (this.errorText=="") return;
		alert(this.errorText);
	}
	
	this.showDescription=function(name){
		var obj;
		if (this.curShowedDescription==name) return;
		if (typeof(name)=='undefined') name="";
		
		if (this.curShowedDescription!=""){
			obj=document.getElementById(this.curShowedDescription + "_desc");
			if (obj){
				//obj.style.fontSize='6px';
				//function hide_wrap(obj){return function(){obj.style.display='none';}}
				//window.setTimeout(hide_wrap(obj),160);
				obj.style.display='none';
			}
		}
		
		obj=document.getElementById(name + "_desc");
		if (obj) {
			obj.style.display='block';
			function showDesc_wrap(obj,name){return function(){ obj.style.display='none';}}
			if (window.opera && obj.addEventListener)
				obj.addEventListener("click", showDesc_wrap(obj,name), false);
			else if (obj.addEventListener)
				obj.addEventListener("click", showDesc_wrap(obj,name), true);
			else if (obj.attachEvent)
				var r = obj.attachEvent("onclick", showDesc_wrap(obj,name));
		}

		this.curShowedDescription=name;
	}
	
	this.initAutoDescription=function(){
		var i, name;
		for (i=0;i<this.form.elements.length;i++){
			//if (this.form.elements[i].type=="button" || this.form.elements[i].type=="submit") continue;
			name=this.form.elements[i].name;
			function showDesc_wrap(obj,name){return function(){ obj.showDescription(name);}}
			if (window.opera && this.form.elements[i].addEventListener)
				this.form.elements[i].addEventListener("focus", showDesc_wrap(this,name), false);
			else if (this.form.addEventListener)
				this.form.elements[i].addEventListener("focus", showDesc_wrap(this,name), true);
			else if (this.form.elements[i].attachEvent)
				var r = this.form.elements[i].attachEvent("onfocus", showDesc_wrap(this,name));
		}
	}
	
	this.init=function(){
		this.form.fv=this;
		var style='style=" display:none; height:0px"';
		if (FormValidator.DEBUG==1) style="width:500px;height:80px;";

		document.write('<iframe onload="window.document.forms[\'' + this.name + '\'].fv.responseHandler()" id="' + this.name + 'FormProcessUnit" name="' + this.name + 'FormProcessUnit"  ' + style + '></iframe>');
		//document.forms[\'' + this.name + '\'].fv.responseHandler()
		function preSubmit_wrap(obj){return function(){ obj.preSubmitProcedure();}}
	
		if (window.opera && this.form.addEventListener){
			this.form.addEventListener("submit", preSubmit_wrap(this), false);
			//document.forms[this.name].fv.responseHandler()
			//document.getElementById(this.name + 'FormProcessUnit').addEventListener("onload", preResponseHandler_wrap(this),false);
		}
		else if (this.form.addEventListener)
			this.form.addEventListener("submit", preSubmit_wrap(this), true);
		else if (this.form.attachEvent)
			var r = this.form.attachEvent("onsubmit", preSubmit_wrap(this));
		
		if (this.autoDescription) this.initAutoDescription();
	}
	
	this.init();
	
	this.deletePasswords=function(){
		var i;
		for (i=0;i<this.form.elements.length;i++){
			if (this.form.elements[i].type=='password') this.form.elements[i].value='';
		}
	}
	
}

FormValidator.fade=function(objID,value){if (Util) Util.setOpacity(objID,value);}
