var _HttpRequests = new Object();

function ReadyStateChangeHandler(id) {
	if (_HttpRequests[id] != null) {
		//alert(_HttpRequests[id].requester.readyState);
	  if (_HttpRequests[id].requester.readyState == 4) {
		  if (_HttpRequests[id].callback != null) {
		    _HttpRequests[id].callback(_HttpRequests[id]);
		    _HttpRequests[id].callback = null;
		  }
			_HttpRequests[id] = null;
		}
	}
}

function DownloadWrapper(url, requester, method, callback, async) {
	this.url = url;
	this.id = Math.random().toString().replace(".", "");
	this.requester = requester;
	this.callback = callback;
	this.async = async;
	this.method = method;
	this.requester.onreadystatechange = new Function("ReadyStateChangeHandler('"+this.id+"')");
	this.requester.open(this.method, this.url, this.async);
	_HttpRequests[this.id] = this;
}

function IEhttp() {
	this.get = function(url, callback) {
		var dl = new DownloadWrapper(url, new ActiveXObject("Msxml2.XMLHTTP"), "GET", callback, (callback == null?false:true));
		dl.requester.send();
		return dl;
	}
	this.post = function(url, data, callback, contenttype) {
	  var dl = new DownloadWrapper(url, new ActiveXObject("Msxml2.XMLHTTP"), "POST", callback, (callback == null ? false : true));
	  if (contenttype != null) dl.requester.setRequestHeader("Content-Type", contenttype);
	  dl.requester.send(data);
	  return dl;
	}
}

function FFhttp() {
	this.get = function(url, callback) {
		var dl = new DownloadWrapper(url, new XMLHttpRequest(), "GET", callback, (callback == null?false:true));
		dl.requester.send(null);
		return dl;
	}
	this.post = function(url, data, callback, contenttype) {
	  var dl = new DownloadWrapper(url, new XMLHttpRequest(), "POST", callback, (callback == null ? false : true));
		dl.requester.setRequestHeader("Content-Type", contenttype != null ? contenttype : "text/xml");
		dl.requester.send(data);
		return dl;
	}
}

if (ivy.browser.isIE) {
	ivy.http = new IEhttp();
} else {
	ivy.http = new FFhttp();
}