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