
// =======
// Add RDI
// =======

$(document).ready(function () {

    var $application = $("#APPLICATION_MNEMONIC").val(), $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}");

    // -----------
    // Input Masks
    // -----------

    $(".phone-mask").inputmask("(999) 999-9999", {showMaskOnHover: false});
    $(".zip-mask").inputmask("9{5}", {showMaskOnHover: false, removeMaskOnSubmit: true});

    $(".date-mask").inputmask({alias: "datetime",
        inputFormat: "mm/dd/yyyy",
        placeholder: "MM/DD/YYYY",
        showMaskOnHover: false});

    // --------------
    // Select Picker.
    // --------------

    $(".search-dropdown").selectpicker({noneSelectedText: "",
        style: "",
        styleBase: "form-select form-select-sm",
        iconBase: "bi",
        virtualScroll: true,
        size: 10,
        selectOnTab: true,
        containter: "body",
        liveSearch: true});

    // ------------
    // Date Pickers
    // ------------

    $("#rdi_entry_date").datepicker({format: "mm-dd-yyyy",
        container: "div.rdi_entry_date",
        keyboardNavigation: false,
        showOnFocus: false,
        autoclose: true,
        endDate: "0d",
        orientation: "left bottom",
        todayBtn: "linked",
        todayHighlight: true});

    $("#rdi_permit_date").datepicker({format: "mm-dd-yyyy",
        container: "div.rdi_permit_date",
        keyboardNavigation: false,
        showOnFocus: false,
        autoclose: true,
        endDate: "0d",
        orientation: "left bottom",
        todayBtn: "linked",
        todayHighlight: true});

    // ---------------------
    // Show Calendar Buttons
    // ---------------------

    $(".btnEntryDateCalendar").on("click", function (e) {

        e.preventDefault();

        $("#rdi_entry_date").focus().datepicker("show");

    });

    $(".btnPermitDateCalendar").on("click", function (e) {

        e.preventDefault();

        $("#rdi_permit_date").focus().datepicker("show");

    });

    // -----------
    // Save Button
    // -----------

    $(".btnSave").on("click", function (e) {

        e.preventDefault();

        $("#addRDI").submit();

    });

    // --------------
    // Cancel Button.
    // --------------

    $(".btnCancel").on("click", function () {

        $sessionStorage.action = $sessionStorage.addRDI || "";

        sessionStorage.setItem($application, JSON.stringify($sessionStorage));

        formSubmit($sessionStorage);

    });

    // ---------------
    // Validate Inputs
    // ---------------

    $("#addRDI").submit(function (e) {

        var rdi_entry_date = $("#rdi_entry_date"), rdi_county = $("#rdi_county");

        clearInputErrors();


        if (isBlank(rdi_entry_date)) {
            createInputError(rdi_entry_date, "Entry Date is required");
            return false;
        }

        if ($("option:selected", rdi_county).val() === "") {
            createInputError(rdi_county, "County is required");
            return false;
        }

        $.post("./", $(this).serialize())

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editRDI || "";

                    sessionStorage.setItem($application, JSON.stringify($sessionStorage));

                    formSubmit($sessionStorage);

                });

    });

});