function getXMLDoc(xmldata){
  try { //Internet Explorer
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(xmldata);
  }
  catch(e){
      try { //Firefox, Mozilla, Opera, etc.
          parser=new DOMParser();
          xmlDoc=parser.parseFromString(xmldata,"text/xml");
      } 
      catch(e){
          alert(e.message);
          return;
      }
  } 
  return xmlDoc;
}

/*

    This presents simplified interface to XML
    Text is handled differently than sub elements
    Children are just elements and not text nodes
    Text made up of whitespace is ignored

*/

function XMLDoc(){
  
  var xmldoc = null;

  this.setXML = function(xmlstr){
    try { //Internet Explorer
      xmldoc=new ActiveXObject("Microsoft.XMLDOM");
      xmldoc.async="false";
      xmldoc.loadXML(xmlstr);
    }
    catch(e){
      try { //Firefox, Mozilla, Opera, etc.
          parser=new DOMParser();
          xmldoc=parser.parseFromString(xmlstr,"text/xml");
      } 
      catch(e){
          alert(e.message);
          return;
      }
    }
  }

  this.getRoot = function(){
    return new XMLElement(xmldoc.documentElement);
  }

}

function XMLElement(node){
  
  var thisnode = node;
  var childArray = new Array();
  var mytext = "";
  
  //////////////////////////////////////////////////////////////////////////////
  this.getName = function(){
    return thisnode.nodeName;
  }
  //////////////////////////////////////////////////////////////////////////////
  this.hasAttribute = function(attrName){
    return !(thisnode.attributes.getNamedItem(attrName)==null);
  }
  //////////////////////////////////////////////////////////////////////////////
  this.getAttribute = function(attrName){
    var attrs = thisnode.attributes;
    if (!this.hasAttribute(attrName)) return null;
    return attrs.getNamedItem(attrName).nodeValue
  }
  //////////////////////////////////////////////////////////////////////////////
  this.hasChildren = function(){
    return (childArray.length>0);
  }
  //////////////////////////////////////////////////////////////////////////////
  this.getChildren = function(){
    var retarr = new Array();
    for (var i=0; i<childArray.length; i++){
        retarr.push(new XMLElement(childArray[i]));
    }
    return retarr;
  }
  //////////////////////////////////////////////////////////////////////////////
  // Get a list of child elements of a given element name
  //////////////////////////////////////////////////////////////////////////////
  this.getChildrenByName = function(name){
    var retarr = new Array();
    for (var i=0; i<childArray.length; i++){
        if (childArray[i].nodeName == name){
           retarr.push(new XMLElement(childArray[i]));
        }
    }
    return retarr;
  }
  //////////////////////////////////////////////////////////////////////////////
  // Get a child element by name.  This is a convenience method for situations
  // where you are expecting only a single returned element.  It's convenient 
  // because you don't have to dereference an array when you only have a single
  // return value.
  //
  // An exception will be thrown if there are more than one match;
  //
  //////////////////////////////////////////////////////////////////////////////
  this.getChildByName = function(name){
    var arr = this.getChildrenByName(name);
    if (arr.length>1){
       throw new Exception("getChildByName found more than one element");
    }
    if (arr.length==0) return null;
    return arr[0];
  }
  //////////////////////////////////////////////////////////////////////////////
  this.hasText = function(){
    return (mytext.length>0);
  }
  //////////////////////////////////////////////////////////////////////////////
  this.getText = function(){
    return mytext;
  }
  
  //parse the node
  for (var i=0; i<thisnode.childNodes.length; i++){
      parseNode(thisnode.childNodes.item(i));
  }
  //trim the text;
  mytext = mytext.replace(/^\s+|\s+$/g,"");

  function parseNode(node){
    switch (node.nodeType){
      case 1:
           childArray.push(node);
           break;
      case 3:
           mytext = mytext+node.nodeValue+" ";
           break;
      default:
           alert("Unknown node type "+node.nodeType+" in XMLElement.parseNode");
    }
  }
  
  
}

