function AjaxFramework(Parent)
{
	this.sajax_debug_mode = false;
	this.sajax_request_type = "POST";
	
	this.__REQUEST_URI = document.location.href;
	this.extraUrl = "";
	//encodeURIComponent(document.location.href)
	
	
	this.sajax_debug = function(text)
	{
		if (this.sajax_debug_mode) 
			alert(text)
	}
	
	this.sajax_init_object = function()
	{
		this.sajax_debug("sajax_init_object() called..")
		
		var A;
		try 
		{
			A = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				A = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (oc) 
			{
				A = null;
			}
		}
		if (!A && typeof XMLHttpRequest != "undefined") 
			A = new XMLHttpRequest();
		if (!A) 
			this.sajax_debug("Could not create connection object.");
		return A;
	}
	
	this.sajax_do_call = function(func_name, args, parameterCallBack, parameterPostData)
	{
		var i, x, n;
		var uri;
		var post_data, strlen, time;
		
		uri = this.__REQUEST_URI+this.extraUrl;
		this.extraUrl = "";
		
		if (parameterPostData) 
		{
			post_data = parameterPostData;
		}
		else
		{
			if (this.sajax_request_type == "GET") 
			{
				if (uri.indexOf("?") == -1) 
					uri = uri + "?rs=" + escape(func_name);
				else 
					uri = uri + "&rs=" + escape(func_name);
				for (i = 0; i < args.length - 1; i++) 
					uri = uri + "&rsargs[]=" + escape(args[i]);
				uri = uri + "&rsrnd=" + new Date().getTime();
				post_data = null;
			}
			else 
			{
				post_data = "rs=" + escape(func_name);
				for (i = 0; i < args.length - 1; i++) 
				{
					post_data = post_data + "&rsargs[]=" + escape(args[i]);
				}
			}
		}
		uri = uri.replace(/#.*/g, "");
		x = this.sajax_init_object();
		x.open(this.sajax_request_type, uri, true);
		if (this.sajax_request_type == "POST") 
		{
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		x.onreadystatechange = function()
		{
			if (x.readyState != 4) 
				return;
			Parent.Frame.sajax_debug("received " + x.responseText);
			
			var status;
			var data;
			var txt = x.responseText.replace(/^\s*|\s*$/g, "");
			txt = txt.replace("%u0161", "š");
			txt = txt.replace("%u0160", "Š");
			txt = txt.replace("%u017D", "Ž");
			txt = txt.replace("%u017E", "ž");
			txt = txt.replace("%u2018", "‘");
			txt = txt.replace("%u2019", "’");
			txt = txt.replace("%u201A", "‚");
			status = txt.charAt(0);
			data = txt.substring(2);
			if (status == "-") 
				alert("Error: " + data);
			else 
			{
				try 
				{
					
					if (!parameterCallBack) 
					{
						var callback;
						var extra_data = false;
						if (typeof args[args.length - 1] == "object") 
						{
							callback = args[args.length - 1].callback;
							extra_data = args[args.length - 1].extra_data;
						}
						else 
						{
							callback = args[args.length - 1];
						}
						callback(eval(data), extra_data);
					}
					else 
					{
						if (typeof parameterCallBack == "object" || typeof parameterCallBack == "function") 
						{
							var extraArguments = Parent.getTmpArguments()
							if (extraArguments)
							{
								
								if (extraArguments.length == 1)
								{
									parameterCallBack(eval(data),extraArguments[0]);
								}
								else if (extraArguments.length == 2)
								{
									parameterCallBack(eval(data),extraArguments[0],extraArguments[1]);
								}
								else if (extraArguments.length == 3)
								{
									parameterCallBack(eval(data),extraArguments[0],extraArguments[1],extraArguments[2]);
								}
								else if (extraArguments.length == 4)
								{
									parameterCallBack(eval(data),extraArguments[0],extraArguments[1],extraArguments[2],extraArguments[3]);
								}
								else if (extraArguments.length == 5)
								{
									parameterCallBack(eval(data),extraArguments[0],extraArguments[1],extraArguments[2],extraArguments[3],extraArguments[4]);
								}
								Parent.Frame.hideStuff();
							}
							else
							{
								parameterCallBack(eval(data));
								Parent.Frame.hideStuff();
							}
							
							if (Parent.doNiceFormPop != false && Msg.lastNr > 0) 
							{
								//this.doNiceFormPop = false
								Msg.doNiceForm();
								;
							}
						}
						else if (typeof parameterCallBack == "string") 
						{
							if (document.getElementById(parameterCallBack)) 
							{
								document.getElementById(parameterCallBack).innerHTML = eval(data);
								Parent.Frame.hideStuff();
							}
							else 
							{
								alert('Ajax/PHP elementi "' + parameterCallBack + '" ei leita');
							}
							
						}
					}
				} 
				catch (e) 
				{
					Parent.Frame.sajax_debug("Caught error " + e + ": Could not eval " + data);
				}
			}
		}
		x.send(post_data);
		Parent.Frame.sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
		Parent.Frame.sajax_debug(func_name + " waiting..");
		delete x;
	}
	
	this.hideStuff = function()
	{
		if (Parent.hideLoading == true) 
		{
			Msg.hideLoad();
			Parent.hideLoading = false;
			Parent.hideLoading_hideMainMsgDiv = false;
			Parent.hideLoading_hideTransparetnDiv = false;
		}
		if (Parent.hidePop != false) 
		{
			Msg.hide();
			Parent.hidePop = false;
		}
	}
}

function AjaxClass()
{
	this.hidePop = false
	this.doNiceFormPop = false;
	this.doNiceFormID = false
	this.lastCalledFunction = "";
	
	this.callBackResultTemp = "";
	
	this.Frame = new AjaxFramework(this);
	
	this.getPHPFunction = function(phpFunction, substring)
	{
		var len = substring.length
		var subs = phpFunction.substr(0, len);
		if (subs.toLowerCase() != substring) 
		{
			phpFunction = substring + phpFunction;
		}
		return phpFunction;
	}


	function pop(res)
	{
		Msg.pop(res);
	}
	function popTpl(res)
	{
		Msg.hideLoad();
		Msg.pop(res);
	}
	function popWin(res)
	{
		Msg.win(res);
	}
	function blankResult()
	{
		
	}
	
	function setTemp (res)
	{
		this.callBackResultTemp = res;
	}
	
	
	
	this.getTemp = function()
	{
		return this.callBackResultTemp;
	}


	this.tpl = function(tplName)
	{
		Msg.load();
		Ajax.hideLoad();
		this.SajaxPrepareCall("getAjaxTpl", pop, cutDownArray(0, this.tpl.arguments));
		this.lastCalledFunction = "tpl"		
	}
	
	this.pop = function(phpFunction)
	{
		this.SajaxPrepareCall(phpFunction, pop, cutDownArray(1, this.pop.arguments));
		this.lastCalledFunction = "pop"
	}
	
	this.popTpl = function(resource,title,width)
	{
		Msg.set(title,width);
		Msg.load();
		this.SajaxPrepareCall("getAjaxTplData", popTpl, this.popTpl.arguments);
		this.lastCalledFunction = "pop"
	}
	this.win = function(phpFunction)
	{
		this.SajaxPrepareCall(phpFunction, popWin, cutDownArray(1, this.win.arguments));
		this.lastCalledFunction = "pop"
	}
	
	this.set = function(phpFunction, div)
	{
		this.SajaxPrepareCall(phpFunction, div, cutDownArray(2, this.set.arguments));
		this.lastCalledFunction = "set"
	}
	
	this.put = function(phpFunction, div)
	{
		this.SajaxPrepareCall(this.getPHPFunction(phpFunction, "get"), div, cutDownArray(2, this.put.arguments));
		this.lastCalledFunction = "put"
	}
	
	this.save = function(phpFunction, callBack)
	{
		this.SajaxPrepareCall(this.getPHPFunction(phpFunction, "save"), callBack, cutDownArray(2, this.save.arguments));
		this.lastCalledFunction = "save"
	}
	
	this.del = function(phpFunction, callBack)
	{
		this.SajaxPrepareCall(this.getPHPFunction(phpFunction, "delete"), callBack, cutDownArray(2, this.del.arguments));
		this.lastCalledFunction = "del"
	}
	
	this.call = function(phpFunction, callBack)
	{
		this.SajaxPrepareCall(phpFunction, callBack, cutDownArray(2, this.call.arguments));
		this.lastCalledFunction = "call"
	}
	this.blank = function(phpFunction)
	{
		this.SajaxPrepareCall(phpFunction, blankResult, cutDownArray(1, this.blank.arguments));
		this.lastCalledFunction = "call"
	}
	
	this.functiongetTemResult
	this.tmp = function(phpFunction)
	{
		this.SajaxPrepareCall(phpFunction, setTemp, cutDownArray(1, this.tmp.arguments));
		this.lastCalledFunction = "call"
	}
	
	this.get = function(phpFunction, callBack)
	{
		var arguments = cutDownArray(2, this.get.arguments);
		this.functionGetTmpArguments = arguments;
		this.SajaxPrepareCall(phpFunction, callBack, arguments);
		this.lastCalledFunction = "call"
	}
	
	this.functionGetTmpArguments = new Array();
	this.getTmpArguments = function()
	{
		if (this.functionGetTmpArguments.length > 0)
		{
			return this.functionGetTmpArguments;			
		}
		else
		{
			return false;
		}
	}



	
	this.hideLoading = false;
	this.hideLoading_hideMainMsgDiv = false;
	this.hideLoading_hideTransparetnDiv = false;
	this.hideLoad = function(hideMainMsgDiv, hideTransparetnDiv)
	{
		this.hideLoading = true;
		if (hideMainMsgDiv) 
		{
			this.hideLoading_hideTransparetnDiv = true;
		}
		if (hideTransparetnDiv) 
		{
			this.hideLoading_hideTransparetnDiv = hideTransparetnDiv;
		}
	}
	
	this.getContentByUrl = false;
	this.postUrl = "";
	this.postParams = "";
	this.setPostData = function(paramString)
	{
		var string = paramString.toString();
		if (string.length > 1 && string.indexOf("&") > 0 && string.indexOf("=") > 0)
		{
			this.postParams = "&"+string;
		}
	}
	this.go = function(callBack,fullUrl)
	{
		this.getContentByUrl = true;
		this.postUrl = fullUrl;
		this.SajaxPrepareCall(false, callBack);
		this.lastCalledFunction = "go"
	}
	
	
	this.extraUrl = "";
	this.addToUrl = function(url)
	{
		this.extraUrl = '&'+url;
	}
	
	
	
	this.SajaxPrepareCall = function (phpFunction, callBack, args)
	{
		var Call = new this.SajaxCall();
		if (phpFunction)
		{
			Call.phpFunctionName = phpFunction;
		}
		if (callBack)
		{
			Call.callBackFunction = callBack;
		}
		if (args)
		{
			Call.args = args;
		}
		Call.callBackFunction = callBack;
		Call.getContentByUrl = this.getContentByUrl;
		Call.postUrl = this.postUrl;
		Call.postParams = this.postParams;
		Call.extraUrl = this.extraUrl;
		
		//Null some values
		this.postParams = "";
		this.getContentByUrl =false
		this.postUrl =""
		this.extraUrl = "";
		Call.doCall();
	}
	
	this.SajaxCall = function()
	{
		this.phpFunctionName = false;
		this.callBackFunction = false;
		this.args = false;
		this.getContentByUrl = "";
		this.postUrl = "";
		this.postParams = "";
		this.extraUrl = "";
		this.doCall = function()
		{
			var startNumber = 0;
			if (this.phpFunctionName == false && this.args != false) 
			{
				this.phpFunctionName = this.args[0];
				startNumber = 1;
			}
			var args = new Array();
			postData = "";
			if (this.phpFunctionName  != false)
			{
				postData += "rs="+escape(this.phpFunctionName)
			}
			else
			{
				if (this.getContentByUrl == true)
				{
					postData += "rs=___GET_AJAX_CONTENT_BY_URL___";
					if (this.postParams)
					{
						postData += this.postParams;
					}
				}
				if (this.postUrl)
				{
					Ajax.Frame.__REQUEST_URI = this.postUrl;
				}
			}
			
			if (this.extraUrl)
			{
				Ajax.Frame.extraUrl = this.extraUrl;
			}
			
			
			if (this.args)
			if (this.args.length > 0) 
			{
				nr = 0;
				for (i = 0; i < this.args.length; i++) 
				{
					postData = postData + "&rsargs[]=" + escape(this.args[i]);
					args[nr] = this.args[i];
					nr++;
				}
			}
			
			Ajax.Frame.sajax_do_call(this.phpFunctionName, args, this.callBackFunction, postData);
			
		}
	}
	
}
var Ajax = new AjaxClass();










