//functions for dealing with XML data

/*
	This file is compressed using packer (http://dean.edwards.name/packer/) with shorten variables
	turned on, but not Base62 encoding. The packed version is saved as xml-min.js.
	Any changes to this file should be accompanied by changes to the compressed version.
*/
function parseXML(text) {
	if (typeof DOMParser != "undefined") {
		// Mozilla, Firefox, and related browsers
		return (new DOMParser()).parseFromString(text, "application/xml");
	}
	else if (typeof ActiveXObject != "undefined") {
		// Internet Explorer.
		var doc = new ActiveXObject("Microsoft.XMLDOM"); // Create an empty document
		doc.loadXML(text);            // Parse text into it
		return doc;                   // Return it
	}
	else {
		// As a last resort, try loading the document from a data: URL
		var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
		var request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
		return request.responseXML;
	}
}

function XmlToText(doc) {
	if (typeof XMLSerializer != "undefined") {
		// Mozilla, Firefox, and related browsers
		return (new XMLSerializer()).serializeToString(doc);
	} else {
		//IE
		return doc.xml;
	}
}

function SelectValue(doc, xpath, contextNode) {
	var context = contextNode ? contextNode : doc;
	if(window.ActiveXObject) {
		//IE
		return context.selectSingleNode(xpath).value;
	} else {
		// Mozilla, Firefox, and related browsers
		return doc.evaluate(xpath, context, null, XPathResult.STRING_TYPE, null).stringValue;
	}
}

function SelectSingleNode(doc, xpath, contextNode) {
	var context = contextNode ? contextNode : doc;
	if(window.ActiveXObject) {
		//IE
		return context.selectSingleNode(xpath);
	} else {
		// Mozilla, Firefox, and related browsers
		return doc.evaluate(xpath, context, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
	}
}

function SelectNodes(doc, xpath, contextNode) {
	var context = contextNode ? contextNode : doc;
	var objResult;
	var xe;
	var arElements = [];
	if(window.ActiveXObject) {
		//IE
		objResult = context.selectNodes(xpath);
		
		if (objResult !== null) {
			xe = objResult.nextNode();
			while(xe) {
				arElements.push(xe);
				xe = objResult.nextNode();
			}
		}
	} else {
		// Mozilla, Firefox, and related browsers
		objResult = doc.evaluate(xpath, context, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
		
		if (objResult !== null) {
			xe = objResult.iterateNext();
			while(xe) {
				arElements.push(xe);
				xe = objResult.iterateNext();
			}
		}
	}
	return arElements;
}