﻿// JScript File
function client_OnTreeNodeChecked(event)
{
    // obj gives us the node on which check or uncheck operation has performed
    var obj = event.srcElement || event.target;
    var treeNodeFound = false; 
    var checkedState; 
    //checking whether obj consists of checkbox or not 
    if (obj.tagName == "INPUT" && obj.type == "checkbox") 
    {
        //easier to read
        var treeNode = obj;        
        //record the checked state of the TreeNode 
        checkedState = treeNode.checked; 
        //work our way back to the parent <table> element 
        do 
        {
            obj = obj.parentNode; 
        } while (obj.tagName != "TABLE") 
        //keep track of the padding level for comparison with any children 
        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) 
            {
                //head1 gets the parent node of the unchecked node 
                var head1 = obj.parentNode.previousSibling;
                if(head1 != null) 
                { 
                    if(head1.tagName == "TABLE") 
                    {
                        //checks for the input tag which consists of checkbox 
                        var matchElement1 = head1.getElementsByTagName("INPUT"); 
                        //matchElement1[0] gives us the checkbox and it is unchecked 
                        matchElement1[0].checked = false; 
                    }
                }
                else 
                {
                    head1 = obj.parentNode.previousSibling;
                } 
                if(head1.tagName == "TABLE") 
                {
                    //head2 gets the parent node of the unchecked node 
                    var head2 = obj.parentNode.parentNode.previousSibling; 
                    if(head2 != null) 
                    {
                        if(head2.tagName == "TABLE") 
                        {
                            //checks for the input tag which consists of checkbox 
                            var matchElement2 = head2.getElementsByTagName("INPUT"); 
                            matchElement2[0].checked = false; 
                        }
                    }
                }
                else 
                {
                    head2 = obj.parentNode.previousSibling;
                } 
                if(head2.tagName == "TABLE") 
                {
                    //head3 gets the parent node of the unchecked node 
                    var head3 = obj.parentNode.parentNode.parentNode.previousSibling; 
                    if(head3 != null) 
                    {
                        if(head3.tagName == "TABLE") 
                        {
                            //checks for the input tag which consists of checkbox 
                            var matchElement3 = head3.getElementsByTagName("INPUT"); 
                            matchElement3[0].checked = false; 
                        }
                    }
                }
                else 
                {
                    head3 = obj.parentNode.previousSibling;
                } 
                if(head3.tagName == "TABLE") 
                { 
                    //head4 gets the parent node of the unchecked node 
                    var head4 = obj.parentNode.parentNode.parentNode.parentNode.previousSibling; 
                    if(head4 != null) 
                    { 
                        if(head4.tagName == "TABLE") 
                        { 
                            //checks for the input tag which consists of checkbox 
                            var matchElement4 = head4.getElementsByTagName("INPUT"); 
                            matchElement4[0].checked = false; 
                        }
                    }
                }
            } //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 then stop 
                            //return;
                            break;
                        }
                    } 
                    if (treeNodeFound == true) 
                    {
                        var childTreeLevel = tables[i].rows[0].cells.length;                            
                        if(childTreeLevel ==  parentTreeLevel) 
                        {                    
                            break;
                            //return;
                        }
                        //if the current node is under the parent 
                        //the level will be deeper (greater) 
                        if (childTreeLevel > parentTreeLevel) 
                        { 
                            //jump to the last cell... it contains the checkbox 
                            var cell = tables[i].rows[0].cells[childTreeLevel - 1]; 
                            //set the checkbox to match the checkedState 
                            //of the TreeNode that was clicked 
                            var inputs = cell.getElementsByTagName("INPUT"); 
                            inputs[0].checked = checkedState; 
                        } 
                        else 
                        { 
                            //if any of the preceding TreeNodes are not deeper stop 
                            //return; 
                            //break;
                        }
                    } //end if 
                }//end for 
            } //end if - numTables >= 1
            // if all child nodes are checked then their parent node is checked
            if (treeNode.checked) 
            {                    
                var chk1 = true;
                var head1 = obj.parentNode.previousSibling;        
                var pTreeLevel1 = obj.rows[0].cells.length;        
                if(head1.tagName == "TABLE") 
                {
                    var tbls = obj.parentNode.getElementsByTagName("TABLE");
                    var tblsCount = tbls.length;        
                    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;                        
                }
                else 
                {
                    head1 = obj.parentNode.previousSibling;
                }                    
                var chk2 = true;
                if(head1.tagName == "TABLE") 
                {        
                    var head2 = obj.parentNode.parentNode.previousSibling; 
                    if(head2.tagName == "TABLE") 
                    {        
                        var tbls = head1.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel2 = head1.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel2) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == false) 
                                {
                                    chk2 = false;
                                    break;
                                }
                            }
                        }
                        var nd = head2.getElementsByTagName("INPUT");                        
                        nd[0].checked = (chk2 && chk1);                            
                    }
                }
                else 
                {
                    head2 = obj.parentNode.previousSibling;
                }                    
                var chk3 = true;
                if(head2.tagName == "TABLE") 
                {        
                    var head3 = obj.parentNode.parentNode.parentNode.previousSibling; 
                    if(head3.tagName == "TABLE") 
                    {
                        var tbls = head2.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel3 = head2.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel3) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == false) 
                                {
                                    chk3 = false;
                                    break;
                                }
                            }
                        }
                        var nd = head3.getElementsByTagName("INPUT");        
                        nd[0].checked = (chk3 && chk2 && chk1);                            
                    }
                }
                else 
                {
                    head3 = obj.parentNode.previousSibling;
                }                    
                var chk4 = true;
                if(head3.tagName == "TABLE") 
                {
                    var head4 = obj.parentNode.parentNode.parentNode.parentNode.previousSibling; 
                    if(head4.tagName == "TABLE") 
                    {
                        var tbls = head3.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel4 = head3.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel4) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == false) 
                                {
                                    chk4 = false;
                                    break;
                                }
                            }
                        }
                        var nd = head4.getElementsByTagName("INPUT");                        
                        nd[0].checked = (chk4 && chk3 && chk2 && chk1);                            
                    }
                }                                   
            }//end if - checked
        } //end if - tagName = TABLE
    } //end if   
}

