var USE_TEXT_LINKS  = 1;
var START_ALL_OPEN  = 0;
var USE_FRAMES      = 1;
var USE_ICONS       = 1;
var WRAP_TEXT       = 0;
var STATE           = 0;
var ICON_PATH       = 'images/';
var HIGHLIGHT       = 1;
var HIGHLIGHT_COLOR = '#FFFFFF';
var HIGHLIGHT_BG    = '#000000';
var BUILD_ALL       = 0;
var BROWSER_VERSION = 0; 
var NUM_ENTRIES     = 0;
var INDEX_ENTRIES   = new Array;
var LAST_CLICKED = null;
var LAST_CLICKED_COLOR;
var LAST_CLICKED_BGCOLOR;
var LAST_OPENED_NODE = null;
var DOC = document;
var SUPPORT_DEFERRAL = false;
var COOKIE_CUTTER = '^';
DOC.yPos = 0;

function TNode( description, hreference ) 
{ 
  this.desc          = description; 
  this.link          = hreference;
  this.id            = -1;
  this.navObj        = 0;
  this.iconImg       = 0; 
  this.nodeImg       = 0;
  this.isLastNode    = 0;
  this.iconSrc       = ICON_PATH + "open.gif";
  this.iconSrcClosed = ICON_PATH + "close.gif";
  this.children      = new Array;
  this.nChildren     = 0;
  this.level         = 0;
  this.leftSideCoded = "";
  this.isLastNode    = false;
  this.parentObj     = null;
  this.maySelect     = true;
  this.prependHTML   = "";
 
  this.isOpen            = false;
  this.isLastOpenedTNode = false;
  this.isRendered        = 0;
 
  this.initialize     = initTNode 
  this.setState       = setStateTNode 
  this.addChild       = addChild 
  this.createIndex    = createEntryIndex 
  this.escondeBlock   = escondeBlock
  this.esconde        = escondeTNode 
  this.tnodeMstr      = tnodeMstr 
  this.renderOb       = drawTNode 
  this.totalHeight    = totalHeight 
  this.subEntries     = tnodeSubEntries 
  this.linkHTML       = linkTNodeHTML
  this.blockStartHTML = blockStartHTML
  this.blockEndHTML   = blockEndHTML
  this.nodeImageSrc   = nodeImageSrc
  this.iconImageSrc   = iconImageSrc
  this.getID          = getID
  this.openParentNode = openParentNode
} 
 
function initTNode( level, lastNode, leftSide ) 
{ 
  var j = 0;
  var i = 0;       
  nc = this.nChildren; 
   
  this.createIndex(); 
  this.level = level;
  this.leftSideCoded = leftSide;

  if( BROWSER_VERSION == 0 || 
      START_ALL_OPEN == 1 )
      this.isOpen = true;

  if( level > 0 )
      if( lastNode )
          leftSide = leftSide + "0";
  else
      leftSide = leftSide + "1";

  this.isLastNode = lastNode;
 
  if( nc > 0 ) 
  { 
      level = level + 1; 
      for( i=0 ; i < this.nChildren; i++ )  
      { 
          if( i == this.nChildren-1 ) 
              this.children[i].initialize( level, 1, leftSide );
          else 
              this.children[i].initialize( level, 0, leftSide );
      } 
  } 
} 
 
