/*
 * xslt.js
 *
 * 2002, 2005
 *
 * Author: Johann Burkard - http://johannburkard.de
 *
 * Version 1.0
 *
 * XSL Transformations.
 */

/**
 * Browser supports XSLT.
 */
function browserSupportsXSLT() {
  var support = false;
  var navegador = navigator.appName;
  var IE = 'Internet Explorer';
  if (navegador.match(IE) == 'Internet Explorer') {       // IExplorer
    support = true;
  }
  if (typeof XMLHttpRequest != 'undefined' && typeof XSLTProcessor != 'undefined'
  && typeof DOMParser != 'undefined' && typeof XMLSerializer != 'undefined') { // Mozilla 0.9.4+, Opera 9+
    support = true;
  }
  return support;
}

/*
 * Transform XML text and XSL text into the targetElement.
 * 
 * @param xml XML text or ID of the element containing XML (for IE)
 * @param xsl XSL text or ID of the element containing XSL (for IE)
 * @param xslParam doble array, params for the XSL document
*/
  function xsltTransform(xmlDoc, xslDoc, xslParams){
	  var resultado,docCache,docProcessor;
	  //Detección de navegador (para IE9)
	  var navegador = navigator.appName;
	  var IE = 'Internet Explorer';
	  if (navegador.match(IE) == 'Internet Explorer') {       // IExplorer

      var xslIE = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
      xslIE.async = false;
      xslIE.load(xslDoc);
      docCache = new ActiveXObject("MSXML2.XSLTemplate");
      docCache.stylesheet = xslIE;
      // instantiate the document processor and submit the xml document
      docProcessor = docCache.createProcessor();
      docProcessor.input = xmlDoc;
      // add parameters to the xsl document
      for (var i=0;i<xslParams.length;i++){
        docProcessor.addParameter(xslParams[i][0],xslParams[i][1], "");
      }
      // process the documents into html and submit to the passed div to the HMTL page
      docProcessor.transform();
      resultado = docProcessor.output;
    } else {                    // Mozilla
      var xsl = new XSLTProcessor();
      for (var i=0;i<xslParams.length;i++){
        xsl.setParameter(null,xslParams[i][0],xslParams[i][1]);
      }
      xsl.importStylesheet(xslDoc);
      var fragment=xsl.transformToFragment(xmlDoc, document);
      resultado = (new XMLSerializer()).serializeToString(fragment);
    }
    return resultado
  }

/**
 * Transform XML text and XSL text into the targetElement.
 * 
 * @param xml XML text or ID of the element containing XML (for IE)
 * @param xsl XSL text or ID of the element containing XSL (for IE)
 * @param targetElement element to write transform output to
 */
function transform(xml, xsl, targetElement) {
  var navegador = navigator.appName;
  var IE = 'Internet Explorer';
  if (navegador.match(IE) == 'Internet Explorer') {       // IExplorer
    if (document.all[xml].readyState == 'complete' && document.all[xsl].readyState == 'complete') {
      document.all[targetElement].innerHTML = document.all[xml].transformNode(document.all[xsl].XMLDocument);
    }
  }
  else {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xml, "text/xml");
    var xslDoc = parser.parseFromString(xsl, "text/xml");
    var resultDoc;
    var processor = new XSLTProcessor();

    if (typeof processor.transformDocument == 'function') {
      // obsolete Mozilla interface
      resultDoc = document.implementation.createDocument("", "", null);
      processor.transformDocument(xmlDoc, xslDoc, resultDoc, null);
    }

    else {
      processor.importStylesheet(xslDoc);
      var resultDoc = processor.transformToDocument(xmlDoc);
    }

    var out = new XMLSerializer().serializeToString(resultDoc);
    if (typeof processor.transformDocument == 'function') {
      out = out.replace(/&amp;/g, "&");
    }
    document.getElementById(targetElement).innerHTML = out;
  }
}

/**
 * Transform an XML document.
 *
 * @param xml the URL of the XML document
 * @param xsl the URL of the XSL document
 * @param targetElement the target element
 */
function transformXML(xml, xsl, targetElement) {
  if (!browserSupportsXSLT()) {
    return;
  }
  var navegador = navigator.appName;
  var IE = 'Internet Explorer';
  if (navegador.match(IE) == 'Internet Explorer') {       // IExplorer
    var id = Math.round(Math.random() * 10000);
    var out = "";
    out += '<xml id="xml' + id + '" src="' + xml + '" onreadystatechange="transform(\'xml' + id + '\', \'xsl' + id + '\', \'' + targetElement + '\')"><\/xml>';
    out += '<xml id="xsl' + id + '" src="' + xsl + '" onreadystatechange="transform(\'xml' + id + '\', \'xsl' + id + '\', \'' + targetElement + '\')"><\/xml>';
    document.body.insertAdjacentHTML("beforeEnd", out);
  }
  else {
    var xmlRequest = new XMLHttpRequest();
    var xslRequest = new XMLHttpRequest();
    xmlRequest.onreadystatechange = function () {
      if (xmlRequest.readyState == 4 && xslRequest.readyState == 4) {
        transform(xmlRequest.responseText, xslRequest.responseText, targetElement);
      }
    }
    xslRequest.onreadystatechange = function () {
      if (xmlRequest.readyState == 4 && xslRequest.readyState == 4) {
        transform(xmlRequest.responseText, xslRequest.responseText, targetElement);
      }
    }
    xmlRequest.open("GET", xml);
    xmlRequest.send(null);
    xslRequest.open("GET", xsl);
    xslRequest.send(null);
  }
}
