﻿function treeViewCheck()
{
    // obj is the node from where the event was fired
    var obj = window.event.srcElement;
    var treeNodeFound = false;
    var checkedState;

    //check whether obj consists of checkbox or not
    if (obj.tagName == "INPUT" && obj.type == "checkbox")
    {
    //call it treenode to improve readability
    var treeNode = obj;

    //store the checked state of the TreeNode
    checkedState = treeNode.checked;

    //work our way back to the parent <table> element

    do
    {
        obj = obj.parentElement;
    } 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.parentElement.getElementsByTagName("TABLE");

    //checking for any node is checked or unchecked during operation
    if((treeNode.checked) || (!treeNode.checked))
    {
    // if any node is unchecked then their parent node are unchecked
    if(!treeNode.checked)
    {
    if(obj.tagName == "TABLE")

    {

    //head1 gets the parent node of the unchecked node

    var head1 = obj.parentElement.previousSibling;

    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.parentElement.previousSibling;

    }

    if(head1.tagName == "TABLE")

    {

    //head2 gets the parent node of the unchecked node

    var head2 = obj.parentElement.parentElement.previousSibling;

    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.parentElement.previousSibling;

    }

    if(head2.tagName == "TABLE")

    {

    //head3 gets the parent node of the unchecked node

    var head3 = obj.parentElement.parentElement.parentElement.previousSibling;

    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.parentElement.previousSibling;

    }

    if(head3.tagName == "TABLE")

    {

    //head4 gets the parent node of the unchecked node

    var head4 = obj.parentElement.parentElement.parentElement.parentElement.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;

    }

    }

    }

    }

    }

    }

    //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;

    }

    }

    if (treeNodeFound == true)

    {

    var childTreeLevel = tables[i].rows[0].cells.length;

    //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;

    }
    //end if

    }
    //end if

    else

    {

    // var childTreeLevel = tables[0].rows[0].cells.length;

    }

    }
    //end for

    }
    //end if

    }
    //end if

}
//end function