function drawTNode( insertAtObj ) 
{ 
  var nodeName = "";
  var auxEv = "";
  var DOCW = "";
  var leftSide = leftSideHTML( this.leftSideCoded );

  if( BROWSER_VERSION > 0 ) 
      auxEv = "<a href='javascript:clickOnNode( \"" + this.getID()+ "\" )'>"; 
  else 
      auxEv = "<a>"; 

  nodeName = this.nodeImageSrc();

  if( this.level> 0 ) 
      if( this.isLastNode ) 
          leftSide = leftSide + "<td valign=top>" + auxEv + "<img name='nodeIcon" + this.id + "' id='nodeIcon" + this.id + "' src='" + nodeName + "' width=16 height=22 border=0></a></td>"
    else 
          leftSide = leftSide + "<td valign=top background=" + ICON_PATH + "vertical_line.gif>" + auxEv + "<img name='nodeIcon" + this.id + "' id='nodeIcon" + this.id + "' src='" + nodeName + "' width=16 height=22 border=0></a></td>"

  this.isRendered = 1

  if( BROWSER_VERSION == 2 ) 
  { 
    if( !DOC.yPos ) 
      DOC.yPos=20 
  } 

  DOCW = this.blockStartHTML( "tnode" );
  DOCW = DOCW + "<tr>" + leftSide + "<td valign=top>";
  
  if( USE_ICONS )
  {
    DOCW = DOCW + this.linkHTML( false ) 
    DOCW = DOCW + "<img id='tnodeIcon" + this.id + "' name='tnodeIcon" + this.id + "' src='" + this.iconImageSrc() + "' border=0></a>"
  }
  else
  {
    if( this.prependHTML == "" )
        DOCW = DOCW + "<img src=" + ICON_PATH + "blank.gif height=2 width=2>"
  }
  
  if( WRAP_TEXT )
      DOCW = DOCW + "</td>"+this.prependHTML+"<td valign=middle width=100%>"
  else
      DOCW = DOCW + "</td>"+this.prependHTML+"<td valign=middle nowrap width=100%>"
      
  if( USE_TEXT_LINKS ) 
  { 
      DOCW = DOCW + this.linkHTML( true ) 
      DOCW = DOCW + this.desc + "</a>"
  } 
  else 
      DOCW = DOCW + this.desc
      
  DOCW = DOCW + "</td>"

  DOCW = DOCW + this.blockEndHTML()

  if( insertAtObj == null )
  {
      if( SUPPORT_DEFERRAL ) {
          DOC.write( "<div id=domRoot></div>" ) 
          insertAtObj = getElById( "domRoot" )
          insertAtObj.insertAdjacentHTML( "beforeEnd", DOCW )
    }
    else
          DOC.write( DOCW )
  }
  else
  {
      insertAtObj.insertAdjacentHTML( "afterEnd", DOCW )
  }
 
  if( BROWSER_VERSION == 2 ) 
  { 
    this.navObj = DOC.layers["tnode"+this.id] 
    if( USE_ICONS )
      this.iconImg = this.navObj.document.images["tnodeIcon"+this.id] 
    this.nodeImg = this.navObj.document.images["nodeIcon"+this.id] 
    DOC.yPos=DOC.yPos+this.navObj.clip.height 
  } 
  else if( BROWSER_VERSION != 0 )
  { 
    this.navObj = getElById( "tnode"+this.id )
    if( USE_ICONS )
      this.iconImg = getElById( "tnodeIcon"+this.id ) 
    this.nodeImg = getElById( "nodeIcon"+this.id )
  } 
} 
 
function setStateTNode( isOpen ) 
{ 
  var subEntries 
  var totalHeight 
  var fIt = 0 
  var i=0 
  var currentOpen
 
  if( isOpen == this.isOpen ) 
    return 
 
  if( BROWSER_VERSION == 2 )  
  { 
    totalHeight = 0 
    for( i=0; i < this.nChildren; i++ ) 
      totalHeight = totalHeight + this.children[i].navObj.clip.height 
      subEntries = this.subEntries() 
    if( this.isOpen ) 
      totalHeight = 0 - totalHeight 
    for( fIt = this.id + subEntries + 1; fIt < NUM_ENTRIES; fIt++ ) 
      INDEX_ENTRIES[fIt].navObj.moveBy( 0, totalHeight ) 
  }  
  this.isOpen = isOpen;

  if( this.getID()!=myTree.getID() && STATE && !this.isOpen )
  {
     currentOpen = GetCookie( "clickedTNode" )
     if( currentOpen != null ) {
         currentOpen = currentOpen.replace( this.getID()+COOKIE_CUTTER, "" )
         SetCookie( "clickedTNode", currentOpen )
     }
  }
  
  if( !this.isOpen && this.isLastOpenedtnode )
  {
    LAST_OPENED_NODE = null;
    this.isLastOpenedtnode = false;
  }
  propagateChangesInState( this ) 
} 
 
function propagateChangesInState( tnode ) 
{   
  var i=0 

  if( tnode.nChildren > 0 && tnode.level>0 )  
    tnode.nodeImg.src = tnode.nodeImageSrc()

  if( USE_ICONS )
    tnode.iconImg.src = tnode.iconImageSrc()

  for( i=tnode.nChildren-1; i>=0; i-- ) 
    if( tnode.isOpen ) 
      tnode.children[i].tnodeMstr( tnode.navObj )
    else 
      tnode.children[i].esconde() 
} 
 
function escondeTNode() 
{ 
  this.escondeBlock()
   
  this.setState( 0 ) 
} 
 
function linkTNodeHTML( isTextLink ) 
{ 
  var DOCW = "";

  if( this.link ) 
  { 
  if( USE_FRAMES )
    // canasai's comment out 
    // DOCW = DOCW + "<a href='" + this.link + "' TARGET=\"coreFrame\" "
    DOCW = DOCW + "<a href='" + this.link + "' TARGET=\"coreFrame\" "    
  else
    DOCW = DOCW + "<a href='" + this.link + "' TARGET=_top "
        
    if( isTextLink ) {
        DOCW += "id=\"itemTextLink"+this.id+"\" ";
    }

    if( BROWSER_VERSION > 0 ) 
      DOCW = DOCW + "onClick='javascript:clickOnTNode( \""+this.getID()+"\" )'"

    DOCW = DOCW + ">"
  } 
  else 
    DOCW = DOCW + "<a>" 

  return DOCW;
} 
 
