// JScript File

function TransformNode()
{
    this.xmlDoc = null;
    this.xslDoc = null;
    this.xmlDocFireFox = null;
    this.xslDocFireFox = null;
        
    // code for IE
    if (window.ActiveXObject)
    {
        this.xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
        //this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        this.xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        this.xmlDocFireFox = document.implementation.createDocument("","",null);
        this.xslDocFireFox = document.implementation.createDocument("","",null);
    }

    this.LoadXMLDocFromText = 
        function(text)
        {
            //  code for IE
            if (window.ActiveXObject)
            {
                return this.LoadXMLTextExplorer(text);
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument)
            {
                return this.loadXMLTextFireFox(text);
            }
            else
            {
                alert('Your browser cannot handle this script');
            }
            return null;
        }
    this.LoadXSLDocFromText = 
        function(text)
        {
            //  code for IE
            if (window.ActiveXObject)
            {
                return this.LoadXSLTextExplorer(text);
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument)
            {
                return this.loadXSLTextFireFox(text);
            }
            else
            {
                alert('Your browser cannot handle this script');
            }
            return null;
        }

    this.LoadXMLDoc = 
        function (fname)
        {
            // code for IE
            if (window.ActiveXObject)
            {
                return this.LoadXMLExplorer(fname);
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument)
            {
                return this.loadXMLFireFox(fname);
            }
            else
            {
                alert('Your browser cannot handle this script');
            }
        }
        
    this.LoadXMLExplorer = 
        function (xmlFile)
        {
          this.xmlDoc.async = "false";
          this.xmlDoc.load(xmlFile);
          //return this.xmlDoc.documentElement;
          return this.xmlDoc;
        }

    this.LoadXMLTextExplorer = 
        function(text)
        {
            this.xmlDoc.async = "false";
            this.xmlDoc.resolveExternals = false;
            this.xmlDoc.loadXML(text);
            //return this.xmlDoc.documentElement;
            return this.xmlDoc;
        }
    this.LoadXSLTextExplorer = 
        function(text)
        {
            this.xslDoc.async = "false";
            this.xslDoc.resolveExternals = false;
            this.xslDoc.loadXML(text);
            return this.xslDoc.documentElement;
        }
        
    this.loadXMLFireFox = 
        function (xmlFile)
        {
            this.xmlDocFireFox.async = false;
            this.xmlDocFireFox.load(xmlFile);
            return this.xmlDocFireFox.documentElement;
        }
    this.loadXSMLFireFox = 
        function (xmlFile)
        {
            this.xslDocFireFox.async = false;
            this.xslDocFireFox.load(xmlFile);
            return this.xslDocFireFox.documentElement;
        }

    this.loadXMLTextFireFox = 
        function (text)
        {
            var parser = new DOMParser();
            this.xmlDocFireFox = parser.parseFromString(text, "application/xml");
            return this.xmlDocFireFox;
        }
    this.loadXSLTextFireFox = 
        function (text)
        {
            var parser = new DOMParser();
            this.xslDocFireFox = parser.parseFromString(text, "application/xml");
            return this.xslDocFireFox;
        }
    this.SetXmlText = 
        function (text)
        {
            this.xmlFile = '';
            this.xmlObj = this.LoadXMLDocFromText(text);
        }
    this.SetXslText = 
        function (text)
        {
			this.xslFile = '';
			this.xslObj = this.LoadXSLDocFromText(text);
        }
        
    this.SetXmlFile = 
        function (file)
        {
            this.xmlFile = file;
            this.xmlObj = this.LoadXMLDoc(this.xmlFile);
        }
    this.SetXslFile = 
        function (file)
        {
			this.xslFile = file;
            this.xslObj = this.LoadXMLDoc(this.xslFile);
        }
    
    this.AddParameter = 
        function(key, value)
        {
            var p = new Param(key, value);
            var i = this.ParametersList.length;
            this.ParametersList[i] = p;
        }
            
    this.TransformNode = 
        function ()
        {
			if(this.IsXmlValid())
			{
				if (window.ActiveXObject)
				{
				    var xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
				    xslt.stylesheet = this.xslObj;
				    var xslProc = xslt.createProcessor();
                    xslProc.input = this.xmlObj;
                    for(var i = 0; i < this.ParametersList.length; i++)
                    {
                        var p = this.ParametersList[i];
                        xslProc.addParameter(p.Key, p.Value);
                    }
                    xslProc.transform();
                    var innerHTML = xslProc.output;
                    return innerHTML;
				    
					//var innerHTML = this.xmlObj.transformNode(this.xslObj);
					//return innerHTML;
				}
				// code for Mozilla, Firefox, Opera, etc.
				else if (document.implementation && document.implementation.createDocument)
				{
					try 
					{
						var xsltProcessor = new XSLTProcessor();
						xsltProcessor.importStylesheet(this.xslObj);
						
						for(var i = 0; i < this.ParametersList.length; i++)
                        {
                            var p = this.ParametersList[i];
                            xsltProcessor.setParameter(null, p.Key, p.Value);
                        }
						
						var fragment = xsltProcessor.transformToFragment(this.xmlObj, document);
						return fragment;
					}
					catch(e) 
					{
						alert( 'this.TransformNode:' + e );
						return '';
					}
				}
			}
			return '';
        }

    this.TransformNodeIntoObj = 
        function (obj)
        {
            if(obj == null)
            {
                return;
            }
            if (window.ActiveXObject)
            {
                var innerHTML = this.TransformNode();
                obj.innerHTML = innerHTML;
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument)
            {
                try 
                {
                    var fragment = this.TransformNode();
                    
                    while(obj.childNodes[0])  
                    {    
                        obj.removeChild(obj.childNodes[0]);  
                    }
                    
                    obj.appendChild(fragment);
                }
                catch(e) 
                {
                    alert('this.TransformNodeIntoObj:' + e);
                }
            }
        }
        
    this.ChangeXmlElement = 
        function(xpath, value)
        {
			if(this.IsXmlValid())
			{
                if (window.ActiveXObject)
                {
                    var elem = this.xmlObj.selectSingleNode(xpath);
				    elem.text = value;
                }
                else if(document.implementation && document.implementation.createDocument)
                {
                    try 
                    {
                        var xpeContent = new XPathEvaluator();
                        var nodes = xpeContent.evaluate(xpath, this.xmlObj, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
                        var node = nodes.snapshotItem(0).firstChild;
                        node.data = value;
				    }
                    catch(e) 
                    {
                        alert('this.ChangeXmlElement:' + e);
                    }
                }
			}
        }

	this.IsXmlValid  =
		function()
		{
			return (this.xmlObj != null && this.xmlObj.xml != '');
		}

    this.xmlFile;
    this.xslFile;
    this.xmlObj;
    this.xslObj;
    this.ParametersList = new Array();
    
    function Param(key, value)
    {
        this.Key = key;
        this.Value = value;
    }
}


