$(document).ready(function () { // ----- Endpoints (same domain as the shop so the OCSESSID cookie is sent) ----- const LURSOFT_LOOKUP_URL = "https://kamigifts.lv/lursoft/l.php"; const SESSION_SAVE_URL = "https://kamigifts.lv/lursoft/session_address.php"; // How long to wait (ms) after the user stops typing before fetching/updating. const ADDRESS_DELAY = 1000; // 1 second let lookupTimer = null; // debounce timer for the registry lookup let sessionTimer = null; // debounce timer for pushing edits to the session // Full mapping of Latvian cities → novads names (matching your to its first option. $('#shipping_address_city').val(''); $('#shipping_address_address_1').val(''); $('#shipping_address_postcode').val(''); $('#shipping_address_zone_id').prop('selectedIndex', 0).trigger('change'); // Unbind the #customer_reg_nr change handler, wait 3 seconds, // then bind it once again. unbindRegHandler(); setTimeout(bindRegHandler, 3000); } // Keep the session in sync with the copied/cleared shipping address. updateSession(true); }); // Attach handler — now with a 1 second delay before updating the address. bindRegHandler(); // If the user edits any address field by hand, keep the session updated. $('#customer_comp_name, #customer_pvn_nr, ' + '#payment_address_address_1, #payment_address_city, #payment_address_postcode, #payment_address_zone_id, ' + '#shipping_address_address_1, #shipping_address_city, #shipping_address_postcode, #shipping_address_zone_id') .on('change keyup', function () { updateSession(false); }); }, 1000); setTimeout(function () { if ($(this).is(':checked')) { $('#shipping_address_city').val($('#payment_address_city').val()); $('#shipping_address_postcode').val($('#payment_address_postcode').val()); $('#shipping_address_address_1').val($('#payment_address_address_1').val()); var selectedZone = $('#payment_address_zone_id').val(); $('#shipping_address_zone_id').val(selectedZone).trigger('change'); } else { // Optional: clear fields when unchecked $('#shipping_address_city, #shipping_address_postcode, #shipping_address_address_1').val(''); $('#shipping_address_zone_id').val('').trigger('change'); } }, 2000); });