function addChild( childNode ) 
{ 
  this.children[this.nChildren] = childNode 
  childNode.parentObj = this
  this.nChildren++ 
  return childNode 
} 
 
function tnodeSubEntries() 
{ 
  var i = 0 
  var se = this.nChildren 
 
  for( i=0; i < this.nChildren; i++ ){ 
    if( this.children[i].children ) 
      se = se + this.children[i].subEntries() 
  } 
 
  return se 
} 

function nodeImageSrc() 
{
  var srcStr = "";

  if( this.isLastNode ) 
  { 
      if( this.nChildren == 0 )
          srcStr = ICON_PATH + "corner_line.gif"
      else
      if( this.isOpen )
          srcStr = ICON_PATH + "minus_lastnode.gif"  
      else
          srcStr = ICON_PATH + "plus_lastnode.gif"  
  } 
  else 
  { 
      if( this.nChildren == 0 )
          srcStr = ICON_PATH + "separate_line.gif"
      else
      if( this.isOpen )
          srcStr = ICON_PATH + "minus_node.gif"
      else
         srcStr = ICON_PATH + "plus_node.gif"
  }   
  return srcStr;
}

function iconImageSrc() 
{
  if( this.isOpen )
      return( this.iconSrc )
  else
      return( this.iconSrcClosed )
} 
 

 
function Item( itemDescription, itemLink, target )
{ 
  this.desc = itemDescription 
  this.link = itemLink    
  this.id = -1 
  this.navObj = 0 
  this.iconImg = 0 
  this.iconSrc = ICON_PATH + "close.gif" 
  this.isRendered = 0
  this.isLastNode = false
  this.level = 0
  this.leftSideCoded = ""
  this.nChildren = 0
  this.target = target
  this.parentObj = null
  this.maySelect=true
  this.prependHTML = ""
 
  this.initialize = initializeItem 
  this.createIndex = createEntryIndex 
  this.escondeBlock = escondeBlock
  this.esconde = escondeBlock
  this.tnodeMstr = tnodeMstr 
  this.renderOb = drawItem 
  this.totalHeight = totalHeight 
  this.blockStartHTML = blockStartHTML
  this.blockEndHTML = blockEndHTML
  this.getID = getID
  this.openParentNode = openParentNode
} 
 
function initializeItem( level, lastNode, leftSide ) 
{  
  this.createIndex() 
  this.level = level
  this.leftSideCoded = leftSide
  this.isLastNode = lastNode
} 
 
function drawItem( insertAtObj ) 
{ 
  var leftSide = leftSideHTML( this.leftSideCoded )
  var DOCW = ""
  // Canasai's comment out
  //var fullLink = "href=\""+this.link+"\" target=\""+this.target+"\" onClick=\"clickOnLink( '"+this.getID()+"\', '"+this.link+"','"+this.target+"' );return false;\"";
  var fullLink = "href=\""+this.link+"\" target=\""+this.target+"\" ";  
  this.isRendered = 1

  if( this.level>0 ) 
    if( this.isLastNode )
    { 
      leftSide = leftSide + "<td valign=top><img src='" + ICON_PATH + "corner_line.gif' width=16 height=22></td>"
    } 
    else 
    { 
      leftSide = leftSide + "<td valign=top background=" + ICON_PATH + "vertical_line.gif><img src='" + ICON_PATH + "separate_line.gif' width=16 height=22></td>"
    } 

  DOCW = DOCW + this.blockStartHTML( "item" )

  DOCW = DOCW + "<tr>" + leftSide + "<td valign=top>"
  if( USE_ICONS )
      DOCW = DOCW + "<a " + fullLink  + " id=\"itemIconLink"+this.id+"\">" + "<img id='itemIcon"+this.id+"' " + "src='"+this.iconSrc+"' border=0>" + "</a>"
  else
    if( this.prependHTML == "" )
        DOCW = DOCW + "<img src=" + ICON_PATH + "blank.gif height=2 width=3>"

  if( WRAP_TEXT )
    DOCW = DOCW + "</td>"+this.prependHTML+"<td valign=middle width=100%>"
  else
    DOCW = DOCW + "</td>"+this.prependHTML+"<td valign=middle nowrap width=100%>"

  if( USE_TEXT_LINKS ) 
    DOCW = DOCW + "<a " + fullLink + " id=\"itemTextLink"+this.id+"\">" + this.desc + "</a>"
  else 
    DOCW = DOCW + this.desc

  DOCW = DOCW + "</td>"

  DOCW = DOCW + this.blockEndHTML()
 
  if( insertAtObj == null )
  {
    DOC.write( DOCW )
  }
  else
  {
      insertAtObj.insertAdjacentHTML( "afterEnd", DOCW )
  }

  if( BROWSER_VERSION == 2 ) { 
    this.navObj = DOC.layers["item"+this.id] 
    if( USE_ICONS )
      this.iconImg = this.navObj.document.images["itemIcon"+this.id] 
    DOC.yPos=DOC.yPos+this.navObj.clip.height 
  } else if( BROWSER_VERSION != 0 ) { 
    this.navObj = getElById( "item"+this.id )
    if( USE_ICONS )
      this.iconImg = getElById( "itemIcon"+this.id )
  } 
} 
 
 

