
// =============
// Edit Dropdown
// =============

$(document).ready(function() {

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

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

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

  // -------------
  // Delete Button
  // -------------

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

    if (formPost({ action : "Dropdowns.dropdownActive", dropdown_serial : $sessionStorage.dropdown_serial }) === true) {

      $("#continue-dialog").find(".modal-title").html("Cannot Delete Dropdown").end()
                           .find(".modal-body").html("This Dropdown is in use and cannot be deleted.").end()
                           .modal("show");

    } else {

      $("#confirm-dialog").removeClass().addClass("modal confirm-delete-dropdown")
                          .find(".modal-title").html("Confirm Delete Dropdown").end()
                          .find(".modal-body").html("Are you sure you wish to delete this Dropdown?").end()
                          .modal("show");

    } 
    
  });

  $("body").on("click", ".confirm-delete-dropdown .btnConfirmDialogConfirm", function() {
    
    $.post("./", { action : "Dropdowns.deleteDropdown", dropdown_serial : $sessionStorage.dropdown_serial })
    
      .done(function() {
        
        $sessionStorage.action = $sessionStorage.editDropdown || "";
        $sessionStorage.toast  = "Dropdown was Deleted";
    
        sessionStorage.setItem($application, JSON.stringify($sessionStorage));

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

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

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

    var dropdowntype_serial = $("#dropdowntype_serial").val(),
        dropdown_serial     = $("#dropdown_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 : dropdown_serial, dropdown_value : dropdown_value.val() }) === true) {
      createInputError(dropdown_value, "Value already exists");
      return false;
    }
    
    e.preventDefault();
    
    $.post("./", $(this).serialize())
    
      .done(function() {
    
        $sessionStorage.action = $sessionStorage.editDropdown || "";
        $sessionStorage.toast  = "Dropdown was Saved";

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

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