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