
// =================
// Edit Dropdowntype
// =================

$(document).ready(function() {

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

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

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

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

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

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

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

    } else {

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

    } 
    
  });

  $("body").on("click", ".confirm-delete-dropdowntype .btnConfirmDialogConfirm", function() {
    
    $.post("./", { action : "Dropdowns.deleteDropdowntype", dropdowntype_serial : $sessionStorage.dropdowntype_serial })
    
      .done(function() {
        
        $sessionStorage.action = $sessionStorage.editDropdowntype || "";
        $sessionStorage.toast  = "Dropdown Type was Deleted";
    
        sessionStorage.setItem($application, JSON.stringify($sessionStorage));

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

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

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

    var dropdowntype_serial = $("#dropdowntype_serial"),
        dropdowntype_name   = $("#dropdowntype_name"),
        dropdowntype_title  = $("#dropdowntype_title"),
        dropdowntype_table  = $("#dropdowntype_table"),
        dropdowntype_column = $("#dropdowntype_column");
        
    clearInputErrors();
    
    dropdowntype_name.val(dropdowntype_name.val().replace(/\s+/g, " ").trim());
    dropdowntype_title.val(dropdowntype_title.val().replace(/\s+/g, " ").trim());
    dropdowntype_table.val(dropdowntype_table.val().replace(/\s+/g, " ").trim());
    dropdowntype_column.val(dropdowntype_column.val().replace(/\s+/g, " ").trim());

    if (isBlank(dropdowntype_name)) {
      createInputError(dropdowntype_name, "Name is required");
      return false;
    }
    
    if (formPost({ action : "Dropdowns.dropdowntypeExists", dropdowntype_serial : dropdowntype_serial.val(), dropdowntype_name : dropdowntype_name.val() }) === true) {
      createInputError(dropdowntype_name, "Name already exists");
      return false;
    }

    if (isBlank(dropdowntype_title)) {
      createInputError(dropdowntype_title, "Title is required");
      return false;
    }
    
    if (formPost({ action : "Dropdowns.dropdowntypetitleExists", dropdowntype_serial : dropdowntype_serial.val(), dropdowntype_title : dropdowntype_title.val() }) === true) {
      createInputError(dropdowntype_title, "Title already exists");
      return false;
    }

    if (isBlank(dropdowntype_table)) {
      createInputError(dropdowntype_table, "Table is required");
      return false;
    }

    if (isBlank(dropdowntype_column)) {
      createInputError(dropdowntype_column, "Column is required");
      return false;
    }
    
    e.preventDefault();
    
    $.post("./", $(this).serialize())
    
      .done(function() {
    
        $sessionStorage.action = $sessionStorage.editDropdowntype || "";
        $sessionStorage.toast  = "Dropdown Type was Saved";

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

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