
// ==========
// Add ProjectType
// ==========

$(document).ready(function () {

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

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

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

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

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

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        var projecttype_name = $("#projecttype_name"), projecttype_area = $("#projecttype_area");

        clearInputErrors();

        projecttype_name.val(trimString(projecttype_name.val()));

        if (isBlank(projecttype_name)) {
            createInputError(projecttype_name, "Name is required");
            return false;
        }

        if (formPost({action: "ProjectTypes.projecttypeExists", projecttype_serial: 0, projecttype_name: projecttype_name.val()}) === true) {
            createInputError(projecttype_name, "Name already exists");
            return false;
        }

        e.preventDefault();

        $.post("./", $(this).serialize())
                .done(function () {
                    formSubmit({action: $sessionStorage.addProjectType});
                });

    });

});