function find(nodeName, chkStart)
{
    var tree = top.treeview.mainTree;
    var found = false;
    if (chkStart.checked || tree.o_selected == 0)
        startIndex = 0;
    else
        startIndex = tree.o_selected.n_id + 1;
    for (i=startIndex; i<tree.a_index.length; i++)
    {
        if (tree.a_index[i].a_config[0].toLowerCase().indexOf(nodeName.toLowerCase()) != -1)
        {
            //we have a match - expand out all the parent nodes and
            //select the matching node
            found = true;
            expandToNode(tree.a_index[i], tree);
            tree.a_index[i].select();
            scrollTree(tree.a_index[i], tree);
            break;
        }
    }
    
    if (found)
		chkStart.checked = false;
	else
    {
        alert("No more matches");
        chkStart.checked = true;
    }
}

function expandToNode(node, tree)
{
    if (node.o_parent != tree.o_root)
    {
        count = expandToNode(node.o_parent, tree);
		node.o_parent.open(0,1);
	}
}

function scrollTree(node, tree)
{
    var numOpenedNodes=0;
    var nodeIndex = 0;
    //Calculate the number of visible elements in the tree and the index of the target element
    //from amongst the visible elements
    for (i=0; i<tree.a_index.length; i++){
        //we need to count every element that is visible - either a top level node or something with an open parent node
        if (tree.a_index[i].o_parent == tree.o_root || tree.a_index[i].o_parent.n_state & 8)
        {
            numOpenedNodes++;
        }
        if (node.n_id == i)
        {
            nodeIndex = numOpenedNodes;
        }
    }
    docElement = parent.treeview.document.documentElement;
    if (docElement.scrollHeight > docElement.clientHeight)
    {
        //Calculate all the variables we need for determining location
        var nodeHeight = docElement.scrollHeight / numOpenedNodes;
        var nodesInClient = docElement.clientHeight / nodeHeight;
        var newScrollPos = Math.floor((nodeIndex - nodesInClient / 2) * nodeHeight);
        var nodePos = nodeIndex * nodeHeight;
        //if the node is not currently visible in the client rectangle
        if (nodePos < docElement.scrollTop || nodePos > docElement.scrollTop + docElement.clientHeight)
        {
            //set the scroll position, but make sure it's within the bounds of scrolling
            if (newScrollPos <= 0)
                docElement.scrollTop = 0;
            else if (newScrollPos >= docElement.scrollHeight - docElement.clientHeight)
                docElement.scrollTop = docElement.scrollHeight - docElement.clientHeight;
            else
                docElement.scrollTop = newScrollPos;
        }
    }
}
