2021-08-27 07:57:20 +02:00
function regenerateQR ( ) {
let username _field = document . getElementById ( 'tabtopup-username' ) ;
let amount _field = document . getElementById ( 'tabtopup-amount' ) ;
let qr _container = document . getElementById ( "tabtopup-qrcode" ) ;
let amountUnparsed = amount _field . value ;
let username = username _field . value ;
username _field . classList . remove ( 'is-danger' ) ;
amount _field . classList . remove ( 'is-danger' ) ;
qr _container . textContent = '' ;
let message _container = document . getElementById ( 'tabtopup-message' ) ;
if ( username == '' ) {
2021-08-27 08:25:01 +02:00
message _container . textContent = 'Please fill in username.' ;
2021-08-27 07:57:20 +02:00
username _field . classList . add ( 'is-danger' ) ;
return ;
}
else if ( ! ( /^[a-z0-9A-Z\-_.]+$/ . test ( username ) ) ) {
message _container . textContent = 'Are you sure that ' + username + ' is your username? Most usernames only contain letters and numbers. If you\'re not sure what your username is, feel free to contact the board.' ;
username _field . classList . add ( 'is-danger' ) ;
return ;
} else if ( amountUnparsed == '' ) {
2021-08-27 08:25:01 +02:00
message _container . textContent = 'Please fill in amount.' ;
2021-08-27 07:57:20 +02:00
amount _field . classList . add ( 'is-danger' ) ;
return ;
} else {
message _container . textContent = '' ;
}
let amount = parseFloat ( document . getElementById ( 'tabtopup-amount' ) . value . replace ( ',' , '.' ) ) ;
2021-08-27 08:25:01 +02:00
message _container . textContent = username + ' will top up for ' + amount . toFixed ( 2 ) + ' euro. It might take a couple of days before this arrives on your Tab.' ;
2021-08-27 07:57:20 +02:00
let message = 'TAB ' + username ;
if ( message . length >= 80 ) {
message _container . textContent = 'Username too long.' ;
username _field . classList . add ( 'is-danger' ) ;
return ;
}
if ( amount <= 0 ) {
2021-08-27 08:25:01 +02:00
message _container . textContent = 'Amount should be at least 0.01 euro (but preferably more of course).' ;
2021-08-27 07:57:20 +02:00
amount _field . classList . add ( 'is-danger' ) ;
return ;
}
let qr _data = generateQrCode ( {
name : 'Zeus WPI' ,
2021-08-27 08:18:39 +02:00
iban : document . getElementById ( 'banknumber' ) . textContent ,
2021-08-27 07:57:20 +02:00
amount : amount ,
unstructuredReference : message ,
information : 'Zeus WPI Tab' ,
} ) ;
new QRCode ( qr _container , {
text : qr _data ,
width : 256 ,
height : 256 ,
correctLevel : QRCode . CorrectLevel . M
} ) ;
return ;
}
window . onload = function ( ) {
document . getElementById ( 'tab-qr-button' ) . addEventListener ( 'click' , function ( event ) {
event . preventDefault ( ) ;
let modal = document . querySelector ( '.modal' ) ;
let html = document . querySelector ( 'html' ) ;
modal . classList . add ( 'is-active' ) ;
html . classList . add ( 'is-clipped' ) ;
modal . querySelector ( '.modal-background' ) . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( ) ;
modal . classList . remove ( 'is-active' ) ;
html . classList . remove ( 'is-clipped' ) ;
} ) ;
} ) ;
document . getElementById ( 'tabtopup-username' ) . addEventListener ( 'input' , regenerateQR ) ;
document . getElementById ( 'tabtopup-amount' ) . addEventListener ( 'input' , regenerateQR ) ;
regenerateQR ( ) ;
}