function TreeView1_click(event)
{    
    // obj gives us the node on which check or uncheck operation has performed
    var obj = event.srcElement || event.target;
    var treeNodeFound = false; 
    var checkedState; 
    //checking whether obj consists of checkbox or not 
    if (obj.tagName == "INPUT" && obj.type == "checkbox") 
    {
        //easier to read        
        var treeNode = obj;        
        //record the checked state of the TreeNode 
        checkedState = treeNode.checked; 
        //work our way back to the parent <table> element 
        do 
        {
            obj = obj.parentNode; 
        } while (obj.tagName != "TABLE") 
        //keep track of the padding level for comparison with any children 
        var parentTreeLevel = obj.rows[0].cells.length;
        var parentTreeNode = obj.rows[0].cells[0];

        if (parentTreeLevel < 4) {
            alert("Selected SIC Codes must be at least 3 digits");
            treeNode.checked = false;
            return false;
        }            
        //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 checked then all its parent nodes are checked 
            if (treeNode.checked) 
            {
                //head1 gets the parent node of the checked node 
//                var head1 = obj.parentNode.previousSibling;
//                if(head1 != null) 
//                { 
//                    if(head1.tagName == "TABLE") 
//                    {
//                        //checks for the input tag which consists of checkbox 
//                        var matchElement1 = head1.getElementsByTagName("INPUT"); 
//                        //matchElement1[0] gives us the checkbox and it is checked 
//                        matchElement1[0].checked = checkedState; 
//                    }
//                }
//                else 
//                {
//                    head1 = obj.parentNode.previousSibling;
//                }
//                if(head1.tagName == "TABLE") 
//                {
//                    //head2 gets the parent node of the checked node 
//                    var head2 = obj.parentNode.parentNode.previousSibling; 
//                    if(head2 != null) 
//                    {
//                        if(head2.tagName == "TABLE") 
//                        {
//                            //checks for the input tag which consists of checkbox 
//                            var matchElement2 = head2.getElementsByTagName("INPUT"); 
//                            matchElement2[0].checked = checkedState; 
//                        }
//                    }
//                }
//                else 
//                {
//                    head2 = obj.parentNode.previousSibling;
//                } 
//                if(head2.tagName == "TABLE") 
//                {
//                    //head3 gets the parent node of the checked node 
//                    var head3 = obj.parentNode.parentNode.parentNode.previousSibling; 
//                    if(head3 != null) 
//                    {
//                        if(head3.tagName == "TABLE") 
//                        {
//                            //checks for the input tag which consists of checkbox 
//                            var matchElement3 = head3.getElementsByTagName("INPUT"); 
//                            matchElement3[0].checked = checkedState; 
//                        }
//                    }
//                }
//                else 
//                {
//                    head3 = obj.parentNode.previousSibling;
//                } 
//                if(head3.tagName == "TABLE") 
//                { 
//                    //head4 gets the parent node of the checked node 
//                    var head4 = obj.parentNode.parentNode.parentNode.parentNode.previousSibling; 
//                    if(head4 != null) 
//                    { 
//                        if(head4.tagName == "TABLE") 
//                        { 
//                            //checks for the input tag which consists of checkbox 
//                            var matchElement4 = head4.getElementsByTagName("INPUT"); 
//                            matchElement4[0].checked = checkedState; 
//                        }
//                    }
//                } 
//                else 
//                {
//                    head4 = obj.parentNode.previousSibling;
//                } 
//                if(head4.tagName == "TABLE") 
//                { 
//                    //head4 gets the parent node of the checked node 
//                    var head5 = obj.parentNode.parentNode.parentNode.parentNode.parentNode.previousSibling; 
//                    if(head5 != null) 
//                    { 
//                        if(head5.tagName == "TABLE") 
//                        { 
//                            //checks for the input tag which consists of checkbox 
//                            var matchElement5 = head5.getElementsByTagName("INPUT"); 
//                            matchElement5[0].checked = checkedState; 
//                        }
//                    }
//                } 
            }//end if - checked
            //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 then stop 
                            //return;
                            break;
                        }
                    } 
                    if (treeNodeFound == true) 
                    {
                        var childTreeLevel = tables[i].rows[0].cells.length;                            
                        if(childTreeLevel ==  parentTreeLevel) 
                        {                    
                            break;
                            //return;
                        }
                        //if the current node is under the parent 
                        //the level will be deeper (greater) 
                        if (childTreeLevel > parentTreeLevel) 
                        { 
                            //jump to the last cell... it contains the checkbox 
                            var cell = tables[i].rows[0].cells[childTreeLevel - 1]; 
                            //set the checkbox to match the checkedState 
                            //of the TreeNode that was clicked 
                            var inputs = cell.getElementsByTagName("INPUT"); 
                            //inputs[0].checked = checkedState; 
                        } 
                        else 
                        { 
                            //if any of the preceding TreeNodes are not deeper stop 
                            //return; 
                            //break;
                        }
                    } //end if 
                }//end for 
            } //end if - numTables >= 1
            // if any node is unchecked then all its parents are unchecked
            // if and only if no other adjacent node is selected.
            if (!treeNode.checked) 
            {                
                var chk1 = false;
                var head1 = obj.parentNode.previousSibling;        
                var pTreeLevel1 = obj.rows[0].cells.length;        
                if(head1.tagName == "TABLE") 
                {
                    var tbls = obj.parentNode.getElementsByTagName("TABLE");
                    var tblsCount = tbls.length;        
                    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 == true) 
                            {                                
                                chk1 = true;
                                break;
                            }
                        }
                    }
                    var nd = head1.getElementsByTagName("INPUT");             
                    nd[0].checked = chk1;                        
                }
                else 
                {
                    head1 = obj.parentNode.previousSibling;
                }                    
                var chk2 = false;
                if(head1.tagName == "TABLE") 
                {        
                    var head2 = obj.parentNode.parentNode.previousSibling; 
                    if(head2.tagName == "TABLE") 
                    {        
                        var tbls = head1.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel2 = head1.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel2) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == true) 
                                {
                                    chk2 = true;
                                    break;
                                }
                            }
                        }
                        var nd = head2.getElementsByTagName("INPUT");                        
                        nd[0].checked = (chk2 || chk1);                            
                    }
                }
                else 
                {
                    head2 = obj.parentNode.previousSibling;
                }                    
                var chk3 = false;
                if(head2.tagName == "TABLE") 
                {        
                    var head3 = obj.parentNode.parentNode.parentNode.previousSibling; 
                    if(head3.tagName == "TABLE") 
                    {
                        var tbls = head2.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel3 = head2.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel3) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == true) 
                                {
                                    chk3 = true;
                                    break;
                                }
                            }
                        }
                        var nd = head3.getElementsByTagName("INPUT");        
                        nd[0].checked = (chk3 || chk2 || chk1);                            
                    }
                }
                else 
                {
                    head3 = obj.parentNode.previousSibling;
                }                    
                var chk4 = false;
                if(head3.tagName == "TABLE") 
                {
                    var head4 = obj.parentNode.parentNode.parentNode.parentNode.previousSibling; 
                    if(head4.tagName == "TABLE") 
                    {
                        var tbls = head3.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel4 = head3.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel4) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == true) 
                                {
                                    chk4 = true;
                                    break;
                                }
                            }
                        }
                        var nd = head4.getElementsByTagName("INPUT");                        
                        nd[0].checked = (chk4 || chk3 || chk2 || chk1);                            
                    }
                }
                else 
                {
                    head4 = obj.parentNode.previousSibling;
                }                    
                var chk5 = false;
                if(head4.tagName == "TABLE") 
                {
                    var head5 = obj.parentNode.parentNode.parentNode.parentNode.parentNode.previousSibling; 
                    if(head5.tagName == "TABLE") 
                    {
                        var tbls = head4.parentNode.getElementsByTagName("TABLE");
                        var pTreeLevel5 = head4.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel == pTreeLevel5) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if (chld[0].checked == true) 
                                {
                                    chk5 = true;
                                    break;
                                }
                            }
                        }
                        var nd = head5.getElementsByTagName("INPUT");                        
                        nd[0].checked = (chk5 || chk4 || chk3 || chk2 || chk1);                            
                    }
                }                                    
            }//end if - unchecked
        } //end if - tagName = TABLE
    } //end if   
}


