
function _getXmlHttp()
{
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
     	// set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
    
  if (!http_request) {
     
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  return http_request;
}

function CachedResponse(response) {
	this.readyState = ReadyState.Complete
	this.status = HttpStatus.OK
	this.responseText = response
}

ReadyState = {
	Uninitialized: 0,
	Loading: 1,
	Loaded:2,
	Interactive:3,
	Complete: 4
	}
	
HttpStatus = {
	OK: 200,
	NotFound: 404
	}

function Request_from_cache(url, f_change) {
	var result = this._cache[url];
	
	if (result != null) {
		var response = new CachedResponse(result)
		f_change(response)
		return true
	}
	else
		return false
}

function Request_cached_get(url, f_change) {
	if (!this.FromCache(url, f_change)){
		var request = this
		this.Get(url,
			/* Cache results if request completed */
			function(x){
				if ((x.readyState==ReadyState.Complete)&&(x.status==HttpStatus.OK))
				{request._cache[url]=x.responseText}
				f_change(x)
			},
			"GET")
	}
}

function Request_get(url, f_change, method) {
	if (!this._get) return;
	
	
	if (method == null) method="GET"
	if (this._get.readyState != ReadyState.Uninitialized)
		this._get.abort() 
	
	this._get.open(method, url, true);
	
	if (f_change != null) {
		var _get = this._get;
		this._get.onreadystatechange = function(){f_change(_get);}
	}
	this._get.send(null);
}
function Request_post(url, f_change,parameters) {
	if (!this._get) return;
	
	if (this._get.readyState != ReadyState.Uninitialized)
		this._get.abort() 

	
	if (f_change != null) 
	{
		var _get = this._get;
		this._get.onreadystatechange = function(){f_change(_get);}
	}
	this._get.open('POST', url, true);
    this._get.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this._get.setRequestHeader("Content-length", parameters.length);
    this._get.setRequestHeader("Connection", "close");
    this._get.send(parameters);
}

function Request_get_no_cache(url, f_change, method){
	var sep = (-1 < url.indexOf("?")) ? "&" : "?"	
	var newurl = url + sep + "__=" + encodeURIComponent((new Date()).toString());
	return this.Get(newurl, f_change, method);
}

function Request() {
	this.Get = Request_get
	this.GetNoCache = Request_get_no_cache
	this.Post = Request_post
	this.CachedGet = Request_cached_get
	this.FromCache = Request_from_cache
	
	this.Use = function(){return this._get!=null}
	this.Cancel = function(){if (this._get) this._get.abort();}
	this._cache = new Object();
	
	this._get = _getXmlHttp();
	if (this._get == null) return;
}