var req;
var reqQueue = new Queue("requestQueue");
var timer = 0;


function loadXMLDoc(url) {
    reqQueue.Enqueue(url);
    timer = window.setTimeout("checkQueue()",150);
}

function checkQueue(){
    if(reqQueue.size() == 1){
        getNextDoc();
        window.clearTimeout(timer);
    }
}

function isAjaxCompatible() {
    var ro = true;
    try {
      var ignore = window.XMLHttpRequest ? new XMLHttpRequest():
    			new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      ro = false;
    }
    return ro;
}

function getNextDoc() {
	req = false;
	var urlToGet = reqQueue.Dequeue();
	
	if(urlToGet){
    	 //d = document.getElementById("curUrl");
    	 //d.innerHTML = url;
        // branch for native XMLHttpRequest object
        if(window.XMLHttpRequest) {
        	try {
    			req = new XMLHttpRequest();
            } catch(e) {
    			req = false;
            }
        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
           	try {
            	req = new ActiveXObject("Msxml2.XMLHTTP");
          	} catch(e) {
            	try {
              		req = new ActiveXObject("Microsoft.XMLHTTP");
            	} catch(e) {
              		req = false;
            	}
    		}
        }
    	if(req) {
    		req.onreadystatechange = processReqChange;
    		req.open("GET", urlToGet, true);
    		req.send("");
    	}
    }
}


function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            //write fields may queue up addtl. requests
            try {
                handleAjaxResponse(req.responseXML);
            } 
            catch (error) {
                alert("Oops... an error occured. Please try again...");
                var eDiv = document.getElementById("error");
                if(eDiv){
                    obj= error;
                    for (prop in obj)
                    {
                    if (typeof(obj[prop]) == "object")
                    {
                        if (obj[prop] && obj[prop].length != undefined)
                            alert("error" + "." + prop + "=[probably array, length "
                                + obj[prop].length + "]");
                        else
                            alert("error" + "." + prop + "=[" + typeof(obj[prop]) + "]");
                    }
                    else
                        alert("error" + "." + prop + "=" + obj[prop]);
                    }
                    eDiv.innerHTML = req.responseText;
               }       
            }
            
            //let the first one out
            getNextDoc(req.responseXML);
        }
        else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}


// This is a simple implementation of a first-in/first-out queue
function Queue(Name)
{
  this.Name = Name; // Makes it pretty when you print the contents
  this.Stack = new Array();
  this.Enqueue = function(Item)
  {
    // Tack the new entry onto the end of the array
    this.Stack[this.Stack.length] = Item;
    return this;
  }
  this.Dequeue = function()
  {
    // Remove the first entry from the array and return it
    if (this.Stack.length > 0)
    {
      var Result = this.Stack[0];
      this.Stack = this.Stack.slice(1);
      return Result;
    }
    return null;
  }
  this.size = function()
  {
      return this.Stack.length;
  }
  this.toString = function()
  {
    Result = this.Name + "Queue Contents:n";
    for (var x = 0; x < this.Stack.length; x++)
    {
      Result += "t" + this.Stack[x];
    }
    return Result;
  }
}

function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}	

