$(document).ready(function () {

    $("body").on("click", ".buttonsk", ModalEntry);
    $("body").on("click", ".saveent", savDeposit);
    $("body").on("change", ".type1,.type2", showbank);

    
    $(".showdescrip").show();
    showdescription();
    
    function showdescription() {
        var printdescrip = $('.printdescrip').val();
        if (printdescrip == "N") {
            $(".showdescrip").hide();
        } else {
            $(".showdescrip").show();
        }
    }


    function ModalEntry() {
        $(".ModalEntry").modal("show");
        $('.ModalEntry').on('shown.bs.modal', function () {
            $('.vouchdt').trigger('focus');
        });

        var btnvalue = $(this).val();
        $('#buttonsAction').val(btnvalue); // Save it

        $(".showtype1, .showtype2").hide();

        $(".Title1, .Title2, .Title3, .Title4, .Title5").hide();

        if (btnvalue == "S") {
            $(".Title1").show();
            $(".showtype1").show();

        } else if (btnvalue == "PU") {
            $(".Title2").show();
            $(".showtype1").show();

        } else if (btnvalue == "R") {
            $(".Title3").show();
            $(".showtype2").show();

        } else if (btnvalue == "PY") {
            $(".Title4").show();
            $(".showtype2").show();

        } else if (btnvalue == "SC" || btnvalue == "PC") {
            $(".Title5").show();
            $(".showtype1").show();
        }
    }


    function savDeposit() {
        // Show loading animation
        $("#loadingIndicator").show();

        var accode = $(".accode").val(),
            vouchdt = $(".vouchdt").val(),
            buttonsk = $("#buttonsAction").val(),
            pairs = $(".pairs").val(),
            amount = $(".amount").val(),
            type1 = $(".type1").val(),
            type2 = $(".type2").val(),
            bankname = $(".bankname").val(),
            remark = $(".remark").val();

        if (amount <= 0) {
            alert("Please Enter Amount");
            $("#loadingIndicator").hide();
            return;
        }

        if (type1 == "U" || type2 == "U") {
            if (bankname == 0) {
                alert("Select Bank");
                $("#loadingIndicator").hide();
                return;
            }
        }

        $.post("saveDeposit.php", {
            accode: accode,
            vouchdt: vouchdt,
            pairs: pairs,
            amount: amount,
            type1: type1,
            type2: type2,
            bankname: bankname,
            buttonsk: buttonsk,
            remark: remark
        }, function (data) {
            // Hide loading animation
            $("#loadingIndicator").hide();

            if (data > 0) {
                location.reload(true);
            } else {
                alert("Failed to save deposit.");
            }
        }).fail(function () {
            $("#loadingIndicator").hide();
            alert("Error occurred during saving.");
        });
    }


    $(".showcashbank").hide();

    function showbank() {
        var type1 = $(".type1").val();
        var type2 = $(".type2").val();

        if (type1 == "U" || type2 == "U") {
            $(".showcashbank").show();
        } else {
            $(".showcashbank").hide();
        }
    }

});