
// =============
// Choose Export
// =============

$(document).ready(function () {

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

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

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

    $(".accounting-period").inputmask("9999-99", {showMaskOnHover: false});

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

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

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

    $(".btnProcessDateCalendar").on("click", function (e) {
        e.preventDefault();
        $("#export_processdate").focus().datepicker("show");
    });

    // --------------
    // Select Pickers
    // --------------

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


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

    $(".btnRun").on("click", function (e) {
        e.preventDefault();
        $("#chooseExport").submit();
    });

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

    $(".btnCancel").on("click", function () {
        $sessionStorage.action = "";
        sessionStorage.setItem($application, JSON.stringify($sessionStorage));
        formSubmit($sessionStorage);
    });

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

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

        var export_process_date = $("#export_processdate"),
                export_accounting_period = $("#export_accounting_period"),
                export_type = $("#export_type");

        clearInputErrors();

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

        if ((!isBlank(export_process_date)) && (!export_process_date.inputmask("isComplete"))) {
            createInputError(export_process_date, "Date is incomplete");
            return false;
        }

        if (isBlank(export_accounting_period)) {
            createInputError(export_accounting_period, "Accounting Period is required");
            return false;
        }

        if ((!isBlank(export_accounting_period)) && (!export_accounting_period.inputmask("isComplete"))) {
            createInputError(export_accounting_period, "Accounting Period is incomplete");
            return false;
        }

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

        e.preventDefault();

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

                .done(function () {

                    $sessionStorage.action = $sessionStorage.run || "";
                    sessionStorage.setItem($application, JSON.stringify($sessionStorage));
                    formSubmit($sessionStorage);

                });

    });

});