/*********************************************************************************
* Copyright (c) ProcessDATA Ltd.
* All rights reserved.
*
* @title XML HTTP Request Library
* @description Contains some utility functions for use with asyncronous requests.
* @note Note: API NOT supported and most likely will change in future releases.
* @version Version: 1.0.0 (beta)
*
* @changes
* - version 1.0.0 (beta) by Sergey Busel
**********************************************************************************/

function XmlHttp() {}

XmlHttp.create = function()
{
	if(typeof ActiveXObject != "undefined")
	{
		try
		{
			return new ActiveXObject("MSXML2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				throw new Error("Could not create new XMLHTTP ActiveXObject.");
			}
		}
	}
	else
	{
		try
		{
			return new XMLHttpRequest();
		}
		catch(e)
		{
			throw new Error("Could not create new XMLHttpRequest object.");
		}
	}
}

XmlHttp.setNoCache = function(req)
{
	req.setRequestHeader("Cache-Control", "no-cache, no-store");
}

XmlHttp.setGetHeaders = function(req)
{
	XmlHttp.setNoCache(req);
}

XmlHttp.setPostHeaders = function(req)
{
	XmlHttp.setNoCache(req);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}

XmlHttp.getXmlDoc = function(xmlhttp)
{
	try
	{
		if(typeof xmlhttp == "undefined")
		{
			return null;
		}
		if(xmlhttp.responseXML != null && typeof xmlhttp.responseXML.documentElement != "undefined")
		{
			return xmlhttp.responseXML.documentElement;
		}
		else if(typeof ActiveXObject != "undefined")
		{
			return new ActiveXObject("MSXML2.DOMDocument").loadXML(xmlhttp.responseText);
		}
		else if(typeof DOMParser != "undefined")
		{
			return new DOMParser().parseFromString(xmlhttp.responseText, "text/xml");
		}
		else
		{
			return null;
		}
	}
	catch(e)
	{
		throw new Error("Could not get XML Document: " + e.description);
	}
}

function el(anID)
{
	return document.getElementById(anID);
}

function xels(doc, tag)
{
	return doc.getElementsByTagName(tag);
}

function xel(doc, tag)
{
	var x = xels(doc, tag);
	if(x == null)
		return null;
	else if(x.length > 0)
		return x[0];
	return null;
}

function xelv(doc, tag)
{
	var x = xel(doc, tag);
	if(x == null || x.firstChild == null)
		return "";
	return x.firstChild.nodeValue;
}

function xela(doc, attr)
{
	return doc.getAttribute(attr);
}
