$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("click", '.proceed', chkfunc);
    $("body").on("change blur", '.subject,.subject2,.subject3', chkrepeat);
    $("body").on("focus keyup change ", '.srno,.srno2,.srno3,.first,.second,.third,.fourth,.fifth,.sixth,.seventh,.eight,.ninth,.tenth,.pqrelm', calpack);
    $("body").on("change", '.first,.second,.third,.fourth', filprim);
    $("body").on("change", '.fifth,.sixth,.seventh,.eight,.ninth,.tenth', filsec);
    $("body").on("change", '.pqrelm', filpril);
    $("body").on("focus", '.third', autoaddpri);
    $("body").on("focus", '.ninth', autoaddsec);
    $("body").on("focus", '.srno3', autoaddpre);
    $("body").on("keydown", 'td', updown);


    function updown() {
        var tr = $(this).closest('tr'),
                newsrno = tr.find(".srno").val(),
                newsrno2 = tr.find(".srno2").val(),
                newsrno3 = tr.find(".srno3").val(),
                srno = parseFloat(newsrno) - 2,
                srno2 = parseFloat(newsrno2) - 2,
                srno3 = parseFloat(newsrno3) - 2,
                section = $('.section').val(),
                curcls = $(this).attr('class');

        $(document).keydown(function (event) {
            if (event.keyCode == 40) { // Down key maps to keycode `40`

                switch (section) {
                    case 'P':
                        if (curcls == 'p1') {
                            $('#first_' + newsrno).focus();
                        } else if (curcls == 'p2') {
                            $('#second_' + newsrno).focus();
                        } else if (curcls == 'p3') {
                            $('#third_' + newsrno).focus();
                        } else if (curcls == 'p4') {
                            $('#fourth_' + newsrno).focus();
                        }
                    case 'S':
                        if (curcls == 'p5') {
                            $('#fifth_' + newsrno2).focus();
                        } else if (curcls == 'p6') {
                            $('#sixth_' + newsrno2).focus();
                        } else if (curcls == 'p7') {
                            $('#seventh_' + newsrno2).focus();
                        } else if (curcls == 'p8') {
                            $('#eight_' + newsrno2).focus();
                        } else if (curcls == 'p9') {
                            $('#ninth_' + newsrno2).focus();
                        } else if (curcls == 'p10') {
                            $('#tenth_' + newsrno2).focus();
                        }
                    case 'M':
                        if (curcls == 'prel') {
                            $('#pqrelm_' + newsrno3).focus();
                        }
                }
            }
        });


        $(document).keydown(function (event) {
            if (event.keyCode == 38) { // Up key maps to keycode `38`

                switch (section) {
                    case 'P':
                        if (curcls == 'p1') {
                            $('#first_' + srno).focus();
                        } else if (curcls == 'p2') {
                            $('#second_' + srno).focus();
                        } else if (curcls == 'p3') {
                            $('#third_' + srno).focus();
                        } else if (curcls == 'p4') {
                            $('#fourth_' + srno).focus();
                        }
                    case 'S':
                        if (curcls == 'p5') {
                            $('#fifth_' + srno2).focus();
                        } else if (curcls == 'p6') {
                            $('#sixth_' + srno2).focus();
                        } else if (curcls == 'p7') {
                            $('#seventh_' + srno2).focus();
                        } else if (curcls == 'p8') {
                            $('#eight_' + srno2).focus();
                        } else if (curcls == 'p9') {
                            $('#ninth_' + srno2).focus();
                        } else if (curcls == 'p10') {
                            $('#tenth_' + srno2).focus();
                        }
                    case 'M':
                        if (curcls == 'prel') {
                            $('#pqrelm_' + srno3).focus();
                        }
                }
            }
        });
    }

    function chkfunc() {
        var exam = $('.exam').val(),
                vouchno = $('.vouchno').val(),
                custcode = $('.custcode').val(),
                medium = $('.medium').val(),
                section = $('.section').val(),
                session = $('.session').val();

        $.post('chkexist.php', {vouchno: vouchno, exam: exam, custcode: custcode, medium: medium, session: session, section: section}, function (data) {
            if (data === 'Fail') {
                alert("This Demand Form Already Exist");
            } else {
                $('#ssubmit').show();
                getsub();
                shfunc();
            }
        });
    }


    function chkrepeat() {
        var section = $('.section').val(),
                tr = $(this).closest('tr'),
                subject = tr.find('.subject').val(),
                srno = tr.find('.srno').val(),
                subject2 = tr.find('.subject2').val(),
                srno2 = tr.find('.srno2').val(),
                subject3 = tr.find('.subject3').val(),
                srno3 = tr.find('.srno3').val();
        if (section == 'P') {
            $(".tbody").find('tr').each(function () {
                var row = $(this),
                        loopsub = row.find('.subject').val(),
                        loopsrno = row.find('.srno').val();

                if (loopsub == subject && loopsrno != srno) {
                    alert('SUBJECT CANNOT BE SAME !!!');
                    tr.find('.subject').val('').trigger("change");
                    return false;
                }
            });
        } else if (section == 'S') {
            $(".tbody2").find('tr').each(function () {
                var row = $(this),
                        loopsub2 = row.find('.subject2').val(),
                        loopsrno2 = row.find('.srno2').val();

                if (loopsub2 == subject2 && loopsrno2 != srno2) {
                    alert('SUBJECT CANNOT BE SAME !!!');
                    tr.find('.subject2').val('').trigger("change");
                    return false;
                }
            });
        } else if (section == 'M') {
            $(".tbody3").find('tr').each(function () {
                var row = $(this),
                        loopsub3 = row.find('.subject3').val(),
                        loopsrno3 = row.find('.srno3').val();

                if (loopsub3 == subject3 && loopsrno3 != srno3) {
                    alert('SUBJECT CANNOT BE SAME !!!');
                    tr.find('.subject3').val('').trigger("change");
                    return false;
                }
            });
        }
    }

    function autoaddpri() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows();
        }
    }

    function autoaddsec() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows2();
        }
    }

    function autoaddpre() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows3();
        }
    }



    function getsub() {

        var medium = $('.medium').val(),
                section = $('.section').val();
        if (section == 'P') {
            $.post('get_subcombpri.php', {medium: medium, section: section}, function (data) {
                $('.prim').html(data);
            });
        } else if (section == 'S') {
            $.post('get_subcombpri.php', {medium: medium, section: section}, function (data) {
                $('.sec').html(data);
            });
        } else {
            $.post('get_subcombpri.php', {medium: medium, section: section}, function (data) {
                $('.pre').html(data);
            });
        }
    }

    var section = $('.section').val();
    if (section == '') {
        $('.primary').hide();
        $('.secondary').hide();
        $('.prelium').hide();
    } else if (section == 'P') {
        $('.primary').show();
        $('.secondary').hide();
        $('.prelium').hide();
    } else if (section == 'S') {
        $('.secondary').show();
        $('.prelium').hide();
        $('.primary').hide();
    } else if (section == 'M') {
        $('.prelium').show();
        $('.primary').hide();
        $('.secondary').hide();
    }

    function shfunc() {
        var section = $('.section').val(),
                session = $('.session').val(),
                exam = $('.exam').val(),
                medium = $('.medium').val();
        if (section != '' && session != '' && medium != '' && exam != '') {

            if (section == 'P') {
                $('.primary').show();
            } else {
                $('.primary').hide();
            }
            if (section == 'S') {
                $('.secondary').show();
            } else {
                $('.secondary').hide();
            }
            if (section == 'M') {
                $('.prelium').show();
            } else {
                $('.prelium').hide();
            }
        }
    }

    function remove() {
        $(this).closest("tr").remove();
    }

    $('#addnew').click(function () {
        AddRows();
    });
    function AddRows() {
        var ctr = $('#items').val(),
                ctr2 = $('.totrow').val();

        if (typeof(ctr2) !== 'undefined') {
            ctr = ctr2;
        }

        var medium = $('.medium').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('dmndprim_add.php', {ctr: ctr, hide: hide_check, medium: medium}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            if (typeof (ctr2) !== 'undefined') {
                $('.totrow').val(ctr);
            } else {
                $('#items').val(ctr);
            }

            $(detail).select2();
        });
    }
    $('#addnew2').click(function () {
        AddRows2();
    });
    $('.ninth').focus(function () {
        if ($(this).closest("tr").is(":last-child")) {
            AddRows2();
        }
    });
    function AddRows2() {
        var ctr = $('#items2').val(),
                ctr2 = $('.totrow2').val();

        if (typeof (ctr2) !== 'undefined') {
            ctr = ctr2;
        }

        var medium = $('.medium').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('dmndsec_add.php', {ctr: ctr, hide: hide_check, medium: medium}, function (data) {
            $(data).appendTo('.tbody2');
            ctr++;
            if (typeof (ctr2) !== 'undefined') {
                $('.totrow2').val(ctr);
            } else {
                $('#items2').val(ctr);
            }

            $(detail).select2();
        });
    }

    $('#addnew3').click(function () {
        AddRows3();
    });
    $('.srno3').focus(function () {
        if ($(this).closest("tr").is(":last-child")) {
            AddRows3();
        }
    });

    function AddRows3() {
        var ctr = $('#items3').val(),
                ctr2 = $('.totrow3').val();
        if (typeof (ctr2) !== 'undefined') {
            ctr = ctr2;
        }
        var medium = $('.medium').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('dmndpril_add.php', {ctr: ctr, hide: hide_check, medium: medium}, function (data) {
            $(data).appendTo('.tbody3');
            ctr++;

            if (typeof (ctr2) !== 'undefined') {
                $('.totrow3').val(ctr);
            } else {
                $('#items3').val(ctr);
            }
            $(detail).select2();
        });
    }

    function calpack() {
        var section = $('.section').val();
        if (section == 'P') {
            var frs = 0,
                    sc = 0,
                    th = 0,
                    four = 0,
                    totpack = 0;
            $(".first").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    frs += 1;
                }
            });
            $(".second").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    sc += 1;
                }
            });
            $(".third").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    th += 1;
                }
            });
            $(".fourth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    four += 1;
                }
            });
            totpack = parseFloat(frs) + parseFloat(sc) + parseFloat(th) + parseFloat(four);
            $('.nopack').val(totpack);
        } else if (section == 'S') {

            var fifth = 0,
                    six = 0,
                    sevn = 0,
                    eight = 0,
                    nine = 0,
                    ten = 0,
                    totpack2 = 0;
            $(".fifth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    fifth += 1;
                }
            });
            $(".sixth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    six += 1;
                }
            });
            $(".seventh").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    sevn += 1;
                }
            });
            $(".eight").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    eight += 1;
                }
            });
            $(".ninth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    nine += 1;
                }
            });
            $(".tenth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    ten += 1;
                }
            });
            totpack2 = parseFloat(fifth) + parseFloat(six) + parseFloat(sevn) + parseFloat(eight) + parseFloat(nine) + parseFloat(ten);
            $('.nopack').val(totpack2);
        } else {
            var prel = 0;
            $(".pqrelm").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    prel += 1;
                }
                var tpck = parseFloat(prel);
                $('.nopack').val(tpck);
            });
        }
    }

    calpack();

    function filprim() {

        $(".tbody").find('tr').each(function () {
            var row = $(this),
                    first = $('.first').val(),
                    f1 = row.find('.f1').val(),
                    mfirst = row.find('.first').val();
            if (f1 == 1 && mfirst == '') {
                row.find('.first').val(first);
            }
            var second = $('.second').val(),
                    f2 = row.find('.f2').val(),
                    msecond = row.find('.second').val();
            if (f2 == 1 && msecond == '') {
                row.find('.second').val(second);
            }

            var third = $('.third').val(),
                    f3 = row.find('.f3').val(),
                    mthird = row.find('.third').val();
            if (f3 == 1 && mthird == '') {
                row.find('.third').val(third);
            }

            var fourth = $('.fourth').val(),
                    f4 = row.find('.f4').val(),
                    mfourth = row.find('.fourth').val();
            if (f4 == 1 && mfourth == '') {
                row.find('.fourth').val(fourth);
            }

        });
    }
    function filpril() {

        $(".tbody3").find('tr').each(function () {
            var row = $(this),
                    pqrelm = $('.pqrelm').val(),
                    fprel = row.find('.fprel').val(),
                    mpqrelm = row.find('.pqrelm').val();
            if (fprel == 1 && mpqrelm == '') {
                row.find('.pqrelm').val(pqrelm);
            }

        });
    }

    function filsec() {


        $(".tbody2").find('tr').each(function () {
            var row = $(this),
                    fifth = $('.fifth').val(),
                    f5 = row.find('.f5').val(),
                    mfifth = row.find('.fifth').val();
            if (f5 == 1 && mfifth == '') {
                row.find('.fifth').val(fifth);
            }

            var sixth = $('.sixth').val(),
                    f6 = row.find('.f6').val(),
                    msixth = row.find('.sixth').val();
            if (f6 == 1 && msixth == '') {
                row.find('.sixth').val(sixth);
            }

            var seventh = $('.seventh').val(),
                    f7 = row.find('.f7').val(),
                    mseventh = row.find('.seventh').val();
            if (f7 == 1 && mseventh == '') {
                row.find('.seventh').val(seventh);
            }

            var eight = $('.eight').val(),
                    f8 = row.find('.f8').val(),
                    meight = row.find('.eight').val();
            if (f8 == 1 && meight == '') {
                row.find('.eight').val(eight);
            }

            var ninth = $('.ninth').val(),
                    f9 = row.find('.f9').val(),
                    mninth = row.find('.ninth').val();
            if (f9 == 1 && mninth == '') {
                row.find('.ninth').val(ninth);
            }

            var tenth = $('.tenth').val(),
                    f10 = row.find('.f10').val(),
                    mtenth = row.find('.tenth').val();
            if (f10 == 1 && mtenth == '') {
                row.find('.tenth').val(tenth);
            }

        });
    }
});