JsonRet=function(ajaxRequest, options){
	this.initialize(ajaxRequest, options);
}
jQuery.extend(JsonRet.prototype,{
	initialize:function(ret, options)
	{
		this.ret=ret;

		this.typeOkMsg='alert'; //alert,show_message,show_message_modal
		this.typeError='alert'; //alert,update,show_message,show_message_modal
		this.typeContent='update'; //update,popup
		this.contOnErr=false; //true,false
		this.popupBoxWidth=600;
		this.popupBoxheight=400;
		this.contentPopupTitle=this.getContentPopupTitle();
		this.errorPopupTitle=this.getErrorPopupTitle();
		this.algsn=0;//AjaxLoaderGifSecNum: Ha kapok azonositot, akkor OkMsg eseten kikapcsolhato a loaderGif

		this.onCompletefunc=function(){};
		this.onBeforeCompletefunc=function(){};
		if (options!=undefined)
		{
			if (options.typeOkMsg) this.typeOkMsg=options.typeOkMsg;
			if (options.typeError) this.typeError=options.typeError;
			if (options.typeContent) this.typeContent=options.typeContent;
			if (options.popupBoxWidth) this.popupBoxWidth=options.popupBoxWidth;
			if (options.popupBoxHeight) this.popupBoxHeight=options.popupBoxHeight;
			if (options.contentPopupTitle) this.contentPopupTitle=options.contentPopupTitle;
			if (options.errorPopupTitle) this.errorPopupTitle=options.errorPopupTitle;
			if (options.contOnErr) this.contOnErr=options.contOnErr;
			if (options.algsn) this.algsn=options.algsn;
			if (typeof (options.onComplete) == "function")
			{
				this.onCompletefunc=options.onComplete;
			}
			if (typeof (options.onBeforeComplete) == "function")
			{
				this.onBeforeCompletefunc=options.onBeforeComplete;
			}
		}
	},

	getStatus:function(){return this.ret.status;},
	getContents:function(){return typeof(this.ret.contents)=='undefined' ? [] : this.ret.contents;},
	getStartScripts:function(){if (this.ret.startScripts!=undefined) return this.ret.startScripts;else return new Array();},
	getEndScripts:function(){if (this.ret.endScripts!=undefined) return this.ret.endScripts;else return new Array();},
	getJsFileNames:function(){if (this.ret.jsFiles!=undefined) return this.ret.jsFiles;else return new Array();},
	getErrors:function(){if (this.ret.errors!=undefined) return this.ret.errors;else return new Array();},
	getContentPopupTitle:function(){if (this.ret.contentPopupTitle!=undefined) return this.ret.contentPopupTitle;else return '';},
	getErrorPopupTitle:function(){if (this.ret.errorPopupTitle!=undefined) return this.ret.errorPopupTitle;else return '';},
	getOkMsg:function(){if (this.ret.okmsg!=undefined) return this.ret.okmsg;else return '';},
	getOkScript:function(){if (this.ret.okscript!=undefined) return this.ret.okscript;else return '';},

	addJsFiles:function()
	{
		jQuery.each(this.getJsFileNames(),function(key,value)
		{
			if (typeof(value)!='function' && !isNaN(key))
			{
				if ($("script[src='"+value+"']").get().length==0)
				{
					jquery.getScript(value);
				}
			}
		});
	},

	doStartScripts:function(){jQuery.each(this.getStartScripts(),function(key,value){eval(value);});},
	doEndScripts:function()
	{
		jQuery.each(this.getEndScripts(),function(key,value)
		{
			eval(value);
		});
	},

	doContents:function()
	{
		switch(this.typeContent)
		{
			default:
			case "update":
			{
				jQuery.each(this.getContents(),function(key,value)
				{
					jQuery("#"+key).html(value);
				});
			}break;

			case "popup":
			{
				var contentStr="";
				var sep="<br><br>";
				jQuery.each(this.getContents(),function(key,value)
				{
					if (contentStr.length>0) contentStr += sep;
					contentStr += value;
				});

				if (contentStr.length>0)
				{
					popupWindow.open({box:{"title":this.contentPopupTitle},width:this.popupBoxWidth,height:this.popupBoxHeight,content:contentStr});
					//Myvippopup.open(contentStr,this.contentPopupTitle,this.popupBoxWidth,this.popupBoxHeight);
				}
			}break;
		}
	},

	doErrors:function()
	{

		switch(this.typeError)
		{
			default:
			case "alert":
			{
				var errorsStr="";
				var sep="\n\n";
				jQuery.each(this.getErrors(),function(key,value)
				{
					if (errorsStr.length>0) errorsStr += sep;
					errorsStr += value;
				});

				if (errorsStr.length>0)
				{
					if (this.algsn>0) ALG.doneAjaxRequest(this.algsn);//Ha nincs mas toltes, akkor leszedi a loading gif-et
					alert(errorsStr);
				}

			}break;

			case "update":
			{
				var tmpElement;
				jQuery.each(this.getErrors(),function(key,value)
				{
					tmpElement=$('#'+key);
					if (tmpElement.get().length>0)
					{
						tmpElement.html(value);
					}
					else
					{
						if (this.algsn>0) ALG.doneAjaxRequest(this.algsn);//Ha nincs mas toltes, akkor leszedi a loading gif-et
						alert(value);
					}
				});
			}break;

			case "show_message":
			case "show_message_modal":
			{

				var errorsStr="";
				var sep="<br><br>";
				jQuery.each(this.getErrors(),function(key,value)
				{
					if (errorsStr.length>0) errorsStr += sep;
					errorsStr += value;
				});

				if (errorsStr.length>0)
				{
					show_message.show(errorsStr,show_message.TYPE_ERR,1,0,0,0);
				}
			}break;

			case "popup":
			{
				var errorsStr="";
				var sep="<br><br>";
				jQuery.each(this.getErrors(),function(key, value)
				{
					if (errorsStr.length>0) errorsStr += sep;
					errorsStr += value;
				});

				if (errorsStr.length>0)
				{
					if (this.algsn>0) ALG.doneAjaxRequest(this.algsn);//Ha nincs mas toltes, akkor leszedi a loading gif-et
					
					popupWindow.open({box:{"title":this.errorPopupTitle},width:this.popupBoxWidth,height:this.popupBoxHeight,content:errorsStr});
					//Myvippopup.open(errorsStr,this.errorPopupTitle,this.popupBoxWidth,this.popupBoxheight);
				}
			}break;
		}
	},

	doOkMsg:function()
	{
		var okMsg=this.getOkMsg();

		if (okMsg!='')
		{
			switch(this.typeOkMsg)
			{
				default:
				case "alert":
				{
					if (this.algsn>0) ALG.doneAjaxRequest(this.algsn);//Ha nincs mas toltes, akkor leszedi a loading gif-et
					alert(okMsg);
					var okScript=this.getOkScript();
					if (okScript!='') eval(okScript);
				}break;

				case "show_message_modal":
				case "show_message":
				{
					var modal=0;
					if (this.typeOkMsg=="show_message_modal") modal=1;
					show_message.show(okMsg,show_message.TYPE_INFO,modal,0,0,0);
				}break;
			}
		}
	},

	process:function()
	{
		this.status=this.getStatus();
		this.onBeforeCompletefunc(this.ret,this.status);
		if (this.status=='ok')
		{
			this.addJsFiles();
			this.doStartScripts();
			this.doContents();
			this.doEndScripts();
			if (this.typeContent!='popup') this.doOkMsg();
		}
		else if (this.status=='error')
		{
			if (this.contOnErr==true && this.typeContent!='popup') this.doContents();
			this.doErrors();
			this.doEndScripts();
		}
		this.onCompletefunc(this.ret,this.status);
	}
});
