]> gitweb.michael.orlitzky.com - charm-bypass.git/blob - index.html.in
a68c273f33bf60bb8153367e759df80935d51e14
[charm-bypass.git] / index.html.in
1 <!doctype html>
2 <html lang="en-US">
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1" />
6 <link rel="icon"
7 type="image/svg+xml"
8 href="data:image/svg+xml;base64,@FAVICON@" />
9
10 <title>
11 CharmBypass: that transit equity y'all
12 </title>
13
14 <style>
15 body {
16 /* For consistency with the SVG */
17 font-family: sans-serif;
18 }
19
20 fieldset {
21 margin-top: 1em;
22 padding-top: 1em;
23 }
24
25 input[type=submit] {
26 margin-top: 1em;
27 margin-bottom: 1em;
28 }
29
30 legend {
31 font-weight: bold;
32 }
33
34 /* Display form errors for the one browser that doesn't support
35 * them natively, mobile Firefox. We use "visibility" to toggle
36 * it on and off, but "display" to hide it completely in non-
37 * stupid web browsers. */
38 #marc-form-errors {
39 color: #a00;
40 visibility: hidden;
41 display: none;
42 }
43
44 svg {
45 /* Set the height to 100% of the screen, which we'll keep; and the
46 * initial position to (0,0), which we're going to change
47 * every time the window is resized, because the exact amount
48 * that we have to slide the SVG to the left to get the ticket
49 * into the center will change. */
50 position: fixed;
51 top: 0;
52 left: 0;
53 height: 100%;
54
55 /* Hide everything by default. We only show it once the user has
56 * submitted the menu form */
57 display: none;
58 }
59
60 /* The blinking fade in/out animation for the ticket date and time */
61 @keyframes blink {
62 25% { opacity: 0.5; }
63 50% { opacity: 0; }
64 75% { opacity: 0.5; }
65 }
66
67 #tickettime, #ticketdate {
68 /* 300 two-second blinks is ten minutes */
69 animation: blink 2s linear 300;
70 }
71
72 /* Define, load, and specify the custom font we use for the ticket
73 * date, time, and service name. */
74 @font-face {
75 font-family: "CharmBypass Regular";
76 src:
77 url("data:font/woff2;base64,@CBPREGULAR@") format("woff2")
78 }
79
80 #origindest, #servicename, #tickettime, #ticketdate, #codetext, #zone {
81 font-family: "CharmBypass Regular", sans-serif;
82 }
83
84 @font-face {
85 font-family: "CharmBypass Bold";
86 src:
87 url("data:font/woff2;base64,@CBPBOLD@") format("woff2")
88 }
89
90 #serviceid {
91 font-family: "CharmBypass Bold", sans-serif;
92 }
93
94 /************************/
95 /* Scrolling animations */
96 /************************/
97
98 /* Bus */
99 @keyframes busroll {
100 from { transform: translateX(0%); }
101 to { transform: translateX(-100%); }
102 }
103
104 #bus {
105 animation: busroll 23s linear infinite;
106 }
107
108
109 /* Tram */
110 @keyframes tramroll {
111 from { transform: translateX(0%); }
112 to { transform: translateX(100%); }
113 }
114
115 #tram {
116 animation: tramroll 17s linear infinite;
117 }
118
119
120 /* Train */
121 @keyframes trainroll {
122 from { transform: translateX(0%); }
123 to { transform: translateX(100%); }
124 }
125
126 #train {
127 animation: trainroll 11s linear infinite;
128 }
129
130
131 /* Clouds */
132 @keyframes cloudsfloat {
133 from { transform: translateX(0%); }
134 to { transform: translateX(-50%); }
135 }
136
137 #clouds {
138 animation: cloudsfloat 40s linear infinite;
139 }
140
141 @keyframes cloudscopyfloat {
142 from { transform: translateX(0%); }
143 to { transform: translateX(-50%); }
144 }
145
146 #cloudscopy {
147 animation: cloudscopyfloat 40s linear infinite;
148 }
149
150
151 /* Trees */
152 @keyframes treespass {
153 from { transform: translateX(0%); }
154 to { transform: translateX(-50%); }
155 }
156
157 #trees {
158 /* The trees move a little faster than the clouds */
159 animation: treespass 30s linear infinite;
160 }
161
162 @keyframes treescopypass {
163 from { transform: translateX(0%); }
164 to { transform: translateX(-50%); }
165 }
166
167 #treescopy {
168 /* The trees move a little faster than the clouds */
169 animation: treescopypass 30s linear infinite;
170 }
171
172
173 /* City skyline */
174 @keyframes cityscroll {
175 from { transform: translateX(0%); }
176 to { transform: translateX(-50%); }
177 }
178
179 #city {
180 /* The city moves faster than the clouds but slower
181 * than the trees */
182 animation: cityscroll 35s linear infinite;
183 }
184
185 @keyframes citycopyscroll {
186 from { transform: translateX(0%); }
187 to { transform: translateX(-50%); }
188 }
189
190 #citycopy {
191 /* The city moves faster than the clouds but slower
192 * than the trees */
193 animation: citycopyscroll 35s linear infinite;
194 }
195 </style>
196 </head>
197
198 <body>
199 <div id="menu">
200 <h1>CharmBypass</h1>
201 <h2>that transit equity y'all</h2>
202
203 <p>
204 <em>Hint:</em> If they start checking it, the daily security
205 code stays the same all day and is the same on a $2 bus ticket
206 as it is on a $9 MARC ticket. It's also the same on every phone.
207 </p>
208
209 <form>
210 <fieldset>
211 <legend>Local Bus, Light Rail, or Metro</legend>
212 <div>
213 <label for="code1">
214 Daily security code (optional):
215 </label>
216 <input id="code1"
217 name="code"
218 type="text"
219 size="2"
220 minlength="2"
221 maxlength="2"
222 pattern="[a-zA-Z0-9]*" />
223 </div>
224 <input type="hidden" name="servicename" value="BaltimoreLink" />
225 <input type="submit" name="go" value="Generate Ticket" />
226 </fieldset>
227 </form>
228 <form>
229 <fieldset>
230 <legend>Commuter Bus</legend>
231 <input type="hidden" name="serviceid" value="R" />
232 <input type="hidden" name="servicename" value="Commuter Bus" />
233
234 <div>
235 <label for="code2">
236 Daily security code (optional):
237 </label>
238 <input id="code2"
239 name="code"
240 type="text"
241 size="2"
242 minlength="2"
243 maxlength="2"
244 pattern="[a-zA-Z0-9]*" />
245 </div>
246
247 <p>
248 <abbr title="Found on the MTA's PDF schedule for your route">
249 Zone</abbr>:
250 </p>
251 <div>
252 <input type="radio" required
253 name="zone"
254 id="zone1"
255 value="Zone 1" />
256 <label for="zone1">Zone 1</label>
257 </div>
258 <div>
259 <input type="radio" required
260 name="zone"
261 id="zone2"
262 value="Zone 2" />
263 <label for="zone2">Zone 2</label>
264 </div>
265 <div>
266 <input type="radio" required checked
267 name="zone"
268 id="zone3"
269 value="Zone 3" />
270 <label for="zone3">Zone 3</label>
271 </div>
272 <div>
273 <input type="radio" required
274 name="zone"
275 id="zone4"
276 value="Zone 4" />
277 <label for="zone4">Zone 4</label>
278 </div>
279 <div>
280 <input type="radio" required
281 name="zone"
282 id="zone5"
283 value="Zone 5" />
284 <label for="zone5">Zone 5</label>
285 </div>
286
287 <input type="submit" name="go" value="Generate Ticket" />
288 </fieldset>
289 </form>
290 <form>
291 <fieldset>
292 <legend>MARC Train</legend>
293
294 <div>
295 <label for="code3">
296 Daily security code (optional):
297 </label>
298 <input id="code3"
299 name="code"
300 type="text"
301 size="2"
302 minlength="2"
303 maxlength="2"
304 pattern="[a-zA-Z0-9]*" />
305 </div>
306
307 <p>Origin:</p>
308 <div>
309 <input type="radio" required checked
310 name="origin"
311 id="origin1"
312 value="BAL" />
313 <label for="origin1">Baltimore/Penn (Penn Line)</label>
314 </div>
315 <div>
316 <input type="radio" required
317 name="origin"
318 id="origin2"
319 value="BCA" />
320 <label for="origin2">Baltimore/Camden (Camden Line)</label>
321 </div>
322 <div>
323 <input type="radio" required
324 name="origin"
325 id="origin3"
326 value="BWE" />
327 <label for="origin3">Bowie State (Penn Line)</label>
328 </div>
329 <div>
330 <input type="radio" required
331 name="origin"
332 id="origin4"
333 value="BWI" />
334 <label for="origin4">BWI Airport (Penn Line)</label>
335 </div>
336 <div>
337 <input type="radio" required
338 name="origin"
339 id="origin5"
340 value="CPK" />
341 <label for="origin5">College Park (Camden Line)</label>
342 </div>
343 <div>
344 <input type="radio" required
345 name="origin"
346 id="origin6"
347 value="SEB" />
348 <label for="origin6">Seabrook (Penn Line)</label>
349 </div>
350 <div>
351 <input type="radio" required
352 name="origin"
353 id="origin7"
354 value="WAS" />
355 <label for="origin7">Washington D.C.</label>
356 </div>
357 <div>
358 <input type="radio" required
359 name="origin"
360 id="origin8"
361 value="WBL" />
362 <label for="origin8">West Baltimore (Penn Line)</label>
363 </div>
364
365 <p>Destination:</p>
366 <div>
367 <input type="radio" required
368 name="destination"
369 id="destination1"
370 value="BAL" />
371 <label for="destination1">Baltimore/Penn (Penn Line)</label>
372 </div>
373 <div>
374 <input type="radio" required
375 name="destination"
376 id="destination2"
377 value="BCA" />
378 <label for="destination2">Baltimore/Camden (Camden Line)</label>
379 </div>
380 <div>
381 <input type="radio" required
382 name="destination"
383 id="destination3"
384 value="BWE" />
385 <label for="destination3">Bowie State (Penn Line)</label>
386 </div>
387 <div>
388 <input type="radio" required
389 name="destination"
390 id="destination4"
391 value="BWI" />
392 <label for="destination4">BWI Airport (Penn Line)</label>
393 </div>
394 <div>
395 <input type="radio" required
396 name="destination"
397 id="destination5"
398 value="CPK" />
399 <label for="destination5">College Park (Camden Line)</label>
400 </div>
401 <div>
402 <input type="radio" required
403 name="destination"
404 id="destination6"
405 value="SEB" />
406 <label for="destination6">Seabrook (Penn Line)</label>
407 </div>
408 <div>
409 <input type="radio" required checked
410 name="destination"
411 id="destination7"
412 value="WAS" />
413 <label for="destination7">Washington D.C.</label>
414 </div>
415 <div>
416 <input type="radio" required
417 name="destination"
418 id="destination8"
419 value="WBL" />
420 <label for="destination8">West Baltimore (Penn Line)</label>
421 </div>
422
423 <p id="marc-form-errors">OK</p>
424
425 <input type="hidden" name="serviceid" value="R" />
426 <input type="hidden" name="servicename" value="MARC Train" />
427 <input type="submit"
428 name="go"
429 id="marc-submit"
430 value="Generate Ticket" />
431 </fieldset>
432 </form>
433 </div>
434
435 @SVGDATA@
436
437 <script>
438
439 /**
440 * Center the ticket within the browser by translating the SVG
441 * until the ticket and the viewport centerlines coincide.
442 */
443 function center_ticket() {
444 /* We're relying on the SVG being the full height of the
445 * viewport already, and on the aspect ratio being
446 * preserved. First, find the center of the ticket. */
447 const r = document.getElementById("ticket").getBoundingClientRect();
448 const c = r.left + (r.width / 2);
449
450 /* That's the center-line of the ticket. We want to move it to
451 * the center-line of the viewport. */
452 const vc = document.documentElement.clientWidth / 2;
453
454 /* This is how much we need to translate the SVG */
455 const hdelta = vc - c;
456
457 /* But before we can set the absolute left-coordinate of the
458 * SVG, we need to know where it is now. Note: without the
459 * "px" this doesn't default to pixels like CSS does. */
460 const svg = document.querySelector("svg");
461 svg.style.left = (svg.getBoundingClientRect().left + hdelta) + "px";
462 }
463
464
465 /**
466 * Set the service identifier from the querystring if it's there.
467 * Otherwise, leave it at the default of "F".
468 */
469 function set_service_id() {
470 const sid = document.getElementById("serviceid");
471
472 /* Get the "serviceid" from the querystring if it's there */
473 const params = new URLSearchParams(document.location.search);
474 if (params.get("serviceid")) {
475 sid.textContent = params.get("serviceid");
476 }
477 }
478
479
480 /**
481 * Set the service name from the querystring if it's there.
482 * Otherwise, leave it at the default of "BaltimoreLink".
483 */
484 function set_service_name() {
485 const sid = document.getElementById("servicename");
486
487 /* Get the "servicename" from the querystring if it's there */
488 const params = new URLSearchParams(document.location.search);
489 if (params.get("servicename")) {
490 sid.textContent = params.get("servicename");
491 }
492 }
493
494
495 /**
496 * Set the zone from the given "zone" parameter and then unhide it.
497 */
498 function set_zone(zone) {
499 const z = document.getElementById("zone");
500
501 z.textContent = zone;
502 z.style.display = "block"; /* hidden by default */
503 }
504
505
506 /**
507 * Resize the ticket background based on the service name.
508 * The BaltimoreLink, Commuter Bus, and MARC Train tickets
509 * are all different heights and are arranged vertically a
510 * bit different.
511 *
512 * Rather than design three completelty separate tickets and
513 * then have to keep track of which one we're using, I have
514 * instead decided to use one ticket and to reposition it
515 * on-the-fly based on the service name. This is necessarily
516 * a bit ugly because it involves a lot of magic numbers that
517 * can only be explained if you open up inkscape with a CharmPass
518 * screenshot to see where things belong and how to get them there.
519 *
520 * The SVG was designed with BaltimoreLink in mind, so this
521 * is a no-op if the service is BaltimoreLink.
522 */
523 function resize_ticket() {
524 /* Get the "servicename" from the querystring if it's there */
525 const params = new URLSearchParams(document.location.search);
526 const tbg = document.getElementById("ticketbg");
527 const t = document.getElementById("ticket");
528 const sn = document.getElementById("servicename");
529
530 if (params.get("servicename") === "Commuter Bus") {
531 /* The top of the background is initially at y=246.859, and
532 * we scale it by a factor of 1.12 to y=276.482 for a change
533 * of 29.623. So after we scale it, we translate it upwards
534 * by that amount to put it back where it started. */
535 tbg.setAttribute("transform", "translate(0 -29.623) scale(1 1.12)");
536
537 /* Now translate the entire ticket up by the magic amount, 1/5
538 * of the size change we made to the background. This ratio
539 * was found by measuring pixels in side-by-side screenshots
540 * of BaltimoreLink and Commuter Bus tickets. */
541 t.setAttribute("transform", "translate(0 -9.33)");
542
543 /* More magic numbers discovered by comparing the two
544 * tickets overlayed in inkscape */
545 sn.setAttribute("transform", "translate(0 64.28)");
546 }
547 else if (params.get("servicename") === "MARC Train") {
548 /* insane tricks are explained above */
549 tbg.setAttribute("transform",
550 "translate(0 -72.378) scale(1 1.2932)");
551 t.setAttribute("transform", "translate(0 -67.17)");
552 sn.setAttribute("transform", "translate(0 131.0)");
553 }
554 }
555
556
557 /**
558 * Set the security code from the querystring if it was given;
559 * otherwise generate a random code.
560 */
561 function set_code() {
562 const ct = document.getElementById("codetext");
563
564 /* Get the "code" from the querystring if it's there */
565 const params = new URLSearchParams(document.location.search);
566 if (params.get("code")) {
567 ct.textContent = params.get("code").toUpperCase();
568 }
569 else {
570 /* Otherwise, use a random code */
571 const bucket = ["0","1","2","3","4","5","6","7","8","9",
572 "A","B","C","D","E","F","G","H","I","J",
573 "K","L","M","N","O","P","Q","R","S","T",
574 "U","V","W","X","Y","Z"];
575
576 /* Two random ints between 0 and 35 */
577 const i1 = Math.floor(Math.random() * 36);
578 const i2 = Math.floor(Math.random() * 36);
579 const d1 = bucket[i1];
580 const d2 = bucket[i2];
581 ct.textContent = d1 + d2;
582 }
583 }
584
585
586 /**
587 * Center the security code within its container.
588 *
589 * Some codes like "II" and "WW" can take up wildly different
590 * amounts of horizonetal space, but they should always be
591 * centered inside their little red box. This turns out to be
592 * harder than it sounds because we can only find the width of
593 * the code in browser coordinates, whereas its "x" coordinate
594 * is in SVG coordinates. Anyway, we do it.
595 */
596 function center_code() {
597 /* Center the security code inside its red box */
598 const ct = document.getElementById("codetext");
599 const bg = document.getElementById("codebg");
600
601 /* First, find the center of the red box */
602 const r1 = bg.getBoundingClientRect();
603 const c1 = r1.left + (r1.width / 2);
604
605 /* Now the center of the code text */
606 const r2 = ct.getBoundingClientRect();
607 const c2 = r2.left + (r2.width / 2);
608
609 /* What do we add to c2 to make it equal to c1? */
610 const hdelta = c1 - c2;
611
612 /* We've measured everything so far in "client rect"
613 * coordinates, because that's the only available measurement
614 * we have for the width of the <text> element. But when we
615 * reposition that <text> element, it will be by adjusting its
616 * "x" attribute, and that attribute uses a different coordinate
617 * system than the client rect does. Specifically, "x" refers to
618 * an offset within the SVG's coordinate system, and the client
619 * rect coordinates are pixels on-screen. To convert between the
620 * two, we can take the "width" attribute of the background
621 * element and compare it to the width of the background
622 * element's client rect. Since the size of the background is
623 * fixed, this should give us a multiplier that turns client rect
624 * distances (what we have) into SVG distances (what we want) */
625 const client_to_svg = parseFloat(bg.getAttribute("width"))/r1.width;
626
627 /* Convert hdelta from client rect to SVG coordinates */
628 const svg_hdelta = hdelta * client_to_svg;
629
630 /* Since this <text> element has an "x" attribute it's easier for
631 * us to shift that than it is to mess with the "left" style. */
632 ct.setAttribute("x", parseFloat(ct.getAttribute("x")) + svg_hdelta);
633 }
634
635 /**
636 * Set the ticket's expiration date and time.
637 *
638 * BaltimoreLink and MARC Train tickets expire after 90 minutes;
639 * while Commuter Bus tickets expire after 10 minutes.
640 */
641 function set_ticket_expiry() {
642 /* There are two parameters, time and date, that we store in one
643 * underlying "date" variable. */
644 const date = new Date();
645
646 /* BaltimoreLink and MARC Train */
647 let minutes = 90;
648 const params = new URLSearchParams(document.location.search);
649 if (params.get("servicename") === "Commuter Bus") {
650 /* Commuter bus tickets are only valid for ten minutes */
651 minutes = 10;
652 }
653
654 /* We use the low-level get/setTime to change the number of
655 * milliseconds since the epoch that this date represents
656 * Obviously correct, and avoids all suspicious corner cases
657 * for a few more decades. */
658 date.setTime(date.getTime() + (minutes*60*1000));
659
660 tt = document.getElementById("tickettime");
661 tt.textContent = date.toLocaleTimeString();
662
663 const td = document.getElementById("ticketdate");
664 const dateopts = {
665 day: "2-digit",
666 month: "2-digit",
667 year: "2-digit"
668 };
669 td.textContent = date.toLocaleDateString("en-US", dateopts);
670 }
671
672
673 /**
674 * Swap the day/night sky colors.
675 *
676 * We use CSS classes to keep track of the current state because
677 * it's a tiny bit cleaner than a global variable, but for some
678 * reason we can't use those same classes to actually change the
679 * color. (The classes, change, but the color doesn't.) Rather
680 * than waste time trying to explain this, we just set the "fill"
681 * attribute ourselves whenever we swap classes.
682 */
683 function swap_day_night() {
684 const sky = document.getElementById("sky");
685
686 if (sky.getAttribute("class") === "night") {
687 sky.setAttribute("fill", "#efb02f");
688 sky.setAttribute("class", "day");
689 }
690 else {
691 /* Put this case second so that the first time the
692 * screen is tapped (when there are no classes on
693 * the sky element) the color still changes. */
694 sky.setAttribute("fill", "#143b66");
695 sky.setAttribute("class", "night");
696 }
697 }
698
699
700 /**
701 * Compute the zone (string) for the given origin/destination pair.
702 *
703 * If we don't know it or if you chose in invalid pair (destination
704 * not on the same line as your origin?) then null is returned.
705 */
706 function compute_marc_zone(src, dest) {
707
708 /* Sorted on the first component, then the second */
709 const zone_map = {
710 BAL_BWE: 2,
711 BAL_BWI: 1,
712 BAL_SEB: 3,
713 BAL_WAS: 4,
714 BAL_WBL: 1,
715 BCA_CPK: 3,
716 BCA_WAS: 4,
717 BWE_BWI: 1,
718 BWE_SEB: 1,
719 BWE_WAS: 2,
720 BWE_WBL: 2,
721 BWI_WAS: 3,
722 BWI_WBL: 1
723 };
724
725 /* Forward direction key for zone_map */
726 const fwd = src + "_" + dest;
727
728 /* Reverse direction key for zone_map. The zone_map only
729 * has them listed in one direction, so we check both
730 * directions here. */
731 const rev = dest + "_" + src;
732
733 /* The default. Obviously wrong for when we don't
734 * have the necessary data. */
735 let zone = -1;
736
737 if (zone_map[fwd]) {
738 zone = zone_map[fwd];
739 }
740 else if (zone_map[rev]) {
741 zone = zone_map[rev];
742 }
743
744 /* Convert the number to a string */
745 switch (zone) {
746 case 1:
747 return "One Zone";
748 case 2:
749 return "Two Zone";
750 case 3:
751 return "Three Zone";
752 case 4:
753 return "Four Zone";
754 default:
755 return null;
756 }
757 }
758
759
760 /**
761 * Compute and set the zone.
762 *
763 * We can be given a zone in two ways. First, on Commuter Bus
764 * tickets, it is given explicitly via the querystring. But
765 * It can also be specified implicitly via the origin and
766 * destination on a MARC Train ticket. Here we try both and
767 * then call set_zone() with the result if something worked.
768 */
769 function compute_and_set_zone() {
770 const params = new URLSearchParams(document.location.search);
771 const src = params.get("origin");
772 const dest = params.get("destination");
773
774 if (src && dest) {
775 /* MARC Train. We can assume that compute_marc_zone() doesn't
776 * return null because that's part of our form validation. */
777 const zone = compute_marc_zone(src, dest);
778 set_zone(zone);
779 }
780 else if (params.get("zone")) {
781 /* Commuter Bus */
782 set_zone(params.get("zone"));
783 }
784 }
785
786
787 /**
788 * Set the origin and destination for the MARC Train if they
789 * were provided, and unhide them if so.
790 */
791 function set_marc_origin_destination() {
792 const params = new URLSearchParams(document.location.search);
793 if (!params.get("origin") || !params.get("destination")) {
794 return;
795 }
796
797 const src = params.get("origin");
798 const dest = params.get("destination");
799
800 /* origindest contains both the origin and destination */
801 const origindest = document.getElementById("origindest");
802
803 const origin = document.getElementById("origin");
804 const destination = document.getElementById("destination");
805
806 origin.textContent = params.get("origin");
807 destination.textContent = params.get("destination");
808
809 origindest.style.display = "block"; /* hidden by default */
810 }
811
812
813 /**
814 * Hide the menu and display the ticket. This is what happens
815 * when you submit the form.
816 */
817 function go() {
818 /* To create our "window" onto the scene, we're going to slide the
819 * SVG off the left-hand side of the screen, and we don't want
820 * scroll bars to appear. */
821 document.body.style.overflow = "hidden";
822 document.body.style.margin = "0";
823 document.body.style.padding = "0";
824
825 const svg = document.querySelector("svg");
826 const menu = document.getElementById("menu");
827 svg.style.display = "initial";
828 menu.style.display = "none";
829 }
830
831 /**
832 * Determine if the user agent is mobile Firefox.
833 */
834 function ua_is_mobile_ff() {
835 const ua = navigator.userAgent.toLowerCase();
836 return (ua.includes("firefox") && ua.includes("mobile"));
837 }
838
839 /**
840 * Validate the MARC form's origin/destination.
841 *
842 * We don't want the user to be able to choose a pair of stops that
843 * aren't actually connected by the same MARC line. If we don't have
844 * zone information for the (origin,destination) pair, that indicates
845 * that it's probably not a valid choice; otherwise I would have
846 * filled in the information from the CharmPass app already.
847 *
848 * All browsers except mobile Firefox let us call setCustomValidity()
849 * to provide an error message that is displayed if the user tries
850 * to submit invalid choices. But amazingly, mobile Firefox does not:
851 *
852 * https://bugzilla.mozilla.org/show_bug.cgi?id=1510450
853 *
854 * Instead we have to work around it (in that one browser) by
855 * showing/hiding a paragraph that we fill with the errors.
856 */
857 function validate_origin_destination(event) {
858 const origins = document.getElementsByName("origin");
859 const destinations = document.getElementsByName("destination");
860 const mfe = document.getElementById("marc-form-errors");
861 const marcsubmit = document.getElementById("marc-submit");
862
863 if (ua_is_mobile_ff()) {
864 /* Even though this is only for one browser, empty paragraphs
865 * are handled inconsistently and should be avoided as a rule.
866 * So, we make it say "OK" before we hide it. */
867 mfe.textContent = "OK";
868 mfe.style.visibility = "hidden";
869 marcsubmit.disabled = false;
870 }
871
872 let src = null;
873 let dest = null;
874 origins.forEach((x) => { if (x.checked) src = x; })
875 destinations.forEach((x) => {
876 if (x.checked) dest = x;
877
878 /* clear all errors before possibly setting one */
879 x.setCustomValidity('');
880 })
881
882 if (src.value === dest.value) {
883 let err = "Origin and destination are the same";
884 dest.setCustomValidity(err);
885
886 if (ua_is_mobile_ff()) {
887 mfe.textContent = err;
888 mfe.style.visibility = "visible";
889 marcsubmit.disabled = true;
890 }
891 }
892 else if (compute_marc_zone(src.value, dest.value) === null) {
893 let err = "Origin and destination are on different lines";
894 dest.setCustomValidity(err);
895
896 if (ua_is_mobile_ff()) {
897 mfe.textContent = err;
898 mfe.style.visibility = "visible";
899 marcsubmit.disabled = true;
900 }
901 }
902 }
903
904 /*****************************************************/
905 /* Add event handlers for all of the functions above */
906 /*****************************************************/
907
908 const params = new URLSearchParams(document.location.search);
909 if (params.get("go")) {
910 /* First unhide the SVG (swap it with the form) */
911 window.addEventListener("load", go);
912
913 /* Center the ticket once when the page has loaded */
914 window.addEventListener("load", center_ticket);
915
916 /* Re-center the ticket when the window is resized */
917 window.addEventListener("resize", center_ticket);
918
919 /* Set the service identifier when the page has loaded */
920 window.addEventListener("load", set_service_id);
921
922 /* Set the service name when the page has loaded */
923 window.addEventListener("load", set_service_name);
924
925 /* Resize the ticket background if necessary */
926 window.addEventListener("load", resize_ticket);
927
928 /* Set the security code text when the page has loaded */
929 window.addEventListener("load", set_code);
930
931 /* Center the security code text when the page has loaded; in
932 * particular, after we set the code. */
933 window.addEventListener("load", center_code);
934
935 /* Set the ticket expiration date/time upon page load */
936 window.addEventListener("load", set_ticket_expiry);
937
938 /* Set the MARC Train origin and destination, if applicable */
939 window.addEventListener("load", set_marc_origin_destination);
940
941 /* Compute and set the zone, if applicable */
942 window.addEventListener("load", compute_and_set_zone);
943
944 /* Swap colors when the screen is tapped */
945 document.body.addEventListener("click", swap_day_night);
946 }
947 else {
948 /* If we haven't submitted the form yet, set up change handlers
949 * for the origin/destination radio buttons that validate that
950 * the origin and destination are on the same line. */
951 document.getElementsByName("origin").forEach(
952 (x) => x.addEventListener("change", validate_origin_destination)
953 );
954 document.getElementsByName("destination").forEach(
955 (x) => x.addEventListener("change", validate_origin_destination)
956 );
957
958 /* Also do it when the page loads, because firefox likes to
959 * remember your selection even after the page reloads. */
960 window.addEventListener("load", validate_origin_destination);
961
962 /* Finally, we have to babysit mobile Firefox, who doesn't
963 * support HTML5 form validation going into 2024. Turn on
964 * the little form errors paragraph so we can toggle its
965 * visibility (and make it display the error) when the user
966 * makes an invalid selection. */
967 window.addEventListener("load", () => {
968 if (ua_is_mobile_ff()) {
969 const mfe = document.getElementById("marc-form-errors");
970 mfe.style.display = "block";
971 }
972 });
973 }
974
975 </script>
976 </body>
977 </html>