/* ToolsOfIndia.com - Shared JavaScript */ (function() { 'use strict'; // Format number with commas (Indian numbering) window.formatIndian = function(num) { if (num === undefined || num === null || isNaN(num)) return '0'; num = Number(num); const parts = num.toFixed(2).split('.'); let intPart = parts[0]; const lastThree = intPart.slice(-3); const rest = intPart.slice(0, -3); if (rest) { intPart = rest.replace(/\B(?=(\d{2})+(?!\d))/g, ',') + ',' + lastThree; } return '\u20B9' + intPart + '.' + parts[1]; }; // Format plain number with commas (Indian) window.formatNumber = function(num) { if (num === undefined || num === null || isNaN(num)) return '0'; num = Math.round(num); const str = String(num); const lastThree = str.slice(-3); const rest = str.slice(0, -3); if (rest) return rest.replace(/\B(?=(\d{2})+(?!\d))/g, ',') + ',' + lastThree; return str; }; // Format percentage window.formatPct = function(val) { return Number(val).toFixed(2) + '%'; }; // Show toast notification window.showToast = function(msg, type) { type = type || 'success'; const div = document.createElement('div'); div.className = 'toast ' + type; div.textContent = msg; document.body.appendChild(div); setTimeout(function() { div.remove(); }, 3000); }; // Copy to clipboard window.copyText = function(text) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text).then(function() { showToast('Copied!'); }).catch(function() { fallbackCopy(text); }); } else { fallbackCopy(text); } }; function fallbackCopy(text) { const ta = document.createElement('textarea'); ta.value = text; ta.style.position = 'fixed'; ta.style.left = '-9999px'; document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta); showToast('Copied!'); } // Get URL params window.getParam = function(name) { const url = new URL(window.location.href); return url.searchParams.get(name); }; // Update range slider display document.addEventListener('input', function(e) { if (e.target.matches('input[type="range"]')) { const display = e.target.closest('.form-group').querySelector('.range-value'); if (display) display.textContent = e.target.value; } }); // Category filter on homepage var catBtns = document.querySelectorAll('.category-btn'); catBtns.forEach(function(btn) { btn.addEventListener('click', function() { catBtns.forEach(function(b) { b.classList.remove('active'); }); this.classList.add('active'); var cat = this.dataset.cat || 'all'; document.querySelectorAll('.tool-card').forEach(function(card) { if (cat === 'all' || card.dataset.cat === cat) { card.style.display = 'flex'; } else { card.style.display = 'none'; } }); }); }); // Search filter on homepage var searchInput = document.getElementById('toolSearch'); if (searchInput) { searchInput.addEventListener('input', function() { var q = this.value.toLowerCase().trim(); document.querySelectorAll('.tool-card').forEach(function(card) { if (!q) { card.style.display = 'flex'; return; } var title = (card.querySelector('h3') || {}).textContent || ''; var desc = (card.querySelector('p') || {}).textContent || ''; card.style.display = (title + ' ' + desc).toLowerCase().includes(q) ? 'flex' : 'none'; }); }); } })();