var _treeCurrentId=null;
function treeHandler(e) {
    e = e?e:window.event;
    var o = e[window.event?'srcElement':'target'];
    var oLi;
 //if( o.tagName == 'INPUT' && o.getAttribute('type') == 'checkbox' &&  o.parentNode.parentNode.className.indexOf('open') !=-1 ) 
 
 if( o.tagName == 'INPUT' && o.getAttribute('type') == 'checkbox' ) 
    checkAll( e, o.parentNode.parentNode);
   // alert(o.tagName  + "  " + o.type + " " +o.parentNode.parentNode.className);  

/*
 some magic: in IE; clickable area belonged to the parent block; when A display:block mode 
so there was no way to know which optin the user wanted to click.
use ie elementFromPoint function to simulate user clicking 40px to the right and use that as the
element clicked
*/
if( o.tagName=="UL" || o.tagName == 'DIV'  && document.elementFromPoint ) 
  o =  document.elementFromPoint( window.event.clientX+60,window.event.clientY );

 
    if (o.tagName == 'A')   oLi = o.parentNode;
    else if (o.tagName == 'LI')  oLi = o;
    else 
    return;
   
     var oFc = _firstChild( oLi );

        if (oLi.className.indexOf('collapse') != -1) {
             oLi.className = oLi.className.replace('collapsed', 'open');
            oFc.className = oFc.className.replace('collapsed', 'open');
             }    
    else       if (oLi.className.indexOf('open') != -1) {
             oLi.className = oLi.className.replace('open', 'collapsed');
            oFc.className = oFc.className.replace('open', 'collapsed');
           
    }    
   var bLoadOnDemand = ( oLi.className.indexOf('loadOnDemand') != -1 );


    var loc = location.href;
    if( location.hash ) loc = loc.replace(location.hash,'');
     
    oFc.blur();
   if ( ( oFc.href.indexOf( loc )!= -1 && oFc.hash=='')  || oFc.href.indexOf('void')>-1 || oFc.hash == "#" || bLoadOnDemand  ){
        
       // stop bubbling
        if ( e.preventDefault ) e.preventDefault(); //Netscape, Gecko
         else e.returnValue = false; //IE
   }         

  if( bLoadOnDemand )    setTimeout(function(){treeLoadXml( oFc.href ,oLi)},10);
 
};

function treeCollapseAll( sTreeId ){
  treeActivate( sTreeId ,'collapsed') ;
  return false;
};

function treeExpandAll( sTreeId ){
  treeActivate( sTreeId ,'open') ;
  return false;
};

function treeActivate( treeId ,sDefaultStyle,bLoadOnDemand) {
   var oTree;
   if( typeof( treeId ) == 'string' ) 
    oTree = document.getElementById(treeId);
   else
      oTree = treeId;
   
    var oUls = oTree.getElementsByTagName("UL");
    var l = oUls.length;
    // No tree get out
    if( l == 0 ) return;
   bLoadOnDemand=bLoadOnDemand||false;

    sDefaultStyle=sDefaultStyle||'open';
    sDefaultStyle=sDefaultStyle.toLowerCase();
     
    sDefaultStyle = 'open:collapsed'.indexOf(sDefaultStyle) == -1 ?'collapsed':sDefaultStyle;
    if( ! bLoadOnDemand )
    oTree.onclick = function(event){ treeHandler(event)};
    
    for (var i = 1; i < l; i++) {
   window.status = "Working on section: " +i + "  of  " + l ;
        var oParent = oUls[i].parentNode; //LI item
       
   if( oParent.className.indexOf('open') != -1 || oParent.className.indexOf('collapsed') != -1  )
          oParent.className = oParent.className.replace(oParent.className, sDefaultStyle);
   else
          oParent.className += ' ' +  sDefaultStyle;
   
   oFc = _firstChild(oParent);
   
   if( oFc.className.indexOf('open') != -1 || oFc.className.indexOf('collapsed') != -1  )
          oFc.className = oFc.className.replace(oFc.className, sDefaultStyle);
   else
          oFc.className += ' ' +  sDefaultStyle;
     
        var oNs = _nextSibling(oParent);
        
        if( ! oNs)
             oParent.className +=  (i==1?'First':'Last');  /*you got no section you must be either the first or last expander*/
        else
         oUls[i].className = 'section';
             
       
        var oLc = _lastChild(oUls[i]);         
        if( oLc ) oLc.className='last';
        
    }
       
       oLc = _lastChild(oUls[0]);         
       if( oLc && oLc.getElementsByTagName('UL')[0] == null ) 
           oLc.className='last';
    
    var oEls = oTree.getElementsByTagName("LI");
    var el;
     l= oEls.length;
     var subMenus;    
     for( var i=0;i < l;i++){
     el = oEls[i];
     subMenus = el.getElementsByTagName('UL')[0];
     if( subMenus == null ){
        el.className = "element";
     }
     }    
window.status = "";
          
};


var ELEMENT_NODE = 1;
function _nextSibling(o){
 do
 {
    o = o.nextSibling;
   } while( o && o.nodeType != ELEMENT_NODE  ) 
     return o;
};

function _previousSibling(o){
 do
 {
    o = o.previousSibling;
   } while( o && o.nodeType != ELEMENT_NODE  ) 
  return o;
};

function _lastChild(o){
  o = o.lastChild;
   while( o && o.nodeType != ELEMENT_NODE ){
     o = o.previousSibling;
   }
   
    
 return o;
} ;

function _firstChild(o){
  o = o.firstChild;
   while( o && o.nodeType != ELEMENT_NODE ){
     o = o.nextSibling;
   }
 return o;
};

function showTitle(e){
 o = (e?e:window.event)[window.event?'srcElement':'target']; //
if(o.tagName=='A' && ! o.title ) o.title=o.innerHTML.replace(/<[^>]+>/g,"");

 };
 
 
 
function checkAll( e,ID ) {
    var el = window.event?event.srcElement:e.target;
    var toggle = el.checked;
    var picked = ID.getElementsByTagName('INPUT');
    var j=0;
    var l = picked.length;
    for (i = 1; i < l; ++i) {
       if( picked[i].type == 'checkbox' ){
           picked[i].checked = toggle;
	       if ( picked[i].checked  ){j++; }
       }
    }
  
};

