var requestRunning = false;
var xmlHttp = null;
var xmlHttpDebug = false;

// Inspired by great work of Webfx in xloadtree
function umbracoStartXmlRequest(scriptUrl, postData, eventFunction) {

	if (xmlHttpDebug)
		alert(scriptUrl)		

	this.requestRunning = true;
	this.xmlHttpObject = XmlHttp.create();
	if (postData != "") {
		if (document.all) {
			this.xmlHttpObject.open("POST", scriptUrl, false);
			this.xmlHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		else {
			eval(eventFunction);
		}
	} else 
		this.xmlHttpObject.open("GET", scriptUrl, true);
	
	
	this.xmlHttpObject.onreadystatechange = function () {
		if (this.xmlHttpObject.readyState == 4) {
		
			this.requestRunning = false;
			// debug
			if (xmlHttpDebug)
				alert(this.xmlHttpObject.responseText)
			eval(eventFunction);
		}
	};
	
	// call in new thread to allow ui to update
	window.setTimeout(function () {
		this.xmlHttpObject.send(postData);
	}, 10);
	
	xmlHttp = this.xmlHttpObject;
	return this;
}

umbracoStartXmlRequest.prototype.ResultText = 
umbracoStartXmlRequest.prototype.ResultText = function () {
	return this.xmlHttpObject.responseText;
}

umbracoStartXmlRequest.prototype.ResultXml = 
umbracoStartXmlRequest.prototype.ResultXml = function () {
	return this.xmlHttpObject.responseXML;
}

function umbracoXmlRequestResult() {
	if (!requestRunning)
		return xmlHttp.responseXML
}

function umbracoXmlRequestResultTxt() {
	if (!requestRunning)
		return xmlHttp.responseText
}

function xmlReturnRandom() {
	day = new Date()
	z = day.getTime()
	y = (z - (parseInt(z/1000,10) * 1000))/10
	return y
}