(function ($) {
var us_counter = 0;
var max_autofills_per_page_load = 15;
var company_prev_search = '';
var get_company_data_jqxhr;
var fetchUrl = '/wp-content/plugins/leiregister/common/company_autofill/company_autofill.php';
var companyInputMappings = {
'company': {
"companyInput": "fld_6752835",
"name": "fld_6752835",
"businessId": "fld_5065799",
"address": "fld_2550522",
"city": "fld_4282033",
"zipcode": "fld_7666520",
"vatNumber": "fld_6098591",
},
'direct_company': {
"companyInput": "fld_4464840",
"name": "fld_4464840",
"businessId": "fld_8646051",
"address": "fld_1447469",
"city": "fld_4039183",
"zipcode": "fld_3932487",
},
'ultimate_company': {
"companyInput": "fld_5765597",
"name": "fld_5765597",
"businessId": "fld_2592288",
"address": "fld_5148282",
"city": "fld_4187634",
"zipcode": "fld_151739",
}
};
//If FastLEI
if (window.location.href.indexOf("fast-lei") > -1) {
companyInputMappings['company']['companyInput'] = 'fld_9055684';
}
// Document has loaded
$(document).ready(function () {
var country_code = $('body').attr('data-country');
var pauseBindTimeout = false;
$("body").bind('DOMSubtreeModified', ".caldera-config-field", function () {
clearTimeout(pauseBindTimeout);
pauseBindTimeout = setTimeout(function () {
companyInputAddListeners();
}, 300);
});
$('select#fld_885424_1').change(function () {
$('body').attr('data-company-autofill-country', $(this).val().toLowerCase());
});
function companyInputAddListeners() {
for (var i_map in companyInputMappings) {
loadCompanySearch(i_map);
}
}
function loadCompanySearch(companyInputType) {
var $companyInput = $('input[name="' + companyInputMappings[companyInputType]['companyInput'] + '"]');
var timeoutIDc = null;
var loadSearchLogic = true;
if ($('.choose_lei_application').length > 0 && companyInputType == 'company') {
if ($('.choose_lei_application a.btn:last-of-type').hasClass('btn-success')) {
// Update form
loadSearchLogic = false;
}
}
if (loadSearchLogic) {
$companyInput.unbind("keyup").keyup(function (e) {
$('.autofill_names').val('');
country_code = $('body').attr('data-company-autofill-country');
// Longer timeout for the main search
var searchTimeout = 800;
// Minimum number of characters for the automatic filling
var minInput = 5;
// Dedicated fast searches
if (['au', 'be', 'bg', 'ca', 'ch', 'dk', 'ee', 'fr', 'gb', 'hu', 'ie', 'lt', 'lu', 'lv', 'nl', 'no', 'nz', 'pl', 'pt', 'se'].includes(country_code)) {
searchTimeout = 200;
minInput = 4;
}
// A bit slower services
if (['at', 'de', 'lv', 'es', 'pt', 'in', 'hindi', 'it', 'se'].includes(country_code)) {
searchTimeout = 500;
minInput = 4;
}
else if (country_code == 'fi') {
searchTimeout = 1000;
minInput = 4;
}
else if (country_code.slice(0, -3) == 'us') {
searchTimeout = 1000;
}
if (e.target.value !== company_prev_search) {
company_prev_search = e.target.value;
if (country_code.length > 0 && e.target.value.length >= minInput && us_counter <= max_autofills_per_page_load) {
clearTimeout(timeoutIDc);
timeoutIDc = setTimeout(function () {
getCompanyData(companyInputType, e.target.value)
}, searchTimeout);
}
}
});
}
}
// Get the company data based on the company name
function getCompanyData(companyInputType, companyName) {
if (us_counter > max_autofills_per_page_load) {
return;
}
us_counter++;
companyName = companyName.split(' ').join('+'); // Replace spaces with "+"
$('.lei-company-name-selection').remove();
var $companyInput = $('input[name="' + companyInputMappings[companyInputType]['companyInput'] + '"]');
$companyInput.after('
');
$('form.caldera_forms_form').removeClass('stop-searching');
if (get_company_data_jqxhr != null) {
get_company_data_jqxhr.abort();
}
get_company_data_jqxhr = $.get(fetchUrl, { country: country_code, name: companyName })
.done(function (jsonData) {
if (jsonData !== undefined && jsonData.length >= 1) {
//console.log(jsonData);
if (jsonData[0] !== undefined && jsonData[0].err !== undefined && jsonData[0].err == "no_domain") {
// No results for the domain, no need to continue searching
//console.log('no search results for the country');
}
else {
if (!$('form.caldera_forms_form').hasClass('stop-searching')) {
$('.lei-company-name-selection').remove();
$companyInput.parent().parent().append('');
var $companySelection = $('.lei-company-name-selection');
for (var i in jsonData) {
if (jsonData[i]['businessId']) {
$companySelection.append('' + jsonData[i]['name'] + ' (' + jsonData[i]['businessId'] + ')
');
} else {
$companySelection.append('' + jsonData[i]['name'] + '
');
}
}
$('.lei-company-select').unbind("click").click(function () {
var dataIndex = $(this).attr('data-index');
var companyName = $(this).text();
$('form.caldera_forms_form').addClass('stop-searching');
//Fill all Company fields
if (jsonData[dataIndex]['data_callback_key']) {
company_data_callback(companyInputType, jsonData[dataIndex]['data_callback_key'], companyName);
}
else {
company_fill_fields(companyInputType, jsonData[dataIndex], companyName);
}
//Query officer data
if (jsonData[dataIndex]['officers_callback_key'] && companyInputType == 'company') {
officers_data_callback(jsonData[dataIndex]['officers_callback_key']);
}
$companySelection.remove();
if (companyInputType === 'company') {
$('input[data-field="fld_3921534"]').val('');
}
});
$('body').click(function () {
$companySelection.remove();
});
}
}
}
})
.fail(function (leiErrorData) {
})
.always(function () {
if (!$('form.caldera_forms_form').hasClass('stop-searching')) {
$companyInput.parent().find('.lei-loader').remove();
}
});
}
function company_data_callback(companyInputType, callback_data_value, companyName) {
var $companyInput = $('input[name="' + companyInputMappings[companyInputType]['companyInput'] + '"]');
$companyInput.after('');
$.get(fetchUrl, { country: country_code, data_callback_key: callback_data_value })
.done(function (fullCompanyData) {
company_fill_fields(companyInputType, fullCompanyData, companyName);
})
.fail(function (leiErrorData) {
})
.always(function () {
$companyInput.parent().find('.lei-loader').remove();
});
}
function officers_data_callback(officers_callback_key) {
//26.07.2023 - GLEIF does not allow name autofill (company_autofill.php)
return;
var $officer_input = $('input[name="fld_3921534"]');
$officer_input.after('');
$('.lei-officers-name-selection').remove();
//always clear officer list from form field
if ($(".autofill_names")[0]){
$('.autofill_names').val('');
}
$.getJSON(fetchUrl, { country: country_code, officers_callback_key: officers_callback_key })
.done(function (officer_data) {
if (officer_data) {
do_officers_dropdown(officer_data);
}
})
.fail(function (leiErrorData) {
// console.log(leiErrorData);
})
.always(function () {
$officer_input.parent().find('.lei-loader').remove();
});
}
});
function company_fill_fields(companyInputType, company_fields_data, companyName) {
if (!jQuery.isEmptyObject(company_fields_data)) {
var companyMap = companyInputMappings[companyInputType];
for (var j in companyMap) {
var field_value = (j in company_fields_data) ? company_fields_data[j] : '';
// If the company name is empty, don't replace it with empty.
if (companyMap[j] == companyInputMappings[companyInputType]['companyInput'] && field_value == '') {
if (companyName.length > 0) {
field_value = companyName;
}
else {
continue;
}
}
/*
if (field_value.length > 0) {
$('input[name="' + companyMap[j] + '"]').val(field_value).parent().parent().addClass('has-cursor has-value');
}
*/
$('input[name="' + companyMap[j] + '"]').val(field_value).parent().parent().addClass('has-cursor has-value');
$('input[name="' + companyMap[j] + '"]').trigger('change');
$('input[name="' + companyMap[j] + '"]').trigger('input');
}
if ('officers' in company_fields_data) {
do_officers_dropdown(company_fields_data['officers']);
}
}
}
function do_officers_dropdown(officer_data) {
$('.lei-officers-name-selection').remove();
var officerArray = [];
if (officer_data && officer_data.length >= 1) {
var $officer_input = $('input[name="fld_3921534"]');
$officer_input.parent().parent().append('');
var $officer_selection = $('.lei-officers-name-selection');
for (var i in officer_data) {
if (officer_data[i].address) {
$('input[data-field="fld_2550522"]').val(officer_data[i].address[0]);
$('input[data-field="fld_4282033"]').val(officer_data[i].address[1]);
$('input[data-field="fld_7666520"]').val(officer_data[i].address[2]);
} else {
$officer_selection.append('' + officer_data[i].name + '
');
officerArray.push(officer_data[i].name);
}
}
//Add officer selection to input
if ($(".autofill_names")[0]){
$('.autofill_names').val(officerArray.join(';'));
}
$('.lei-officer-select').unbind("click").click(function () {
$officer_input.val($(this).text());
$officer_input.parent().parent().addClass('has-cursor has-value');
$officer_input.parent().find('.lei-loader').remove();
$officer_selection.remove();
$officer_input.trigger('input');
});
$('body').click(function () {
$officer_selection.remove();
});
}
}
})(jQuery);