// Attach the session CSRF token to existing forms and same-origin AJAX requests. $.ajaxPrefilter(function (options, original, xhr) { if (!options.crossDomain && !/^(GET|HEAD|OPTIONS)$/i.test(options.type)) { xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content') || ''); } }); $(function () { $('form[method="post" i]').each(function () { if (!$(this).find('[name="csrf_token"]').length) { $('', {type: 'hidden', name: 'csrf_token', value: $('meta[name="csrf-token"]').attr('content') || ''}).appendTo(this); } }); }); // ============== // Base Functions // ============== // -------------------------------- // Clear the Error State for Inputs // -------------------------------- function clearInputErrors() { $(".is-invalid").removeClass("is-invalid"); $(".invalid-feedback").remove(); } // ----------------------------------- // Create an Error State for an Input. // ----------------------------------- function createInputError(input, message = "", scroll = false) { if ($(input).hasClass("search-dropdown")) { // Select Picker $(input).closest(".bootstrap-select") .find("button").first() .addClass("is-invalid") .after("
" + message + "
") .focus(); } else { $(input).focus().addClass("is-invalid"); if ($(input).is(":last-child")) { $(input).after("
" + message + "
"); } else { $(input).siblings(":last").after("
" + message + "
"); } } if (scroll === true) { var element = document.getElementById(input.attr("id")); var header_offset = 130; var element_position = element.getBoundingClientRect().top; var offset_position = element_position + window.pageYOffset - header_offset; window.scrollTo({ top: offset_position, behavior: "smooth" }); } } // --------------------- // Test for Blank Value. // --------------------- function isBlank(object) { if ($.trim(object.val()).length == 0) { return true; } else { return false; } } // ------------------------ // Verify an Email Address. // ------------------------ function isEmail(email) { var valid = false; var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,6})+$/; return regex.test(email.val()); } // ------------------------ // Validate a URL (syntax). // ------------------------ function isURL(object) { return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(object.val()); } // --------------------------------- // AJAX Call to Get Zip Code Detail. // --------------------------------- async function getZipCodeDetail(zipcode) { return await formPost({action:'getZipcode',zip_code:zipcode}); } // ----------------------------- // AJAX Call to Validate a Date. // ----------------------------- async function isDate(date) { return await formPost({action:'validateDate',date:date.val()}); } // --------------------------------------------- // Ending Date cannot be prior to Starting Date. // --------------------------------------------- function checkEndingDate(startingDate, endingDate) { var parts = startingDate.val().match(/(\d+)/g); var sDate = new Date(parts[0], parts[1] - 1, parts[2]); parts = endingDate.val().match(/(\d+)/g); var eDate = new Date(parts[0], parts[1] - 1, parts[2]); sDate.setHours(0, 0, 0, 0); eDate.setHours(0, 0, 0, 0); // The Ending Date cannot be prior to the Starting Date. if (eDate.getTime() < sDate.getTime()) { return false; } else { return true; } } // ------------------------------------------------------- // Convert a string to Title Case and remove extra spaces. // ------------------------------------------------------- function titleCase(string) { string = string.replace(/([^\W_]+[^\s-]*) */g, function (s) { return s.charAt(0).toUpperCase() + s.slice(1); }); string = string.replace(/\s+/g, " ").trim(); return string; } // ------------------------------------------ // Trim a string and remove extra white space // ------------------------------------------ function trimString(string) { return string.replace(/\s+/g, " ").trim(); } // ------------------- // Post a Form (AJAX). // ------------------- function formPost(inputs) { return $.ajax({url:'./',type:'POST',dataType:'json',data:inputs}); } // -------------- // Submit a Form. // -------------- function formSubmit(inputs, target = "") { const form=$("#formSubmit"); form.prop('target',target); form.find('input').not('[name="csrf_token"]').remove(); Object.entries(inputs).forEach(([name,value])=>{ if(name!=='csrf_token' && value!==undefined && value!==null)$('',{type:'hidden',name:name,value:String(value)}).appendTo(form); }); form.get(0).submit(); } $(document).ajaxError(function(event,xhr){ const message=(xhr.responseJSON && xhr.responseJSON.error) || (xhr.status===401?'Your session has expired. Reload the page to sign in.':'The request failed. Please try again.'); showRequestMessage(message,'danger'); }); $(document).ajaxStart(function(){ $('.btnSave,.btnConfirmDialogConfirm').filter(':enabled').addClass('request-busy').prop('disabled',true).attr('aria-busy','true'); }).ajaxStop(function(){ $('.request-busy').prop('disabled',false).removeClass('request-busy').removeAttr('aria-busy'); }); // jQuery reports these failures through ajaxError above. Stop the rejected event // handler without also producing an unhandled promise error in the browser. window.addEventListener('unhandledrejection',function(event){ if(event.reason && typeof event.reason.status==='number')event.preventDefault(); }); function showRequestMessage(message,kind='info'){ let box=$('#request-message'); let target=$('.modal.show .modal-body').first(); if(!target.length)target=$('.hr-workspace,main,body').first(); if(!box.length)box=$('
',{id:'request-message',role:'alert',tabindex:'-1'}); box.prependTo(target); box.attr('class','alert alert-'+kind).text(message).trigger('focus'); } $(document).ready(function () { var $application = $("#APPLICATION_MNEMONIC").val(), $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}"); // ---------------------------- // Enable Tooltips and Popovers // ---------------------------- document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(element=>{ const title=element.getAttribute('data-bs-title')||element.getAttribute('title')||''; if(title)bootstrap.Tooltip.getOrCreateInstance(element,{title:title,trigger:'hover',container:'body'}); }); document.querySelectorAll('[data-bs-toggle="popover"]').forEach(element=>{ const title=element.getAttribute('data-bs-title')||element.getAttribute('title')||''; const content=element.getAttribute('data-bs-content')||''; if(title||content)bootstrap.Popover.getOrCreateInstance(element,{title:title,content:content,trigger:'focus',container:'body'}); }); // ------------- // Display Toast // ------------- const toast = $sessionStorage.toast || ""; if (toast !== "") { $(".toast-body").text(toast); $("#toast-message").toast("show"); $sessionStorage.toast = ""; sessionStorage.setItem($application, JSON.stringify($sessionStorage)); } });