// Original code taken from http://developer.apple.com/internet/webcontent/xmlhttpreq.html
// Code has been wrapped as an object

// Object Constructor
// Pass the name of the function to call, if any, after the request has succeeded
function XmlHttpRequestWrapper(successFunction) {
	// Properties of the object
	var req = false;
	var onSuccess = successFunction;
	var loadedurl;
	// Methods of the object
    this.label = null;
    this.key = null;
	this.loadDocument = loadXMLDoc;
    this.getElementText = getElementTextNS;
    this.getRequest = request;
	
	// alias loadDocument()
	function loadXMLDoc(url, data) {
		//alert(url);
		loadedurl = url;
		req = false;
		// 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) {
			if ( data == '' ) {
				req.onreadystatechange = processReqChange;
				req.open("GET", url, true);
				req.send("");
			} else {
				req.onreadystatechange = processReqChange;
				req.open("POST", url, true);
				req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				req.send(data);
			}
		}
	}
	
	function request( ) {
		return req;
	}

	function processReqChange() {
		// If request is "loaded"
		if (req.readyState == 4) {
			// If request is "OK"
			if (req.status == 200) {
				try {
					// Call the specified function on success
					if ( onSuccess!='' ) {
						try {
							eval(onSuccess);
						} catch (e) {
							// Do nothing
						}
					}
				} catch(e) {
					alert('Error calling function on success: '+e);
				}
			} else {
				alert("There was a problem retrieving the XML data from "+loadedurl+":\n" + req.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";
    }
}