/*
	JumperObject, HTTPRequestObject 
	=========================	
*/



/*
JumperObject állapotai

ENABLED
DISABLED
WARN
AUTO
*/

function handleKeyPress(e,page,form,func)
{
	var key=e.keyCode || e.which;
	//Enter
	if (key==13)
	{
 		javascript:jumper.send(page, form, func);
	}
}

//JumperObject factory
function JumperObjectFactory(_instance,_autosave)
{
	eval(_instance + " = new JumperObject(\"" + _instance + "\","+_autosave+");");
}


//konstruktor
function JumperObject(_instance,_autosave)
{
	this.instance=_instance;
	this.state="ENABLED";
	this.warnMsg="";
	this.prevHash="";
	this.autosave=_autosave;
	this.registeredFunctions = new Array();
}


//Jumper első fázisa
JumperObject.prototype.send = function(_redirect, _currentform, _func) 
{
	if(this.state=="DISABLED")
	{
		return;
	}
	else if(this.state=="WARN")
	{
		alert(this.warnMsg);
		return;
	}
	else if(this.state=="AUTO")
	{
				
		if((_currentform != undefined && _currentform != null && _func != undefined && _func != null)||(_redirect != undefined && _redirect != null))
			this.delayedSend(700,_redirect, _currentform, _func);
		return;
	}
	else if(this.state=="ENABLED")
	{

		//oldalon szereplő formokból adatgyűjtés
		var formVals=getFormValues();
		
		var request="page_url="+escape("page=saveforms")+formVals;
		//amikor manuálisan lett beállítva a következő állapot
		if(this.nextState!=null && this.nextState!=undefined)
		{
			this.setState(this.nextState,this.warnMsg);
			show_loader();
			this.redirect=_redirect;
			this.currentform=_currentform;
			this.func=_func;
			if(this.autosave && request!="page_url=page%3Dsaveforms")
			{
				
				this.ajax=new HTTPRequestObject(this.instance+".ajax");
				this.ajax.sendAsyncPost(request,this.instance+".redirector",4,this.instance);				
			}
			else
			{
				var tempParent= new Object();
				tempParent.currentform=this.currentform;
				tempParent.func=this.func;
				tempParent.redirect=this.redirect;
	
				this.redirector(tempParent);
				return;
			}
		}
		//amikor form mentés volt, ez nyilván lapujratöltéssel fejeződik be
		if(_currentform != undefined && _currentform != null && _func != undefined && _func != null)
		{
			this.setState("DISABLED");
			show_loader();
			this.redirect=_redirect;
			this.currentform=_currentform;
			this.func=_func;

			if(this.autosave && request!="page_url=page%3Dsaveforms")
			{
				this.ajax=new HTTPRequestObject(this.instance+".ajax");
				this.ajax.sendAsyncPost(request,this.instance+".redirector",4,this.instance);		
				return;
			}
			else
			{
				var tempParent= new Object();
				tempParent.currentform=this.currentform;
				tempParent.func=this.func;
				tempParent.redirect=this.redirect;	
				this.redirector(tempParent);
				return;
			}
		}
		//lapváltáskor - navigáláskor
		else if(_redirect != undefined && _redirect != null)
		{
			this.setState("DISABLED");
			show_loader();
			this.redirect=_redirect;
			this.currentform=_currentform;
			this.func=_func;

			if(this.autosave && request!="page_url=page%3Dsaveforms")
			{

				this.ajax=new HTTPRequestObject(this.instance+".ajax");
				this.ajax.sendAsyncPost(request,this.instance+".redirector",4,this.instance);
				return;
			}
			else
			{
				var tempParent= new Object();
				tempParent.currentform=this.currentform;
				tempParent.func=this.func;
				tempParent.redirect=this.redirect;
	
				this.redirector(tempParent);
				return;
			}
		}
		//automentéskor
		else if(this.autosave && request!="page_url=page%3Dsaveforms")
		{
			this.setState("AUTO");
			this.redirect=_redirect;
			this.currentform=_currentform;
			this.func=_func;
			this.ajax=new HTTPRequestObject(this.instance+".ajax");
			this.tempAttrib;

			this.ajax.sendAsyncPost(request,this.instance+".setState",4,"\"ENABLED\"");
			return;
		}
		else
			return;	
	}
}


