// Gestione Form Contatto
const contactForm = document.getElementById('contact-form');
const contactFormFeedback = document.getElementById('contact-form-feedback');
if (contactForm && contactFormFeedback) {
contactForm.addEventListener('submit', function(event) {
event.preventDefault();
contactFormFeedback.className = 'form-feedback';
contactFormFeedback.textContent = 'Invio in corso...';
contactFormFeedback.style.color = ''; // Resetta colore
contactFormFeedback.style.display = 'block';
const formData = new FormData(contactForm);
const recaptchaResponse = typeof grecaptcha !== 'undefined' ? grecaptcha.getResponse() : '';
if (!recaptchaResponse && typeof grecaptcha !== 'undefined') { // Controlla solo se grecaptcha è definito
contactFormFeedback.textContent = 'Per favore, completa il reCAPTCHA.';
contactFormFeedback.classList.add('error');
return;
}
if (typeof grecaptcha !== 'undefined') { // Aggiungi solo se grecaptcha è definito
formData.append('g-recaptcha-response', recaptchaResponse);
}
fetch('/temi/temabase/scripts/send_email.php', { // Assicurati che il percorso sia corretto!
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
return response.json().then(errData => {
throw new Error(errData.message || 'Errore dal server: ' + response.status);
});
}
return response.json();
})
.then(data => {
contactFormFeedback.textContent = data.message;
if (data.status === 'success') {
contactFormFeedback.classList.add('success');
contactForm.reset();
if (typeof grecaptcha !== 'undefined') grecaptcha.reset();
} else {
contactFormFeedback.classList.add('error');
if (typeof grecaptcha !== 'undefined') grecaptcha.reset();
}
})
.catch(error => {
console.error('Errore fetch form contatti:', error);
contactFormFeedback.textContent = error.message || 'Errore di comunicazione con il server.';
contactFormFeedback.classList.add('error');
if (typeof grecaptcha !== 'undefined') grecaptcha.reset();
});
});
}
// Precompilazione codice segnalatore e scroll all'ancora
window.addEventListener('load', () => {
const urlParams = new URLSearchParams(window.location.search);
const referralCodeFromUrl = urlParams.get('cs');
const referralCodeInput = document.getElementById('codice_segnalatore_input');
if (referralCodeFromUrl && referralCodeInput) {
referralCodeInput.value = referralCodeFromUrl;
const appliedCodeMessage = document.createElement('p');
appliedCodeMessage.innerHTML = 'Codice segnalatore ' + referralCodeFromUrl + ' applicato.
Se il codice è attivo riceverai uno sconto del 10%.';
appliedCodeMessage.style.color = 'green';
appliedCodeMessage.style.fontSize = '0.9em';
appliedCodeMessage.style.marginTop = '10px';
referralCodeInput.parentNode.insertBefore(appliedCodeMessage, referralCodeInput.nextSibling);
}
if (window.location.hash && window.location.hash.length > 1) {
try {
const targetElement = document.querySelector(window.location.hash);
if (targetElement) {
setTimeout(() => {
targetElement.scrollIntoView({ behavior: 'smooth' });
}, 250); // Aumentato leggermente il delay
}
} catch (e) { console.error('Errore scroll ancore:', e); }
}
});