// --------------- HtmlReader --------------------------------------------
function HtmlReader(aDomDocument)
{
  this.document = aDomDocument;
  this.xStat = new StatisticFactory().createStatistic('HtmlReader');
  this.setRootNode(this.document);
}

HtmlReader.prototype.setRootNode=function(aDocument)
{
 this.xStat.incCall('setRootNode');
 if (aDocument != null)
 {
    this.rootNode = aDocument.documentElement;
    
    if(this.rootNode != null)
    {
     //alert("RootNode initialized "+this.rootNode.nodeName);
    }
  }
  this.xStat.endCall('setRootNode');
}

HtmlReader.prototype.getRootNode=function()
{
    return this.rootNode;
}

HtmlReader.prototype.getText=function(aNode)
{
  this.xStat.incCall('getText');
	if(aNode != null && aNode['childNodes'] != null && aNode['childNodes'].length >0)
	{
	  var theValue = aNode['childNodes'][0]['nodeValue'];
		this.xStat.endCall('getText');
		return theValue;
	}
	this.xStat.endCall('getText');
	return null;
}

HtmlReader.prototype.getFirstNodeByPath=function(aPath)
{
  return this.getNode(this.rootNode, aPath);
}

HtmlReader.prototype.getNodesByPath=function(aPath)
{
  return this.getNodes(this.rootNode, aPath);
}

HtmlReader.prototype.getNode=function(aParentNode, aPath)
{
	this.xStat.incCall('getNode');
	
  var thePath = this.getPathArray(aPath);
  //alert("Path array"+thePath.length);
  var theValue = this.getNodeByArray(aParentNode, thePath);
  this.xStat.endCall('getNode');
  return theValue;
}
 
HtmlReader.prototype.getNodeByArray=function(aParentNode, aPath)
{  
  var theParentNode = aParentNode;
  this.xStat.incCall('getNodeByArray');
  var theL=aPath.length;
  var i = 0;
  for (i = 1; i<theL; i++)
  {
  	//alert("Get node by array inspecting: "+theParentNode.nodeName);
    if (theParentNode != null && theParentNode.hasChildNodes())
    {
      theParentNode = this.getFirstChildNode(theParentNode, aPath[i]);
    }
    else
    { 
      this.xStat.endCall('getNodeByArray');
      return null;
    }
  }
  this.xStat.endCall('getNodeByArray');
  return theParentNode;
}



HtmlReader.prototype.getFirstChildNode=function(aParentNode, aNodeName)
{
  if (aParentNode == null)
  {
    return null;
  }
  this.xStat.incCall('getFirstChildNode');
  var theChildNodes = aParentNode['childNodes'];
  var theReturnNode = null;
  var i = 0;
  var theLength=theChildNodes.length;
  for (i = 0; i < theLength; i++)
  {
    if (theChildNodes[i]['nodeName'] == aNodeName)
    {
      this.xStat.endCall('getFirstChildNode');
      return theChildNodes[i];
    }
  }
  return null;
}

HtmlReader.prototype.getNodes=function(aParentNode, aPath)
{
  this.xStat.incCall('getNodes');
  var thePathArray = this.getPathArray(aPath);  
  // reducing path
  var theNewPath = new Array();
  var i = 0;
  var theLength = thePathArray.length-1;
  for (i=0;i<theLength; i++)
  {
  	theNewPath[i]=thePathArray[i];
  }
  //alert("Get nodes from: "+aParentNode.nodeName+" path:"+aPath);
  var theNode = this.getNode(aParentNode, theNewPath);
  var theValue = this.getChildNodes(theNode, thePathArray[thePathArray.length-1]);
  this.xStat.endCall('getNodes');
  return theValue;
}

HtmlReader.prototype.getChildNodes=function(aNode, aNodeName)
{
 if (aNode == null || aNodeName == null || !aNode.hasChildNodes())
 {
 	return null;
 }
 this.xStat.incCall('getChildNodes');
 var theNodes = aNode['childNodes'];
 var i = 0;
 var theNodeArray = new Array();
 for (i = 0; i<theNodes.length;i++)
 {
 	if (theNodes[i]['nodeName'] == aNodeName)
 		theNodeArray[theNodeArray.length] = theNodes[i];
 }
 this.xStat.endCall('getChildNodes');
 return theNodeArray;
 
}

