
// ========
// Add Note
// ========

$(document).ready(function () {

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

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

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

        e.preventDefault();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

    $("#addNote").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.addNote || "";

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

                    formSubmit($sessionStorage);

                });

    });

});