function showState(state) { ['signupInitial', 'signupLoading', 'signupResult'].forEach(function(id) { document.getElementById(id).classList.remove('active'); }); document.getElementById(state).classList.add('active'); } function openSignup() { document.getElementById('signupModal').classList.add('active'); showState('signupInitial'); document.getElementById('signupError').style.display = 'none'; } function closeSignup() { document.getElementById('signupModal').classList.remove('active'); } async function submitSignup() { var errEl = document.getElementById('signupError'); var btn = document.getElementById('signupBtn'); errEl.style.display = 'none'; btn.disabled = true; showState('signupLoading'); try { var res = await fetch('/v1/signup/free', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}) }); var data = await res.json(); if (!res.ok) { showState('signupInitial'); errEl.textContent = data.error || 'Something went wrong. Please try again.'; errEl.style.display = 'block'; btn.disabled = false; return; } document.getElementById('apiKeyText').textContent = data.apiKey; showState('signupResult'); } catch (err) { showState('signupInitial'); errEl.textContent = 'Network error. Please try again.'; errEl.style.display = 'block'; btn.disabled = false; } } function copyKey() { var key = document.getElementById('apiKeyText').textContent; var btn = document.getElementById('copyBtn'); function showCopied() { btn.textContent = '✓ Copied!'; setTimeout(function() { btn.textContent = 'Copy'; }, 2000); } try { navigator.clipboard.writeText(key).then(showCopied).catch(function() { var ta = document.createElement('textarea'); ta.value = key; ta.style.position = 'fixed'; ta.style.opacity = '0'; document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta); showCopied(); }); } catch(e) { showCopied(); } } async function checkout() { try { var res = await fetch('/v1/billing/checkout', { method: 'POST' }); var data = await res.json(); if (data.url) window.location.href = data.url; else alert('Checkout is not available yet. Please try again later.'); } catch (err) { alert('Something went wrong. Please try again.'); } } document.addEventListener('DOMContentLoaded', function() { document.getElementById('btn-signup').addEventListener('click', openSignup); document.getElementById('btn-signup-2').addEventListener('click', openSignup); document.getElementById('btn-checkout').addEventListener('click', checkout); document.getElementById('btn-close-signup').addEventListener('click', closeSignup); document.getElementById('signupBtn').addEventListener('click', submitSignup); document.getElementById('copyBtn').addEventListener('click', copyKey); document.getElementById('signupModal').addEventListener('click', function(e) { if (e.target === this) closeSignup(); }); // Smooth scroll for nav links document.querySelectorAll('a[href^="#"]').forEach(function(a) { a.addEventListener('click', function(e) { e.preventDefault(); var el = document.querySelector(this.getAttribute('href')); if (el) el.scrollIntoView({ behavior: 'smooth' }); }); }); });