forked from HPQC-LABS/HPQC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lectures_tickets.js
629 lines (507 loc) · 22.9 KB
/
lectures_tickets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
var vis_pages = document.getElementsByClassName("page visible");
var width_per_page = 1/vis_pages.length*100;
window.onload = function() {
var part1 = document.getElementById("part1");
var part2 = document.getElementById("part2");
var part3 = document.getElementById("part3");
var part4 = document.getElementById("part4");
var part5 = document.getElementById("part5");
// var next1 = document.getElementById("next1");
// var next2 = document.getElementById("next2");
// var next3 = document.getElementById("next3");
// var next4 = document.getElementById("next4");
// var back1 = document.getElementById("back1");
// var back2 = document.getElementById("back2");
// var back3 = document.getElementById("back3");
// var back4 = document.getElementById("back4");
var progress = document.getElementById("progress");
// Set the progress and step col width based on the number of visible pages
progress.style.width = width_per_page + "%";
document.documentElement.style.setProperty('--default-step-width', width_per_page+"%");
// Display the current page
currentPage = 0;
showPage(currentPage);
// next1.onclick = function(){
// // part1.style.transform = "translateX(-650px)";
// // part2.style.transform = "translateX(0px)";
// // part1.style.webkitTransform = "translateX(-650px)";
// // part2.style.webkitTransform = "translateX(0px)";
// if (validateForm()) {
// progress.style.width = "50%";
// showPage(1);
// currentPage += 1;
// }
// }
// back1.onclick = function(){
// // part1.style.transform = "translateX(0px)";
// // part2.style.transform = "translateX(650px)";
// // part1.style.webkitTransform = "translateX(0px)";
// // part2.style.webkitTransform = "translateX(650px)";
// progress.style.width = "25%";
// showPage(0);
// currentPage -= 1;
// }
// next2.onclick = function(){
// // part2.style.transform = "translateX(-650px)";
// // part3.style.transform = "translateX(0px)";
// // part2.style.webkitTransform = "translateX(-650px)";
// // part3.style.webkitTransform = "translateX(0px)";
// if (validateForm()) {
// progress.style.width = "75%";
// showPage(2);
// currentPage += 1;
// }
// }
// back2.onclick = function(){
// // part2.style.transform = "translateX(0px)";
// // part3.style.transform = "translateX(650px)";
// // part2.style.webkitTransform = "translateX(0px)";
// // part3.style.webkitTransform = "translateX(650px)";
// progress.style.width = "50%";
// showPage(1);
// currentPage -= 1;
// }
// next3.onclick = function(){
// // part3.style.transform = "translateX(-650px)";
// // part4.style.transform = "translateX(0px)";
// // part3.style.webkitTransform = "translateX(-650px)";
// // part4.style.webkitTransform = "translateX(0px)";
// if (validateForm()) {
// progress.style.width = "100%";
// showPage(3);
// currentPage += 1;
// }
// }
// back3.onclick = function(){
// // part3.style.transform = "translateX(0px)";
// // part4.style.transform = "translateX(650px)";
// // part3.style.webkitTransform = "translateX(0px)";
// // part4.style.webkitTransform = "translateX(650px)";
// progress.style.width = "75%";
// showPage(2);
// currentPage -= 1;
// }
//Code for event reveal when clicking student or graduated checkboxes
var student = document.getElementById('student')
var graduated = document.getElementById('graduated')
var header = document.getElementById('listHeader')
//Onclick list will be revealed and the header text will be changed given the radio chosen
$('.inputSpace').click(function(){
if (student.checked) {
document.getElementById('whenClicked').style.display = 'block';
header.innerHTML = "Current studies:"
}
else if (graduated.checked) {
document.getElementById('whenClicked').style.display = 'block';
header.innerHTML = "Highest completed level of education:"
//$('input[name=level]').prop('checked',false);
}
else document.getElementById('whenClicked').style.display = 'none';
});
//Code for event reveal when clicking facebook, discord, or email radio buttons
//Variable declaration
var school = document.getElementById('schoolOption');
var discord = document.getElementById('discordOption');
var fb = document.getElementById('fbOption');
var emailOption = document.getElementById('emailOption');
console.log(document.getElementsByName('noticeText'));
$('.radio').click(function(){
//Reveal input box when clicking specific radio
if (school.checked) {
document.getElementById('ifSchool').style.display = 'block';
}
else document.getElementById('ifSchool').style.display = 'none';
if (discord.checked) {
document.getElementById('ifDiscord').style.display = 'block';
}
else document.getElementById('ifDiscord').style.display = 'none';
if (fb.checked) {
document.getElementById('ifFb').style.display = 'block';
}
else document.getElementById('ifFb').style.display = 'none';
if (emailOption.checked) {
document.getElementById('ifEmail').style.display = 'block';
}
else document.getElementById('ifEmail').style.display = 'none';
});
//Function goes through checkboxes and removes required field from those that aren't selected (as long as at least one is selected)
$(function(){
var requiredCheckboxes = $('.options :checkbox[required]');
requiredCheckboxes.change(function(){
if(requiredCheckboxes.is(':checked')) {
requiredCheckboxes.removeAttr('required');
} else {
requiredCheckboxes.attr('required', 'required');
}
});
});
document.addEventListener('invalid', (function () {
return function (e) {
alert("Please fill out all required fields before submitting");
e.preventDefault();
document.getElementById("name").focus();
};
})(), true);
}
function showPage(n) {
// Display the specified page of the form
vis_pages[n].style.display = "block";
// Display the correct buttons
var backBtn = document.getElementById("backBtn");
var nextBtn = document.getElementById("nextBtn");
var submitBtn = document.getElementById("submit-form");
submitBtn.style.display = "none";
if (n == 0) {
backBtn.style.display = "none";
} else if (n == vis_pages.length - 1) {
nextBtn.style.display = "none";
submitBtn.style.display = "inline";
} else {
backBtn.style.display = "inline";
nextBtn.style.display = "inline";
nextBtn.innerHTML = "Next";
}
}
function nextPrev(n) {
// This function will figure out which page to display
// pass n = 1 to go to the next page
// pass n = -1 to go to the previous page
if (n == 1 && !validateForm()) return false;
// Hide the current page:
vis_pages[currentPage].style.display = "none";
// Increase or decrease the current page by 1:
currentPage = currentPage + n;
// Display the correct page:
showPage(currentPage);
// Increase or decrease the progress
const new_width = (currentPage+1)/vis_pages.length*100;
progress.style.width = new_width.toString() + "%";
}
function resetForm(){
//Function will reset form after submission
showPage(0);
// hide the studies section
var studies = document.getElementById("whenClicked");
studies.style.display = "none";
// hide last step
vis_pages[vis_pages.length-1].style.display = "none";
// reset progress
progress.style.width = width_per_page + "%";
}
function validateForm(){
var valid = true;
if (currentPage == 0){
// Text input check for required fields
var req_fields = vis_pages[currentPage].querySelectorAll(".required");
for (var i = 0; i < req_fields.length; ++i) {
// Format check for email input
if (req_fields[i].id == "email"){
var mailformat = /^[^@]+@\w+(\.\w+)+\w$/;
if (!mailformat.test(req_fields[i].value.toLowerCase())){
req_fields[i].className += " invalid";
valid = false;
}
}
// Empty string check for normal text input
else if (req_fields[i].value == ""){
req_fields[i].className += " invalid";
valid = false;
}
}
// Selection check for radio buttons
if (student.checked) {
// Selection check for education level
var marked = vis_pages[currentPage].querySelector('input[name="level"]:checked');
if (!marked) {
valid = false;
}
}
else if (graduated.checked) {
// Empty string check for graduation year
// var grad_yr = vis_pages[currentPage].querySelector("#graduationYear");
// if (grad_yr.value == "" || isNaN(grad_yr.value) ){
// valid = false;
// grad_yr.className += " invalid";
// }
// Selection check for education level
var marked = vis_pages[currentPage].querySelector('input[name="level"]:checked');
if (!marked) {
valid = false;
}
} else {
valid = false;
}
}
if (currentPage == 1) {
var marked = vis_pages[currentPage].querySelector('input:checked');
if (!marked) {
valid = false;
}
}
if (currentPage == 2) {
var marked = document.querySelector('input[name="notice"]:checked');
if (!marked){
valid = false;
} else {
if (marked.value == "input"){
// get the corresponding input text element
// by replacing "Option" in the id with "Text"
// ex. "fbOption" id becomes "fbText"
var input = document.getElementById(marked.id.replace("Option", "Text"));
if (input.value == ""){
input.className += " invalid";
valid = false;
}
}
}
}
return valid;
}
//For Message popup after submitting the form
let exit = document.getElementsByClassName('close');
let overlay = document.getElementById("eContainer");
let endScreen = document.getElementById('end');
//Toggle div visibility on close button
$(exit).click(function(){
$(overlay).css("display", "none");
$(endScreen).css("display", "none");
});
//Reveal End Screen after form is submitted
function revealEndScreen(){
console.log("test");
$(overlay).css("display", "block");
$(endScreen).css("display", "block");
}
/* Code for creating new id - for this to work, also uncomment the two id declaration lines in lectures_tickets.html
function getIdList(){
return new Promise(resolve => {
const url = "https://script.google.com/macros/s/AKfycbyr7PrhYsP5EbwFiKLLiAiDzfSQB8MHJqUXp6BH971tW711OgcviDrAcpMxaeGjVmMdCQ/exec";
fetch(url)
.then(d => d.json())
.then(d => {
//console.log(d[0].id);
idList2 = d[0].id;
resolve(idList2);
});
});
}
//Returns idList containing all current ids in Google Sheet
function getId(){
return idList;
}
//Function creates a unique id after checking ids in Google Sheets
async function createId(){
var list;
var newId = Math.floor((Math.random() * 1000000000) + 1);
await getId().then(result => list = result);
var create = false;
//While loop will run until the id is new
while(create != true){
//Creates a new number if the current one already exists
if(list.indexOfForArrays([newId]) != -1){
newId = Math.floor((Math.random() * 1000000000) + 1);
console.log("id already exists")
}
else{
//Returns new id
create = true;
return newId;
}
}
}
//Function is used to search through array of arrays to see if the variable search
Array.prototype.indexOfForArrays = function(search)
{
var searchJson = JSON.stringify(search);
var arrJson = this.map(JSON.stringify);
return arrJson.indexOf(searchJson);
};
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////THIS IS OLD CODE THAT WAS USED BEFORE LECTURES_PAYMENT, FOR UP TO DATE VERSION OF PAYPAL AND IP LOOK AT LECTURES_PAYMENT.JS///////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
//Function initializes paypal payment button
function initPayPalButton() {
//Variable declariation / initialization
var shipping = 0;
var itemOptions = document.querySelector("#smart-button-container #item-options");
var quantity = parseInt();
var quantitySelect = document.querySelector("#smart-button-container #quantitySelect");
if (!isNaN(quantity)) {
quantitySelect.style.visibility = "visible";
}
//Payment description
var orderDescription = 'Lecture Series Ticket';
if (orderDescription === '') {
orderDescription = 'Item';
}
//Styling of button
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
//Creation of new order
createOrder: function (data, actions) {
var selectedItemDescription = itemOptions.options[itemOptions.selectedIndex].value;
var selectedItemPrice = parseFloat(itemOptions.options[itemOptions.selectedIndex].getAttribute("price"));
var tax = (0 === 0) ? 0 : (selectedItemPrice * (parseFloat(0) / 100));
if (quantitySelect.options.length > 0) {
quantity = parseInt(quantitySelect.options[quantitySelect.selectedIndex].value);
} else {
quantity = 1;
}
tax *= quantity;
tax = Math.round(tax * 100) / 100;
var priceTotal = quantity * selectedItemPrice + parseFloat(shipping) + tax;
priceTotal = Math.round(priceTotal * 100) / 100;
var itemTotalValue = Math.round((selectedItemPrice * quantity) * 100) / 100;
return actions.order.create({
purchase_units: [{
description: orderDescription,
//Price, currency, & location settings
amount: {
currency_code: 'CAD',
value: priceTotal,
breakdown: {
item_total: {
currency_code: 'CAD',
value: itemTotalValue,
},
shipping: {
currency_code: 'CAD',
value: shipping,
},
tax_total: {
currency_code: 'CAD',
value: tax,
}
}
},
items: [{
name: selectedItemDescription,
unit_amount: {
currency_code: 'CAD',
value: selectedItemPrice,
},
quantity: quantity
}]
}],
application_context: {
shipping_preference: 'NO_SHIPPING'
},
payer: {
address: {
country_code: 'CA'
}
}
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onError: function (err) {
console.log(err);
},
}).render('#paypal-button-container');
}
initPayPalButton();
//Setting default conditions
let code = 'CA';
let currency = 'CAD';
let currencyName = 'Dollars';
//Logging country, and curreny from users ip
function getIp() {
$.getJSON('https://ipapi.co/json/', function(data) {
console.log(JSON.stringify(data, null, 2));
code = data.country;
currency = data.currency;
});
}
//Running the following once website has loaded
window.onload = function() {
//Takes in the currency symbol and exchange rate from exchange rate xml file
function readXML(){
xmlContent = '';
fetch('exchangeRates/eurofxref-daily02-25-21.xml').then((response)=>{
//Parsing content from file
response.text().then((xml)=>{
xmlContent = xml;
let parser = new DOMParser();
let xmlDOM = parser.parseFromString(xmlContent, 'application/xml');
let xmlCube = xmlDOM.querySelectorAll('Cube');
xmlCube.forEach(CubeXmlNode => {
//Initializing variables
var list;
var listCurrency;
var listExchangeRate;
console.log(CubeXmlNode.children.length);
//Checking each entry to see if it matches users currency from their ip
for(i=0; i < CubeXmlNode.children.length; i++){
list = CubeXmlNode.children[i];
listExchangeRate = list.getAttribute('rate');
listCurrency = list.getAttribute('currency');
listSymbol = list.getAttribute('symbol');
if(listCurrency == currency){
getPrice(list);
break;
console.log("Country is: " + listCurrency + " = " + currency + "\n Exchange rate is: " + listExchangeRate);
}
}
});
});
});
}
//Determines the price of the ticket given the users location
function getPrice(input) {
list = input;
console.log(list.getAttribute('rate'));
console.log(list.getAttribute('currency') + "TEST");
var select = document.getElementsByTagName('select');
/*Exchange rate is based on EUR so prices will look off here*/
/*
var lecPrice = [20, 40, 50]; /*Original prices in CAD: 35, 70, 105, 140, 115*/
/*
var lecPriceUSD = [16, 32, 40]; /*Original prices in CAD: 35, 70, 105, 140, 115*/
//var lecPriceDiscount = [20, 40, 60, 75, 80]; /*Original discount prices in CAD: 20, 40, 60, 80, 75*/
//Altering currency based on multiplier by location
//document.getElementById("1").innerHTML = "TEST";
/*
document.getElementsByName('item-options')[0].options[0].innerHTML = "Water";
for(i=0; i < lecPrice.length; i++){
if(list.getAttribute('currency') == 'CAD'){
document.getElementsByName('item-options')[0].options[i].innerHTML = i+1 + " lecture(s): $" + lecPrice[i];
}
else if(list.getAttribute('currency') == 'USD'){
document.getElementsByName('item-options')[0].options[i].innerHTML = i+1 + " lecture(s): $" + lecPriceUSD[i];
}
//lecPrice[i] = Math.round(lecPrice[i] * list.getAttribute('rate'));
//lecSymbol = list.getAttribute('symbol');
//$('select option:contains("Newest")').text('TEMPEST');
//$('select[0] option[' + i + ']').text('Pog');
//select[0].options[i] = new Option((i + 1) + " Lecture: " + lecSymbol + " " + lecPrice[i] );
}
console.log(lecPrice);
}
//Running the above functions
getIp();
setTimeout(function() {
readXML();
}, 200);
}
*/
/*
If the following api stops working below is a stack overflow page containing a list of possible other apis with pros/cons
https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript
**Note when accessing information from api (example: currency) the codes may differ with a new api and you will have to change the declaration lines:
code = data.country;
currency = data.currency;
In this example the .country and .currency will need to be changed to their corresponding titles in the api being used
The same goes for the xml file containing the exchange rates, if that website ever goes down and new rates are needed
https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html
*/