//////////////////////////////
// Images
//////////////////////////////
function newImage(arg)
{
        if (document.images)
        {
                rslt = new Image();
                rslt.src = arg;
                return rslt;
        }
}

//x_over = newImage("images/treeview_plus.gif");

//////////////////////////////
// CTreeNode
//////////////////////////////
function CTreeNode(parent, htmlObject, imgObject)
{
        // Properties
        this.parent = parent;
        this.children = new Array();
        this.htmlObject = htmlObject;
        this.imgObject = imgObject;

        this.expanded = true;
        this.hasChildren = false;
        this.firstChild = false;

        // Methods
        this.AddChild = CTreeNode_AddChild;
        this.FindNode = CTreeNode_FindNode;
        this.ExpandChildren = CTreeNode_ExpandChildren;
        this.ShowChildren = CTreeNode_ShowChildren;
}

function CTreeNode_AddChild(childNode)
{
        this.children[this.children.length] = childNode;
}

function CTreeNode_FindNode(htmlObject, curNode)
{
        if (this.htmlObject == htmlObject)
        {
                return this;
        }
        else
        {
                var result = null;
                for (var i in this.children)
                {
                        if (result = this.children[i].FindNode(htmlObject))
                        {
                                return result;
                        }
                }
                return result;
        }
}

function CTreeNode_ShowChildren(show)
{
        for (var i in this.children)
        {
                if (!show || this.children[i].expanded)
                {
                        this.children[i].ShowChildren(show);
                }

                htmlChildObject = this.children[i].htmlObject;
                if (show)
                {
                        htmlChildObject.style.display = "";
                }
                else
                {
                        htmlChildObject.style.display = "none";
                }
        }

}

function CTreeNode_ExpandChildren()
{
        if ((this.imgObject != null) && this.hasChildren)
        {
                this.expanded = !this.expanded;
                this.ShowChildren(this.expanded);
                this.imgObject.src = this.expanded ? "/i/icon_minus.gif" : "/i/icon_plus.gif";
        }
}

var g_nodes = new CTreeNode(null, null, null);

function AddChild(parentHtmlObject, htmlObject, imageObject)
{
        var parentNode;
        parentNode = g_nodes.FindNode(parentHtmlObject);
        if (parentNode)
        {
                newObject = new CTreeNode(parentNode, htmlObject, imageObject)
                parentNode.AddChild(newObject);
                if (!parentNode.hasChildren)
                {
                    if (parentNode.imgObject)
                    {
                        parentNode.imgObject.src = "/i/icon_minus.gif";
                    }
                    parentNode.hasChildren = true;
                    newObject.firstChild = true;
                }
                else
                {
                    newObject.firstChild = false;
                }
                return true;
        }
        return false;
}

function expandit(htmlObject, imageObject)
{
        var node = g_nodes.FindNode(htmlObject);
        if (node)
        {
                node.ExpandChildren();
        }
}

function CollapseRoot()
{
        for (var i in g_nodes.children)
        {
                g_nodes.children[i].ExpandChildren();
        }
}
