'use strict'; // Pricing per inputs/brief.md: CAD 240/seat, 10% off subtotal for 3+ seats, // 12% tax applied AFTER the discount. const PRICE_PER_SEAT = 240; // Returns an integer 1-8, or null for blank, non-numeric, fractional, // zero, negative or 9+ values. function parseSeats(raw) { if (typeof raw !== 'string' || raw.trim() === '') return null; const seats = Number(raw); if (!Number.isInteger(seats) || seats < 1 || seats > 8) return null; return seats; } function calculateTotal(seats) { const subtotal = seats * PRICE_PER_SEAT; const discount = seats >= 3 ? 0.10 : 0; return subtotal * (1 - discount) * 1.12; } const seatsInput = document.querySelector('#seats'); const totalEl = document.querySelector('#total'); const seatsError = document.querySelector('#seats-error'); const statusEl = document.querySelector('#status'); function renderTotal() { const seats = parseSeats(seatsInput.value); if (seats === null) { totalEl.textContent = 'CAD —'; seatsError.textContent = 'Enter a whole number of seats from 1 to 8.'; seatsError.hidden = false; } else { totalEl.textContent = 'CAD ' + calculateTotal(seats).toFixed(2); seatsError.textContent = ''; seatsError.hidden = true; } } seatsInput.addEventListener('input', renderTotal); renderTotal(); // Mirrors the server's plausibility check in server.py (do_POST). const EMAIL_RE = /^[^@\s]+@[^@\s]+\.[^@\s]+$/; document.querySelector('#signup').addEventListener('submit', async event => { event.preventDefault(); statusEl.classList.remove('error'); const name = document.querySelector('#name').value.trim(); const email = document.querySelector('#email').value.trim(); const seats = parseSeats(seatsInput.value); const problems = []; if (!name) problems.push('name'); if (!EMAIL_RE.test(email)) problems.push('email'); if (seats === null) problems.push('seats (whole number, 1–8)'); if (problems.length) { statusEl.textContent = 'Please check: ' + problems.join(', ') + '.'; statusEl.classList.add('error'); renderTotal(); return; // invalid: show error, never send a request or create a record } statusEl.textContent = 'Saving your request…'; try { const response = await fetch('/api/leads', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: name, email: email, seats: seats }) }); if (!response.ok) { let message = 'Signup failed. Please check your details and try again.'; try { const body = await response.json(); if (body && body.error) message = body.error; } catch (_) { /* keep fallback message */ } throw new Error(message); } statusEl.textContent = 'Thanks! Your workshop request is saved.'; } catch (error) { statusEl.textContent = error.message === 'Failed to fetch' ? 'Could not reach the server. Please try again.' : error.message; statusEl.classList.add('error'); } });