
// =======================
// Edit Users Subscription
// =======================

$(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});

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

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

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


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

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

        e.preventDefault();

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

    });

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

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

        e.preventDefault();

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

    });

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

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

        e.preventDefault();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        var start_date = $("#subscription_start_date"), end_date = $("#subscription_end_date");


        clearInputErrors();

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

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

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

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

        let startDate = new Date(start_date.val()),
                endDate = new Date(end_date.val());

        if (startDate > endDate) {
            createInputError(start_date, "Date is after End Date");
            return false;
        }

        e.preventDefault();

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

                .done(function () {

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

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

                    formSubmit($sessionStorage);

                });

    });

});