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