AjaxRequest = new Object;
AjaxRequest.loadedRequisitions = new Array();

//Start Request Object
function Ajax_Request(serverUrl, ID)
{
    
  //Set some default variables
    this.parms = new Array();
    this.parmsIndex = 0;
 
  //Set the server url
    this.server = serverUrl;
    this.requestMethod = 'GET'; 

    this.id = ID;
  
    var handler = null;
    var httpRequest;
    var resultXML;
    var currentObject = this;
  
    this.execute = function ()
	{
		//Try to create our XMLHttpRequest Object
		try
		{
			this.httpRequest = this.createXMLHttp();
			httpRequest = this.httpRequest;			
		}
		catch (e)
		{
			alert('Error creating the connection!');
			return;
		}
		
		//Make the connection and send our data
		try
		{
			var txt = "?1";
			for(var i in this.parms)
			{
			  txt = txt + '&'+this.parms[i].name + '=' + this.parms[i].value;
			}

			if (this.requestMethod == 'GET')
			{	
			    httpRequest.open("GET", this.server + txt + "&rand="+Math.random(), true);			    
			    //document.write(this.server + txt);
			    httpRequest.setRequestHeader('content-type', 'text/xml');
			}
			else
			{
			    httpRequest.open("POST", this.server + txt, true);
			    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			
			httpRequest.onreadystatechange = this.AJAX_onReadyStateChange;
			httpRequest.send('');
		}
		catch (e)
		{
			alert('An error has occured calling the external site: ' + e);
			return false;
		}
		
	}
	
	// --- AJAX onreadystatechange handler --- //
		this.AJAX_onReadyStateChange = function ()
		{
			if (httpRequest.readyState != 4) return;
			
			if (httpRequest.status == 200)
			{
			    
				// --- create xml document object				
				try 
				{
					XMLDocument = new ActiveXObject("Msxml2.DOMDocument");
					XMLDocument.loadXML(httpRequest.responseText);	
					resultXML = XMLDocument;	
		        }
		        catch (e)
		        {
					try
					{
						resultXML = httpRequest.responseXML;	
					}
					catch (ee)
					{
						//...
					}
				}
				
			}
			else
			{
				alert('The server respond with a bad status code: ' + httpRequest.status);
		        return false;
			}
			
			// --- Load current handler--- //
			currentObject.resultXML = resultXML;
			if (typeof(handler) == 'function') handler(currentObject);
				
			// alert(resultXML.getElementsByTagName('control')[0].firstChild.data);
			return resultXML;
		}
	// --- AJAX onreadystatechange handler --- //
	
	this.setHandler = function (handlerName)
	{
		handler = handlerName;
	}
	
	this.setRequestMethod = function (methodName)
	{
	    if (methodName == 'POST') 
	    {
	        this.requestMethod = 'POST';
	    }
	    else
	    {
	        this.requestMethod = 'GET';
	    }
	}
	
	this.add = function (name, value)
	{
	  //Add a new pair object to the params
	  this.parms[this.parmsIndex] = new pair(name,value);
	  this.parmsIndex++;
	}
	
	this.createXMLHttp = function ()
	{
		// var httpRequest = null;
	    try
	    {
	        httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
	    }
	    catch (e)
	    {
	        try
	        {
				if (window.XMLHttpRequest) 
				{ // Mozilla, Safari, ...
					httpRequest = new XMLHttpRequest();
				} 
				else if (window.ActiveXObject) 
				{ // IE
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
	        }
	        catch (ee)
	        {
	            httpRequest = null;
	        }
	    }
	    
	    if (!httpRequest && typeof XMLHttpRequest != 'undefined')
	    {
	    	httpRequest = new XMLHttpRequest();
	    }
	
	    return httpRequest;
	}
  
}


//Utility pair func
function pair(name, value)
{
  this.name = name;
  this.value = value;
}