// Function for AJAX request to the server
function AJAXRequest(method, url, process, async)
{
	// self = this, creates a pointer to the current function
	var self = this;
	
	if (window.XMLHttpRequest)
	{
		// Not IE
		self.AJAX = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		if(typeof xmlActiveX != 'undefined')
		{
			// Instantiate the latest MS ActiveX Objects
			self.AJAX = new ActiveXObject(xmlActiveX);
		}
		else
		{
			// loops through the various versions of XMLHTTP to ensure we're using the latest
			var activeXVersions = ['Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 
					'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
			
			for(var i=0; i<activeXVersions.length; i++)
			{
				try
				{
					// Try to create the object, if it doesn't work, try again and save a reference to the proper
					// one to speed up future instantiations
					self.AJAX = new ActiveXObject(activeXVersions[i]);
					if(self.AJAX)
					{
						xmlActiveX = activeXVersions[i];
						break;
					}
				}
				catch(objException)
				{
					// Trap : try next
				}
			}
		}
	}
	
	// If no callback process is specified, then assing a default which executes the code returned by the server
    if (typeof process == 'undefined' || process == null)
    	process = executeReturn;
	
	self.process = process;
	
    // create an anonymous function to log state changes
    self.AJAX.onreadystatechange = function(){
    	self.process(self.AJAX);
    }
    
    if(!method)
    	method = 'POST';
    
    // Change method to uppercase
    method = method.toUpperCase();
    
    // Make suse async is true
    if(typeof async == 'undefined' || async == null)
    	async = true;
    
    self.AJAX.open(method, url, async);
    
    if(method == 'POST')
    {
    	self.AJAX.setRequestHeader('Connection', 'close');
    	self.AJAX.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	self.AJAX.setRequestHeader('Method', 'POST ' + url + 'HTTP/1.1');
    }
    
    // AJAX send request
    self.AJAX.send(url);
    
    return self.AJAX;
}

// Callback process executes the code returned by the server
function executeReturn(AJAX)
{
	if(AJAX.readyState == 4)
	{
		if(AJAX.status == 200)
		{
			// Complete AJAX request
			if(AJAX.responseText)
				eval(AJAX.responseText);
		}
	}
}


function perseXML(xmlText)
{
	// Code for IE
	if (window.ActiveXObject)
  	{
  		var _doc = new ActiveXObject('Microsoft.XMLDOM');
  		_doc.async = false;
  		_doc.loadXML(xmlText);
  	}
	// Code for Mozilla, Firefox, Opera, etc.
	else
  	{
  		var parser = new DOMParser();
  		var _doc = parser.parseFromString(xmlText, 'text/xml');
  	}
  	
  	// documentElement always represents the root node
  	return _doc.documentElement;
}