
// ==========
// Add County
// ==========

$(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();
        $("#addCounty").submit();
    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        var county_name = $("#county_name"), county_area = $("#county_area");

        clearInputErrors();

        county_name.val(trimString(county_name.val()));

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

        if (formPost({action: "Counties.countyExists", county_serial: 0, county_name: county_name.val()}) === true) {
            createInputError(county_name, "Name already exists");
            return false;
        }

        if ($("option:selected", county_area).val() === "") {
            createInputError(county_area, "Area is required");
            return false;
        }


        e.preventDefault();

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

    });

});