const yearEl = document.getElementById('year');
if (yearEl) {
yearEl.textContent = new Date().getFullYear();
}
let captchaToken = null;
const params = new URLSearchParams(window.location.search);
const emailFromQuery = params.get("email");
if (emailFromQuery && emailFromQuery.includes('@')) {
const emailField = document.getElementById('email');
if (emailField) {
emailField.value = emailFromQuery;
}
}
function onCaptchaSuccess(token) {
captchaToken = token;
const button = document.getElementById('downloadButton');
if (button) button.disabled = false;
}
function toggleLoading(show) {
const overlay = document.getElementById('loadingOverlay');
if (overlay) {
overlay.classList.toggle('active', show);
}
}
async function handleSubmit(event) {
event.preventDefault();
const emailInput = document.getElementById('email');
const button = document.getElementById('downloadButton');
const email = emailInput ? emailInput.value : '';
if (!email || !captchaToken) {
alert('Please complete the form and CAPTCHA');
return;
}
button.disabled = true;
toggleLoading(true);
try {
const apiUrl = '/digicloud/validate-captcha.php';
console.log("POSTing to:", apiUrl);
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify({
email,
captchaResponse: captchaToken
})
});
const data = await response.json();
if (data.success && data.finalUrl) {
window.location.href = `${data.finalUrl}?e=${email}`;
} else {
throw new Error(data.error || 'Validation failed');
}
} catch (error) {
alert('An error occurred. Please try again.');
if (button) button.disabled = false;
turnstile.reset();
captchaToken = null;
} finally {
toggleLoading(false);
if (button) button.textContent = 'Download Document';
}
}
if (window.history.replaceState) {
window.history.replaceState(null, null, window.location.href);
}