//
// Copyright © Anygraaf Oy. All rights reserved.
//
AgLocaleStrInstall({
	ready:		{en: 'Ready',		fi: 'Valmis'},
	sending:	{en: 'Sending',		fi: 'Lähetetään kyselyä'},
	loading:	{en: 'Loading',		fi: 'Ladataan'},
	reading:	{en: 'Reading',		fi: 'Luetaan'}
});

function AgAjax (url)
{
	this.url				= url;
	this.xmlHttp			= false;
	this.queryQueue			= new Array();
	this.readyFunction		= false;
	this.rFuncAttributes	= false;
	this.statusElem			= false;
	this.Timeout			= false;
	
	// Public functions
	this.Query = Query;
	this.XmlQuery = XmlQuery;
		
	this.AjaxQuery = AjaxQuery;
	function AjaxQuery ()
	{
		if(this.queryQueue.length > 0)
		{
			if(this.xmlHttp && this.xmlHttp.readyState != 0 && this.xmlHttp.readyState != 4)
			{
				this.Timeout = setTimeout(AgAjaxQuery, 100, this);
			}
			else
			{
				this.statusElem = document.getElementById("ajax_status");
				if(this.statusElem)
					this.statusElem.innerHTML = AgStr('sending');
					
				var query = this.queryQueue.shift();
				this.readyFunction = query[2];
				this.rFuncAttributes = query[3];
				
				this.xmlHttp = AgXmlHttp();
				try {
					this.xmlHttp.open("POST",this.url,query[1]);
					this.xmlHttp.setRequestHeader("Content-Type", query[4]); 
					var self = this;
					this.xmlHttp.onreadystatechange = function () { self.StateChange(); };
					this.xmlHttp.send(query[0]);
				}
				catch (e)
				{
					this.queryQueue.push(query);
					this.Timeout = setTimeout(AgAjaxQuery, 100, this);
					return;
				}
				
				if(!query[1] && this.xmlHttp.readyState==4)
					this.ExecReady(); // Mozilla refuses to use onreadystatechange with synchronous requests.
					
				if(this.queryQueue.length > 0)
					this.AjaxQuery();
			}
		}
	}
	function Query (keys,values,async,func,funcAttr)
	{
		var queryStr="";
		for(var i=0; i < keys.length; i++)
		{
			if(i>0) queryStr += "&";
			queryStr += keys[i] + "=" + values[i];
		}
		this.queryQueue.push(Array(queryStr,async,func,funcAttr,"application/x-www-form-urlencoded"));
		this.AjaxQuery();
	}
	function XmlQuery (xml,async,func,funcAttr)
	{
		this.queryQueue.push(Array(xml,async,func,funcAttr,"text/xml"));
		this.AjaxQuery();
	}
	this.StateChange = function ()
	{
		if(this.statusElem)
			switch (this.xmlHttp.readyState)
			{
				case 0: this.statusElem.innerHTML = ""; break;
				case 1: this.statusElem.innerHTML = AgStr('sending'); break;
				case 2: this.statusElem.innerHTML = AgStr('loading'); break;
				case 3: this.statusElem.innerHTML = AgStr('reading'); break;
			}
	
		if(this.xmlHttp.readyState==4)
			this.ExecReady();
	}
	this.ExecReady = function () {
		if(this.readyFunction)
		{
			var tmp_func = this.readyFunction;
			var tmp_attr = this.rFuncAttributes;
			this.readyFunction = false;
			this.rFuncAttributes = false;
			tmp_func(this.xmlHttp, tmp_attr);
		}
		if(this.queryQueue.length == 0 && this.statusElem)
			this.statusElem.innerHTML = AgStr('ready');
	}
	
	this.ClearQueue = function () { this.queryQueue.splice(0,this.queryQueue.length); clearTimeout(this.Timeout); }
}

function AgAjaxQuery(obj) { obj.AjaxQuery(); }
function AgIsSet ( test ) { return (typeof(test)!='undefined'&&test!=null); }

function AgQuickAjax (url,keys,values,async,func)
{
	var queryStr="";
	for(var i=0; i < keys.length; i++)
	{
		if(i>0) queryStr += "&";
		queryStr += keys[i] + "=" + values[i];
	}
	var xmlHttp = AgXmlHttp();
	if(async)
		xmlHttp.onreadystatechange = function () { func(xmlHttp); };
	xmlHttp.open(keys.length==0?"GET":"POST",url,async);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xmlHttp.send(queryStr);
	if(!async)
		func(xmlHttp);
	return xmlHttp;
}

function AgXmlHttp ()
{
	var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				MessageBox("Your browser does not support AJAX!");
			}
		}
	}
	return xmlHttp;
}

function AgUrlExists(url)
{
	var xmlHttp = GetXmlHttp();
	xmlHttp.open("GET", url, false);
	// This could (& should) be done with HEAD-request, but
	// for some reason Opera hangs when trying to GET the icon
	// after HEAD-request..
	xmlHttp.send(null);
	if (xmlHttp.status==200)
	{
		xmlHttp.abort();
		return true;
	}
	else
	{
		xmlHttp.abort();
		return false;
	}
}
