var ACTIVEX_PREFIX_XML = ["Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument.2.6", "Msxml2.DOMDocument", "Microsoft.XMLDOM"];
var ACTIVEX_PREFIX_XMLHTTP = ["Microsoft.XMLHTTP", "Msxml2.ServerXMLHTTP"];

  var Browser_Name = navigator.appName;
  var Browser_Version = parseInt(navigator.appVersion);
  //Document Handler
  var documentHandler = null;
  var xslHandler = null;
  
  //alert(Browser_Name  + " " + Browser_Version + " " + navigator.userAgent);

// return the first not-null ActiveX object from the list
function getActiveX(arrObjects){
  //alert("here");
  for(var i = 0; i < arrObjects.length; i++ ){
    try
    {
      //alert(i + ": " + arrObjects[i]);
      var result = new ActiveXObject(arrObjects[i]);
      if( result != null )
        return result;
    } catch (e)
    {
      //alert(e);
    }
  }
  return null;
}


// Reset the source doc
function prepareDocumentHandler()
{
  try
  {
    
    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
      //alert("trying for Mozilla");
      documentHandler = document.implementation.createDocument("", "", null);
    }else if (navigator.userAgent.indexOf("MSIE") >= 0  )
    {
      //alert("trying for ie");
      documentHandler = getActiveX(ACTIVEX_PREFIX_XML);
    }  
    if ( documentHandler == null )
    {
      alert("Can not Create document Handler unknown browser");
    }
  } catch(Exception) 
  {
    alert(Exception);
    alert("Can not Create document Handler");
  }
}

//Load a document from the specified URL.
function load(URLString)
{
  try
  {
    prepareDocumentHandler();
    documentHandler.async = false;
    //alert(URLString);
    documentHandler.load(URLString);
  }  catch (Exception)
  {
    alert(Exception);
  };
}

// Reset the source doc
function prepareXSLHandler()
{
  try
  {
    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
      //alert("trying for Mozilla");
      xslHandler = document.implementation.createDocument("", "", null);
    }else if (navigator.userAgent.indexOf("MSIE") >= 0  )
    {
      //alert("trying for ie");
      xslHandler = getActiveX(ACTIVEX_PREFIX_XML);
    }  
    if ( xslHandler == null )
    {
      alert("Can not Create document Handler unknown browser");
    }
  } catch(Exception) 
  {
    alert(Exception);
    alert("Can not Create document Handler");
  }
}

//Load a document from the specified URL.
function loadXSL(URLString)
{
  try
  {
    prepareXSLHandler();
    xslHandler.async = false;
    xslHandler.load(URLString);
  }  catch (Exception)
  {
    alert(Exception);
  };
}
//New Function
function CallXML(theXSL, theXML, paramName, paramValue)
 {
  var xsltTree = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");
  xsltTree.async = false;
  xsltTree.load(theXSL)

  var srcTree = new ActiveXObject("Msxml2.DOMDocument.4.0");
  srcTree.async = false;
  srcTree.load(theXML);

  var xsltThread = new ActiveXObject("Msxml2.XSLTemplate.4.0");
  xsltThread.stylesheet = xsltTree;
  var xsltProc = xsltThread.createProcessor();
  xsltProc.input = srcTree;
  if (paramName){
    xsltProc.addParameter(paramName, paramValue);
  }
  xsltProc.transform();
  return xsltProc.output;
 } 

 //transform xml with XSLT and returns transformed content  
 function transformContent(url, xslURL, parameter, paramValue){
   load(url);
   loadXSL(xslURL);
   //loadXSL("http://smellyfrog/web2/testxsl.xsl");
   var result = null;
      if (navigator.userAgent.indexOf("MSIE") >= 0  )
      {
        //alert("IE");
      try
      {
        result = CallXML(xslURL,url, parameter, paramValue);
      }catch (Exception)
      {
      //alert(Exception);
      };
        //result = documentHandler.transformNode(xslHandler);
      }
      else {
        //alert("Mozilla");
        try{
        var processor = new XSLTProcessor();
        var docResult;
        //alert((new XMLSerializer()).serializeToString(xslHandler));
        //alert((new XMLSerializer()).serializeToString(documentHandler));
        processor.importStylesheet(xslHandler);
	if (parameter){
	  processor.setParameter(null, parameter, paramValue);
	}
        docResult = processor.transformToDocument(documentHandler);
        result = (new XMLSerializer()).serializeToString(docResult);        
        }catch(Exception){
        alert("ERROR: \n" + Exception);
        }
      }
      //alert(result);
   return result;
 }
