﻿/****************************************************************************************/
/* Author:                                                                              */
/* Shawn Karnoski                                                                       */
/*                                                                                      */
/* Date:                                                                                */
/* 7/17/2008                                                                            */
/*                                                                                      */
/* Description:                                                                         */
/* This class is designed to provide the methods for loading and reading an XML         */
/* document.  The class contains functions for loading the XML document, returning the  */
/* content of a node in the document, returning the content of a child of a node in the */
/* document and returning the value of an attribute of a node (parent or child) in the  */
/* document                                                                             */
/*                                                                                      */
/* Notes:                                                                               */
/* The following is an example of how to use this class in a javascript program.  In    */
/* this example, the attributes of each child node of a parent node are outputted via   */
/* an alert box                                                                         */  
/*                                                                                      */
/* var objXML = new xmlObject();                                                        */
/* objXML.docPath = "Assets/xml/langText.xml";                                          */
/* objXML.onloadFunction = "displayChildNodeAttribs()";                                 */
/* objXML.loadXMLDocument();                                                            */
/* function displayChildNodeAttribs()                                                   */
/* {                                                                                    */
/*      var objXMLNode;                                                                 */
/*      var objXMLChildNode;                                                            */
/*      objXMLNode = objXML.getXMLNode("appSection");                                   */
/*      for(var i = 0; i <= objXMLNode.length - 1; i++)                                 */
/*      {                                                                               */ 
/*          if(objXML.getXMLNodeAttribute(objXMLNode, i, "id") == 31)                   */
/*          {                                                                           */
/*              objXMLChildNode = objXML.getXMLChildNode(objXMLNode, i, "appQuestion"); */
/*              for(var x = 0; x <= objXMLChildNode.length - 1; x++)                    */
/*              {                                                                       */
/*                  alert(objXML.getXMLNodeAttribute(objXMLChildNode, x, "quesText"));  */
/*              }                                                                       */ 
/*          }                                                                           */
/*      }                                                                               */
/* }                                                                                    */
/*                                                                                      */
/* Tested On:                                                                           */
/* Firefox 2.0.15, IE 7, IE 6, Safari 3.1.2 for Windows                                 */
/****************************************************************************************/
function xmlObject()
{
    //these are the public properties of the class.  docPath will accept the file path to the physical XML
    //document while onloadFunction will accept the name of a function that should be executed when the
    //XML document is fully loaded
    this.docPath = "";
    this.onloadFunction = "";
    
    //this is a class global variable that will be used to create an XML document object that the class
    //can work with
    var objXMLDoc;
    
    
    //this method will load an XML document.  
    this.loadXMLDocument = function()
    {
        var strFuncName = this.onloadFunction;
        
        //determine what browser is being used
        if(navigator.userAgent.indexOf("MSIE") > 0)
        {
            objXMLDoc = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
            objXMLDoc = new XMLHttpRequest();
        }
        objXMLDoc.onreadystatechange = function(){if(objXMLDoc.readyState == 4){eval(strFuncName);}};
        objXMLDoc.open("GET", this.docPath, true);
        objXMLDoc.send(null);
    }
    
  
    //this method will accept the name of a node in an XML document and return an array of all the elements 
    //within that node 
    this.getXMLNode = function(strNodeName)
    {
        var objXMLNode;
    
        objXMLNode = objXMLDoc.responseXML.getElementsByTagName(strNodeName);
        return objXMLNode;
    }
    
    
    //this method will accept an array of XML Nodes, a specific index value for that array and the name of a
    //child node and will return an array of all the elements within that child node
    this.getXMLChildNode = function(objXMLNode, intNodeIndex, strChildNodeName)
    {
        var objXMLChildNode;
    
        objXMLChildNode = objXMLNode[intNodeIndex].getElementsByTagName(strChildNodeName);
        return objXMLChildNode;
    }
    
    
    //this function will accept an array of XML nodes, a specific index value for that array and the name of a
    //node attribute and will return the value of that attribute
    this.getXMLNodeAttribute = function (objXMLNode, intNodeIndex, strAttribName)
    {
        var strNodeAttribValue;
  
        strNodeAttribValue = objXMLNode[intNodeIndex].getAttribute(strAttribName);
        return strNodeAttribValue;
    }
}