const nricElement = document.getElementById('NRIC'); const taxReceiptCheckbox = document.getElementById('tax-receipt'); const nricValue = document.getElementById('NRIC-FIN-or-UEN-No'); nricElement.style.display = 'none'; nricValue.value = 'inapplicable'; taxReceiptCheckbox.addEventListener('change', function () { if (this.checked) { nricElement.style.display = 'block'; nricValue.value = ''; } else { nricElement.style.display = 'none'; nricValue.value = 'inapplicable'; } }); // 1. Function to generate a UUID function generateUUID() { if (typeof crypto !== 'undefined' && crypto.randomUUID) { return crypto.randomUUID(); } return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } // 2. Function to get URL parameter by name function getUrlParam(paramName) { const urlParams = new URLSearchParams(window.location.search); return urlParams.get(paramName); } // 3. Find the input fields and assign values document.addEventListener('DOMContentLoaded', function () { var sessionInput = document.querySelector('input[name="sessionid"]'); if (sessionInput) { sessionInput.value = generateUUID(); console.log('Session ID generated: ' + sessionInput.value); } else { console.warn("Input field with name='sessionid' not found."); } var employeeInput = document.querySelector('input[name="employeeid"]'); const defaultEmployeeId = 'b0a987ef-5994-49d6-90bd-ce9654d081d6'; const urlId = getUrlParam('id'); const employeeId = urlId || defaultEmployeeId; if (employeeInput) { employeeInput.value = employeeId; console.log('Employee ID assigned: ' + employeeInput.value); if (urlId) { let angleDonor = document.querySelector('#AngleDonor'); let friendDonor = document.querySelector('#FriendDonor'); let tableAdoption = document.querySelector('#TableAdoption'); let searDonor = document.querySelector('#SeatDonor'); let availableForm = document.querySelector('#availableForm'); angleDonor.classList.remove('is-disabled'); friendDonor.classList.remove('is-disabled'); tableAdoption.classList.remove('is-disabled'); searDonor.classList.remove('is-disabled'); availableForm.style.display = 'none'; } } else { console.warn("Input field with name='employeeid' not found."); } }); document.addEventListener('DOMContentLoaded', function () { // CONFIGURATION const N8N_WEBHOOK_URL = 'https://ffth.app.n8n.cloud/webhook/0f92f43e-788b-4b7a-a172-6dde93d9da75'; // Select the form with mainform="true" attribute const formelement = document.querySelector('form[mainform="true"]'); const emailInput = formelement.querySelector('#Email'); const sessionidInput = formelement.querySelector('input[name="sessionid"]'); const successMessage = formelement.querySelector('.w-form-done'); const successText = formelement.querySelector('#successText'); if (!formelement || !emailInput) { console.error("Could not find Form or Email input with ID 'Email'"); return; } console.log('Main form found:', formelement); // Function to poll n8n function pollForPaymentLink(email) { console.log('Starting poll for:', email); let attempts = 0; const maxAttempts = 30; const intervalId = setInterval(async () => { attempts++; try { const fetchUrl = `${N8N_WEBHOOK_URL}?email=${encodeURIComponent( email )}&sessionid=${sessionidInput.value}`; const response = await fetch(fetchUrl, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); const data = await response.json(); console.log('n8n response:', data); if (data.link) { clearInterval(intervalId); window.location.href = data.link; } if (attempts >= maxAttempts) { clearInterval(intervalId); } } catch (error) { console.error('Polling error:', error); } }, 2000); } // Listen for the form submission formelement.addEventListener('submit', function (e) { console.log('Form submit event triggered!'); const emailValue = emailInput.value; console.log('EMAIL VALUE: ' + emailValue); setTimeout(() => { if (emailValue) { pollForPaymentLink(emailValue); } }, 500); }); });