'use strict'; // Pricing rules from inputs/brief.md: CAD 240 per seat, 10% off the subtotal // for 3+ seats, then 12% tax on the discounted amount. Work in cents so the // displayed two-decimal value is exact. const PRICE_PER_SEAT_CENTS = 24000; const MIN_SEATS = 1; const MAX_SEATS = 8; const DISCOUNT_MIN_SEATS = 3; const DISCOUNT_PERCENT = 10; const TAX_PERCENT = 12; const EMAIL_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/; // same shape the server checks const SEATS_MESSAGE = 'Enter a whole number of seats from 1 to 8.'; // Returns an integer 1-8, or null for blank, zero, negative, fractional, // non-numeric or 9+ input. function parseSeats(raw) { const text = String(raw).trim(); if (!/^\d+$/.test(text)) return null; const seats = Number(text); return seats >= MIN_SEATS && seats <= MAX_SEATS ? seats : null; } function calculateTotalCents(seats) { const subtotal = seats * PRICE_PER_SEAT_CENTS; const discount = seats >= DISCOUNT_MIN_SEATS ? subtotal * DISCOUNT_PERCENT / 100 : 0; return Math.round((subtotal - discount) * (100 + TAX_PERCENT) / 100); } function calculateTotal(seats) { return calculateTotalCents(seats) / 100; } function formatCad(cents) { return 'CAD ' + (cents / 100).toFixed(2); } const form = document.querySelector('#signup'); const nameInput = document.querySelector('#name'); const emailInput = document.querySelector('#email'); const seatsInput = document.querySelector('#seats'); const submitButton = form.querySelector('button[type="submit"]'); const status = document.querySelector('#status'); const total = document.querySelector('#total'); function setFieldError(input, message) { const note = document.querySelector('#' + input.id + '-error'); if (message) { input.setAttribute('aria-invalid', 'true'); note.textContent = message; note.hidden = false; } else { input.removeAttribute('aria-invalid'); note.textContent = ''; note.hidden = true; } } function setStatus(message, kind) { status.textContent = message; status.className = kind ? 'is-' + kind : ''; } function renderTotal() { const seats = parseSeats(seatsInput.value); if (seats === null) { total.textContent = 'CAD —'; setFieldError(seatsInput, SEATS_MESSAGE); return null; } total.textContent = formatCad(calculateTotalCents(seats)); setFieldError(seatsInput, ''); return seats; } seatsInput.addEventListener('input', renderTotal); nameInput.addEventListener('input', () => setFieldError(nameInput, '')); emailInput.addEventListener('input', () => setFieldError(emailInput, '')); renderTotal(); let submitting = false; form.addEventListener('submit', async event => { event.preventDefault(); if (submitting) return; const name = nameInput.value.trim(); const email = emailInput.value.trim(); const seats = renderTotal(); const nameError = name ? '' : 'Enter your name.'; const emailError = EMAIL_PATTERN.test(email) ? '' : 'Enter a valid email address, like name@example.test.'; setFieldError(nameInput, nameError); setFieldError(emailInput, emailError); const firstInvalid = nameError ? nameInput : emailError ? emailInput : seats === null ? seatsInput : null; if (firstInvalid) { // Nothing is sent to the server, so no record is created. setStatus('Signup not sent. Please fix the highlighted fields and try again.', 'error'); firstInvalid.focus(); return; } submitting = true; submitButton.disabled = true; setStatus('Sending your request…', ''); try { let response; try { response = await fetch('/api/leads', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({name, email, seats}) }); } catch (networkError) { throw new Error('Signup failed. We could not reach the server. Please try again.'); } if (!response.ok) { let message = 'Signup failed. Please check your details and try again.'; try { const body = await response.json(); if (body && typeof body.error === 'string') message = 'Signup failed. ' + body.error; } catch (parseError) { /* keep the default message */ } throw new Error(message); } const summary = seats + (seats === 1 ? ' seat, ' : ' seats, ') + formatCad(calculateTotalCents(seats)) + ' including tax.'; nameInput.value = ''; emailInput.value = ''; setStatus('Thanks! Your workshop request is saved. ' + summary, 'success'); } catch (error) { setStatus(error.message, 'error'); } finally { submitting = false; submitButton.disabled = false; } });