/*---------------------------------------------------------------------------
* Clears the value of an input element.
* Used for resetting fields dynamically.
--------------------------------------------------------------------------- */
function clear_value( element_id ) {
$( element_id ).val("");
}
/*---------------------------------------------------------------------------
* Prevents form submission when Enter key is pressed.
* Useful in modal forms to avoid accidental submits.
--------------------------------------------------------------------------- */
function disable_form_on_enter(element_id){
$(element_id).on("keypress", function (event) {
var keyPressed = event.keyCode || event.which;
if (keyPressed === 13) {
//alert("You pressed the Enter key!!");
event.preventDefault();
return false;
}
});
};
/* ---------------------------------------------------------------------------
* Handles a complete modal-based AJAX workflow:
* 1. Loads form HTML into Modal_1
* 2. Validates form
* 3. Submits data via AJAX
* 4. Displays response via modal or inline alert
*
* @param title Modal title
* @param button Button labels [primary, secondary]
* @param step Array of URLs & optional JS callback
* @param form_id Form selector
* @param notify modal | inline | none
* @param notify_div Target div for inline messages
---------------------------------------------------------------------------*/
function modal_ajax_process ( title, button, step, form_id, notify = 'modal', notify_div = '' ) {
$('#Modal_1 .modal_button_1').removeAttr('disabled');
$('#Modal_1 .modal-title').html( title );
$.ajax({
url: step[0],
method: 'POST',
success: function( result ) {
//console.log(result);
$('#Modal_1 .modal-body').empty();
$('#Modal_1 .modal-body').html(result);
disable_form_on_enter('form input');
// REMOVE PREVIOUS CLICK FUNCTION AND ADD NEW ONE
$('#Modal_1 .modal_button_1').unbind('click');
$('#Modal_1 .modal_button_1').on('click').click(function() {
var form = $(form_id);
//alert ( form_id + ' = ' + form.serialize() );
if(!$(form_id)[0].checkValidity())
{
$(form_id)[0].reportValidity();
//$(form_id).find("#submit-hidden").click();
return false;
}
$('#Modal_1 .modal_button_1').attr('disabled','disabled');
let formToWorkOn = document.querySelector(form_id);
var formData = new FormData(formToWorkOn);
$.ajax({
url: step[1],
method: 'POST',
//data: form.serialize(),
data: formData,
async: true,
cache: false,
contentType: false,
processData: false,
success: function( result,textStatus, jqXHR ) {
//alert(result);
if (step[2]) { eval( step[2] ); }
// ***** START - BRING THIS INTO PANEL *****
if ( notify == 'modal') {
// SHOW FINAL NOTIFICATION MODAL
//modal_alert( result, 'ok', true);
if(jqXHR.status==200){
modal_alert( result, 'ok', true);
}else if(jqXHR.status==250){
modal_alert( result, 'warn', true);
}else if(jqXHR.status==260){
modal_alert( result, 'error', true);
}else{
modal_alert( result, 'ok', true);
}
} else if ( notify == 'inline') {
// SHOW FINAL NOTIFICATION MESSAGE INLINE
if(jqXHR.status==200){
inline_alert( result, 'ok', notify_div);
}else if(jqXHR.status==250){
inline_alert( result, 'warn', notify_div);
}else if(jqXHR.status==260){
inline_alert( result, 'error', notify_div);
}else{
inline_alert( result, 'error', notify_div);
}
/*
$(notify_div).html(result);
autoCloseAlert();
*/
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
} else if ( notify == 'none') {
// SKIP FINAL NOTIFICATION MODAL
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
}
// ***** END - BRING THIS INTO PANEL *****
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
});
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
}); //END-AJAX()
$('#Modal_1 .modal_button_1').html( button[0] );
$('#Modal_1 .modal_button_2').html( button[1] );
$('#Modal_1').modal('show');
}
/**---------------------------------------------------------------------------
* Displays alert messages in Modal_2.
* Supports success, warning, and error states.
* Can optionally refresh UI after close.
--------------------------------------------------------------------------- */
function modal_alert( message, status, refresh=false ) {
if ( status == "error" ) {
color = '#FF0000';
icon = '';
text = 'Error';
$('#Modal_2 .modal-header').css('color', '#000000');
$('#Modal_2 .modal-header').css('background-color', '#FF0000');
$('#Modal_2 .modal-title').html(icon +' '+ text);
$('#Modal_2 .modal-body').html( message );
$('#Modal_2 .modal_button_1').html(icon +' OK');
}
if ( status == "warn" ) {
color = '#FF0000';
icon = '';
text = 'Warning';
$('#Modal_2 .modal-header').css('color', '#000000');
$('#Modal_2 .modal-header').css('background-color', '#FFFF00');
$('#Modal_2 .modal-title').html(icon +' '+ text);
$('#Modal_2 .modal-body').html( message );
$('#Modal_2 .modal_button_1').html(icon +' OK');
}
if ( status == "ok" ) {
color = '#00FF00';
icon = '';
text = 'Success';
$('#Modal_2 .modal-header').css('color', '#FFFFFF');
$('#Modal_2 .modal-header').css('background-color', '#00FF00');
$('#Modal_2 .modal-title').html(' Success');
$('#Modal_2 .modal-body').html( message );
$('#Modal_2 .modal_button_1').html(' OK');
if ( refresh ) {
$('#Modal_2').on('hidden.bs.modal', function (e) {
//location.reload(true);
$('#Modal_1').modal('dispose');
$('#Modal_2').modal('dispose');
});
}
}
$('#Modal_1').modal('hide');
$('#Modal_2').modal('show');
$('#Modal_3').modal('hide');
}
/**---------------------------------------------------------------------------
* Displays Bootstrap inline alerts inside a target container.
* Automatically closes after a timeout.
---------------------------------------------------------------------------*/
function inline_alert( message, status, notify_div ) {
if ( status == "error" ) {
var errorClass = 'alert-danger';
}else if ( status == "warn" ) {
var errorClass = 'alert-warning';
}else if ( status == "ok" ) {
var errorClass = 'alert-success';
}
var alert_message = '';
alert_message += '
';
alert_message += '
';
alert_message += '
';
alert_message += message;
alert_message += '
';
alert_message += '
';
$(notify_div).html(alert_message);
autoCloseAlert();
}
/**---------------------------------------------------------------------------
* Automatically fades out and removes alerts after 4 seconds.
--------------------------------------------------------------------------- */
function autoCloseAlert()
{
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 4000);
}
/* --------------------------------------------------------------------------- */
function checkinput(input)
{
if (input.value != "")
{
input.setCustomValidity('');
}
}
/**---------------------------------------------------------------------------
* Validates a single form field via AJAX.
* Used on blur/change events for real-time validation.
*
* @param element_id Input field ID
* @param form_id Parent form selector
* @param notify modal | inline
---------------------------------------------------------------------------*/
function field_validation_ajax_process ( element_id, form_id, notify = 'modal') {
var element = $('#' + element_id);
if(notify=='inline')
{
element.removeClass("is-invalid");
$(".validation-feedback-" + element_id).removeClass('invalid-feedback')
$(".validation-feedback-" + element_id).html('');
//element.next(".validation-feedback").removeClass('invalid-feedback')
//element.next(".validation-feedback").html('');
}
var form = $(form_id);
var validateurl = 'https://dev.portal.losspreventionfoundation.org/core/ajax/validate.php';
$.ajax({
url: validateurl,
method: 'POST',
data: form.serialize() + "&validate_field=" + element_id,
dataType: 'JSON',
success: function( result,textStatus, jqXHR ) {
var error_count = Object.keys(result).length;
if(error_count > 0)
{
var error_string = '';
$.each(result, function(key, val){
if(notify=='inline')
{
element.addClass("is-invalid");
$(".validation-feedback-" + element_id).addClass('invalid-feedback')
$(".validation-feedback-" + element_id).html(val);
//element.next(".validation-feedback").addClass('invalid-feedback')
//element.next(".validation-feedback").html(val);
}
error_string += val + '
';
});
if ( notify == 'modal')
{
modal_alert( error_string, 'error' );
}
}
else
{
return true;
}
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
}
/**---------------------------------------------------------------------------
* Wrapper for triggering field validation
* based on form configuration.
---------------------------------------------------------------------------*/
function validate_form_field(element_id,form_id)
{
var form = $('#'+form_id);
var formID = '#'+form_id;
var notify_type = form.attr("data-validate-display-edit");
if(notify_type==''){
notify_type = 'inline';
}
field_validation_ajax_process( element_id , formID , notify_type);
}
/**---------------------------------------------------------------------------
* Validates entire form via AJAX and submits data.
* Supports:
* - Inline or modal error display
* - Optional file upload validation
* - Multiple file inputs
*
* @param form_id
* @param url
* @param notify_form_type modal | inline | replace | none
* @param notify_div
* @param fileinputID
* @param validateFileUpload
---------------------------------------------------------------------------*/
function form_validate_save(form_id, url, notify_form_type='modal', notify_div='', fileinputID='', validateFileUpload=false) {
var form = $('#'+form_id);
var notify_type = form.attr("data-validate-display-save");
if(notify_type==''){
notify_type = 'inline';
}
if(notify_type=='inline')
{
$('input').removeClass("is-invalid");
$("[class*=validation-feedback]").removeClass("invalid-feedback");
$("[class*=validation-feedback]").html("");
//$('.validation-feedback').removeClass("invalid-feedback");
//$('.validation-feedback').html("");
}
var validateurl = 'https://dev.portal.losspreventionfoundation.org/core/ajax/validate.php';
$.ajax({
url: validateurl,
method: 'POST',
data: form.serialize(),
dataType: 'JSON',
success: function( result,textStatus, jqXHR ) {
var error_count = Object.keys(result).length;
if(error_count > 0)
{
var error_string = '';
$.each(result, function(key, val){
if(notify_type=='inline')
{
$('input[name='+key+']').addClass('is-invalid ');
$('select[name='+key+']').addClass('is-invalid ');
$(".validation-feedback-" + key).addClass('invalid-feedback')
$(".validation-feedback-" + key).html(val);
//$('input[name='+key+']').next(".validation-feedback").html(val);
//$('input[name='+key+']').next(".validation-feedback").addClass('invalid-feedback');
}
error_string += val + '
';
});
if ( notify_type == 'modal')
{
modal_alert( error_string, 'error' );
}
}
else
{
//IF FORM HAS FILE UPLOAD THEN "fileinputID" FIELD IS PASSED
//IT CAN BE A STING WITH ID OF SINGLE INPUT FIELD IF ONLY ONE FILE UPLOAD FIELD IN THE FORM
//IT CAN BE ARRAY ALSO IF FORM HAS MULTIPLE FILE UPLOAD FIELDS
//EG. var arr1 = ["datasheet", false, "Please Upload Datasheet File!"];
//FIRST VALUE IS ID OF THE FIELD, 2ND IS FLAG IF IT IS MANDATORY TO UPLOAD IMAGE BEFORE SAVING
// 3RD IS MESSAGE TO DISPLAY IF REQUIRED AND IMAGE NOT SELECTED
if(jQuery.isArray(fileinputID)){
for(var i = 0; i < fileinputID.length; i++) {
//LOOP THROUGH ALL THE ARRAY ELEMENTS AND DISPLAY ERROR IF IT IS REQUIRED AND FILE NOT SELECTED
// THIS IS JUST DONE BEFORE STORING THE DATA TO DATABASE
//THIS WILL NOT UPLOAD THE IMAGE HERE. IT JUST TO PREVIENT FORM SUBMISSION IF IT IS REQUIRED AND FILE NOT SELECTED
if(fileinputID[i][1]==true)
{
if($('#'+fileinputID[i][0]).fileinput('getFilesCount')==0){
modal_alert( fileinputID[i][2], 'error' );
return;
}
}
}
}
else{
//IF ONLY SINGLE FILE UPLOAD WE HAVE IN FORM AND FILE IS NOT SELECTED IT WILL GIVE ERROR MODAL TO SELECT FILE
if(fileinputID != '' && validateFileUpload==true)
{
if($('#'+fileinputID).fileinput('getFilesCount')==0){
modal_alert( 'Please select file', 'error' );
return;
}
}
}
$.ajax({
url: url,
method: 'POST',
data: form.serialize(),
processData: false,
dataType: 'JSON',
//contentType: false,
success: function( result,textStatus, jqXHR ) {
$("#validate_id").val(result.record_id);
if(result.status==200)
{
//CALL FILEINPUT JQUERY OF ALL THE INPUT FILDS
if(jQuery.isArray(fileinputID)){
for(var i = 0; i < fileinputID.length; i++) {
//MANUALLY UPLOADING filesize
$('#'+fileinputID[i][0]).fileinput('upload');
}
}
else
{
// IF SINGLE FILEUPLOAD WE WILL CALL THE UPLOAD FUNCTION MANUALLY
if(fileinputID != '')
{
$('#'+fileinputID).fileinput('upload');
}
}
}
// ***** START - BRING THIS INTO PANEL *****
if ( notify_form_type == 'modal') {
// SHOW FINAL NOTIFICATION MODAL
if(jqXHR.status==200){
modal_alert( result.content, 'ok', true);
}else if(jqXHR.status==250){
modal_alert( result.content, 'warn', true);
}else if(jqXHR.status==260){
modal_alert( result.content, 'error', true);
}else{
modal_alert( result.content, 'error', true);
}
} else if ( notify_form_type == 'inline') {
// SHOW FINAL NOTIFICATION MESSAGE INLINE
if(jqXHR.status==200){
inline_alert( result.content, 'ok', notify_div);
}else if(jqXHR.status==250){
inline_alert( result.content, 'warn', notify_div);
}else if(jqXHR.status==260){
inline_alert( result.content, 'error', notify_div);
}else{
inline_alert( result.content, 'error', notify_div);
}
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
}else if ( notify_form_type == 'replace') {
$(notify_div).html(result.content);
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
} else if ( notify_form_type == 'none') {
// SKIP FINAL NOTIFICATION MODAL
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
}
// ***** END - BRING THIS INTO PANEL *****
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
}
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
}
/**---------------------------------------------------------------------------
* Copies input field value to clipboard.
---------------------------------------------------------------------------*/
function copy_to_clipboard( element_id ) {
var copyText = $( element_id ).val()
navigator.clipboard.writeText(copyText);
}
/* ---------------------------------------------------------------------------
* Generic AJAX content replacement utility.
* Used for search, pagination, and filters.
--------------------------------------------------------------------------- */
function fun_ajax_replace(url,form_id,element_id)
{
var form = $(form_id);
$.ajax({
url: url,
method: 'POST',
data: form.serialize() + '&request_type=ajax',
success: function( result,textStatus, jqXHR ) {
$(element_id).html(result);
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
}
/** ---------------------------------------------------------------------------
* AJAX-powered search.
---------------------------------------------------------------------------*/
function fun_search(url,form_id,element_id)
{
fun_ajax_replace(url,form_id,element_id);
}
/** ---------------------------------------------------------------------------
* AJAX-powered pagination.
--------------------------------------------------------------------------- */
function fun_pagination(url,form_id,element_id)
{
fun_ajax_replace(url,form_id,element_id);
}
/** ---------------------------------------------------------------------------
* AJAX-powered pagination. NEW UPDATED FUNCTION
--------------------------------------------------------------------------- */
function fun_paginate(url,element_id)
{
$.ajax({
url: url,
method: 'GET',
success: function( result,textStatus, jqXHR ) {
$(element_id).html(result);
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
}
/** ---------------------------------------------------------------------------
* Lightweight AJAX form submission with notification support.
--------------------------------------------------------------------------- */
function ajax_process(form_id,step,notify='modal',notify_div='')
{
var form = $(form_id);
if(!$(form_id)[0].checkValidity())
{
$(form_id)[0].reportValidity();
return false;
}
$.ajax({
url: step[0],
method: 'POST',
data: form.serialize(),
success: function( result,textStatus, jqXHR ) {
if (step[1]) { eval( step[1] ); }
// ***** START - BRING THIS INTO PANEL *****
if ( notify == 'modal') {
// SHOW FINAL NOTIFICATION MODAL
//modal_alert( result, 'ok', true);
if(jqXHR.status==200){
modal_alert( result, 'ok', true);
}else if(jqXHR.status==250){
modal_alert( result, 'warn', true);
}else if(jqXHR.status==260){
modal_alert( result, 'error', true);
}else{
modal_alert( result, 'ok', true);
}
} else if ( notify == 'inline') {
// SHOW FINAL NOTIFICATION MESSAGE INLINE
if(jqXHR.status==200){
inline_alert( result, 'ok', notify_div);
}else if(jqXHR.status==250){
inline_alert( result, 'warn', notify_div);
}else if(jqXHR.status==260){
inline_alert( result, 'error', notify_div);
}else{
inline_alert( result, 'error', notify_div);
}
/*
$(notify_div).html(result);
autoCloseAlert();
*/
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
} else if ( notify == 'none') {
// SKIP FINAL NOTIFICATION MODAL
$('#Modal_1').modal('hide');
$('#Modal_2').modal('dispose');
}
// ***** END - BRING THIS INTO PANEL *****
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
});
}
/** ---------------------------------------------------------------------------
* Loads dynamic content into Modal_3 with custom buttons.
--------------------------------------------------------------------------- */
function custom_modal_ajax_process ( title, buttons, url) {
$('#Modal_3 .modal-title').html( title );
$('#Modal_3 .modal-footer').html('');
$.each(buttons, function(key, value) {
$('#Modal_3 .modal-footer').append(value);
});
$.ajax({
url: url,
method: 'POST',
success: function( result ) {
$('#Modal_3 .modal-body').empty();
$('#Modal_3 .modal-body').html(result);
disable_form_on_enter('form input');
},
error: function( xhr, status, error ) {
if (xhr.status == 404 || xhr.status == 400) {
modal_alert( xhr.responseText, 'error');
}else{
modal_alert( xhr.responseText, 'warn');
}
},
}); //END-AJAX()
$('#Modal_3').modal('show');
}
/** ---------------------------------------------------------------------------
* Wraps dynamic HTML content inside Shadow DOM
* to prevent CSS conflicts and enforce image scaling.
--------------------------------------------------------------------------- */
function loadShadowDomContent() {
$('div.shadow-dom-content').each(function() {
var $this = $(this);
// Save original content (HTML or Text)
var originalContent = this.innerHTML; // Use plain DOM, not jQuery .html()
// Attach Shadow DOM without clearing first
var shadowRoot = this.attachShadow({ mode: 'open' });
// Now move the content into the Shadow DOM
shadowRoot.innerHTML = `
${originalContent}
`;
// After that, optional: you can clear the light DOM to avoid duplication
$this.html('');
});
}