$(document).ready(function () {
    $("body").on("click", '.proceed', checkvalues);
    $("body").on("click", '#submit', insert);
    $("body").on('keyup change', '.tsearch', searchschl);
    $("body").on('change', '.fexam,.texam', ntsme);
    $("body").on('change', '.allchk', chkall);
    $("body").on('change', '.section', showselection);

    function showselection() {
        var section = $('.section').val();
        if (section == 'P') {
            var elements = "<div class = 'row'>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>1</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c1' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>2</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c2' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>3</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c3' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>4</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c4' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "</div>";
            $('.chkselection').html(elements);
            $('.selectdiv').attr('style', '');
        } else if (section == 'S' || section == 'M') {
            var elements = "<div class = 'row'>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>5</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c5' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>6</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c6' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>7</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c7' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>8</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c8' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>9</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c9' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "<div class = 'col-lg-1 text-right pad'>" +
                    "<label class='label-control' style='font-size: 21px'>10</label>" +
                    "</div>" +
                    "<div class = 'col-lg-1 pad'>" +
                    "<input type='checkbox' class='form-control' name='c10' value='1' style='width: 17px' checked>" +
                    "</div>" +
                    "</div>";

            $('.chkselection').html(elements);
            $('.selectdiv').attr('style', '');
        } else {
            $('.selectdiv').css('display', 'none');
        }
    }

    function ntsme() {
        var fexam = $('.fexam').val(),
                texam = $('.texam').val();
        if (fexam == texam) {
            alert('From Exam And To Exam Cannot Be Same');
            $(".texam").val('');
        }
    }

    function insert() {
//        $(".myTable").find('input[type="checkbox"]:checked').each(function(){
        $(".myTable").find('tr').each(function () {
            var row = $(this);
            if (row.find('input[type="checkbox"]').is(':checked')) {
                var accode = row.find('.accode').val(),
                        ncust = row.find('.scust').val();
                row.find('.saccode').val(accode);
                row.find('.ncust').val(ncust);
            }
        });
    }


    function checkvalues() {
        var fexam = $('.fexam').val(),
                texam = $('.texam').val(),
                section = $('.section').val(),
                session = $('.session').val();

        if (fexam != '' && texam != '' && section != '' && session != '') {

            $.post('checkval.php', {fexam: fexam, texam: texam, section: section, session: session}, function (data) {
                if (data.fexam == true) {
                    alert('This Selected From Exam Does Not Exist For This Section & Session.');
                } else if (data.toexam == true) {
                    alert('This Selected Exam + Section + Session Already Exist');
                } else {
                    $.post('gendmnd_school.php', {fexam: fexam, section: section, session: session}, function (data) {
                        $(".schooldiv").empty();
                        $(".schooldiv").html(data);
                        var ctr = $('.myTable').find('.scount').val();
                        $('#items').val(ctr);
                    });


                    $('#custmodal').modal('show');
                    $("#custmodal").on('shown.bs.modal', function () {
                        $(".tsearch").val("");
                        $(".tsearch").focus();

                    });
                }
            }, 'json');
        } else {
            alert("From Exam and To Exam and Section and Session.");
        }
    }



    function chkall() {
        $(".mcheck").prop('checked', $(this).prop('checked'));
    }

    function searchschl() {
        var fexam = $('.fexam').val(),
                section = $('.section').val(),
                session = $('.session').val();
        $.post('gendmnd_school.php', {fexam: fexam, section: section, session: session}, function (data) {
            $(".schooldiv").empty();
            $(".schooldiv").html(data);
        });
    }

    function searchschl() {
        var search = $(".tsearch").val();
        $.post('get_schl.php', {search: search}, function (data) {
            $(".myTable").html(data);
        });
    }


});