$(document).ready( function() {
init()
alert("back from init().");
}); // end doc.ready

/*
** def fn init()
** ^^^^^^^^^^^^^
**
** set the event listener on the registration form; on submit,
** build an array of objects from the form’s
** <select> fields; ea. obj is 2 name:value pairs
** {‘index’: n, ‘room’: x};
** index determines timeslot, zero relative;
** call fn updateTable to update the AWS table
** FHCRoomCounts
*/
function init() {
$('form').submit(function(e) {
var a = Array.from($('select'))
.map((select, i) => ({
'index': i,
'room': select.value
}))
.filter(select => select.room !== 'unsure' && select.room !== 'not attend');
if( !a || a.length == 0 ) {alert('a is null or undefined, or a.length == 0');}
else {
if(a[0].room == 'attend') {a[0].room = 'chapel';}
updateTable(a);
} // end else
}); // end def anonymous fn & submit
} // end def fn init

/*
** def fn updateTable(classes)
** ^^^^^^^^^^^^^^^^^^^^^^^^^^^
**
** var classes == [{idx: n, rm: x}, ...] where
** n == the index position in timeslots[] of the
** beginning time of the class & x == room letter
** where the class will meet.
**
** using DynamoDB syntax, update the DynamoDB table
** FHCRoomCounts on Amazon Web Services servers for
** that timeslot & room by one
*/

function updateTable(classes) {
var timeslots = ['0830', '0900', '1030', '1245', '1415', '1545'];
var params = {};
var ts = '';
var rm = '';
var dynamoDB = new AWS.DynamoDB();
var err = null;

for(var i = 0; i < classes.length; i++) {
ts = timeslots[classes[i].index];
rm = classes[i].room;
console.log('ts, rm : ', ts, rm);
params = {
'TableName': 'FHCRoomCounts',
'Key': {
'room': {
'S' : rm
},
'timeSlot': {
'S': ts
}
},
'ExpressionAttributeNames': {
'#registrants': 'registrants'
},
'ExpressionAttributeValues': {
':n': { 'N': '1'}
},
'UpdateExpression': 'ADD #registrants :n',
'ReturnValues': 'ALL_NEW'
}; // end params
dynamoDB.updateItem(params, function(err, data) {
if (err) {
console.log('Unable to update registrant: ' + '\n' + JSON.stringify(err, undefined, 2));
} else {
console.log('Increase registrant succeeded: ' + '\n' + JSON.stringify(data, undefined, 2));
} // end else
}); // end def anonymous fn & def updateItem stateme
} // end for
} // end def fn updateTable