
function HttpReq()
{
	this.action = null;
	this.XML = null;
	this.httpInterface = null;

	// Initialise XMLHttpRequest object
	this.resetInterface();

	return true;
}

/* Check if the HttpReq object is available */
HttpReq.prototype.isAvailable = function()
{
	if (this.httpInterface == null)
	{
		return false;
	}
	
	return true;
}

/* Execute the action which has been associated with the completion of this object */
HttpReq.prototype.executeAction = function()
{
	// If XMLHR object has finished retrieving the data
	if (this.httpInterface.readyState == 4)
	{
		// If the data was retrieved successfully
		try
		{
			if (this.httpInterface.status == 200)
			{
				this.action();
			}
			// IE returns status = 0 on some occasions, so ignore
			else if (this.httpInterface.status != 0)
			{
				//alert("There was an error while retrieving the URL: " + this.httpInterface.statusText);
			}
		}
		catch (error)
		{
		}
	}

	return true;
}

/* Return responseText */
HttpReq.prototype.getText = function()
{
	return this.httpInterface.responseText;
}

/* Return responseXML */
HttpReq.prototype.getXML = function()
{
	return this.httpInterface.responseXML;
}

/* Initialise object and load URL */
HttpReq.prototype.loadURL = function(url, cgi)
{
	this.resetInterface();
	//alert(url + "?" + cgi);
	this.httpInterface.open("GET", url + "?" + cgi);
	this.httpInterface.send(null);

	return true;
}

/* Turn off existing connections and create a new XMLHR object */
HttpReq.prototype.resetInterface = function()
{
	var self = this;

	if (this.httpInterface != null && this.httpInterface.readyState != 0 && this.httpInterface.readyState != 4)
	{
		this.httpInterface.abort();
	}

    this.httpInterface = getHttpObj();
    
	this.httpInterface.onreadystatechange = function()
		{
			self.executeAction();

			return true;
		};

	return true;
}

/* Assign the function which will be executed once the XMLHR object finishes retrieving data */
HttpReq.prototype.setAction = function(actionFunction)
{
	this.action = actionFunction;

	return true;
}

function getHttpObj()
{
    var httpObj = null;
    try
	{
	    httpObj = new ActiveXObject("Microsoft.XMLHTTP");
		
	}
	catch (error)
	{
		try
		{
		    httpObj = new XMLHttpRequest();	
		}
		catch (error)
		{
		    httpObj = null;
		}
	}
	
	return httpObj;
}

function loadSyncHttp(url, cgi)
{
	var httpReq = getHttpObj();
	var response = null;
	
	if (null != httpReq)
	{
	    httpReq.open("GET", url + "?" + cgi, false); 
	    httpReq.send(null);
	
	    if (httpReq.status == 200)
		    response =  httpReq.responseText;
	}
	
	return response;
}

function loadSyncHttpXML(url, cgi)
{
	var httpReq = getHttpObj();
	var response = null;
	
	if (null != httpReq)
	{
	    httpReq.open("GET", url + "?" + cgi, false); 
	    httpReq.send(null);
	}
	 if (httpReq.status == 200)
		    response =  httpReq.responseXML;
	
	return response;
}