//késleltetett küldés
JumperObject.prototype.delayedSend = function(_delay,_redirect, _currentform, _func)
{
	if(_currentform != undefined && _currentform != null && _func != undefined && _func != null)
	{
		window.setTimeout(this.instance+".send(\""+_redirect+"\",\""+ _currentform+"\",\""+_func+"\");", _delay);
	}
	else
	{
		window.setTimeout(this.instance+".send(\""+_redirect+"\");", _delay);
	}
}


//megerősített küldés
JumperObject.prototype.confirmedSend = function(_question ,_redirect, _currentform, _func)
{
	var ans = confirm(_question);

	if(!ans)
		return;

	if(_currentform != undefined && _currentform != null && _func != undefined && _func != null)
		this.send(_redirect, _currentform, _func);
	else
		this.send(_redirect);
}


JumperObject.prototype.dataRequest = function(_function, _params, _processor)
{
	if(_params != undefined &&_params != null)
		post_request = "page_url=" + escape(addslashes("page=ajax_data_source&function=" + _function + "&" + _params));
	else
		post_request = "page_url=" + escape(addslashes("page=ajax_data_source&function=" + _function));

	this.ajax=new HTTPRequestObject(this.instance+".ajax");
	this.ajax.sendAsyncPost("page_url="+escape("page=saveforms"),_processor,4,this);
}


JumperObject.prototype.download = function(_redirect)
{
	var newForm= document.createElement("form");
	newForm.setAttribute("method","post");

	var pageUrl=document.createElement("input");
	pageUrl.setAttribute("type","hidden");
	pageUrl.setAttribute("name","page_url");
	pageUrl.setAttribute("value",_redirect);

	newForm.appendChild(pageUrl);
	document.body.appendChild(newForm);

	newForm.submit();
}


//Jumper második fázisa, amikor már megérkezett az adat
JumperObject.prototype.redirector= function(_parent)
{
	if(_parent.currentform != undefined && _parent.currentform != null && _parent.func != undefined && _parent.func != null && _parent.redirect != undefined && _parent.redirect != null && _parent.redirect != "")
	{		
		_currentform=eval("document."+_parent.currentform);		
		var pageUrl = document.createElement("input");
		pageUrl.setAttribute("type", "hidden");
		pageUrl.setAttribute("name", "page_url");
		pageUrl.setAttribute("value", _parent.redirect);
		_currentform.appendChild(pageUrl);

		var formName = document.createElement("input");
		formName.setAttribute("type", "hidden");
		formName.setAttribute("name", "form_name");
		formName.setAttribute("value", _parent.currentform);
		_currentform.appendChild(formName);

		var submitFunc = document.createElement("input");
		submitFunc.setAttribute("type", "hidden");
		submitFunc.setAttribute("name", "form_function");
		submitFunc.setAttribute("value", _parent.func);
		_currentform.appendChild(submitFunc);
		_currentform.setAttribute("method","post");
		var constraint=1;
		for(var cntr in this.registeredFunctions)
		{
			
			if(cntr!="contains" && cntr != "getLast" && cntr!="indexOf" && (this.registeredFunctions[cntr]["form"] =="0ALL" || this.registeredFunctions[cntr]["form"] ==_currentform.name))
				constraint=constraint*this.registeredFunctions[cntr]["function"]();
		}

		if(constraint==1)
		{
			_currentform.submit();
			this.setState("ENABLED");
			hide_loader();	
		}
		else
		{	
			this.setState("ENABLED");
			hide_loader();
		}
	}
	//ha csak átugrott valahova máshova, akkor átirányítjuk arra azz oldalra
	else if(_parent.redirect != undefined && _parent.redirect != null && _parent.redirect != "")
	{	
		
		var newForm= document.createElement("form");
		newForm.setAttribute("method","post");
		//newForm.setAttribute("name","jumper_redirector");

		var pageUrl=document.createElement("input");
		pageUrl.setAttribute("type","hidden");
		pageUrl.setAttribute("name","page_url");
		pageUrl.setAttribute("value",_parent.redirect);


		newForm.appendChild(pageUrl);
		document.body.appendChild(newForm);

		newForm.submit();
		this.setState("ENABLED");

	}
	
}


