//XMLParser.js
//Base xml parser object.
//includes some utility functions.
function XMLParser(newXmlDoc)
{
  return this.init(newXmlDoc);
};

XMLParser.prototype.init = function (newXmlDoc)
{
  this.xmlDoc = newXmlDoc||null;   // XmlDocument
  if (this.xmlDoc == null)
    return (this);
};

XMLParser.prototype.setDocumentByDoc = function(newXmlDoc)
{
  if (!newXmlDoc) 
    return null;
  else
    this.xmlDoc = newXmlDoc;
};

XMLParser.prototype.setDocumentByStr = function(XMLParserStr)
{
  if (!XMLParserStr) 
    return;
  if (this.xmlDoc == null)
    this.xmlDoc = XmlDocument.create();
  this.xmlDoc.loadXML(XMLParserStr);
};

/**********************************************************
              Generic data handling
***********************************************************/
XMLParser.decodeSTRING = function(theNode)
{
  if(theNode)
    if (theNode.childNodes.length > 0) // has textNode
      return (theNode.childNodes[0].nodeValue);
  return ('');
};
XMLParser.prototype.decodeSTRING = XMLParser.decodeSTRING;
XMLParser.prototype.decodeINT = XMLParser.decodeINT = function(theNode) // leaf node
{ 
  var theSTRING = XMLParser.decodeSTRING(theNode);
  return (parseInt(theSTRING,10));
};

XMLParser.prototype.decodeBOOLEAN = XMLParser.decodeBOOLEAN = function(theNode)
{
  var theSTRING = this.decodeSTRING(theNode);
  return (parseInt(theSTRING) == 1);
};

XMLParser.prototype.decodeDOUBLE = XMLParser.decodeDOUBLE = function(theNode)
{
  var theSTRING = this.decodeSTRING(theNode);
  return (parseFloat(theSTRING));
};

XMLParser.prototype.decodeDATETIME = function(theNode) {};
XMLParser.prototype.decodeBASE64 = function(theNode) {};

/**********************************************************
                  Helper Functions
***********************************************************/

XMLParser.prototype.getNextChildNodeIndex = 
XMLParser.prototype.__helper__getNextElementChildNode = 
XMLParser.getNextElementChildNode = function(aNode,firstpos)
{
  var foundpos = -1;
  for (var lcv=firstpos;(lcv < aNode.childNodes.length) && (foundpos == -1); lcv++)
    if (aNode.childNodes[lcv].nodeType == 1)
      foundpos = lcv;
  return (foundpos);
};

XMLParser.prototype.getNextNamedChildNodeIndex = 
XMLParser.prototype.getNextElementNamedChildNode = 
XMLParser.prototype.__helper__getNextElementNamedChildNode = 
XMLParser.getNextElementNamedChildNode = function(aNode,firstpos,nodeName)
{
  for (var lcv=firstpos;lcv < aNode.childNodes.length; lcv++)
    if ((aNode.childNodes[lcv].nodeType == 1) && (aNode.childNodes[lcv].nodeName == nodeName))
      return(lcv);
  return (-1);
};

XMLParser.prototype.getRemainingNamedChildNodes =
XMLParser.prototype.__helper__getRemainingNextElementNamedChildNodes =
XMLParser.getRemainingNextElementNamedChildNodes = function(aNode,firstpos,nodeName)
{
  var nodeCollection = new Array();
  for (var lcv=firstpos;lcv < aNode.childNodes.length; lcv++)
    if ((aNode.childNodes[lcv].nodeType == 1) && (aNode.childNodes[lcv].nodeName == nodeName))
      nodeCollection[nodeCollection.length] = aNode.childNodes[lcv];
  return (nodeCollection);
};

