
// =========
// Edit Note
// =========

$(document).ready(function () {

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

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

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

        e.preventDefault();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

    // -------------
    // Delete Button
    // -------------

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

        $("#confirm-dialog").removeClass().addClass("modal confirm-delete-note")
                .find(".modal-title").html("Confirm Delete Note").end()
                .find(".modal-body").html("Are you sure you wish to delete this Note?").end()
                .modal("show");

    });

    $("body").on("click", ".confirm-delete-note .btnConfirmDialogConfirm", function () {

        $.post("./", {action: "Notes.deleteNote", note_serial: $sessionStorage.note_serial})
                .done(function () {
                    $sessionStorage.action = $sessionStorage.deleteNote || "";
                    formSubmit($sessionStorage);
                });

    });

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

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

        var note_text = $("#note_text");

        clearInputErrors();

        if (isBlank(note_text)) {
            createInputError(note_text, "Note is required");
            return false;
        }

        e.preventDefault();

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

                .done(function () {

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

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

                    formSubmit($sessionStorage);

                });

    });

});