﻿
$(document).ready(function() {
    $("#btnVybratKatedra").click(function(e) {
        $("#pnlPopup_katedry").modal();

        $("input[name *= 'btnOk_katedry']").click(function() {
            __doPostBack('ctl00$cph_hlavnipanelvnitrek$hledaniAkci$btnOk_katedry', '');
            $.modal.close();
        });
    });

    $("#btnVybratKmenZaklad").click(function(e) {
        $("#pnlPopupKmenovyZaklad").modal();
        $("input[name *= 'btnOk_kmenZaklad']").click(function() {
            __doPostBack('ctl00$cph_hlavnipanelvnitrek$hledaniAkci$btnOk_kmenZaklad', '');
            $.modal.close();
        });
    });

    $("#btnVybratMisto").click(function(e) {
        $("#pnpPopupMista").modal();
        $("input[name *= 'btnOk_mista']").click(function() {
            __doPostBack('ctl00$cph_hlavnipanelvnitrek$hledaniAkci$btnOk_mista', '');
            $.modal.close();
        });
    });

    $("#btnVybratLektor").click(function(e) {
        $("#pnlPopupLektori").modal();
        $("input[name *= 'btnOk_lektori']").click(function() {
            __doPostBack('ctl00$cph_hlavnipanelvnitrek$hledaniAkci$btnOk_lektori', '');
            $.modal.close();
        });
    });
});

function checkTerminOd(id) {
    var terminOd = $get(id);

    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    curr_month++;

    if (terminOd.value.trim() == "")
        terminOd.value = curr_date + "." + curr_month + "." + curr_year;
}

function SetTerminyHistoryDisplay(plusIcon, minusIcon, imgClientId) {
    var terminyHistory = $get("terminyHistorie");
    var plusMinusIcon = $get(imgClientId);

    if (terminyHistory.style.display == "") {
        terminyHistory.style.display = "none";
        plusMinusIcon.src = plusIcon;
    }
    else {
        terminyHistory.style.display = "";
        plusMinusIcon.src = minusIcon;
    }
}

function goDeeperChecked(obj) {
    var chk1 = true;
    var head1 = obj.parentNode.previousSibling;
    
    if (obj.rows == null) {
        return;
    }
    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;
        goDeeperChecked(obj.parentNode);
    }
}

function goDeeper(check, obj) {
    var head = obj.parentNode.previousSibling;
    
    if(head.tagName == "TABLE") {
        var matchElement = head.getElementsByTagName("INPUT");
        matchElement[0].checked = false;
    }
    else {
        head = obj.parentNode.previousSibling;
    }
    if(head.tagName == "TABLE") {
        goDeeper(check, obj.parentNode);
    }
}

function treeViewCheck(event) {
    var obj = event.srcElement || event.target;
    var treeNodeFound = false;
    var checkedState;

    if(obj.tagName == "INPUT" && obj.type == "checkbox") {
        var treeNode = obj;
        checkedState = treeNode.checked;
        do {
            obj = obj.parentNode;

            if (obj.tagName == "TR") {
                var script = obj.childNodes[0].firstChild.href;
                eval(script);                
            }
        }
        while(obj.tagName != "TABLE")
        var parentTreeLevel = obj.rows[0].cells.length;
        var parentTreeNode = obj.rows[0].cells[0];
        var tables = obj.parentNode.getElementsByTagName("TABLE");

        if(obj.tagName == "TABLE") {
            if(!treeNode.checked) {
                goDeeper(false, obj);
            }
            var numTables = tables.length
            if(numTables >= 1) {
                for(i = 0; i < numTables; i++) {
                    if(tables[i] == obj) {
                        treeNodeFound = true;
                        i++;
                        if(i == numTables) {
                            break;
                        }
                    }
                    if(treeNodeFound == true) {
                        var childTreeLevel = tables[i].rows[0].cells.length;
                        if(childTreeLevel > parentTreeLevel) {
                            var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                            var inputs = cell.getElementsByTagName("INPUT");
                            inputs[0].checked = checkedState;
                        }
                        else {
                            break;
                        }
                    }
                }
            }
            if(treeNode.checked) {
                goDeeperChecked(obj);
            }
        }
    }
}
