/*
VS AJAX Class
Built 2010 Duncan Laidlaw Virtusoft Limited
*/

/* USAGE:
	AJAXLib = new vsAJAXLib("default.asp", 10000, "POST", "TEXT");
	AJAXLib.CreateXMLHTTPObj();
	AJAXLib.asynchronous = false;
	AJAXLib.onprocesscomplete = "processResponse";
	AJAXLib.SendRequest("","AJAXLib");
	value = AJAXLib.textresponse;
	AJAXLib.CleanUp();
*/

//class definition
function vsAJAXLib(url, timeout, method, responsemode)
{
	//the XMLHttp Object
	this.xmlhttp = null;
	
	//the XML Response var
	this.xmlresponse = null;
	
	//the Text Response var
	this.textresponse = null;
	
	//The Service URL
	this.url = url;
	
	//The Service Timeout (default=5000)
	if (parseInt(timeout) != "NaN")
	{
		if (parseInt(timeout) > 0)
		{
			this.timeout = timeout;
		}
		else
		{
			this.timeout = 5000;
		}
	}
	else
	{
		this.timeout = 5000;
	}
	
	//the method used to communicate with the server (GET|POST, default = POST)
	if (method == "GET")
	{
		this.method = "GET";
	}
	else
	{
		this.method = "POST";
	}
	
	//Parameters to be sent
	this.parameters = "";
	
	//Content Type to be sent
	this.contenttype = "application/x-www-form-urlencoded";
	
	//Use asynchronous mode
	this.asynchronous = true;
	
	//Defines the function to call once the reponse has been processed, note: is not passed parameters
	this.onprocesscomplete = "";
	
	//Defines the function to call if a timeout error is raised, note: is passed parameters
	this.ontimeout = "";
	
	//Defines the function to call if an error is raised, note: is not passed parameters
	this.onerror = "";
	
	//Response mode (TEXT|XML, default = XML)
	if (responsemode == "TEXT")
	{
		this.responsemode = "TEXT";
	}
	else
	{
		this.responsemode = "XML";
	}
	
	//error tracking variables
	this.errorcode = "";
	this.xmlhttpstatus = null;
	this.xmlhttpstatustext = "";
	this.xmlhttpreadystate = null;
}

//checks for browser comptibility and references the object
function vsAJAX_CreateXMLHTTPObj()
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		this.xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function vsAJAX_SendRequest(parameters, objectname)
{
	//set parameters if passed through
	if (parameters != null && parameters != "")
	{
		this.parameters = parameters;
	}
	
	//enable asynchronous mode
	if (this.asynchronous == true)
	{
		if (this.responsemode == "TEXT")
		{
			this.xmlhttp.onreadystatechange=function() { 
				var libObj = eval(objectname);
				libObj.ReturnTextResponse(objectname);
			}
		}
		else
		{
			this.xmlhttp.onreadystatechange=function() { 
				var libObj = eval(objectname);
				libObj.ReturnXMLResponse(objectname);
			}
		}
	}
	
	//open request
	if (this.method == "GET") //Get Method
	{
		this.xmlhttp.open(this.method, this.url + "?" + this.parameters, this.asynchronous);
	}
	else  //Post Method
	{
		this.xmlhttp.open(this.method, this.url, this.asynchronous);
	}
	
	//set timout features
	this.xmlhttp.ontimout=function() { 
				var libObj = eval(objectname);
				libObj.DoOnTimeout(objectname);
			}
	this.xmlhttp.timeout = this.timeout;
	
	//send request
	if (this.method == "GET") //Get Method
	{
		this.xmlhttp.send();
	}
	else  //Post Method
	{
		this.xmlhttp.send(this.parameters);
	}
	
	
	//if not in asynchronous mode
	if (this.asynchronous == false)
	{
		if (this.responsemode == "TEXT")
		{
			this.ReturnTextResponse(objectname);
		}
		else
		{
			this.ReturnXMLResponse(objectname);
		}
	}
}

