var objLocalTree = null;

function jsTree() {
    this.root = null;      
    this.nodes = new Array;       
    objLocalTree = this;
}

jsTree.prototype.createRoot = function(strName, strVal) {    
    this.root = new jsTreeNode(strName, strVal);    
    this.root.id = "root";   
    this.nodes["root"] = this.root;       
    this.root.expanded = true;    
    return this.root;
}

function jsTreeNode(strName, strVal) {    
    this.nodename = strName;          
    this.nodeval = strVal;            
    this.childNodes = new Array;    
}

jsTreeNode.prototype.addChild = function (strName, strVal) {    
    var objNode = new jsTreeNode(strName, strVal);   
    objNode.id = this.id + "_" + this.childNodes.length;    
    objNode.indent = this.indent + 1;    
    this.childNodes[this.childNodes.length] = objNode;    
    objLocalTree.nodes[objNode.id] = objNode;    
    return objNode;
}

jsTree.prototype.popCat2 = function(list,str,def) {   
    while (list.options.length > 0) {                        
       var eachGroup = list.firstChild;           
       if(eachGroup) {                     
           list.removeChild(eachGroup);  
       } else {        
         list.remove(0);
       }
    }
    list.options.length = 0;     
    
    if(def != "") {
        var objOption = new Option(def, "");
        list.options[list.options.length]=objOption;
    }

    this.root.findNode(list,str,0,1);   
}

function fillCombo(list,node) {
   var hasOptGroups = false;
   var optGroup = null;
   for (var i=0; i < node.childNodes.length; i++) {        
        nodeName = node.childNodes[i].nodename;
        nodeValue = node.childNodes[i].nodeval;       
        if(nodeName.indexOf("head")!=-1) {
            hasOptGroups = true;
            optGroup = document.createElement('optgroup');
            optGroup.label = nodeValue;
            list.appendChild(optGroup);            
        } else {
                        
            var objOption = new Option(nodeValue,nodeName);
            if(hasOptGroups)
                optGroup.appendChild(objOption);
            else            
                list.options[list.options.length]=objOption;
        }            
        fillCombo(list,node.childNodes[i]);        
    }    
}

jsTreeNode.prototype.findNode = function (list,nodeName,actDepth,maxDepth) {    

    if(this.nodename == nodeName) {                
        fillCombo(list,this);
        return this;
    }

    if(actDepth < maxDepth){
        actDepth = actDepth + 1;
        for (var i=0; i < this.childNodes.length; i++) {                  
            if(this.childNodes[i].findNode(list,nodeName,actDepth,maxDepth) != null) {           
                return this;   
            }
        }
    }
    return null;
}




