var xsl = false;
var elemToAddTransformation = "myList";

function xslRequest(xmlDoc, xslFilePath) {
	if (window.ActiveXObject) {
		activeXTransform(xmlDoc, xslFilePath);
	} else if (window.XMLHttpRequest) {
		getXSL(xmlDoc, xslFilePath);
	}
}

function activeXTransform(xmlDoc, xslFilePath) {
	if(!xsl){
	    xsl = new ActiveXObject("MSXML2.DOMDocument");
	    xsl.async = false;
	    xsl.load(xslFilePath);
	}
	
	if (xmlDoc && xsl) {
		document.getElementById(elemToAddTransformation).innerHTML = xmlDoc.transformNode(xsl);
	}
}
/*
function getXML(url, reqType) {
	if (!reqType) {
		reqType = "text";
	}
	var xhrRec = xhrRequest(reqType);
	xhrRec.open('GET', url, true);
	xhrRec.onreadystatechange = function() {
		if (xhrRec.readyState == 4 && xhrRec.status == 200) {
			xml = xhrRec.responseXML;
			if (xsl) {
				xslTransform();
			}
			xhrRec = null;
		}
	};
	xhrRec.send(null);
}
*/
function getXSL(xmlDoc, xslFilePath) {
    if(!xsl){
	    var xhrRec = new XMLHttpRequest();
	    xhrRec.open('GET', xslFilePath);
	    xhrRec.onreadystatechange = function() {
		    if (xhrRec.readyState == 4 && xhrRec.status == 200) {
			    xsl = xhrRec.responseXML;
			    if (xmlDoc) {
				    xslTransform(xmlDoc);
			    }
			    xhrRec = null;
		    }
	    };
	    xhrRec.send(null);
    }else{
	    xslTransform(xmlDoc);
    }
}

function xslTransform(xmlDoc) {
	var xsltProc = new XSLTProcessor();
	if (xsltProc) {
		xsltProc.importStylesheet(xsl);
		var transformedXML = xsltProc.transformToDocument(xmlDoc, document);
		document.getElementById(elemToAddTransformation).innerHTML = transformedXML.xml;
	}
}


function orderList(field){
	xsl.selectSingleNode("//xsl:sort/@select").nodeValue = field
	xsl.selectSingleNode("//xsl:sort/@order").nodeValue = "ascending";
	xslRequest(xmlDoc);
}
