/**
 * XML Request handling, with support for multiple connections
 * (c) 2006 IVinity
 * http://www.ivinity.nl/
 * Tested with:
 *  IE 6 (win)
 *  Opera 8.5 (win / mac)
 *  Firefox 1.5 (win / mac)
 *  Safari 1.3 (mac)
 */
 
// global request and XML document objects
var http_request;

function XMLRequestHandler(method, url, asynch, data) {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		this.xmlRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		this.xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var self = this;
	this.handle = processReqChange;
	this.requestID = requestID++;
	
	try {
		if(arguments[4] == false) {
		
		} else {
			this.xmlRequest.onreadystatechange = function() { self.handle(); }	
		}
	}
	catch(e) { this.xmlRequest.onreadystatechange = function() { self.handle(); } }
	
	this.xmlRequest.open(method, url, asynch);
	if (method == "POST") {
		this.xmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	this.xmlRequest.send(data);
	return this;
}

var requests = new Array();
var requestID = 1;

function loadXMLDoc(url) {
	var index = requests.length;
	requests[index] = new XMLRequestHandler('GET', url, true, null);	
}

function loadXMLDocPost(url, postdata) {
	var index = requests.length;
	requests[index] = new XMLRequestHandler('POST', url, true, postdata);	
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (this.xmlRequest.readyState == 4) {
        // only if "OK"
        if (this.xmlRequest.status == 200) {
					handleXMLupdate(this.xmlRequest);
					// cleanup
					for (i = 0; i < requests.length; i++) {
						if (requests[i].requestID == this.requestID) {
							requests.splice(i, 1);							
						}
					}					
        } else {
           alert("There was a problem retrieving the XML data:\n" +
               this.xmlRequest.statusText);
        }
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
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";
    }
}