var people;
var events;
var selectedEvent;
// Work-around for missing JQuery.postJSON
jQuery["postJSON"] = function(url,data,callback) {
$.ajax({
url:url,
type:'POST',
data:JSON.stringify(data),
contentType:'application/json',
dataType:'json',
success: callback
});
};
// Helper function to map event ids to event objects
function mapIDtoEvent(id) {
var n;
for(n = 0;n < events.length;n++) {
if(events[n].id == id)
return events[n];
}
return null;
}
// Callback function for the GET that fetches a list of people for an event
function receiveAttendees(people) {
var n;
for(n = 0;n < people.length;n++) {
// For each person in the list of people
// create an option in the select for people
var newItem = $('
');
newItem.text(people[n].firstname + " " + people[n].lastname);
$('#attendees').append(newItem);
}
}
// Callback function for the POST that posted a new registration
function confirm() {
$("#main").hide();
$('#confirm').show();
// Go back to the server and fetch the list
// of people for the event
$.getJSON("https://cmsc106.net/Registration/events/" +$('#events').val()+ "/people",receiveAttendees);
}
// This function is linked to the register button
function registerForEvent() {
var registration = {person: $("#people").val(), event: $('#events').val()};
$.postJSON("http://cmsc106.net/Registration/registrations",registration,confirm);
}
// This function is linked to the 'View Event' button
function showDetails() {
$('#start').hide();
$('#main').show();
var eventId = $('#events').val();
selectedEvent = mapIDtoEvent(eventId);
$('#eventName').text(selectedEvent.title);
$('#date').text(selectedEvent.day);
$('#description').text(selectedEvent.description);
}
// Callback function for the GET that fetches a list of people
function receivePeople(data) {
people = data;
var n;
for(n = 0;n < people.length;n++) {
// For each person in the list of people
// create an option in the select for people
var newOption = $('