JumperObject.prototype.setState= function(_state,_msg)
{
	
	state=_state.toLowerCase();
	switch(state)
	{
		case "enabled":
		{
			this.state="ENABLED";
			//document.onclick=null;
			//document.ondblclick=null;
			break;
		}
		case "disabled":
		{
			this.state="DISABLED";
			//egér letiltása minden vonatkozásban azaz: click és dblclick
			//document.onclick=this.noClick;
			//document.ondblclick=this.noClick;
			break;
		}
		case "warn":
		{
			this.state="WARN";
			if(_msg!=null && _msg!=undefined)
			{
				this.warnMsg=_msg;
			}
			else
			{
				this.warnMsg="Nem klikkelhet másodszor!";
			}
			break;
		}
		case "auto":
		{
			this.state="AUTO";
			break;		
		}
		default:
		{
			
			this.state="DISABLED";
			break;
		}
	}
	
}


JumperObject.prototype.setNextState= function(_state,_msg)
{
	state=_state.toLowerCase();
	switch(state)
	{
		case "enabled":
		{
			this.nextState="ENABLED";
			break;
		}
		case "disabled":
		{
			this.nextState="DISABLED";
			break;
		}
		case "warn":
		{
			this.nextState="WARN";
			if(_msg!=null && _msg!=undefined)
			{
				this.warnMsg=_msg;
			}
			else
			{
				this.warnMsg="Nem klikkelhet másodszor!";
			}
			break;
		}
		case "auto":
		{
			this.nextState="AUTO";
			break;		
		}
		default:
		{
			this.nextState="DISABLED";
			break;
		}
	}

}

JumperObject.prototype.getState= function()
{
	return this.state;
}

JumperObject.prototype.noClick= function(e)
{
	return false;
}


JumperObject.prototype.startAutoSave= function(_period, _timeout)
{
	if(this.autosave==true)
		setTimeout(this.instance+".autoSave("+_period+")", _timeout);
	
}


JumperObject.prototype.autoSave = function(_period)
{
	setInterval(this.instance+".send()", _period);
}


JumperObject.prototype.registerOnSubmit = function( _functionReference, _formName)
{


	if(_functionReference.constructor.toString().indexOf("function")==-1)
	{
		return null;
	}
	else
	{
		this.registeredFunctions[_functionReference.name]= new Array();

		if(_formName!=null && _formName!=undefined)
		{
			this.registeredFunctions[_functionReference.name]["form"]=_formName;
			
		}
		else
			//a számot azért tettem az amugy konstansnak tűnő string elé mert ennek a helyén form nevek 
			//állnak többnyire, hogy ne legyen ütközés így egy szabálytalanul számmal kezdődő nevet választottam
			this.registeredFunctions[_functionReference.name]["form"]="0ALL"; 
		this.registeredFunctions[_functionReference.name]["function"]=_functionReference;
		return this.registeredFunctions[_functionReference.name]["function"];
	}
	
}

JumperObject.prototype.unregisterOnSubmit = function( _functionReference)
{
	if(_functionReference.constructor.toString().indexOf("function")==-1)
		return null;
	else
	{
		delete this.registeredFunctions[_functionReference.name];
	}

}



