$("#save,#update").on("click", function (e) { 
    var base_url = $("#base_url").val().trim();
    //Initially flag set true
    var flag = true;
  
    function check_field(id) {
      var fieldValue = $("#" + id).val();
      if (!fieldValue || !fieldValue.trim()) {
          //Also check Others????
          $("#" + id + "_msg")
              .fadeIn(200)
              .show()
              .html("Required Field")
              .addClass("required");
          // $('#'+id).css({'background-color' : '#E8E2E9'});
          flag = false;
      } else {
          $("#" + id + "_msg")
              .fadeOut(200)
              .hide();
          //$('#'+id).css({'background-color' : '#FFFFFF'});    //White color
      }
  }
  //Validate Input box or selection box should not be blank or empty
  check_field("bank_name");
  check_field("account_number"); 
  check_field("status");
  check_field("date");
  check_field("note"); 
  
  
    if (flag == false) {
      toastr["warning"]("You have Missed Something to Fillup!");
      return;
    }
  
    var this_id = this.id;  
    if (this_id == "save") {
      //Save start
      // if (confirm("Do You Wants to Save Record ?")) {
        e.preventDefault();
        data = new FormData($("#account-form")[0]); //form name
        /*Check XSS Code*/
        if (!xss_validation(data)) {
          return false;
        }
  
        $(".box").append(
          '<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>'
        );
        $("#" + this_id).attr("disabled", true); //Enable Save or Update button
        $.ajax({
          type: "POST",
          url: base_url + "account/save",
          data: data,
          cache: false,
          contentType: false,
          processData: false,
          success: function (result) {
            // alert(result);return;
            toastr["success"]("Record Added Successfully!");
            window.location = base_url + "account"; 
            $("#" + this_id).attr("disabled", false); //Enable Save or Update button
            $(".overlay").remove();
          },
        });
      // }
  
      //e.preventDefault
    } //Save end
    else if (this_id == "update") { 
      //Save start
      // if (confirm("Do You Wants to Update Record ?")) {
        e.preventDefault(); 
        data = new FormData($("#account-form")[0]); //form name
        /*Check XSS Code*/
        if (!xss_validation(data)) {
          return false;
        }
  
        $(".box").append(
          '<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>'
        );
        $("#" + this_id).attr("disabled", true); //Enable Save or Update button
        $.ajax({
          type: "POST",
          url: base_url + "account/update",
          data: data,
          cache: false,
          contentType: false,
          processData: false,
          success: function (response) {
            console.log("Response received:", response); // Log the response
            var result = JSON.parse(response);
        
            if (result.status === 'success') {
                toastr["success"](result.message);
                // Redirect to account list or perform other actions
                window.location.href = base_url + "account";
            } else {
                toastr["error"](result.message);
            }
            $("#" + this_id).attr("disabled", false); //Enable Save or Update button
            $(".overlay").remove();
        },
        });
      // }
  
      //e.preventDefault
    } //Save end
  });
  
  //update status start
  function update_status(id, status) {
    $.post("update_status", { id: id, status: status }, function (result) {
      if (result == "success") {
        toastr["success"]("Status Updated Successfully!");
        //alert("Status Updated Successfully!");
        success.currentTime = 0;
        success.play();
        if (status == 0) {
          status = "Inactive";
          var span_class = "label label-danger";
          $("#span_" + id).attr("onclick", "update_status(" + id + ",1)");
        } else {
          status = "Active";
          var span_class = "label label-success";
          $("#span_" + id).attr("onclick", "update_status(" + id + ",0)");
        }
  
        $("#span_" + id).attr("class", span_class);
        $("#span_" + id).html(status);
        return false;
      } else if (result == "failed") {
        toastr["error"]("Failed to Update Status.Try again!");
        failed.currentTime = 0;
        failed.play();
  
        return false;
      } else {
        toastr["error"](result);
        failed.currentTime = 0;
        failed.play();
        return false;
      }
    });
  }
  //update status end
  
  //Delete Record start
  function delete_account(q_id) {
    // if (confirm("Do You Wants to Delete Record ?")) {
      $(".box").append(
        '<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>'
      );
      $.post("account/delete", { q_id: q_id }, function (response) { 
          // console.log("Response received:", response);  
          // var result = response;
          // console.log(result.result) 

          // if (result.status === "success") {
            toastr["success"]('Account Deleted Successfully'); 
            window.location.href = "account";
          // } else {
          //   toastr["error"](result.message);
          // }
          $("#" + this_id).attr("disabled", false); //Enable Save or Update button
          $(".overlay").remove(); 
        return false;
      });
    // } //end confirmation
  }
  