AJAX wrapper

JavaScript AJAX wrapper for Ajax calls
 
/* Jscript */


//// AJAX wrap
function AjaxWrapper()
{
var object = this;

object.Request = NewRequest();
object.Request.onreadystatechange = CompleteRequest;

this.Sync = true;
this.Method = "GET";
this.URL = "";
this.WebServiceMethod = "";
this.Parameters = new ParameterCollection();

this.Execute = ExecuteRequest;
this.AsyncCallbackMethod = null;

this.ResultXML = null;
this.ResultText = null;

function NewRequest()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest();
else
return new ActiveXObject("Microsoft.XMLHTTP");
}

function ExecuteRequest()
{
var parameters = object.Parameters.toString();

ResetRequest();

if (this.Method.toUpperCase() == "POST")
{
if (object.WebServiceMethod.length > 0)
object.Request.open(object.Method, object.URL + "/" + object.WebServiceMethod, !object.Sync);
else
object.Request.open(object.Method, object.URL, !object.Sync);

object.Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
object.Request.send(object.Parameters);
}
else if (this.Method.toUpperCase() == "GET")
{
if (object.WebServiceMethod.length > 0 && parameters.length > 0)
object.Request.open(object.Method, object.URL + "/" + object.WebServiceMethod + "?" + parameters, !object.Sync);
else if (object.WebServiceMethod.length > 0)
object.Request.open(object.Method, object.URL + "/" + object.WebServiceMethod, !object.Sync);
else if (parametres.length > 0)
object.Request.open(object.Method, object.URL + "?" + parameters, !object.Sync);
else
object.Request.open(object.Method, object.URL, !object.Sync);

object.Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
object.Request.send();
}
else
{
throw "The method '" + this.Method.toUpperCase() + "' is not supported !";
}

if (object.Sync)
FinishRequest(object.Request.responseText);
}

function CompleteRequest()
{
if (object.Request.readyState == 4)
{
if (object.Request.status == 200)
{
FinishRequest(object.Request.responseText);

if (object.AsyncCallbackMethod != null)
object.AsyncCallbackMethod();
}
}
}

function ResetRequest()
{
object.Request = NewRequest();
object.Request.onreadystatechange = CompleteRequest;
}

function FinishRequest(retourTexte)
{
var xmlDoc = new ActiveXObject("MSXML2.DOMDocument");

object.ResultText = object.Request.responseText;

try
{
xmlDoc.loadXML(object.Request.responseText);

if (xmlDoc.parsed && xmlDoc.xml.length > 0)
object.ResultXML = xmlDoc;
else
object.ResultXML = null;
}
catch (ex)
{
object.ResultXML = null;
}
}
}