function openParentNode() {
  if( this.parentObj == null || this.parentObj.isOpen )
    return
  else {
    this.parentObj.openParentNode()
    clickOnNodeObj( this.parentObj )
  }
}

function escondeBlock() 
{ 
  if( BROWSER_VERSION == 1 || BROWSER_VERSION == 3 ) { 
    if( this.navObj.style.display == "none" ) 
      return 
    this.navObj.style.display = "none" 
  } else { 
    if( this.navObj.visibility == "hidden" ) 
      return 
    this.navObj.visibility = "hidden" 
  }     
} 
 
function tnodeMstr( domObj ) 
{ 
  if( !this.isRendered )
     this.renderOb( domObj )
  else
    if( BROWSER_VERSION == 1 || BROWSER_VERSION == 3 ) 
      this.navObj.style.display = "block" 
    else 
      this.navObj.visibility = "show" 
} 

function blockStartHTML( idprefix ) {
  var idParam = "id='" + idprefix + this.id + "'"
  var DOCW = ""

  if( BROWSER_VERSION == 2 ) 
    DOCW = "<layer "+ idParam + " top=" + DOC.yPos + " visibility=show>"
  else if( BROWSER_VERSION != 0 )
    DOCW = "<div " + idParam + " style='display:block; position:block;'>"
     
  DOCW = DOCW + "<table border=0 cellspacing=0 cellpadding=0 width=100% >"

  return DOCW
}

function blockEndHTML() {
  var DOCW = ""

  DOCW = "</table>"
   
  if( BROWSER_VERSION == 2 ) 
    DOCW = DOCW + "</layer>"
  else if( BROWSER_VERSION != 0 )
    DOCW = DOCW + "</div>"

  return DOCW
}
 
function createEntryIndex() 
{ 
  this.id = NUM_ENTRIES;
  INDEX_ENTRIES[NUM_ENTRIES] = this;
  NUM_ENTRIES++; 
} 
 

function totalHeight() 
{ 
  var h = this.navObj.clip.height 
  var i = 0 
   
  if( this.isOpen )
    for( i=0 ; i < this.nChildren; i++ )  
      h = h + this.children[i].totalHeight() 
 
  return h 
} 


function leftSideHTML( leftSideCoded ) 
{
  var i;
  var retStr = "";

  for( i=0; i < leftSideCoded.length; i++ )
  {
      if( leftSideCoded.charAt( i ) == "1" )
      {
          retStr = retStr + "<td valign=top background=" + ICON_PATH + 
                   "vertical_line.gif><img src='" + ICON_PATH + "vertical_line.gif' width=16 height=22></td>"
      }
      if( leftSideCoded.charAt( i ) == "0" )
      {
          retStr = retStr + "<td valign=top><img src='" + ICON_PATH + "blank.gif' width=16 height=22></td>"
      }
  }
  
  return retStr
}

function getID()
{
  if( typeof this.xID != "undefined" ) 
    return this.xID
  else
    return this.id
}

 

function clickOnTNode( tnodeId ) 
{ 
  var clicked = findObj( tnodeId )

  if( !clicked.isOpen ) 
  {
      clickOnNodeObj( clicked ) 
  }

  if( LAST_OPENED_NODE != null && LAST_OPENED_NODE != tnodeId )
      clickOnNode( LAST_OPENED_NODE ); 

  if( clicked.nChildren==0 ) 
  {
      LAST_OPENED_NODE = tnodeId;
      clicked.isLastOpenedtnode = true
  }

  if( isLinked( clicked.hreference ) ) 
  {
      //highlightObjLink( clicked );
  }
} 
 
function clickOnNode( tnodeId ) 
{ 
  clickOnNodeObj( findObj( tnodeId ) )
}

function clickOnNodeObj( tnodeObj ) 
{ 
  var state = 0 
  var currentOpen
 
  state = tnodeObj.isOpen 
  tnodeObj.setState( !state ) 

  if( tnodeObj.id!=myTree.id && STATE )
  {
      currentOpen = GetCookie( "clickedTNode" )
      if( currentOpen == null )
          currentOpen = ""

      if( !tnodeObj.isOpen ) 
      {
          currentOpen = currentOpen.replace( tnodeObj.getID()+COOKIE_CUTTER, "" )
          SetCookie( "clickedTNode", currentOpen )
      }
      else
          SetCookie( "clickedTNode", currentOpen+tnodeObj.getID()+COOKIE_CUTTER )
  }
}

