﻿// JScript File
function XmlObject(strNamespaceURI, strRootTagName ) {
    
    this.isMoz = false;
    this.isIE = false;
    
    /********** first create the xml dom object to hold xml data ***********/
    
    //obj dom creation begin
    
    //if mozilla
    if ( document.implementation && document.implementation.createDocument ) {
        this.isMoz = true;
        this.objDOM = document.implementation.createDocument(strNamespaceURI, strRootTagName, null);
    } else {
        this.isIE = true; 
    // so browser is ie
       //first find installed msxml
       var ARR_ACTIVEX = ["MSXML4.DOMDocument", 
                   "MSXML3.DOMDocument",
                   "MSXML2.DOMDocument", 
                   "MSXML.DOMDocument",
                   "Microsoft.XmlDom"]
       var STR_ACTIVEX = "";
        //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)
            this.objDOM = null;
       else {
            //we found msxml version so create dom object
            this.objDOM = new ActiveXObject(STR_ACTIVEX);

            //if there is a root tag name, we need to preload the DOM
            if (strRootTagName) {
               //If there is both a namespace and root tag name, then
               //create an artifical namespace reference and load the XML.
//               if (strNamespaceURI) {
//                  this.objDOM.loadXML("<a0:" + strRootTagName + "xmlns:a0=\"" + 
//                                 strNamespaceURI + "\" />");
//               } else {
//                  this.objDOM.loadXML("<" + strRootTagName + "/>");
//               }
                this.objDOM.loadXML("<" + strRootTagName + "/>");
            }
       }
    }
    // obj dom creation end
    
    // getXml begin
        this.getXML = function () {
            if ( this.isIE ) {
                return this.objDOM.xml;  
            } else if ( this.isMoz ) {
                //create a new XMLSerializer
                var objXMLSerializer = new XMLSerializer;
                
                //get the XML string
                var strXML = objXMLSerializer.serializeToString(this.objDOM);
                
                //return the XML string
                return strXML;               
            }
            
        }
    // getXml end
    
    // create a loadXML method for mozilla
    this.loadXML = function(strXML) {
        if ( this.isIE ) {
            this.objDOM.loadXML(strXML);
        } else if ( this.isMoz ) {
            if ( !DOMParser )
                return;
            //create a DOMParser
            var objDOMParser = new DOMParser();
                
            //create new document from string
            var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
            
            while (this.objDOM.hasChildNodes())
                 this.objDOM.removeChild(this.objDOM.lastChild);
                 
            for (var i=0; i < objDoc.childNodes.length; i++) { 
              //import the node
              var objImportedNode = this.objDOM.importNode(objDoc.childNodes[i], true);     
              //append the child to the current document
              this.objDOM.appendChild(objImportedNode);
            }
            
            
        }
    }
    
    this.showElementInfo = function ( elmName ) {
        if ( this.objDOM && this.objDOM.hasChildNodes() ) {
            var nodeList = this.objDOM.documentElement.getElementsByTagName(elmName);
            for ( var i = 0; i < nodeList.length ; i++ ) {
                if ( nodeList[i].hasChildNodes ) {
                    alert ( nodeList[i].firstChild.nodeValue );
                }
            }
        }
    }
    
    
}
