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