function clickOnLink( clickedId, target, windowName ) 
{  
  highlightObjLink( findObj( clickedId ) );

  if( isLinked( target ) ) 
      window.open( target, windowName );

}


 
function findObj( id )
{
  var i=0;
  var nodeObj;
  if( typeof myTree.xID != "undefined" ) {
      nodeObj = INDEX_ENTRIES[i];
      for( i=0;i<NUM_ENTRIES&&INDEX_ENTRIES[i].xID!=id;i++ ) 
      ;
      id = i
  }
  if( id >= NUM_ENTRIES )
      return null; 
  else
      return INDEX_ENTRIES[id];
}

function isLinked( hrefText ) {
    var result = true;
    result = ( result && hrefText !=null );
    result = ( result && hrefText != '' );
    result = ( result && hrefText.indexOf( 'undefined' ) < 0 );
    result = ( result && hrefText.indexOf( 'parent.op' ) < 0 );
    return result;
}


function highlightObjLink( nodeObj ) {
  if( !HIGHLIGHT || nodeObj==null || nodeObj.maySelect==false ) {
    return;
  }

  if( BROWSER_VERSION == 1 || BROWSER_VERSION == 3 ) {
    var clickedDOMObj = getElById( 'itemTextLink'+nodeObj.id );
    if( clickedDOMObj != null ) {
        if( LAST_CLICKED != null ) {
            var prevClickedDOMObj = getElById( 'itemTextLink'+LAST_CLICKED.id );
            prevClickedDOMObj.style.color=LAST_CLICKED_COLOR;
            prevClickedDOMObj.style.backgroundColor=LAST_CLICKED_BGCOLOR;
        }
        
        LAST_CLICKED_COLOR    = clickedDOMObj.style.color;
        LAST_CLICKED_BGCOLOR  = clickedDOMObj.style.backgroundColor;
        clickedDOMObj.style.color=HIGHLIGHT_COLOR;
        clickedDOMObj.style.backgroundColor=HIGHLIGHT_BG;
    }
  }
  LAST_CLICKED = nodeObj;
  if( STATE )
    SetCookie( 'highlightedTreeviewLink', nodeObj.getID() );
}

function genNode( description, hreference ) 
{ 
  tnode = new TNode( description, hreference );
  return tnode;
} 
 
function genLeaf( optionFlags, description, linkData ) 
{ 
  var fullLink = "";
  var targetFlag = "";
  var target = "";
  var protocolFlag = "";
  var protocol = "";

  if( optionFlags>=0 ) 
  {
    return oldGLnk( optionFlags, description, linkData )
  }

  targetFlag = optionFlags.charAt( 0 )
  if( targetFlag=="B" )
    target = "_blank"
  if( targetFlag=="P" )
    target = "_parent"
  if( targetFlag=="R" )
    target = "coreFrame"
  if( targetFlag=="S" )
    target = "_self"
  if( targetFlag=="T" )
    target = "_top"
 if( targetFlag=="M" )
    target = "coreFrame"
    
    
  if( optionFlags.length > 1 ) {
    protocolFlag = optionFlags.charAt( 1 )
    if( protocolFlag=="h" )
      protocol = "http://"
    if( protocolFlag=="s" )
      protocol = "https://"
    if( protocolFlag=="f" )
      protocol = "ftp://"
    if( protocolFlag=="m" )
      protocol = "mailto:"
  }

  fullLink = "'" + protocol + linkData + "' target=" + target 

  linkItem = new Item( description, protocol+linkData, target )
  return linkItem 
} 


function oldGLnk( target, description, linkData )
{
  var fullLink = "";

  if( USE_FRAMES )
  {
    if( target==0 ) 
    { 
    fullLink = "'"+linkData+"' target=\"coreFrame\"" 
    } 
    else 
    { 
    if( target==1 ) 
       fullLink = "'http://"+linkData+"' target=_blank" 
    else 
       if( target==2 )
        fullLink = "'http://"+linkData+"' target=\"coreFrame\"" 
       else
        fullLink = linkData+" target=\"_top\"" 
    } 
  }
  else
  {
    if( target==0 ) 
    { 
    fullLink = "'"+linkData+"' target=_top" 
    } 
    else 
    { 
    if( target==1 ) 
       fullLink = "'http://"+linkData+"' target=_blank" 
    else 
       fullLink = "'http://"+linkData+"' target=_top" 
    } 
  }

  linkItem = new Item( description, fullLink )   
  return linkItem 
}
 
function putNode( parentTNode, childTNode ) 
{ 
  return parentTNode.addChild( childTNode ) 
} 
 
function putLeaf( parentTNode, document ) 
{ 
  return parentTNode.addChild( document ) 
} 

