/**
 * Utility class for cross-browser xml functions
 */
function XmlUtil() {}

/**
 * Creates a new xml document
 */
XmlUtil.createXmlDocument = function() {
	var xmlDocument;

	if (window.ActiveXObject) {
		xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocument.async = false;
	} else if (document.implementation && document.implementation.createDocument) {
		xmlDocument = document.implementation.createDocument("ns", "root", null);
	} else {
		throw 'XmlUtil.createXmlDocument not supported!';
	}
	
	return xmlDocument;
}

XmlUtil.loadXmlDocument = function(xml) {
}

/**
 * Transforms supplied xml document using specified xslt
 * @param xmlDoc	the xml document to transform
 * @param xslUri	uri of the xslt use in the transform
 * @param displayDiv	div to send transformed results to
 */
XmlUtil.transformXml = function(xmlDoc, xslDoc) {
	var html;

	// if no xml document is supplied, create a blank document
	// and apply transform to it. This would be for an xslt with
	// an attribute match="/"
	if (xmlDoc == null) xmlDoc = XmlUtil.createXmlDocument();
	
	// do the transform
	if (window.ActiveXObject) {
		html = xmlDoc.transformNode(xslDoc);
	} else {
		var xsltProcessor = new XSLTProcessor();
		var xmlSerializer = new XMLSerializer();
	
		xsltProcessor.importStylesheet(xslDoc);
		html = xmlSerializer.serializeToString(xsltProcessor.transformToFragment(xmlDoc, document));		
	}
	
	return html;

}

XmlUtil.getXml = function(url){
	
	var xmlHttp;
			
	xmlHttp = createXmlHttp();
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);

	return xmlHttp.responseXML;
	
}	


XmlUtil.getText = function(url){
	
	var xmlHttp;
			
	xmlHttp = createXmlHttp();
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);

	return xmlHttp.responseText;
	
}

XmlUtil.httpGet = function(xmlHttp, url){
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

//////////////////////////////////////////////////	
function selectNode (xmlDoc, path){

	if (window.ActiveXObject)
		return xmlDoc.selectSingleNode(path);
	else
		return xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ANY_TYPE, null).iterateNext();								
}	
	
//////////////////////////////////////////////////
function selectNodes(xmlDoc, path){

	var xmlNodeList;
	var xmlNode;
	var xPathResult;
	var i;

	if (window.ActiveXObject)
		xmlNodeList = xmlDoc.selectNodes(path);
	else{
		xPathResult = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);	
		xmlNodeList = new Array();
	
		for (i = 0; i != xPathResult.snapshotLength; i++)
			xmlNodeList[xmlNodeList.length] = xPathResult.snapshotItem(i);
	}
	
	return xmlNodeList;
}	
	
//////////////////////////////////////////////////
function createXmlHttp(){

	var xmlHttp = null;
	
	if(window.ActiveXObject)
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	else 
		xmlHttp = new XMLHttpRequest();

	return xmlHttp;		

}
