var aAjaxObjects = Array();

aAjaxObjects.getFreeAjaxObject = function()
{
	for (var i=0;i<this.length;i++)
		if (this[i].bFree)
			return this[i];

	oAjax = new Ajax();
	this.push(oAjax);

	return oAjax;
}

function Ajax()
{
	this.bFree = true;
	this.oRequest = null;
	this.oCaller = null;
	this.sParseFunc = null;
}

Ajax.prototype.setCaller = function(oCaller)
{
	this.oCaller = oCaller;

	return true;
}

Ajax.prototype.setParseFunc = function(sFunc)
{
	this.sParseFunc = sFunc;

	return true;
}

Ajax.prototype.loadXMLDoc = function(sURL)
{
	this.bFree = false;

	if (window.XMLHttpRequest)
	{
		this.oRequest = new XMLHttpRequest();
		this.oRequest.onreadystatechange = delegate(this, this.processReqChange);
		this.oRequest.open('GET', sURL, true);
		this.oRequest.send(null);
	}
	else if (window.ActiveXObject)
	{
		this.oRequest = new ActiveXObject('Microsoft.XMLHTTP');
		if (this.oRequest)
		{
			this.oRequest.onreadystatechange = delegate(this, this.processReqChange);
			this.oRequest.open('GET', sURL, true);
			this.oRequest.send();
		}
	}
}

Ajax.prototype.processReqChange = function()
{
	var oRequest = this.oRequest;
	if (oRequest.readyState == 4)					// only if oRequest shows "complete"
	{
		if (oRequest.status == 200)		{			// only if "OK"	
			this.parseRequest(oRequest.responseXML);
		}
		else
			alert('There was a problem retrieving the XML data.\n\nServer return code: '+oRequest.status+'\nStatus text: '+oRequest.statusText+'\n\nIf this problem persists, please report this information.');

		this.oCaller = null;
		this.sParseFunc = null;
		this.bFree = true;
	}

	return true;
}

Ajax.prototype.parseRequest = function(oResponse)
{
//	alert(getElementsByTagName('XML'));
	var aXML = oResponse.getElementsByTagName('XML');
	

	for (var i=0;i<aXML.length;i++)
	{
		var oXML = aXML[i];

		if (oXMLRemark = oXML.getElementsByTagName('REMARK')[0])
			if (!this.parseRemark(oXMLRemark))
				return false;

		if (this.oCaller)
		{
			if (this.sParseFunc)
				eval('this.oCaller.'+this.sParseFunc+'(oXML)');
			else
				this.oCaller.parseAjaxRequest(oXML);
		}
		else
		{
			var sFunc = 'ParseNothing';
			for (var j=0;j<oXML.attributes.length;j++)
				if (oXML.attributes[j].nodeName == 'func')
					sFunc = oXML.attributes[j].value;
			eval(sFunc+'(oXML)');
		}
	}

	return true;
}

Ajax.prototype.parseRemark = function(oXMLRemark)
{
	var iErrorID = 0;
	for (var i=0;i<oXMLRemark.attributes.length;i++)
		if (oXMLRemark.attributes[i].nodeName == 'error_id')
			iErrorID = oXMLRemark.attributes[i].value;

	var oXMLAction = oXMLRemark.getElementsByTagName('ACTION')[0];

	var sRemarkText = aXMLErrors[iErrorID]?aXMLErrors[iErrorID]:oXMLRemark.firstChild.data;

	oPopup = new Popup();
	oPopup.setConfirmButtonText(sButtonOK);
	oPopup.addText(sRemarkText);
	oPopup.addBlankLine();
	if (oXMLAction)
	{
		var sURL = oXMLAction.firstChild.data;

		oPopup.setActionURL(sURL);
		oPopup.setActionTarget(FindParent(window));
	}

	oPopup.show();

	if (oXMLAction)
		return false;

	return true;
}

function ParseNothing(oXML)
{
	window.alert('ParseNothing: '+oXML);
}
