var Ajax = {
	Events:['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'],
	init:function(){
		if(window.ActiveXObject){
			try{
				new ActiveXObject('Msxml2.XMLHTTP');
				Ajax.getSender = function(){ return new ActiveXObject('Msxml2.XMLHTTP'); }
			}catch(ex){
				try{
					new ActiveXObject("Microsoft.XMLHTTP");
					Ajax.getSender = function(){ return new ActiveXObject("Microsoft.XMLHTTP"); }
				}catch(ex){}
			}
		}else if(window.XMLHttpRequest){
			Ajax.getSender = function(){	return new XMLHttpRequest();	}
		}
	},
	getSender:function(){ return null; },
	doRequest:function(method, url, parameters, headers, events){

		var sender = Ajax.getSender(),		// Get xmlhttp object
			pastBody = null;		// the body of POST Request

		// if there isn't xmlhttp object supported by the browser, we exit from the function
		if(!sender) return;
		sender.onreadystatechange = function(){
			var event = events["on"+Ajax.Events[sender.readyState]];
			if(sender.readyState===4){
				if(sender.status==200){
					if(events["onSuccess"]) events["onSuccess"](sender);
				}else{
					if(events["onFailure"]) events["onFailure"](sender);
				}
			}
			if(event) event();
		}

		// make parameters string
		if((typeof parameters) != 'undefined'){
			var p = parameters;
			parameters = [];
			for(var pa in p){
				parameters.push(pa+"="+p[pa]);
			}
			p = null;
			parameters = parameters.join("&");
		}else{
			parameters = "";
		}
		if(method.toUpperCase()!="POST"){
			// if the request method is not POST, then concat the parameters with url
			url = url.concat((url.match(/\?/)?"&":"?"), parameters);
		}else{
			// if the request method is POST, we define the content-type
			headers["Content-Type"] = "application/x-www-form-urlencoded";
			if(sender.overrideMimeType){
				/* Force "Connection: close" for Mozilla browsers to work around
				* a bug where XMLHttpReqeuest sends an incorrect Content-length
				* header. See Mozilla Bugzilla #246651.
				*/
				headers["Connection"] = "close";
			}
			pastBody = parameters;
		}

		sender.open(method, url, true);

		// Add the headers to the xmlhttp object
		for(var h in headers){
			sender.setRequestHeader(h, headers[h]);
		}

		try{
			sender.send(pastBody);
		}catch(ex){
			if(events["onException"]) events["onException"](sender, ex);
		}
	}
}

Ajax.init();