function setTransform(el, xmlData, xsltProcessor){
	// If this is running in the juniper IVE. Juniper needs to rewrite urls and will only
	// change the urls if we use innerHTML.
	if(typeof DanaGetHref == "function"){
		// Create a temporary div to contain the transform.
		var div = document.createElement("div");
		// Append the transformed data to the div.
		div.appendChild(xsltProcessor.transformToFragment(xmlData, document));
		// Take the div's innerHTML and place it in the receiving element.
		el.innerHTML += div.innerHTML;
	}else{
		// Append the transformed data to the receiving element.
		el.appendChild(xsltProcessor.transformToFragment(xmlData, document));
	}
}

function getTransform(el, xmlData, xslData){
	// Is there data?
	if(!(xmlData && xslData)){
		return;
	}

	// Are we running under Mozila?
	if(typeof XSLTProcessor != "undefined") {
		var xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslData);
		setTransform(el, xmlData, xsltProcessor);
		return;
	}else{
		// This is how it works in IE.
		try{
			el.innerHTML =  xmlData.transformNode(xslData.documentElement);
		}catch(e){
			if(typeof e.number != "undefined"){
				alert(e.number + ":" + e.description);
			}else{
				alert(e);
			}
		}
	}
}

function getXML(req) {
	// Sometimes you can access the xml in requestText but not in requestXML.
	var xmlData = null;
	try {
		// Try to access the data in responseXML. Trying to access the firstChild
		// throws an error if not in IE.
		if(typeof req.responseXML.firstChild != "undefined"){
			xmlData = req.responseXML;
		}
	}catch(e){
		// responseXML failed, get the data from responseText.
		try {
			// Create a new xmlDocument from responseText.
			xmlData = (new DOMParser()).parseFromString(req.responseText, "text/xml");
		}catch(e){
			if(typeof e.number != "undefined"){
				alert(e.number + ":" + e.description);
			}else{
				alert(e);
			}
		}
	}

	return xmlData;
}

// For use with an asynchronous call.
function createXMLHTTPObject() {
	var req = null;
	
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = null;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		bActiveX = true;
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = null;
			}
		}
	}
	
	return req;
}

// Use a synchronous call.
function loadXMLDoc(url) {
	if(url.indexOf("?") == -1) {
		url += "?Open&" + nocache;
	} else {
		url += "&" + nocache;
	}

	var req = createXMLHTTPObject();
	if(req) {
		// Use a synchronous call.
		req.open("GET", url, false);
		req.send(null);
	}
	
	return getXML(req);
}

// Sample usage.

// Get the XML data for a widget.
function getWidget(el, sWidgetID, sTitle, sUrl, sXsl) {
	// el: 		Element to add the widget to.
	// sWidgetID:	Unique ID of widget, used for ID of containing div.
	// sTitle:	Title of widget.
	// sUrl:	URL of XML data.
	// sXsl:	URL of XSL data.
	
	var req = createXMLHTTPObject();
	if (!req) {
		alert("Error creating XMLHTTP object for XML data.");
		return;
	}

	// Create the containing div for the widget.
	var div = document.createElement("div");
	div.id = sWidgetID;
	div.className = "widget";
	div.innerHTML = "<h3>Loading " + sTitle + "...</h3>";
	el.appendChild(div);

	// Use an asynchronous call.
	req.open("GET",sUrl,true);
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			return;
		}

		// Once the data has arrived, go get the XSL and transform the XML data.
		setWidget(div, req, sXsl);
	}
	if (req.readyState == 4) return;
	req.send(null);
}

// Once we have the XML data, get the XSL and transform the data.
function setWidget(el, reqXML, sXsl) {
	var req = createXMLHTTPObject();
	if (!req) {
		alert("Error creating XMLHTTP object for XSL data.");
		return;
	}

	// Use an asynchronous call.
	req.open("GET",sXsl,true);
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			return;
		}

		// Clear the contents of the widget.
		el.innerHTML = "";
		// Transform the XML with XSL.
		getTransform(el, getXML(reqXML), getXML(req));

		// If there isn't any data, hide it.
		if(el.innerHTML == "") {
			el.className = "hide";
		}
	}

	if (req.readyState == 4) return;
	req.send(null);
}

