document.addEventListener('DOMContentLoaded', function() { const loginForm = document.getElementById('loginForm'); const loginAlert = document.getElementById('loginAlert'); const demoBtns = document.querySelectorAll('.demo-btn'); // Handle Demo Buttons demoBtns.forEach(btn => { btn.addEventListener('click', function() { const email = this.getAttribute('data-email'); const pass = this.getAttribute('data-pass'); const emailInput = document.getElementById('email'); const passInput = document.getElementById('password'); if (emailInput && passInput) { emailInput.value = email; passInput.value = pass; // Auto submit loginForm.dispatchEvent(new Event('submit')); } }); }); if (loginForm) { loginForm.addEventListener('submit', function(e) { e.preventDefault(); const formData = new FormData(loginForm); formData.append('action', 'login'); const submitBtn = loginForm.querySelector('button[type="submit"]'); const originalText = submitBtn.innerText; submitBtn.disabled = true; submitBtn.innerHTML = 'Authenticating...'; fetch('auth.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { window.location.reload(); } else { loginAlert.innerText = data.error; loginAlert.classList.remove('d-none'); submitBtn.disabled = false; submitBtn.innerText = originalText; } }) .catch(error => { console.error('Error:', error); loginAlert.innerText = 'Clinical gateway error. Please try again.'; loginAlert.classList.remove('d-none'); submitBtn.disabled = false; submitBtn.innerText = originalText; }); }); } });