﻿// JScript File



// Value of obj is retrieved by the below code for IE and Firefox both browsers.
// Wherever there parentElement, it has been replaced by parentNode as parentElement does not work in firefox.

function client_OnTreeNodeChecked (evt)
{ 
    // obj gives us the node on which check or uncheck operation has performed

    var obj = window.event != window.undefined ? window.event.srcElement : evt.target;
    
    var treeNodeFound = false;

    var checkedState;

    //checking whether obj consists of checkbox to avoid exception

    if ((obj.tagName == "INPUT" && obj.type == "checkbox") || obj.tagName == "A" || obj.tagName == "IMG")
    {
        var treeNode = obj;
        var objchk = obj;
        
        if(obj.tagName == "A")
        {
            var treeNode = obj.parentNode.firstChild;
        }
        
        if(obj.tagName == "IMG")
        {
            //var treeNode = obj.parentNode.parentNode.nextSibling.firstChild;
            var treeNode = obj.parentNode.parentNode.firstChild;
        }

        checkedState = treeNode.checked;
        
        //work our way back to the parent <table> element

        do
        { obj = obj.parentNode; }
        
        while(obj.tagName != "TABLE")
        
        var parentTreeLevel = obj.rows[0].cells.length;
        
        var parentTreeNode = obj.rows[0].cells[0];
        
        //get all the TreeNodes inside the TreeView (the parent <div>)

        var tables = obj.parentNode.getElementsByTagName("TABLE");
        
        //checking for any node is checked or unchecked during operation

        if(obj.tagName == "TABLE")
        {
            // if any node is unchecked then their parent node are unchecked

            if (!treeNode.checked)
            { 
                goDeeper(false, obj); 
            } 
            //end if - unchecked

            //total number of TreeNodes

            var numTables = tables.length

            if(numTables >= 1)
            {
                //cycle through all the TreeNodes

                //until we find the TreeNode we checked

                for (i=0; i < numTables; i++)
                { 
                    if (tables[i] == obj)
                    { 
                        treeNodeFound = true;

                        i++;

                        if(i == numTables)
                        {
                            //if we're on the last TreeNode, we are done 
                            break; 
                        } 
                    }
                    
                    if(treeNodeFound == true)
                    {
                        var childTreeLevel = tables[i].rows[0].cells.length;

                        if(childTreeLevel > parentTreeLevel)
                        {
                            var cell = tables[i].rows[0].cells[childTreeLevel - 1];

                            //set the checkbox to match the checkedState

                            var inputs = cell.getElementsByTagName("INPUT");

                            if(objchk.tagName == "INPUT")
                            {
                                inputs[0].checked = checkedState; 
                            }

                            if(checkedState == true && (objchk.tagName == "A" || objchk.tagName == "IMG" ))
                            {
                                inputs[0].checked = checkedState; 
                            }
                        }

                        else 
                        { 
                            //if any of the preceding TreeNodes are not deeper stop 
                            break; 
                        } 
                    } 
                    //end if 
                }
                //end for 
            } 
            //end if - numTables >= 1

            // if all child nodes are checked then their parent node is checked

            if(treeNode.checked) 
            { 
                goDeeperChecked(obj); 
            }
            //end if - checked 
        } 
        //end if - tagName = TABLE 
    } //end if 
} //end function
                

    //********************************************************************************

    //Internally it calls these two functions to check and uncheck nodes(you dont need to call them) -

    function goDeeperChecked(obj)
    {
        var chk1 = true;

        //Get the mom.

        var head1 = obj.parentNode.previousSibling;

        //no Table, cant do my work.

        if(head1.tagName != "TABLE")
        {
            return ;
        }


        //This is how may rows are at this level.

        var pTreeLevel1

        if(obj.rows == null || obj.rows == "undefined")
        {
            pTreeLevel1 = head1.rows[0].cells.length;
        }
        else
        {
            pTreeLevel1 = obj.rows[0].cells.length;
        }

        //Are we a mommy?

        if(head1.tagName == "TABLE")
        {
            //Get the list of rows ahead of us.

            var tbls = obj.parentNode.getElementsByTagName("TABLE");

            //get the count of that list.

            var tblsCount = tbls.length;

            //determine if any of the rows underneath are unchecked.

            for(i=0; i < tblsCount; i++)
            {
                var childTreeLevel = tbls[i].rows[0].cells.length;

                if(childTreeLevel = pTreeLevel1)
                {
                    var chld = tbls[i].getElementsByTagName("INPUT");

                    if(chld[0].checked == false)
                    { 
                        chk1 = false; 
                        break; 
                    } 
                } 
            }

            var nd = head1.getElementsByTagName("INPUT");

            nd[0].checked = chk1;

            //do the same for the level above

            goDeeperChecked(obj.parentNode);

        }
        else 
        { 
            return; 
        } 
    }

    //********************************************************************************

    function goDeeper(check, obj)
    {
    
        //head1 gets the parent node of the unchecked node

        var head = obj.parentNode.previousSibling;

        if(head.tagName == "TABLE")
        {
            //checks for the input tag which consists of checkbox

            var matchElement = head.getElementsByTagName("INPUT");

            //matchElement1[0] gives us the checkbox and it is unchecked

            matchElement[0].checked = false; 
        }

        else
        { 
            head = obj.parentNode.previousSibling; 
        }

        if(head.tagName == "TABLE")
        { 
            goDeeper(check, obj.parentNode); 
        }

        else
        { 
            return; 
        } 
    } 


    // Validate TreeNodeChecked function to check if atleast one node in the TreeView control is selected.
    function ValidateTreeNodeChecked(obj)
    {
        var treeNodeFound = false;

        var node = obj.parentNode.getElementsByTagName("INPUT");
        var nodeCount = node.length;
        //alert(cnt);
        
        for(i=0; i < nodeCount-1; i++)
        {
            //alert(node[i].checked);
            if(node[i].checked == true)
            {
                treeNodeFound = true;
                break;
            }
        }

        //alert(treeNodeFound);
        return treeNodeFound;
    }
    

