// Main javascript file document.addEventListener('DOMContentLoaded', function() { const locationInputs = document.querySelectorAll('input[name="location"]'); if (locationInputs.length === 0) { return; } const COMUNI_URL = 'https://raw.githubusercontent.com/matteocontrini/comuni-json/master/comuni.json'; const IP_GEOLOCATION_URL = 'http://ip-api.com/json'; // Create a datalist for suggestions const datalist = document.createElement('datalist'); datalist.id = 'comuni-list'; document.body.appendChild(datalist); // Attach the datalist to the input fields locationInputs.forEach(input => { input.setAttribute('list', datalist.id); input.setAttribute('autocomplete', 'off'); // Disable browser's own autocomplete }); // Fetch comuni and populate datalist fetch(COMUNI_URL) .then(response => response.json()) .then(data => { data.forEach(comune => { const option = document.createElement('option'); option.value = comune.nome; datalist.appendChild(option); }); }) .catch(error => { console.error('Error fetching Italian municipalities:', error); }); // Fetch IP-based geolocation and pre-fill the input fetch(IP_GEOLOCATION_URL) .then(response => response.json()) .then(data => { if (data && data.city) { locationInputs.forEach(input => { // Only set the value if the input is currently empty if (input.value.trim() === '') { input.value = data.city; } }); } }) .catch(error => { console.error('Error fetching IP geolocation:', error); }); });