
// ============
// Add SIC Code
// ============

$(document).ready(function () {

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

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

    $("#siccode_code").inputmask("9{1,5}"); //mask with dynamic syntax

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

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

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        var siccode_code = $("#siccode_code"), siccode_description = $("#siccode_description");

        clearInputErrors();

        siccode_code.val(trimString(siccode_code.val()));
        siccode_description.val(trimString(siccode_description.val()));

        if (isBlank(siccode_code)) {
            createInputError(siccode_code, "Code is required");
            return false;
        }

        if (formPost({action: "SICCodes.siccodeExists", siccode_serial: 0, siccode_code: siccode_code.val()}) === true) {
            createInputError(siccode_code, "Code already exists");
            return false;
        }

        if (isBlank(siccode_description)) {
            createInputError(siccode_description, "Description is required");
            return false;
        }

        e.preventDefault();

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

                .done(function () {

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

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

                    formSubmit($sessionStorage);

                });
    });

});