function preLoadIcons() {
  var auxImg
  auxImg = new Image();
  auxImg.src = ICON_PATH + "vertical_line.gif";
  auxImg.src = ICON_PATH + "minus_lastnode.gif";
  auxImg.src = ICON_PATH + "minus_node.gif";
  auxImg.src = ICON_PATH + "plus_lastnode.gif";
  auxImg.src = ICON_PATH + "plus_node.gif";
  auxImg.src = ICON_PATH + "blank.gif";
  auxImg.src = ICON_PATH + "corner_line.gif";
  auxImg.src = ICON_PATH + "separate_line.gif";
  auxImg.src = ICON_PATH + "close.gif";
  auxImg.src = ICON_PATH + "open.gif";
  auxImg.src = ICON_PATH + "DOC.gif";
}


function setInitialLayout() {
  if( BROWSER_VERSION > 0 && !START_ALL_OPEN )
    clickOnNodeObj( myTree );
  
  if( !START_ALL_OPEN && ( BROWSER_VERSION > 0 ) && STATE )
    PersistentTNodeOpening();
}


function renderAllTree( nodeObj, parent ) {
  var i=0;
  nodeObj.renderOb( parent )
  if( SUPPORT_DEFERRAL )
    for( i=nodeObj.nChildren-1; i>=0; i-- ) 
      renderAllTree( nodeObj.children[i], nodeObj.navObj )
  else
    for( i=0 ; i < nodeObj.nChildren; i++ ) 
      renderAllTree( nodeObj.children[i], null )
}

function hideWholeTree( nodeObj, hideThisOne, nodeObjMove ) {
  var i=0;
  var heightContained=0;
  var childrenMove=nodeObjMove;

  if( hideThisOne )
    nodeObj.escondeBlock()

  if( BROWSER_VERSION == 2 )
    nodeObj.navObj.moveBy( 0, 0-nodeObjMove )

  for( i=0 ; i < nodeObj.nChildren; i++ ) {
    heightContainedInChild = hideWholeTree( nodeObj.children[i], true, childrenMove )
    if( BROWSER_VERSION == 2 ) {
      heightContained = heightContained + heightContainedInChild + nodeObj.children[i].navObj.clip.height
      childrenMove = childrenMove + heightContainedInChild
  }
  }

  return heightContained;
}

if( typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement ){
  HTMLElement.prototype.insertAdjacentElement = function ( where,parsedNode )
  {
    switch ( where ){
    case 'beforeBegin':
      this.parentNode.insertBefore( parsedNode,this )
      break;
    case 'afterBegin':
      this.insertBefore( parsedNode,this.firstChild );
      break;
    case 'beforeEnd':
      this.appendChild( parsedNode );
      break;
    case 'afterEnd':
      if( this.nextSibling ) 
        this.parentNode.insertBefore( parsedNode,this.nextSibling );
      else this.parentNode.appendChild( parsedNode );
      break;
    }
  }

  HTMLElement.prototype.insertAdjacentHTML = function( where,htmlStr )
  {
    var r = this.ownerDocument.createRange();
    r.setStartBefore( this );
    var parsedHTML = r.createContextualFragment( htmlStr );
    this.insertAdjacentElement( where,parsedHTML )
  }
}

function getElById( idVal ) 
{
  if( document.getElementById != null )
      return document.getElementById( idVal )
  if( document.all != null )
      return document.all[ idVal ]
  
  alert( "Problem getting element by id" )
  return null
}



function PersistentTNodeOpening()
{
  var stateInCookie;
  var fldStr=""
  var fldArr
  var fldPos=0
  var id
  var nodeObj
  stateInCookie = GetCookie( "clickedTNode" );
  SetCookie( 'clickedTNode', "" ) 

  if( stateInCookie!=null )
  {
    fldArr = stateInCookie.split( COOKIE_CUTTER )
    for( fldPos=0; fldPos<fldArr.length; fldPos++ )
    {
      fldStr=fldArr[fldPos]
      if( fldStr != "" ) {
        nodeObj = findObj( fldStr )
        if( nodeObj!=null ) 
          if( nodeObj.setState ) {
            nodeObj.openParentNode()
            clickOnNodeObj( nodeObj );
          }
          else
            alert( "Internal id is not pointing to a tnode anymore. Consider using external IDs" )
      }
    }
  }
}

function storeAllNodesInClickCookie( treeNodeObj )
{
  var currentOpen
  var i = 0

  if( typeof treeNodeObj.setState != "undefined" ) 
  {
      currentOpen = GetCookie( "clickedTNode" )
      if( currentOpen == null )
          currentOpen = ""

      if( treeNodeObj.getID() != myTree.getID() )
          SetCookie( "clickedTNode", currentOpen+treeNodeObj.getID()+COOKIE_CUTTER )

      for( i=0; i < treeNodeObj.nChildren; i++ ) 
          storeAllNodesInClickCookie( treeNodeObj.children[i] )
  }
}

