
// ============
// Add Dropdown
// ============

$(document).ready(function() {

   var $application = $("#APPLICATION_MNEMONIC").val(),
      $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}");
  
  // -----------
  // Save Button
  // -----------
  
  $(".btnSave").on("click", function(e) {
    e.preventDefault();
    $("#addDropdown").submit();
  });

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

  $(".btnCancel").on("click", function() {
    formSubmit({ action : $sessionStorage.addDropdown });
  });

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

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

    var dropdowntype_serial = $("#dropdowntype_serial").val(),
        dropdown_value      = $("#dropdown_value");
        
    clearInputErrors();
    
    dropdown_value.val(trimString(dropdown_value.val()));

    if (isBlank(dropdown_value)) {
      createInputError(dropdown_value, "Value is required");
      return false;
    }
    
    if (formPost({ action : "Dropdowns.dropdownExists", dropdowntype_serial : dropdowntype_serial, dropdown_serial : 0, dropdown_value : dropdown_value.val() }) === true) {
      createInputError(dropdown_value, "Value already exists");
      return false;
    }
    
    e.preventDefault();
    
    $.post("./", $(this).serialize())
    
      .done(function() {
    
        $sessionStorage.action = $sessionStorage.addDropdown || "";
        $sessionStorage.toast  = "Dropdown was Added";

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

        formSubmit($sessionStorage);
    
    });
    
  });
  
});