//------------------------------------------------------------------------------
var pendingXML = new Array();
var priorityPendingXML = new Array();
var xmlsInProcess = new Array();
var maxInProcess = 2;

var xmlQueues = new Array();
xmlQueues.push(new Array());
xmlQueues.push(new Array());
xmlQueues.push(new Array());

//------------------------------------------------------------------------------
function getXML(url, callback, object, priority, timeout)
{
    if(typeof(timeout) == 'undefined')
        timeout = -1;
        
	var job = new Object();
    job.url = url;
    job.xmlCallback = callback;    
    job.callbackObject = object;
    job.timeout = timeout;
    
    xmlQueues[priority].push(job);
}

//------------------------------------------------------------------------------
function updateXML()
{   
    var count = xmlsInProcess.length;
    var a;
    for(a = 0; a < count; a++)
    {
        var xml = xmlsInProcess[a];
        if(xml.job.timeout != -1 
                && xml.start + xml.job.timeout < getMilliseconds())
        {
            xml.http_request.onreadystatechange = null;
            xml.http_request.abort();
            xml.job.xmlCallback(null, xml.job.callbackObject);            
            xmlsInProcess.splice(a, 1);
            break;  // even though there may be more done, only process one per cycle; 
                    // also, our iteration has been invalidated by the splice
        }            
    }
    
    if(count < maxInProcess)
    {
        var job = null;
        for (a = 0; a < xmlQueues.length; a++)
        {
            if(xmlQueues[a].length)
            {
                job = xmlQueues[a].shift();
                break;
            }
        }
        
        if(!job)
            return;
    
        // ___ start one        
        var xml = new Object();
        xml.job = job;
        xml.http_request = false;
        xml.start = getMilliseconds();
        
        if (window.XMLHttpRequest)
        { // Mozilla, Safari,...
            xml.http_request = new XMLHttpRequest();
            if (xml.http_request.overrideMimeType)
                xml.http_request.overrideMimeType('text/xml');
        }
        else if (window.ActiveXObject)
        { // IE
            try
            {
                xml.http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    xml.http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                }
            }
        }
    
        if (!xml.http_request)
        {
            xml.job.xmlCallback(null, xml.job.callbackObject);
            return;
        }
        
        xmlsInProcess.push(xml);
        
        xml.http_request.onreadystatechange = xmlReadyStateChange;
        xml.http_request.open('GET', xml.job.url, true);
        xml.http_request.send(null);
    }
}

//------------------------------------------------------------------------------
function xmlReadyStateChange()
{
    var count = xmlsInProcess.length;
    var a;
    for(a = 0; a < count; a++)
    {
        var xml = xmlsInProcess[a];
        if (xml.http_request.readyState == 4)
        {
        	try
        	{
	            xml.job.xmlCallback(xml.http_request, xml.job.callbackObject);
	        }
	        catch(e)
	        {
	        }
	        
            xmlsInProcess.splice(a, 1);
            break;  // even though there may be more done, only process one per cycle; 
                    // also, our iteration has been invalidated by the splice
        }
    }
}

//------------------------------------------------------------------------------
function getXMLNode(fromNode, tag)
{
    var nodes = fromNode.getElementsByTagName(tag);
    return nodes[0];
}

//------------------------------------------------------------------------------
function getXMLData(fromNode, tag)
{
    var node;
    if(typeof(tag) == 'undefined')
        node = fromNode;
    else   
        node = getXMLNode(fromNode, tag);   
        
    if(node && node.firstChild)
        return node.firstChild.data;
        
    return null;
}

//------------------------------------------------------------------------------
function getXMLAttribute(fromNode, tag)
{
    return fromNode.attributes.getNamedItem(tag).nodeValue;
}
