//
//$Log: agit_async.js,v $
//Revision 1.2  2008/07/12 16:23:09  agitpap
//sync for oldtimer
//
//Revision 1.1  2008/05/24 21:14:16  agitpap
//initial check-in
//
//
function getHttpRequest()
{                
	var httpRequest = false;
	try 
	{
		httpRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft) 
	{
		try 
		{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) 
		{
			try 
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) 
			{
				httpRequest = false;
			}
		}
	}

	
  	return httpRequest;
  
}




function _asyncObj()
{
	
	var waitState = -1;
	var _httpRequest;
	var isOpen = false;

	this.init = function()
	{
		try {
			_httpRequest  = getHttpRequest();
		}
		catch (err)
		{
			_httpRequest = false;
			alert("ERROR: Unable to get HttpRequest");
		}
	}
	
	this.getState = function ()
	{
		if (_httpRequest)
		{
			return true;
		}
		return false;
	}
	
	this.openForGet = function(url, async)
	{
		
		
		
		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to open request for GET");
			return;
		}
		
		try {
			_httpRequest.open("GET", url, async);
			isOpen = true;
		}
		catch (err)
		{
			alert("ERROR: Unable to open httpRequest for GET");
		}
		
	}
	
	this.openForPost = function(url, async)
	{
		
		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to open request for POST");
			return;
		}
		
		try {
			_httpRequest.open("POST", url, async);
			_httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
			isOpen = true;
			writeLog(url +" opened for POST");
		}
		catch (err)
		{
			alert("ERROR: Unable to open httpRequest for POST");
		}
	}
	
	
	this.setOnReadyStateChange = function(foo)
	{

		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to setOnReadyStaeChange function");
			return;
		}
		
		try {
			_httpRequest.onreadystatechange = foo;
		}
		catch (err)
		{
			alert("ERROR: Unable to setOnReadyStaeChange function");
		}	
	}
	
	this.send = function(parms)
	{
		
		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to send");
			return;
		}
		
		try {
                        parms = '' + parms;
			_httpRequest.send(parms);
		}
		catch (err)
		{
			alert("ERROR: Unable to send");
		}	
	}
	
	this.getReadyState = function()
	{
		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to getReadyState");
			return;
		}
		
		try {
			return _httpRequest.readyState;
		}
		catch (err)
		{
			alert("ERROR: Unable to get ready state");
		}	
	}
	
	this.getStatus = function()
	{
		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to get status");
			return;
		}
		
		try {
			return _httpRequest.status;
		}
		catch (err)
		{
			alert("ERROR: Unable to get httpRequest status");
		}	
	}
	
	this.getResponseText = function()
	{
		
		if (!this.getState())
		{
			alert("_asyncObj NOT initialised - unable to get Response Text");
			return;
		}
		
		try {
			return _httpRequest.responseText;
		}
		catch (err)
		{
			alert("ERROR: Unable to get Response Text");
		}
	}
	
}
