var delegates = 2;

function addDelegate() {
    if (delegates < 9) {
        delegates++;

        document.getElementById("DelegateBox" + delegates).style.display = "block";
    }
}

function removeDelegate() {
    if (delegates > 1) {
        document.getElementById("DelegateBox" + delegates).style.display = "none";

        delegates--;
    }
}

// This function is used to hide all the rows in the events table, presumably
// as part of a larger operation to show just one row
function hideAllEvents() {
    // Make sure that the event table exists - if not, this function should
    // never have been called, so do nothing
    if (document.getElementById("eventTable")) {
        // Cycle through each table row that is in the event table
        rows = document.getElementById("eventTable").getElementsByTagName("tr");
        for (i = 0; i < rows.length; i++) {
            rows[i].style.display = "none";
        }
    }
}

// This function is called after the user selects one event from the list in
// order to register for it
function eventSelect(eventName) {
    // Make sure that the event table exists, AND the parameter passed into this
    // function refers to a valid element - if not, this function should
    // never have been called, so do nothing
    if ( (document.getElementById("eventTable")) && (document.getElementById(eventName)) ) {
        // Start by hiding ALL rows, regardless
        hideAllEvents();

        // Show the single row that we want to see
        document.getElementById(eventName).style.display = "block";

        // Show the rest of the form now
        document.getElementById("Delegates").style.display = "block";
        document.getElementById("contactDetails").style.display = "block";
        document.getElementById("eventCancel").style.display = "block";

        // Fill in the hidden input with the name of the chosen event, ready for when
        // the form is submitted
        document.getElementById("ChosenEvent").value = eventName;

        // we need to hide the 'tour' option for Brands Hatch and Celtic Manor - this is the easiest place to add it
       	document.getElementById("reqTour").style.display = (eventName == 'b4882ef3-e9e2-4f7b-8a34-9725933a3e8e' || eventName == '8ef05137-12af-4342-ae6d-86104bff84dd' || eventName == 'b5d7702f-46ee-4206-925e-9bf01d3ce36d' || eventName == '1365c028-bc4a-4a1f-916c-49f8938e84da') ? "none" : "";
    }
}

// Called when a user cancels the event they have selected, in order to return
// to the events list and select a new one
function eventCancel() {
    // Make sure that the event table exists - if not, this function should
    // never have been called, so do nothing
    if (document.getElementById("eventTable")) {
        // Cycle through each table row in the event table and turn its display on
        rows = document.getElementById("eventTable").getElementsByTagName("tr");
        for (i = 0; i < rows.length; i++) {
            rows[i].style.display = ""; // Set to blank to avoid table row reappearing mangled
        }

        // Turn off the rest of the form for now
        document.getElementById("delegates").style.display = "none";
        document.getElementById("contactDetails").style.display = "none";
        document.getElementById("eventCancel").style.display = "none";
    }
}