/**
 * Creates a new XML HTTP request object browser independent.<b>
 */
function createXmlHttpRequest() {
	var xml = false;
	try {
		xml = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error) {
		try {
			xml = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error) {
			try {
				xml = new XMLHttpRequest();
			}
			catch (error) {
				xml = false;
			}
		}
	}
	return xml;
}

// create the browser-universal http request object
HttpRequest =
function () {
	var xmlhttp = null;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e_) {
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

// create the dom parser for browsers not supporting it
var req = null;
if (typeof DOMParser == "undefined") {
	DOMParser = function () {}
   	DOMParser.prototype.parseFromString = function (str, contentType) {
		if (typeof ActiveXObject != "undefined") {
			var d = new ActiveXObject("MSXML.DomDocument");
			 d.loadXML(str);
			 return d;
		  }
		  else {
			  if (typeof XMLHttpRequest != "undefined") {
				 req = new XMLHttpRequest;
				 req.open("GET", "data:" + (contentType || "application/xml") +
					";charset=utf-8," + encodeURIComponent(str), true);
				 if (req.overrideMimeType) {
					req.overrideMimeType(contentType);
				}
				  req.send(null);
				 return req.responseXML;
			  }
   		}
   	}
}


/**
 * Loads the XML data from the specified URL and, when it is fully loaded,
 * passes that document and the URL to the specified handler function.
 * @param url The url from which to fetch the xml data.
 * @param handler The handler function to call with the xml data.
 * @param optional argument object, The object the to call a method on, if this argument
 * is given the handler must be a string containing the methods name.
 */
function loadXML(url, handler) {
	var request = new HttpRequest();
	var ts = new Date().valueOf();
	var loadUrl = url + "&ts=" + ts;

   	request.open("GET", loadUrl, true);
	var len = arguments.length;
	if (len == 3) {
		var object = arguments[2];
	}

   	request.onreadystatechange =
   		function() {
   			if (request.readyState == 4 && request.responseText) {
   				var response = (new DOMParser()).parseFromString(request.responseText, "text/xml");
				if (len == 3 && object[handler]) {
					object[handler](response, loadUrl);
				}
				else {
	   				handler(response, loadUrl);
	   			}
				delete request.onreadystatechange;
	   			request = null;
			 }
		};
	request.send(null);
}


/**
 * Sends a get request and forwards the response to a named method.
 * @param url The get request to send.
 * @param method The method to forward the result to.
 * @param optional argument object, The object the to call a method on, if this argument
 * is given the method must be a string containing the methods name.
 */
function getResponse(url, method) {
	var ts = new Date().valueOf();
	var loadUrl = url + "&ts=" + ts;

	var request = new HttpRequest();
   	request.open("GET", loadUrl, true);
   	var len = arguments.length;
	if (len == 3) {
		var object = arguments[2];
	}
   	request.onreadystatechange =
   		function () {
			if (request.readyState == 4) {
				var result = request.responseText;
				if (len == 3 && object[method]) {
					object[method](result, loadUrl);
				}
				else {
					eval(method + '(result)');
				}
			}
		};
  	request.send(null);
 }

