function getValuesFromDOMObject(dom, isRoot) {
	if (!isRoot && dom.childNodes.item(0).nodeType == 3) {
		return dom.childNodes.item(0).nodeValue;
	}
	var result = {};
	for (var i = 0; i < dom.childNodes.length; i++) {
		if (dom.childNodes.item(i).nodeType == 1) {
			result[dom.childNodes.item(i).nodeName] = getValuesFromDOMObject(dom.childNodes.item(i), false);
		}
	}
	return result;
}

function AJAX() {

	this.urlEncode = function(str) {
		if (str != null) {
			str = encodeURI(str);
			str = str.replace(/\+/g, "%2B");
			return str.replace(/&/g, "%26");
		} else {
			return '';
		}
	}

	this.createRequest = function() {
		if (navigator.appName.indexOf('Microsoft') >= 0) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			return new XMLHttpRequest();
		}
	}
	//this.myAbort = function() {
		//window.stop();
	//}
	this.sendRequest = function(command, onResponse) {
		var http = this.createRequest();
		
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				try {
					if (http.status == 200) {
						if (http.responseXML && (rootNode = http.responseXML.documentElement)) {
							if (rootNode.nodeName == 'response') {
								onResponse(getValuesFromDOMObject(rootNode, true));
							} else if (rootNode.nodeName == 'error') {
								var error = getValuesFromDOMObject(rootNode, true);
								window.alert("There were errors while retrieving request from " + command + " command:\n"
									+ error.message + "\n"
									+ error.description);
								return;
							} else {
								window.alert("Unsupported response format:\n" + http.responseText);
							}
						} else {
							
							onResponse(http.responseText);
							
						}
					} else {
						window.alert("There was a problem retrieving the XML data:\n" + http.statusText);
					}
				} catch (ex) {
					//window.alert(ex.message); //uncomment if neccessary (in debug case)
				}
			}
		}
		var content = '';
		for (var i = 2; i < arguments.length; i++) {
			for (var name in arguments[i]) {
				var value = arguments[i][name];
				content += this.urlEncode(name) + '=' + this.urlEncode(value) + '&';
			}
		}
		http.open('POST', 'ajax.' + command);
		try {
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		} catch (ex) {}
		http.send(content);
	}	
}

var ajax = new AJAX();