/*
 * Wrapper object for making XMLHttpRequests
 */
 
// Global constants
var HTTP_METHOD_GET = "GET";
var HTTP_METHOD_POST = "POST";

 function AjaxWrapper(url) {
 	this.url = url;
	
	this.request = function(httpMethod, url, callBackMethod) {
		this.httpRequest = this.buildXMLHttpRequest();
		this.httpRequest.onreadystatechange = callBackMethod;
		this.httpRequest.open(httpMethod, url, true);
		this.httpRequest.send(url);
	}

	this.setUrl = function(u) {
		this.url = u;
	}
	
	this.isProcessed = function() {
		if ( this.httpRequest.readyState == 4 && this.httpRequest.status == 200) {
			return true;
		}
		return false;
	}
	
	this.getResponseText = function() {
		return this.httpRequest.responseText;
	}
	
	this.getResponseXml = function() {
		return this.httpRequest.responseXML;
	}
	
	this.getRequestObj = function() {
		return this.httpRequest;
	}
	
	this.buildXMLHttpRequest = function() {
		if(window.XMLHttpRequest) {
			return new XMLHttpRequest();
		}
		else if(window.ActiveXObject) {
			var msxmls = new Array(
				"Msxml2.XMLHTTP.5.0",
				"Msxml2.XMLHTTP.4.0",
				"Msxml2.XMLHTTP.3.0",
				"Msxml2.XMLHTTP",
				"Microsoft.XMLHTTP");
			for(var idx=0; idx<msxmls.length; idx++) {
				try {
					return new ActiveXObject(msxmls[idx]);
				} catch (e) {
					// Trying the next object
				}
			}
		}
		throw new Error("Failed to instantiate XMLHttpRequest");
	}
	
	this.get = function(url, callBackMethod) {
		this.request(HTTP_METHOD_GET, url, callBackMethod);
	}
	
	this.post = function(url, callBackMethod) {
		this.request(HTTP_METHOD_POST, url, callBackMethod);
	}
	
	this.sendXML = function(url, callBackMethod, contentXML) {
		this.httpRequest = this.buildXMLHttpRequest();
		this.httpRequest.onreadystatechange = callBackMethod;
		this.httpRequest.open(HTTP_METHOD_POST, url, true);
		this.httpRequest.send(contentXML);
	}
 } 

