﻿//Counts all cheched nodes.
var checkedNodes = 0;
//Reference to the tree object
var tree;

//Initialize the treevariable 
//Fired when Tree is initialized on client-side.
function init(treeID) {

    tree = igtree_getTreeById(treeID)
    //CheckNode(null, null);
/*    
    var Nodes = tree.getNodes();



    for (var i = 0; i < Nodes.length; i++) {

        // expand all collapse all root nodes. true/false

        Nodes[i].setExpanded(false);


    }   
*/

}
/*
function Collapse() {


    var Nodes
    if (tree != null) {
        Nodes = tree.getNodes();
        if (Nodes != null) {
            for (var i = 0; i < Nodes.length; i++) {

                // expand all collapse all root nodes. true/false

                Nodes[i].setExpanded(false);

            }
        }
    }
    CheckNode(null, null);
}

*/
function CheckNode(sender, nodeId) {

    //Gets reference to the WebDropDown
    //var dropdown = $find("WebDropDown1");
    
    // Gets the UltraWebTree reference  and utilize the client - side API
    //var tree = igtree_getTreeById(sender);
    var Nodes
    checkedNodes = 0;
    if (tree != null) {
        Nodes = tree.getNodes();
        if (Nodes != null) {
            //checkedNodes = 0;
            //enumerate top level nodes
            for (var i = 0; i < Nodes.length; i++) {

                var nodes
                nodes = Nodes[i].getChildNodes();
                if (nodes != null) {
                    for (var n = 0; n < nodes.length; n++) {

                        var rootNode = nodes[n];
                        checked = rootNode.getChecked();
                        if (checked) {
                            checkedNodes++;
                        }
                    }
                }
            }
        }
     }  
    var dropdown = $find("WebDropDown1");
    if (dropdown != null) {
        //Sets the text of the value display.
        dropdown.set_currentValue(checkedNodes + ' occupational areas selected', true);
    }
    
}


/*
//Recursive invocation for eache Node of the tree
function CheckNode(sender, nodeId) {
    //function InitializeTreeHandler() {

    // Gets the UltraWebTree reference  and utilize the client - side API

    var Nodes = tree.getNodes();
    
    for (var i = 0; i < Nodes.length; i++) {
        
        CountNodes(Nodes[i]);
    }

    //Show the number of checked Nodes.
    //alert("Checked nodes: "+checkedNodes);

    var dropdown = $find("WebDropDown1");

    //Sets the text of the value display.
    dropdown.set_currentValue(checkedNodes, true);

    //reset the count
    checkedNodes = 0;
}


//Recursive check of all nodes of the tree.
function CountNodes(rNode) {

    if (rNode == null) {
        return;
    }

    var checked
    if (rNode.getLevel() == 1) {
        checked = rNode.getChecked();
        if (checked) {
            checkedNodes++;
        }
        if (rNode.getLevel() == 0) {
    var nodes = rNode.getChildNodes();

    for (var n = 0; n < nodes.length; n++) {

        var rootNode = nodes[n];

        CountNodes(rootNode);
    }

    }
    }
/*
    var nodes = rNode.getChildNodes();

    for (var n = 0; n < nodes.length; n++) {

        var rootNode = nodes[n];

        CountNodes(rootNode);

    }

} //end

function UltraWebTree1_NodeChecked(treeId, nodeId, bChecked) {
    //Add code to handle your event here.
    
    checkedNodes = 0;
    var node = igtree_getNodeById(nodeId);
    if (node.getLevel() == 1) {
        
        var nodes = new Array();
        nodes = node.getChildNodes();
        for (i = 0; i < nodes.length; i++) {
            if (nodes[i].getChecked == true) {
                checkedNodes++;
            }
            //nodes[i].setChecked(bChecked);
        }
    }
    var dropdown = $find("WebDropDown1");

    //Sets the text of the value display.
    dropdown.set_currentValue(checkedNodes, true);
    //nodes_onclick(treeId);
}

function nodes_onclick(treeId) {
    var tree = igtree_getTreeById(treeId);
    var nodes = new Array();
    count = 0;
    nodes = tree.getNodes();
    for (i = 0; i < nodes.length; i++) {
        count++;
    }
    var dropdown = $find("WebDropDown1");

    //Sets the text of the value display.
    dropdown.set_currentValue(count, true);
}
*/