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