Southlake Pain Institute { if (sett_obj._id === widget_id) { setting = sett_obj; } }); let ret_str; if (setting && (typeof (setting.display_composition) !== "undefined")) { const json_text = JSON.stringify({ display_composition: setting.display_composition, phone_numb_format: setting.phone_numb_format }); ret_str = ``; } else { ret_str = ``; } return ret_str; }, 10, 4); } elementorFrontend.hooks.addAction('frontend/element_ready/form.default', function (element) { let t_selector_options = ""; const phone_prefix_selectors = document.querySelectorAll('.startklar_country_selector'); const selectors = jQuery(element).find('.startklar_country_selector'); if (phone_prefix_selectors.length) { if (typeof (window.phone_number_prefix_selector_options) !== "undefined") { const setting = jQuery(".startklar_country_selector").data("options"); if (typeof (setting.phone_numb_format) !== "undefined" && setting.phone_numb_format === "old_format") { t_selector_options = window.phone_number_prefix_selector_options.replace(/\(\+([0-9\s]+?)\)/gi, '(00$1)'); } else if (typeof (setting.phone_numb_format) !== "undefined" && setting.phone_numb_format === "clean_format") { t_selector_options = window.phone_number_prefix_selector_options.replace(/\(\+([0-9\s]+?)\)/gi, '+$1'); } else { t_selector_options = window.phone_number_prefix_selector_options; } if (typeof (setting.display_composition) !== "undefined" && (setting.display_composition.toLowerCase().indexOf("flag") === -1)) { t_selector_options = t_selector_options.replace(/data-icon="[^"]*"/gi, ''); } if (typeof (setting.display_composition) !== "undefined" && (setting.display_composition.toLowerCase().indexOf("name") === -1)) { jQuery(".startklar_country_selector").addClass("hide_counties_names"); } } jQuery(".startklar_country_selector").html(t_selector_options); jQuery('.startklar_country_selector').select2({ templateSelection: startklarCountrySelectorformatText, templateResult: startklarCountrySelectorRsltformatText, selectionTitleAttribute: false }); clearSelectTitle(); jQuery(document).on('change.select2', () => { clearSelectTitle(); }); jQuery('.select2-selection.select2-selection--single').addClass("elementor-field-textual"); jQuery('.select2-selection.select2-selection--single').addClass("elementor-field"); jQuery(document).on('select2:open', () => { document.querySelector('.select2-search__field').focus(); if (jQuery(".startklar_country_selector.hide_counties_names").length) { jQuery(".select2-container .select2-dropdown").addClass("hide_counties_names"); } }); const p_form = jQuery(".startklar_country_selector").closest("form"); if (typeof p_form !== "undefined") { jQuery(p_form).on('submit_success', function () { jQuery('.startklar_country_selector', this).val('').trigger("change"); }); } } window.addEventListener('load', function (event) { selectors.each(function (index, selector) { const default_val = jQuery(selector).data("default_val"); if (default_val) { selectOptionByCountry(default_val, selector); } else { getDetectedCountry().then(function (country_en) { selectOptionByCountry(country_en, selector); }); } const parent = selector.parentElement; if (isNextFieldPhoneNumberField(parent)) { const phoneField = parent.nextElementSibling.children[0] phoneField.addEventListener('input', function() { debouncedHandleAutocomplete(selector); }); } }); }); }); function isNextFieldPhoneNumberField(prefixField) { const nextField = prefixField.nextElementSibling; return nextField && nextField.classList.contains('elementor-field-type-tel'); } function containsPotentialPrefix(phoneNumberField) { const phoneNumberValue = phoneNumberField.value; const potentialPrefixPatterns = [ /\(\+\d+\)/, // Matches patterns like "(+380)" /\+\d+\s/ // Matches patterns like "+345 " ]; return potentialPrefixPatterns.some(function(pattern) { return pattern.test(phoneNumberValue); }); } function debounce(func, delay) { let timeoutId; return function() { clearTimeout(timeoutId); timeoutId = setTimeout(() => { func.apply(this, arguments); }, delay); }; } const debouncedHandleAutocomplete = debounce(function(prefixField) { const phoneNumberField = prefixField.parentElement.nextElementSibling.children[0]; if (containsPotentialPrefix(phoneNumberField)) { const phoneNumberValue = phoneNumberField.value; const potentialPrefix = phoneNumberValue.match(/\(\+\d+\)|\+\d+\s/); const cleanedPrefix = potentialPrefix[0].replace("(", "").replace(")", " "); if (cleanedPrefix) { const prefixOption = prefixField.querySelector('option[value="' + cleanedPrefix + '"]'); if (prefixOption) { jQuery(prefixOption).prop('selected', true).parent().trigger('change'); } phoneNumberField.value = phoneNumberValue.replace(potentialPrefix[0], ''); } } }, 300); }); } else { setTimeout(testjstartklarCountrySelectorQueryExist, 100); } } function startklarCountrySelectorformatText(icon) { let str = ""; if (typeof icon.element !== "undefined") { const phone_code = /\+\d+/g.exec(icon.text); const icon_src = jQuery(icon.element).data('icon'); let icon_code = ''; if (typeof icon_src !== "undefined" && icon_src.length) { icon_code = ''; } if (typeof phone_code !== "undefined" && phone_code != null && phone_code.length) { str = '' + icon_code + phone_code[0] + ''; } } return jQuery(str); } function startklarCountrySelectorRsltformatText(icon) { let str; if (typeof icon.element !== "undefined") { const icon_src = jQuery(icon.element).data('icon'); let icon_code = ''; if (typeof icon_src !== "undefined" && icon_src.length) { icon_code = ''; } str = '' + icon_code + icon.text + ''; } return jQuery(str); } function getDetectedCountry() { return new Promise((resolve, reject) => { jQuery.post("https://ispusa.net/wp-admin/admin-ajax.php?action=startklar_country_selector_process", function (data) { const data_country = data["country"]; let country_en = null; if (typeof data_country !== "undefined") { jQuery('.startklar_country_selector > option').each(function () { const optionCountry = jQuery(this).data('country_en'); if (optionCountry === data_country) { country_en = optionCountry; return false; } }); } if (country_en !== null) { resolve(country_en); } else { reject("Country code not found"); } }, "json"); }); } function selectOptionByCountry(countryName, selector) { jQuery(selector).find('option').each(function () { const optionCountry = jQuery(this).data('country_en'); if (optionCountry === countryName) { jQuery(this).prop('selected', true).parent().trigger('change'); return false; } }); } function clearSelectTitle() { const select_items = jQuery(".select2.select2-container .select2-selection.select2-selection--single > .select2-selection__rendered"); if (select_items.length > 0) { jQuery(select_items).each(function (index) { const select_itm = jQuery(this); const title = select_itm.attr("title"); if (typeof title !== 'undefined' && title) { const rslt = title.match(/([^<]+)]*>([^<]+)<\/span>/ism); if (rslt !== null) { const new_title = rslt[1] + " " + rslt[2]; select_itm.attr("title", new_title); } } }); } }