document.addEventListener('DOMContentLoaded', function() { // Select all forms with the class lpa-form const lpaForms = document.querySelectorAll('.lpa-form'); lpaForms.forEach(form => { form.addEventListener('submit', function(e) { e.preventDefault(); const submitBtn = document.activeElement && document.activeElement.type === 'submit' ? document.activeElement : form.querySelector('button[type="submit"]'); const originalHTML = submitBtn.innerHTML; const nextAction = document.activeElement && document.activeElement.name === 'next_action' ? document.activeElement.value : null; // Show loading state submitBtn.disabled = true; submitBtn.innerHTML = ' Saving...'; const formData = new FormData(form); // If the button clicked was one of the buttons with next_action, ensure it's set if (nextAction) { formData.set('next_action', nextAction); } fetch('api/save_lpa.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { // Show success state submitBtn.classList.remove('btn-primary', 'btn-secondary', 'btn-outline-primary'); submitBtn.classList.add('btn-success'); submitBtn.innerHTML = ' Saved!'; if (nextAction === 'add_another') { // Clear the form fields as requested form.reset(); } setTimeout(() => { if (data.redirect) { window.location.href = data.redirect; } else if (data.next_step) { const currentStep = parseInt(new URLSearchParams(window.location.search).get('step') || 1); if (data.next_step === currentStep && nextAction !== 'next_step') { window.location.reload(); } else { window.location.href = 'apply.php?step=' + encodeURIComponent(data.next_step) + '&id=' + encodeURIComponent(data.id || ''); } } else { // Default fallback if next_step isn't provided but success is true window.location.reload(); } }, 800); } else { alert(data.error || 'An error occurred. Please try again.'); submitBtn.disabled = false; submitBtn.innerHTML = originalHTML; submitBtn.classList.remove('btn-success'); } }) .catch(error => { console.error('Error:', error); alert('Something went wrong. Please check your connection.'); submitBtn.disabled = false; submitBtn.innerHTML = originalHTML; }); }); }); // Handle attorney deletion const deleteBtns = document.querySelectorAll('.btn-delete-attorney'); deleteBtns.forEach(btn => { btn.addEventListener('click', function() { if (confirm('Are you sure you want to remove this attorney?')) { const attorneyId = btn.getAttribute('data-id'); const lpaId = btn.getAttribute('data-lpa-id') || new URLSearchParams(window.location.search).get('id'); const formData = new FormData(); formData.append('action', 'delete_attorney'); formData.append('attorney_id', attorneyId); formData.append('lpa_id', lpaId); fetch('api/save_lpa.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { window.location.reload(); } else { alert(data.error || 'Failed to remove attorney.'); } }) .catch(error => { console.error('Error:', error); alert('Failed to remove attorney. Please try again.'); }); } }); }); // Step 10: Witness Selection Toggling const witnessSelectors = document.querySelectorAll('.witness-selector'); witnessSelectors.forEach(selector => { selector.addEventListener('change', function() { const attorneyId = this.getAttribute('data-attorney-id'); const newWitnessFields = document.getElementById('new_witness_fields_' + attorneyId); if (this.value === 'new') { newWitnessFields.classList.remove('d-none'); } else { newWitnessFields.classList.add('d-none'); } }); }); // Step 10: Manual Witness Checkbox Toggling const manualWitnessCheckboxes = document.querySelectorAll('.manual-witness-checkbox'); manualWitnessCheckboxes.forEach(checkbox => { checkbox.addEventListener('change', function() { const attorneyId = this.id.replace('add_later_', ''); const fieldsContainer = this.closest('.row').querySelectorAll('input:not(.manual-witness-checkbox)'); fieldsContainer.forEach(input => { input.disabled = this.checked; if (this.checked) { input.value = ''; } }); }); }); // Step 12: Correspondence Toggling const corrWhoRadios = document.querySelectorAll('input[name="correspondence_who"]'); const corrDetails = document.getElementById('correspondence_details'); const attSelectorContainer = document.getElementById('attorney_selector_container'); if (corrWhoRadios.length > 0) { corrWhoRadios.forEach(radio => { radio.addEventListener('change', function() { if (this.value === 'Attorney' || this.value === 'Other') { if (corrDetails) corrDetails.classList.remove('d-none'); if (this.value === 'Attorney') { if (attSelectorContainer) attSelectorContainer.classList.remove('d-none'); } else { if (attSelectorContainer) attSelectorContainer.classList.add('d-none'); } } else { if (corrDetails) corrDetails.classList.add('d-none'); if (attSelectorContainer) attSelectorContainer.classList.add('d-none'); } }); }); } // Step 12: Attorney Selector Population const attSelector = document.getElementById('attorney_selector'); if (attSelector) { attSelector.addEventListener('change', function() { if (this.value) { try { const data = JSON.parse(this.value); document.getElementById('correspondence_title').value = data.title || ''; document.getElementById('correspondence_first_name').value = data.first_name || ''; document.getElementById('correspondence_last_name').value = data.last_name || ''; document.getElementById('correspondence_address_line1').value = data.address_line1 || ''; document.getElementById('correspondence_address_line2').value = data.address_line2 || ''; document.getElementById('correspondence_address_line3').value = data.address_line3 || ''; document.getElementById('correspondence_postcode').value = data.postcode || ''; document.getElementById('correspondence_email').value = data.email || ''; } catch (e) { console.error('Error parsing attorney data', e); } } }); } // Step 12: Contact Preference Toggling const contactPrefChecks = document.querySelectorAll('.contact-pref-check'); const phoneBox = document.getElementById('phone_input_box'); const emailBox = document.getElementById('email_input_box'); function updateContactPrefVisibility() { let phoneSelected = false; let emailSelected = false; contactPrefChecks.forEach(check => { if (check.checked) { if (check.value === 'Phone') phoneSelected = true; if (check.value === 'Email') emailSelected = true; } }); if (phoneBox) { if (phoneSelected) phoneBox.classList.remove('d-none'); else phoneBox.classList.add('d-none'); } if (emailBox) { if (emailSelected) emailBox.classList.remove('d-none'); else emailBox.classList.add('d-none'); } } if (contactPrefChecks.length > 0) { contactPrefChecks.forEach(check => { check.addEventListener('change', updateContactPrefVisibility); }); // Run once on load to set initial state updateContactPrefVisibility(); } });