/*extern ActiveXObject */

if (typeof Ajax === "undefined") { var Ajax = {}; }

Ajax.READY_STATE_UNINITIALIZED = 0;      // created, but not initialized
Ajax.READY_STATE_LOADING       = 1;      // object created, but send method has not been called.
Ajax.READY_STATE_LOADED        = 2;      // send method has been called, but the status and headers are not yet available.
Ajax.READY_STATE_INTERACTIVE   = 3;      // Some data has been received. Calling the responseBody and responseText properties at this state to obtain partial results will return an error, because status and response headers are not fully available.
Ajax.READY_STATE_COMPLETED     = 4;      // All the data has been received, and the complete data is available in the responseBody and responseText properties.

//
// See: http://www.w3.org/TR/XMLHttpRequest/#xmlhttprequest
//

Ajax.makeRequest = function( method, url, callback, param ) {
	method = method.toUpperCase();
	this.request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
	this.request.onreadystatechange = callback;
	this.request.open( method, url, true );
	if (method === "POST")
	{
		this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.request.setRequestHeader("Content-length", param.length);
		this.request.send(param);
	}
	else
	{
		this.request.send( null );
	}
};

Ajax.makeGetRequest = function( url, callback ) {
	this.makeRequest("GET", url, callback, null);
};

Ajax.makePostRequest = function( url, callback, param ) {
	this.makeRequest("POST", url, callback, param);
};

Ajax.isReady = function() {
	if ( this.request.readyState === this.READY_STATE_COMPLETED ) {
		return true;
	}
	return false;
};

Ajax.getRequestStatus = function() {
	// this should only be called when isReady returns true
	if (this.request)
	{
		return this.request.status;
	}
	return -1;
};

Ajax.getRequestStatusText = function() {
	// this should only be called when isReady returns true
	if (this.request)
	{
		return this.request.statusText;
	}
	return null;
};

Ajax.getResponse = function() {
	// this should only be called when isReady returns true
	if (this.request)
	{
		return this.request.responseText;
	}
	return null;
};

Ajax.getResponseHeaders = function() {
	if (this.request && this.request.getAllResponseHeaders) {
		return this.request.getAllResponseHeaders();
	}
	return null;
};


