// Smooth scroll for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const targetId = this.getAttribute('href'); if (targetId === '#') return; const targetElement = document.querySelector(targetId); if (targetElement) { window.scrollTo({ top: targetElement.offsetTop - 80, behavior: 'smooth' }); } }); }); // Dark mode toggle functionality const darkModeToggle = document.getElementById('darkModeToggle'); if (darkModeToggle) { darkModeToggle.addEventListener('click', () => { document.documentElement.classList.toggle('dark'); localStorage.setItem('darkMode', document.documentElement.classList.contains('dark')); }); } // Check for saved dark mode preference if (localStorage.getItem('darkMode') === 'true') { document.documentElement.classList.add('dark'); } // Animate elements when they come into view const animateOnScroll = () => { const elements = document.querySelectorAll('.animate-in'); elements.forEach(element => { const elementPosition = element.getBoundingClientRect().top; const screenPosition = window.innerHeight / 1.3; if (elementPosition < screenPosition) { element.style.opacity = '1'; element.style.transform = 'translateY(0)'; } }); }; window.addEventListener('scroll', animateOnScroll); window.addEventListener('load', animateOnScroll); // Initialize tooltips const initTooltips = () => { const tooltipElements = document.querySelectorAll('.tooltip'); tooltipElements.forEach(el => { const tooltipText = el.getAttribute('data-tooltip'); if (tooltipText) { const tooltipSpan = document.createElement('span'); tooltipSpan.className = 'tooltip-text'; tooltipSpan.textContent = tooltipText; el.appendChild(tooltipSpan); } }); }; document.addEventListener('DOMContentLoaded', initTooltips); // Form validation helper const validateForm = (formId) => { const form = document.getElementById(formId); if (!form) return false; let isValid = true; const inputs = form.querySelectorAll('input[required], select[required], textarea[required]'); inputs.forEach(input => { if (!input.value.trim()) { input.classList.add('border-red-500'); isValid = false; } else { input.classList.remove('border-red-500'); } }); return isValid; }; // Auto-resize textareas const autoResizeTextareas = () => { const textareas = document.querySelectorAll('textarea.auto-resize'); textareas.forEach(textarea => { textarea.style.height = 'auto'; textarea.style.height = (textarea.scrollHeight) + 'px'; textarea.addEventListener('input', function() { this.style.height = 'auto'; this.style.height = (this.scrollHeight) + 'px'; }); }); }; document.addEventListener('DOMContentLoaded', autoResizeTextareas); // Debounce function for performance optimization const debounce = (func, wait) => { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; }; // Responsive table handling const makeTablesResponsive = () => { const tables = document.querySelectorAll('.responsive-table'); tables.forEach(table => { const headers = []; table.querySelectorAll('th').forEach(th => { headers.push(th.textContent); }); table.querySelectorAll('tr').forEach((tr, rowIndex) => { if (rowIndex === 0) return; // Skip header row tr.querySelectorAll('td').forEach((td, cellIndex) => { if (cellIndex < headers.length) { td.setAttribute('data-label', headers[cellIndex]); } }); }); }); }; document.addEventListener('DOMContentLoaded', makeTablesResponsive); // Notification system const showNotification = (message, type = 'success', duration = 3000) => { const notification = document.createElement('div'); notification.className = `fixed top-4 right-4 z-50 px-6 py-4 rounded-lg shadow-lg text-white ${ type === 'success' ? 'bg-green-500' : type === 'error' ? 'bg-red-500' : 'bg-blue-500' }`; notification.textContent = message; document.body.appendChild(notification); setTimeout(() => { notification.classList.add('opacity-0', 'transition-opacity', 'duration-300'); setTimeout(() => { notification.remove(); }, 300); }, duration); }; // Example usage: showNotification('Transaction added successfully!', 'success'); // Currency formatter const formatCurrency = (amount, currency = 'USD') => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: currency, minimumFractionDigits: 2 }).format(amount); }; // Date formatter const formatDate = (dateString, format = 'medium') => { const date = new Date(dateString); return new Intl.DateTimeFormat('en-US', { dateStyle: format === 'short' ? 'short' : format === 'long' ? 'long' : 'medium' }).format(date); }; // Initialize all components when DOM is loaded document.addEventListener('DOMContentLoaded', () => { animateOnScroll(); initTooltips(); autoResizeTextareas(); makeTablesResponsive(); // Initialize feather icons if (typeof feather !== 'undefined') { feather.replace(); } // Check for prefers-reduced-motion const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; if (prefersReducedMotion) { document.body.classList.add('reduce-motion'); } }); // Responsive menu toggle for mobile const menuToggle = document.getElementById('mobileMenuToggle'); if (menuToggle) { const mobileMenu = document.getElementById('mobileMenu'); menuToggle.addEventListener('click', () => { mobileMenu.classList.toggle('hidden'); menuToggle.querySelectorAll('svg').forEach(svg => { svg.classList.toggle('hidden'); }); }); } // Page transition effect const enhancePageTransitions = () => { document.querySelectorAll('a').forEach(link => { if (link.href && link.href.includes(window.location.origin) && !link.href.includes('#') && link.href !== window.location.href) { link.addEventListener('click', (e) => { e.preventDefault(); document.body.classList.add('opacity-0', 'transition-opacity', 'duration-300'); setTimeout(() => { window.location.href = link.href; }, 300); }); } }); }; document.addEventListener('DOMContentLoaded', enhancePageTransitions); // Helper function to copy text to clipboard const copyToClipboard = (text, successMessage = 'Copied to clipboard!') => { navigator.clipboard.writeText(text).then(() => { showNotification(successMessage, 'success'); }).catch(err => { console.error('Could not copy text: ', err); showNotification('Failed to copy text', 'error'); }); };