/*******************************************************
 *      Set of methods reperesents tree hierarchy structure.
 *******************************************************/
// adds a child to current parent
function addChild(child)
{
        this.childs[this.childs.length++] = child;
}

// adds a child to specified parent
function addChildToParent(parentid, child)
{
        par = this.lookup(parentid);
        if (par != null)
        {
                par.addChild(child);
        }
}

// return all childs belonged to parent
function getChilds()
{
        res = new Array();
        
        for (i in this.childs)
        {
                res[i] = this.childs[i];
        }
        
        for (i in this.childs)
        {
                res = res.concat(this.childs[i].getChilds());
        }
        
        return res;
}

// lookup item starting from current parent
// return an item or null if not found
function lookup(trid)
{
        if (this.trid == trid)
        {
                return this;
        }

        for (i in this.childs)
        {
                res = this.childs[i].lookup(trid);
                if (res != null)
                {
                        return res;
                }
        }
        
        return null;
}

// constructor for tree item. takes an item id
// which should exists in document hierarchy
// inistate - boolean flag, false if item should be initially collapsed
function rowtree(trid, inistate)
{
        this.trid = trid;
        this.isopen = true;
        this.inistate = inistate;
        this.childs = new Array(0);
        this.lookup = lookup;
        this.addChild = addChild;
        this.addChildToParent = addChildToParent;
        this.getChilds = getChilds;
}

var root = new rowtree('root', true);

function manageItem(rowid)
{
        foundrow = root.lookup(rowid);
        
        if (foundrow != null)
        {
        
                collapse = foundrow.isopen;
                arr = foundrow.getChilds();
                
                for (i in arr)
                {       
                        document.all[(arr[i].trid)].style.display = collapse ? "none" : "";

                        childImg = document.images['ImgTarif' + (arr[i].trid)];
                        
                        if (childImg != null)
                        {
                                childImg.src = collapse ? "etel.files/blue_arrow_vniz.gif" : "etel.files/blue_arrow.gif";
                        }
                        
                        arr[i].isopen = !collapse;
                }

                if (document.images('ImgTarif' + rowid) != null)
                {
                        document.images['ImgTarif' + rowid].src = collapse ? "etel.files/blue_arrow_vniz.gif" : "etel.files/blue_arrow.gif";
                }
                foundrow.isopen = !collapse;
        }
}

function setupTree()
{
        arr = root.getChilds();
        st = new Array();
        
        // collect all items to be collapsed
        for (i in arr)
        {
                if (!arr[i].inistate)
                {
                        st[st.length++] = arr[i].trid;
                }
        }

        for (i in st)
        {
                manageItem(st[i]);
        }
}