function ZiXONutilities()
{
	this.loadingHTML = '<BR><BR><IMG SRC="http://www.climateday.eu/images/loading.gif"><BR><BR>';
	
	GetURL = function(url, async, handleStateChange)
	{
		try{xmlHttp=new XMLHttpRequest();}catch(e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("No AJAX support!");return false;}}}

		if (handleStateChange)
		{
			xmlHttp.onreadystatechange = function() { handleStateChange(xmlHttp); };
		}
		else
		{
			xmlHttp.onreadystatechange = function() {};
		}
	
		xmlHttp.open("GET", url, async);
		xmlHttp.send(null);
	}
	
	GetURLResponseCallbackXML = function(xmlHttp, callback, elemID)
	{
		GetURLResponseCallback(xmlHttp);
		document.getElementById(elemID).innerHTML = GetNodeValue(xmlHttp.responseXML, "content");
		
		if (callback != null)
			callback(xmlHttp);
	}
	
	GetURLResponseCallbackJSON = function(xmlHttp, callback)
	{
		GetURLResponseCallback(xmlHttp);
		callback(eval(GetNodeValue(GetRootNode(xmlHttp), "content")));
	}
	
	GetURLResponseCallback = function(xmlHttp, callback)
	{
		if (xmlHttp.responseXML == null)
		{
			alert("Error while processing your request.");
			return;
		}
		
		rootNode = GetRootNode(xmlHttp);
		returnCode = GetNodeValue(rootNode, "returncode");
		if (returnCode == 0)
		{
			redirect = GetNodeValue(rootNode, "success_redirect");
			if (redirect != null)
			{
				window.location = redirect;
			}
			else
			{
				message = GetNodeValue(rootNode, "success_message");
				if (message != null && message.length != 0)
					alert(message);
				
				if (callback != null)
					callback(xmlHttp);
			}
		}
		else
		{
			redirect = GetNodeValue(rootNode, "error_redirect");
			if (redirect != null)
			{
				window.location = redirect;
			}
			else
			{
				message = GetNodeValue(rootNode, "error_message");
				if (message == null || message.length == 0)
					alert("An error occured while performing this operation.");
				else
					alert(message);
			}
		}
	}
	
	GetRootNode = function(xmlHttp)
	{
		return xmlHttp.responseXML.getElementsByTagName("root")[0];
	}
	
	GetNodeValue = function(elem, tag)
	{
		node = elem.getElementsByTagName(tag);
		if (node != null && node.length > 0)
		{
			return node[0].firstChild.nodeValue;
		}
		else
		{
			return null;
		}
	}
	
	this.ShowLoading = function(elemID)
	{
		document.getElementById(elemID).innerHTML = this.loadingHTML;
		document.body.focus();
	}
	
	this.GetURLXML = function(url, successCallback)
	{
		GetURL(url, true, ZiXONutilitiesOnSuccess(GetURLResponseCallback, successCallback));
	}
	
	this.GetURLXMLwithID = function(url, elemID, successCallback)
	{
		GetURL(url, true, ZiXONutilitiesOnSuccess(GetURLResponseCallbackXML, successCallback, elemID));
	}
	
	this.GetURLJSON = function(url, successCallback)
	{
		GetURL(url, true, ZiXONutilitiesOnSuccess(GetURLResponseCallbackJSON, successCallback));
	}
}

ZiXONutilitiesOnSuccess = function(stateChangeCallback, successCallback, elemID)
{
	return function(xmlHttp){
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
		{
			if (elemID)
			{
				stateChangeCallback(xmlHttp, successCallback, elemID);
			}
			else
			{
				stateChangeCallback(xmlHttp, successCallback);
			}
		}
	};
}

ZiXONutilities.AddEvent = function(elem, type, func)
{
	if (elem.addEventListener)
	{
		elem.addEventListener(type, func, false);
		return true;
	}
	else if (elem.attachEvent)
	{
		return elem.attachEvent('on' + type, func);
	}
		
	return false;
}

ZiXONutilities.RemoveEvent = function(elem, type, func)
{
	if (elem.removeEventListener)
		elem.removeEventListener(type, func, false);
	else if (elem.detachEvent)
		elem.detachEvent('on' + type, func);
}

ZiXONutilities.GetPosLeft = function(elem)
{
	var pos = elem.offsetLeft;
	while (elem.parentNode)
	{
		elem = elem.parentNode;
		pos += elem.offsetLeft;
	}
	
	return pos;
}

ZiXONutilities.GetPosTop = function(elem)
{
	var pos = elem.offsetTop;
	while (elem.parentNode)
	{
		elem = elem.parentNode;
		pos += elem.offsetTop;
	}
	
	return pos;
}
