// Wrapper around xmlhttprequest object
function XmlHttpReq(handler, type){
	this.xmlhttpreq = null;
	this.responseType = type || XmlHttpReq.RESPONSE_TYPE_JSON;
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest){
		try{
			this.xmlhttpreq = new XMLHttpRequest();
		} catch(e) {
		}
	} else if (window.ActiveXObject) {
		// branch for IE/Windows ActiveX version
		try {
			this.xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try{
				this.xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
			}
		}
	}
	
	this.handler = handler;
}


XmlHttpReq.prototype.setHandler = function() {
	if (this.xmlhttpreq != null){
		// closure
		var self = this;
		this.xmlhttpreq.onreadystatechange = function(){self.handleResponse()};
	}
}

// handleResponse - Handles response, and calls handler function
//----------------------------------------------------------
XmlHttpReq.prototype.handleResponse = function() {
	if (this.handler != null)
	{
		// only if req shows "loaded"
		if (this.xmlhttpreq.readyState == 4)
		{	
			var responseData = null;
			try {
				// only if "OK"
				if (this.xmlhttpreq.status == 200)
				{
					if(this.xmlhttpreq.responseText != null &&
						this.xmlhttpreq.responseText != ""){
						if (this.responseType == XmlHttpReq.RESPONSE_TYPE_JSON) {
							// response is in json notation
							// http://www.json.org/
							responseData = eval('(' + this.xmlhttpreq.responseText + ');');
						} else {
							responseData = this.xmlhttpreq.responseText;
						}
					}
				}
			} catch(e) {
				// Most likely an exception related to network issues
				// ignore
			}
			
			if (responseData != null)
				this.handler(responseData);
		}
	}
}

// open - sets up attributes for request
//----------------------------------------------------------
// method - Required:String:"GET", "POST", or "HEAD".
// url - Required:String:Specifies either the absolute or a relative URL.
// asyncFlag - Optional:Boolean:Setting to true processes asynchronous.  Defaults to true.
// userName - Optional:String:User name for authetication.  Defaults to null.
// password - Optional:String:Password for authetication. Defaults to null.
XmlHttpReq.prototype.open = function(method, url, asyncFlag, userName, password){
	if (this.xmlhttpreq != null){
		if (asyncFlag == null)
			asyncFlag = true;
		this.xmlhttpreq.open(method, url, asyncFlag, userName, password);
	}
}
// send - sends request
//----------------------------------------------------------
// body - Optional:Object:Body of request
XmlHttpReq.prototype.send = function(body){
	// this must be called after every open and prior to every send,
	// otherwise the handler is not registered with IE
	this.setHandler();
	if (this.xmlhttpreq != null){
		if (body == null)
			this.xmlhttpreq.send("");
		else
			this.xmlhttpreq.send(body);
	}
}

XmlHttpReq.RESPONSE_TYPE_JSON = 0;
XmlHttpReq.RESPONSE_TYPE_TEXT = 1;