function CookieBranding( name ) {
  if( typeof myTree.treeID != "undefined" )
      return name+myTree.treeID 
  else
      return name
}
 
function GetCookie( name )
{  
  name = CookieBranding( name )

  var arg = name + "=";  
  var alen = arg.length;  
  var clen = document.cookie.length;  
  var i = 0;  

  while ( i < clen ) 
  {    
      var j = i + alen;    
      if( document.cookie.substring( i, j ) == arg )      
          return getCookieVal( j );    
      i = document.cookie.indexOf( " ", i ) + 1;    
      if( i == 0 ) 
          break;   
  }  
  return null;
}

function getCookieVal( offset ) {  
  var endstr = document.cookie.indexOf ( ";", offset );  
  if( endstr == -1 )    
      endstr = document.cookie.length;  
  return unescape( document.cookie.substring( offset, endstr ) );
}

function SetCookie( name, value ) 
{  
  var argv = SetCookie.arguments;  
  var argc = SetCookie.arguments.length;  
  var expires = ( argc > 2 ) ? argv[2] : null;  
  var domain = ( argc > 4 ) ? argv[4] : null;  
  var secure = ( argc > 5 ) ? argv[5] : false;  
  var path = "/"; 

  name = CookieBranding( name )

  document.cookie = name + "=" + escape ( value ) + 
  ( ( expires == null ) ? "" : ( "; expires=" + expires.toGMTString() ) ) + 
  ( ( path == null ) ? "" : ( "; path=" + path ) ) +  
  ( ( domain == null ) ? "" : ( "; domain=" + domain ) ) +    
  ( ( secure == true ) ? "; secure" : "" );
}

function ExpireCookie ( name ) 
{  
  var exp = new Date();  
  exp.setTime ( exp.getTime() - 1 );  
  var cval = GetCookie ( name );  
  name = CookieBranding( name )
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}




function detectUserBrowser()
{
  //var oldOnError = window.onerror;
  //var element = null;
  //window.onerror = null;
  // work around bug in xpcdom Mozilla 0.9.1
  //window.saveNavigator = window.navigator;

  navigator.OS      = '';
  navigator.version = parseFloat( navigator.appVersion );
  navigator.org     = '';
  navigator.family  = '';

  var platform;
  if( typeof( window.navigator.platform ) != 'undefined' )
  {
      platform = window.navigator.platform.toLowerCase();

      if( platform.indexOf( 'win' ) != -1 )
          navigator.OS = 'win';
      else 
      if( platform.indexOf( 'mac' ) != -1 )
          navigator.OS = 'mac';
      else 
      if( platform.indexOf( 'unix' ) != -1 || 
          platform.indexOf( 'linux' ) != -1 || 
          platform.indexOf( 'sun' ) != -1 )
          navigator.OS = 'nix';
  }

  var i = 0;
  var userAgent = window.navigator.userAgent.toLowerCase();

  if( userAgent.indexOf( 'safari' ) != -1 )
  {
      i = userAgent.indexOf( 'safari' );
      navigator.family = 'safari';
      navigator.org = 'safari';
      navigator.version = parseFloat( '0' + userAgent.substr( i + 7 ), 10 );
  }
  else 
  if( userAgent.indexOf( 'opera' ) != -1 )
  {
      i = userAgent.indexOf( 'opera' );
      navigator.family  = 'opera';
      navigator.org = 'opera';
      navigator.version  = parseFloat( '0' + userAgent.substr( i + 6 ), 10 );
  }
  else 
  if( ( i = userAgent.indexOf( 'msie' ) ) != -1 )
  {
      navigator.org    = 'microsoft';
      navigator.version  = parseFloat( '0' + userAgent.substr( i+5 ), 10 );
    
      if( navigator.version < 4 )
          navigator.family = 'ie3';
      else
          navigator.family = 'ie4'
  }
  else 
  if( userAgent.indexOf( 'gecko' ) != -1 )
  {
      navigator.family = 'gecko';
      var rvStart = userAgent.indexOf( 'rv:' );
      var rvEnd   = userAgent.indexOf( ' )', rvStart );
      var rv      = userAgent.substring( rvStart+3, rvEnd );
      var rvParts = rv.split( '.' );
      var rvValue = 0;
      var exp     = 1;

      for( var i = 0; i < rvParts.length; i++ )
      {
          var val = parseInt( rvParts[i] );
          rvValue += val / exp;
          exp *= 100;
      }
    navigator.version = rvValue;

    if( userAgent.indexOf( 'netscape' ) != -1 )
        navigator.org = 'netscape';
    else 
    if( userAgent.indexOf( 'compuserve' ) != -1 )
        navigator.org = 'compuserve';
    else
        navigator.org = 'mozilla';
  }
  else 
  if( ( userAgent.indexOf( 'mozilla' ) !=-1 ) && 
      ( userAgent.indexOf( 'spoofer' ) == -1 ) && 
      ( userAgent.indexOf( 'compatible' ) == -1 ) && 
      ( userAgent.indexOf( 'opera' ) == -1 ) && 
      ( userAgent.indexOf( 'webtv' ) == -1 ) && 
      ( userAgent.indexOf( 'hotjava' ) == -1 ) )
  {
      var is_major = parseFloat( navigator.appVersion );
    
      if( is_major < 4 )
          navigator.version = is_major;
      else
      {
          i = userAgent.lastIndexOf( '/' )
          navigator.version = parseFloat( '0' + userAgent.substr( i + 1 ), 10 );
      }
      navigator.org = 'netscape';
      navigator.family = 'nn' + parseInt( navigator.appVersion );
  }
  else 
  if( ( i = userAgent.indexOf( 'aol' ) ) != -1 )
  {
    // aol
    navigator.family  = 'aol';
    navigator.org     = 'aol';
    navigator.version = parseFloat( '0' + userAgent.substr( i + 4 ), 10 );
  }
  else 
  if( ( i = userAgent.indexOf( 'hotjava' ) ) != -1 )
  {
    // hotjava
    navigator.family  = 'hotjava';
    navigator.org     = 'sun';
    navigator.version = parseFloat( navigator.appVersion );
  }
  //window.onerror = oldOnError;
}


