﻿/*********************
* XML utils
* Alessandro Piccione
* last date: 28/03/2007
/*********************/

// Alex: Array of possible ActiveX, from most recento to oldest one
var ARR_ACTIVEX = ["MSXML4.DOMDocument", 
                   "MSXML3.DOMDocument",
                   "MSXML2.DOMDocument", 
                   "MSXML.DOMDocument",
                   "Microsoft.XmlDom"];
                   
//if this is IE, determine which string to use
/*
if (isIE) {

   //define found flag
   var bFound = false;

   //iterate through strings to determine which one to use
   for (var i=0; i < ARR_ACTIVEX.length && !bFound; i++) {

      //set up try...catch block for trial and error 
      //of strings
      try {

         //try to create the object, it will cause an 
         //error if it doesn't work
         var objXML = new ActiveXObject(ARR_ACTIVEX[i]);

         //if it gets to this point, the string worked, 
         //so save it
         STR_ACTIVEX = ARR_ACTIVEX[i];
         bFound = true

      } catch (objException) {
      } //End: try
   } //End: for

   //if we didn't find the string, send an error
   if (!bFound)
      throw "MSXML not found on your computer."
}
*/

// create an XML Document from file
function CreateXMLDocument_fromFile(xmlFile)
{
    var xmldoc;
	if (window.ActiveXObject)
	{
	    xmlDoc = new ActiveXObject("MSXML2.DomDocument.4.0");
		//xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		/*xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createTable()
		};*/
 	}
 	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		//xmlDoc.onload = createTable;
	}
	else
	{
		alert('Your browser can\'t handle XML Document');
		return;
	}

	xmlDoc.load(xmlFile);
	
    return xmlDoc;
}

// create an XML Document from string
function CreateXMLDocument_fromString(xmlString)
{
    try
    {
        var xmldoc;
	    if (window.ActiveXObject)
	    {
	        //xmlDoc = new ActiveXObject("MSXML2.DomDocument.4.0");   /// non  va !
		    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		    /*xmlDoc.onreadystatechange = function () {
			    if (xmlDoc.readyState == 4) createTable()
		    };*/
 	    }
 	    else if (document.implementation && document.implementation.createDocument)
	    {
		    xmlDoc = document.implementation.createDocument("", "", null);
		    // Alex: return xml only when it's loaded 
		    //xmlDoc.onload = returnXML(xmlDoc);
	    }
	    else
	    {
		    alert('Your browser can\'t handle XML Document');
		    return;
	    }


        try
        {
            // Explorer
	        xmlDoc.loadXML(xmlString);
	    }
	    catch (error_1)
	    {
	        // Mozilla and friend
	        try
	        {
	            xmlDoc = (new DOMParser()).parseFromString(xmlString, "text/xml");
	        }
	        catch (error_3)
	        {
	            alert("error_3: " + error_3.description);
	        }
	            
	    }    
    }
    catch (error_2) {alert("Error on create XML document from string: " + error_2.description);}


    return xmlDoc;
}



function Transform(xmlFile, xslFile)
{
    var xmlFile = new ActiveXObject("MSXML2.DomDocument.4.0");
    xmlDom.async = false;
    xmlDom.load(xmlFile);
    
    var xslDom = new ActiveXObject("MSXML2.DomDocument.4.0");
    xslDom.async = false;
    xslDom.load(xslFile);
    
    return xmlDom.transformNode(xslDom);
}


function Test(text)
{
    alert(text);
}