HtmlReader.prototype.getAllChildNodesByNodeName=function(aNode, aNodeName)
{
 if (aNode == null || !aNode.hasChildNodes())
 {
 	return null;
 }
 this.xStat.incCall('getAllChildNodesByNodeName');
 var theNodes = aNode.childNodes;
 var i = 0;
 var theNodeArray = new Array();
 for (i = 0; i<theNodes.length;i++)
 {
   if (aNodeName==null || theNodes[i].nodeName == aNodeName)
 	  theNodeArray[theNodeArray.length] = theNodes[i];
 }
 this.xStat.endCall('getAllChildNodesByNodeName');
 return theNodeArray;
}

HtmlReader.prototype.getAllChildNodes=function(aNode)
{
  return this.getAllChildNodesByNodeName(aNode, null);
}

HtmlReader.prototype.printNodeList=function(aNode)
{
  var theNodeList = this.getAllChildNodes(aNode);
  var i;
  var theList='';
  if(theNodeList !=null && theNodeList.length > 0)
  {
  for(i=0;i<theNodeList.length;i++)
  {
    theList+='\n' + theNodeList[i].nodeName;
    theList+=this.printNodeList(theNodeList[i]);
  }
  }
  return theList;
}  

HtmlReader.prototype.getNodeAttributeValue=function(aNode, aAttributeName)
{
  if(aNode == null)
  {
    return null;
  }
  else
  {
    var theNode = this.getNodeAttribute(aNode, aAttributeName);
    if (theNode != null)
    {
      return theNode.nodeValue;
    }
  }
  //alert("Found Attributes:"+theLog);
  return null;
}

HtmlReader.prototype.getNodeAttribute=function(aNode, aAttributeName)
{
  if(aNode == null)
  {
    return null;
  }
  this.xStat.incCall('getNodeAttribute');
  var theAttributes = aNode.attributes;
  aAttributeName =aAttributeName.toLowerCase();
  if (theAttributes != null && theAttributes.length > 0)
  {
    var i;
   
    for (i = 0; i<theAttributes.length; i++)
    {
      if (theAttributes[i].nodeName.toLowerCase() == aAttributeName)
      {
        this.xStat.endCall('getNodeAttribute');
        return theAttributes[i];
      }
    }
  }
  this.xStat.endCall('getNodeAttribute');
  return null;
}
  
  
 HtmlReader.prototype.getNodeByAttributeValue=function(aParentNode, aPath, aAttributeName, aValue)
 {
   this.xStat.incCall('getNodeByAttributeValue');
   var theNodeList = this.getNodes(aParentNode, aPath);
   if (theNodeList != null)
   {
     var i;
     var theNext;
     var theValue;
     for (i=0; i<theNodeList.length;i++)
     {
       theNext = theNodeList[i];
       theValue = this.getNodeAttributeValue(theNext, aAttributeName);
       if(theValue == aValue)
       {
         this.xStat.endCall('getNodeByAttributeValue');
         return theNext;
       }
     }
  }
  this.xStat.endCall('getNodeByAttributeValue');
  return null;
  
}


HtmlReader.prototype.getPathArray=function(aPath)
{
	this.xStat.incCall('getPathArray');
  //alert("get Path array entered for path "+aPath);
  var theArray = new Array();
  var theEnd = false;
  var theIndex = -1;
  var theNext = null;
  var theRest = aPath;
  //alert("The rest inited "+theRest);
  while (!theEnd)
  {
  	//alert("while entered");
	if(!theRest.indexOf)
	{
		//alert(theRest+" has no indexof method");
		theIndex = -1;
	}
	else
    	theIndex = theRest.indexOf(".");
		
	//alert("Index of dot:"+theIndex);
    if (theIndex <= 0)
    {
      theEnd=true;
	  theArray[theArray.length]=theRest;
    }
    else
    {
      theNext = theRest.substring(0,theIndex);
      theArray[theArray.length] = theNext;
      theRest = theRest.substring(theIndex+1);
    }  
  }
  this.xStat.endCall('getPathArray');
  return theArray;
}// JavaScript Document