function initMain() 
{ 
  preLoadIcons();
  detectUserBrowser();
  switch( navigator.family )
  {
    case 'ie4':
      BROWSER_VERSION = 1 //Simply means IE > 3.x
      break;
    case 'opera':
      BROWSER_VERSION = ( navigator.version > 6 ? 1 : 0 ); //opera7 has a good DOM
      break;
    case 'nn4':
      BROWSER_VERSION = 2 //NS4.x 
      break;
    case 'gecko':
      BROWSER_VERSION = 3 //NS6.x
      break;
    case 'safari':
      BROWSER_VERSION = 1 //Safari Beta 3 seems to behave like IE in spite of being based on Konkeror
      break;
    default:
      BROWSER_VERSION = 0 //other, possibly without DHTML  
      break;
  }

  SUPPORT_DEFERRAL = ( ( navigator.family == 'ie4' && navigator.version >= 5 && navigator.OS != "mac" ) || BROWSER_VERSION == 3 );
  SUPPORT_DEFERRAL = SUPPORT_DEFERRAL & ( !BUILD_ALL )
  
  if( !USE_FRAMES && BROWSER_VERSION == 2 )
      BROWSER_VERSION = 0;

  if( STATE && GetCookie( "clickedTNode" ) != null )
      START_ALL_OPEN = 0

  myTree.initialize( 0, true, "" ) 
  if( SUPPORT_DEFERRAL && !START_ALL_OPEN )
      myTree.renderOb( null ) 
  else 
  {
      renderAllTree( myTree, null );

      if( STATE && START_ALL_OPEN )
          storeAllNodesInClickCookie( myTree )

    if( BROWSER_VERSION == 2 ) 
      DOC.write( "<layer top=" + INDEX_ENTRIES[NUM_ENTRIES-1].navObj.top + ">&nbsp;</layer>" ) 

    if( BROWSER_VERSION != 0 && !START_ALL_OPEN )
      hideWholeTree( myTree, false, 0 )
  }

  setInitialLayout()

  if( STATE && GetCookie( 'highlightedTreeviewLink' ) != null  && GetCookie( 'highlightedTreeviewLink' ) != "" ) 
  {
      var nodeObj = findObj( GetCookie( 'highlightedTreeviewLink' ) )
      if( nodeObj!=null )
      {
          nodeObj.openParentNode()
          highlightObjLink( nodeObj );
      }
      else
          SetCookie( 'highlightedTreeviewLink', '' )
  }
} 
 


function expandTree(tnodeObj)
{
  var childObj;
  var i;


  if (!tnodeObj.isOpen)
    parent.treeFrame.clickOnNodeObj(tnodeObj)

  for (i=0 ; i < tnodeObj.nChildren; i++)  {
    childObj = tnodeObj.children[i]
    if (typeof childObj.setState != "undefined") {
    expandTree(childObj)
    }
  }
}


function collapseTree()
{

  parent.treeFrame.clickOnNodeObj(parent.treeFrame.myTree)
  //restore first level
  parent.treeFrame.clickOnNodeObj(parent.treeFrame.myTree)
}


function openTNodeInTree(linkID) 
{
  var tnodeObj;
  tnodeObj = parent.treeFrame.findObj(linkID);
  tnodeObj.openParentNode();
  if (!tnodeObj.isOpen)
  parent.treeFrame.clickOnNodeObj(tnodeObj);
} 





