/**
* ajax.js $Id 1.23
*
* This script is a wrapper for the XMLHttpRequest class available in the DOM
* implementation on several browsers. Additional support is built-in for the
* Microsoft MSXML.XMLHTTP activeXObjects.
*
* @author Thomas Devriendt
*/
// Holds the different ActiveXObjects that can be used for XML Http Requests
var aXMLHttpActiveXObjects = new Array ('MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
// When the defaultCallbackHandler function is used the
// XMLHttpRequest.responseXML is stored in here
var oResponseXML = new Object ();
// When the defaultCallbackHandler function is used the
// XMLHttpRequest.responseText is stored in here
var sResponseText = new String ();
/**
* Initiates a new XMLHttpRequest
*/
function AJAX () {
this.sRequestMethod = "POST";
this.sRequestPage = null;
this.oAjaxRequest = null;
this.aParams = new Array ();
this.createAjaxRequestObject ();
}
/**
* Creates a new XMLHttpRequest object.
* When no object could be created, it alerts the viewer of the page
*/
AJAX.prototype.createAjaxRequestObject = function () {
if (window.XMLHttpRequest) {
this.oAjaxRequest = new XMLHttpRequest ();
} else if (window.ActiveXObject) {
for (var i=0; i<aXMLHttpActiveXObjects.length; i++)
try {
this.oAjaxRequest = new ActiveXObject (aXMLHttpActiveXObjects[i]);
break;
} catch (e) {}
} else {
alert ("Apparently this browser doesn't support AJAX");
return;
}
this.oAjaxRequest.onreadystatechange = this.defaultCallbackHandler;
};
/**
* Sets the callbackHandler.
* The callbackHandler is the function that will process the data once
* the XMLHttpRequest is completed
*
* @param function The callbackHandler function
*/
AJAX.prototype.setCallbackHandler = function (callbackHandler) {
if (typeof (callbackHandler) == 'function')
this.oAjaxRequest.onreadystatechange = callbackHandler;
else
alert ("setCallbackHandler expects it's first argument to be of type function, got " + typeof (callbakcHandler));
};
/**
* Sets the request method of the XMLHttpRequest.
* Only the values "POST" and "GET" are allowed
*
* @param string Either "POST" or "GET", default is "POST"
*/
AJAX.prototype.setRequestMethod = function (requestMethod) {
if (typeof (requestMethod) == 'string') {
switch (requestMethod.toUpperCase()) {
case "POST":
this.sRequestMethod = "POST";
break;
case "GET":
this.sRequestMethod = "GET";
break;
default:
this.sRequestMethod = "POST";
}
} else
alert ("setRequestMethod expects it's first argument to be of type string, got " + typeof (requestMethod));
};
/**
* Sets the page to which the request needs to be made.
*
* @param string The request page
*/
AJAX.prototype.setRequestPage = function (requestPage) {
if (typeof (requestPage) == 'string')
this.sRequestPage = requestPage;
else
alert ("setRequestPage expects it's first argument to be of type string, got " + typeof (requestPage));
};
/**
* When additonal headers need to be send, you can use this function
*
* @param string The header identifier
* @param string The header contents
*/
AJAX.prototype.setRequestHeader = function (headerId, headerContent) {
if (typeof (headerId) == 'string' && typeof (headerContent) == 'string')
this.oAjaxRequest.setRequestHeader (headerId, headerContent);
else
alert ("setRequestHeader expects it's first and second argument to be of type string, got " + typeof (headerId) + " and " + typeof (headerContent));
};
/**
* Makes a request to the previously set request page.
* It is possible to pass on parameter that need to be sent to the request
* page as well.
* It is also possible to pass on a different requestPage than the one that
* was set earlier. This way you only need 1 AJAX object to make requests
* to different pages
*
* @param Array An associative array containing the parameters that need
* to be sent to the request page
* @param string A request page the request needs to be made to [optional]
*/
AJAX.prototype.makeRequest = function (params, requestPage) {
if (requestPage != null)
this.setRequestPage (requestPage);
if (this.sRequestMethod == 'GET') {
this.oAjaxRequest.open (this.sRequestMethod, this.sRequestPage + "?" + this.buildQueryString (params));
this.oAjaxRequest.send ();
} else if (this.sRequestMethod == 'POST') {
this.setRequestHeader ('content-type', 'application/x-www-form-urlencoded');
this.oAjaxRequest.open (this.sRequestMethod, this.sRequestPage);
this.oAjaxRequest.send (this.buildQueryString (this.aParams));
} else
alert ("Invalid requestMethod found; found " + this.sRequestMethod);
};
/**
* Sets a param that needs to be sent to the requestPage.
*
* @param string The name of the parameter
* @param object The value of the parameter
*/
AJAX.prototype.setParam = function (key, value) {
this.aParams[key] = value;
};
/**
* Creates a query string.
* A query string has the following form:
* var1=value1&var2=value2
* This is the way data will be sent via the XMLHttpRequest to the request
* page
*
* @param Array An array containing all parameters that need to be sent to
* the request page
*/
AJAX.prototype.buildQueryString = function (params) {
var queryString = new String ();
for (var i in params)
queryString += i + "=" + escape (params[i]) + "&";
return queryString.substr (0, queryString.length-1);
};
/**
* The default callback handler.
* This function will simply store the responseXML and responseText
* in 2 global variables that can be accessed anywhere in your script
*/
AJAX.prototype.defaultCallbackHandler = function () {
/*if (this.oAjaxRequest.readyState == 4 && this.oAjaxRequest.status == 200) {
sResponseText = this.oAjaxRequest.responseText;
oResponseXML = this.oAjaxRequest.responseXML;
}*/
};