// Shared JavaScript functions document.addEventListener('DOMContentLoaded', function() { // Set language attribute document.documentElement.lang = 'en'; // Skip to content link const skipLink = document.createElement('a'); skipLink.href = '#main-content'; skipLink.className = 'skip-link sr-only focus:not-sr-only focus:fixed focus:top-2 focus:left-2 focus:bg-white focus:p-4 focus:z-50'; skipLink.textContent = 'Skip to main content'; document.body.insertBefore(skipLink, document.body.firstChild); // Add accessible names to interactive elements document.querySelectorAll('[data-feather]').forEach(icon => { const parent = icon.parentElement; if (parent.tagName === 'A' || parent.tagName === 'BUTTON') { const label = parent.textContent.trim() || icon.getAttribute('aria-label') || parent.getAttribute('title'); if (label) { parent.setAttribute('aria-label', label); } if (parent.tagName === 'BUTTON' && !parent.getAttribute('type')) { parent.setAttribute('type', 'button'); } } }); // Ensure all images have alt text document.querySelectorAll('img:not([alt])').forEach(img => { img.setAttribute('alt', ''); }); // Add landmark roles const main = document.querySelector('main'); if (main && !main.getAttribute('role')) { main.setAttribute('role', 'main'); } // Manage focus for modals document.addEventListener('keydown', function(e) { if (e.key === 'Tab' && document.querySelector('.modal[aria-modal="true"]')) { handleModalFocus(e); } }); // Initialize tooltips const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')); tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl); }); // Form validation example const forms = document.querySelectorAll('.needs-validation'); Array.prototype.slice.call(forms).forEach(function(form) { form.addEventListener('submit', function(event) { if (!form.checkValidity()) { event.preventDefault(); event.stopPropagation(); } form.classList.add('was-validated'); }, false); }); // Password visibility toggle const passwordToggles = document.querySelectorAll('.password-toggle'); passwordToggles.forEach(toggle => { toggle.addEventListener('click', function() { const input = this.previousElementSibling; const icon = this.querySelector('i'); if (input.type === 'password') { input.type = 'text'; icon.setAttribute('data-feather', 'eye-off'); } else { input.type = 'password'; icon.setAttribute('data-feather', 'eye'); } feather.replace(); }); }); }); // API call example for account balance async function fetchAccountBalance() { try { // In a real app, this would be a secure API call with authentication const response = await fetch('https://api.example.com/balance', { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_ACCESS_TOKEN' } }); const data = await response.json(); return data; } catch (error) { console.error('Error fetching account balance:', error); return null; } } // Format currency function formatCurrency(amount) { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(amount); } // Format date function formatDate(dateString) { const options = { year: 'numeric', month: 'short', day: 'numeric' }; return new Date(dateString).toLocaleDateString('en-US', options); }