function XHR(method) {
	this.method = (method) ? method : 'POST';
	this.aSync = true;
	this.url = '';
	this.paramsGet = Array();
	this.paramsPost = Array();
	this.onsuccess = null;
	this.onerror = null;
	this.pageStatus = "";
	this.pageStatusCode = 0;
	this.responseText = "";
	this.active = false;
	this.getXHR = function() {
		if (this.xhr) {
			return this.xhr;
		}
		
		if (window.XMLHttpRequest) {
			this.xhr = new XMLHttpRequest();
		} else {
			if (window.ActiveXObject) {
				this.xhr = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
		if (this.xhr) {
			var xhrObj = this;
			this.xhr.onreadystatechange = function() {
				
				if (xhrObj.xhr.readyState == 4) {
					
					xhrObj.pageStatusCode = xhrObj.xhr.status;
					xhrObj.pageStatus = xhrObj.xhr.statusText;
					if (xhrObj.xhr.status == 200) {
						xhrObj.responseText = xhrObj.xhr.responseText;
						xhrObj.doOnSuccess();
					} else {
						xhrObj.doOnError();
					}
					xhrObj.close();
					xhrObj.active = false;
				}
			}
		}
		return this.xhr;
	}
	
	this.doOnSuccess = function() {
		if (typeof(this.onsuccess) == "function") {
			this.onsuccess(this);
		}
	}
	
	this.doOnError = function() {
		if (typeof(this.onerror) == "function") {
			this.onerror(this);
		}
	}
	
	this.getParamStr = function(method) {
		var params = (method.toUpperCase() == "GET") ? this.paramsGet : this.paramsPost;
		var str = "";
		for (var c = 0; c < params.length; c++) {
			str += (str != "") ? "&" : "";
			str += params[c].name + '=' + params[c].value;
		}
		return str;
	}
	
	this.open = function(url) {
		if (this.active) {return;}
		if (!this.xhr) {
			this.getXHR()
		}
		this.url = url;
		var paramStr = this.getParamStr('GET');
		if (paramStr != "") {
			if (url.indexOf('?', 0) == -1) {
				url += '?';
			} else {
				url += '&';
			}
			url += paramStr;
		}

		this.xhr.open(this.method, url, this.aSync);
	}
	
	this.close = function() {
		this.xhr = null;
	}
	
	this.addParam = function(method, aname, avalue) {
		var obj = {name: aname, value: encodeURIComponent(avalue)}
		switch(method.toUpperCase()) {
			case 'GET':
				this.paramsGet.push(obj);
				break;
			case 'POST':
				this.paramsPost.push(obj);
				break;
		}
	}	
	this.resetParams = function() {
		this.paramsGet = new Array();
		this.paramsPost = new Array();
	}
	this.send = function() {
		if (this.active) {return;}
		this.active = true;
		this.responseText = "";
		var vars = this.getParamStr('POST');
		if (this.method.toUpperCase() == "POST") {
			this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.xhr.setRequestHeader("Content-length", vars.length);
			this.xhr.setRequestHeader("Connection", "close");		
		}
		this.xhr.send(vars);
	}
	
}