//returns the response as xml
function vsAJAX_ReturnXMLResponse(objectname)
{
	var libObj = eval(objectname);
	
	//in asynchronous mode check the response
	if (libObj.asynchronous == true)
	{
		libObj.xmlhttpreadyState = libObj.xmlhttp.readyState;
		if (libObj.xmlhttp.readyState == 4)
		{
			libObj.xmlhttpstatus = libObj.xmlhttp.status;
			libObj.xmlhttpstatustext = libObj.xmlhttp.statusText;
		}
		
		//if the response is good return the response
		if (libObj.xmlhttp.readyState==4 && libObj.xmlhttp.status==200)
		{
			libObj.xmlresponse = libObj.xmlhttp.responseXML.documentElement;
			libObj.DoOnProcessComplete();
		}
		else
		{
			if (libObj.xmlhttp.readyState==4 && libObj.xmlhttp.status!=200)
			{
				libObj.DoOnError();
			}
			libObj.xmlresponse = null;
		}
	}
	else
	{
		libObj.xmlresponse = libObj.xmlhttp.responseXML.documentElement;
		libObj.DoOnProcessComplete();
	}
	libObj.textresponse = null;
}

//returns the response as text
function vsAJAX_ReturnTextResponse(objectname)
{
	var libObj = eval(objectname);
	
	//in asynchronous mode check the response
	if (libObj.asynchronous == true)
	{
		libObj.xmlhttpreadyState = libObj.xmlhttp.readyState;
		if (libObj.xmlhttp.readyState == 4)
		{
			libObj.xmlhttpstatus = libObj.xmlhttp.status;
			libObj.xmlhttpstatustext = libObj.xmlhttp.statusText;
		}
		
		//if the response is good return the response
		if (libObj.xmlhttp.readyState==4 && libObj.xmlhttp.status==200)
		{
			libObj.textresponse = libObj.xmlhttp.responseText;
			libObj.DoOnProcessComplete();
		}
		else
		{
			if (libObj.xmlhttp.readyState==4 && libObj.xmlhttp.status!=200)
			{
				libObj.DoOnError();
			}
			libObj.textresponse = null;
		}
	}
	else
	{
		libObj.textresponse = libObj.xmlhttp.responseText;
		libObj.DoOnProcessComplete();
	}
	libObj.xmlresponse = null;
}

function vsAJAX_DoOnProcessComplete()
{
	//if the onprocesscomplete callback function has been set call it
	if (this.onprocesscomplete != null && this.onprocesscomplete != "")
	{
		eval(this.onprocesscomplete + "()");
	}
}

function vsAJAX_DoOnTimeout(objectname)
{
	//if the ontimeout callback function has been set call it
	if (this.ontimeout != null && this.ontimeout != "")
	{
		eval(this.ontimeout + "(objectname)");
	}
}

function vsAJAX_DoOnError()
{
	//if the onerror callback function has been set call it
	if (this.onerror != null && this.onerror != "")
	{
		eval(this.onerror + "()");
	}
}

//removes the object reference
function vsAJAX_CleanUp()
{
	this.xmlhttp = null;
}

//Prototyped Class Methods
vsAJAXLib.prototype.CreateXMLHTTPObj=vsAJAX_CreateXMLHTTPObj;
vsAJAXLib.prototype.SendRequest=vsAJAX_SendRequest;
vsAJAXLib.prototype.ReturnXMLResponse=vsAJAX_ReturnXMLResponse;
vsAJAXLib.prototype.ReturnTextResponse=vsAJAX_ReturnTextResponse;
vsAJAXLib.prototype.DoOnProcessComplete=vsAJAX_DoOnProcessComplete;
vsAJAXLib.prototype.DoOnTimeout=vsAJAX_DoOnTimeout;
vsAJAXLib.prototype.DoOnError=vsAJAX_DoOnError;
vsAJAXLib.prototype.CleanUp=vsAJAX_CleanUp;
