// Initialize Javascript for the schedule page
function init_schedule() {
    switch_tab_schedule(-1);
}


// Initialize Javascript for the faculty/staff directory page
function init_employees() {
    switch_tab("employees", 0);
}


// Initialize Javascript for the campus directory page
function init_campus() {
    switch_tab("rooms", 0);
}


// Initialize Javascript for the course page
function init_course() {
    switch_tab("classes", -1);
}


// Initialize Javascript for the employee page
// --DefaultTab: default tab
function init_employee(DefaultTab) {
    switch_tab("classes", DefaultTab);
}


// Initialize Javascript for the room information page
// --DefaultTab: default tab for minitabs
function init_room(DefaultTab) {
    switch_tab("classes", 0);
}


// Initialize Javascript for the floor information page
function init_floor() {
    switch_tab("rooms", 0);
}


// Initialize Javascript for the section page
// --DefaultTab: default tab
function init_section(DefaultTab) {

    // Initialize the newest minitab
    switch_tab("classes", DefaultTab);

    // Sort the textbook table by the default column
    sort_table("table_textbooks", 0);
}


// Initialize Javascript for the textbook page
function init_book() {
    switch_tab("classes", -1);
}


// Switch the tabs on the schedule page
// --TabName: tab to switch to
function switch_tab_schedule(TabName) {

    // Call the regular switch_tab() function
    switch_tab("classes", TabName, true, true);

    // Get the parent of the tab
    TabParent = document.getElementById("tabs_classes").parentNode;

    // Construct an array to hold the tabs as they are found
    TabSet = new Array ();

    // Step through each child of the parent node, finding the tabs and adding them to the list
    for (TabIndex in TabParent.childNodes) {
        if (TabParent.childNodes[TabIndex].id &&
          (TabParent.childNodes[TabIndex].id.substr(0, 8) == "div_tab_") &&
          (TabParent.childNodes[TabIndex].style.display != "none")) {

            // Process the PDF link if it is present
            if (PDF = document.getElementById("tool_pdf")) {

                // Remove the TermCode setting from the PDF link if one is present
                PDF.href = PDF.href.replace(/&TermCode=.*/, "");

                // Insert the chosen TermCode setting into the PDF link
                PDF.href += "&TermCode=" + TabParent.childNodes[TabIndex].id.replace("div_tab_classes_", "");
            }
        }
    }

    return false;
}


// Initialize Javascript for the administration page
// --DefaultTab: tab to set as the default
function init_admin(DefaultTab) {
    switch_tab_admin(DefaultTab);
}


// Initialize Javascript for the edit_employee_details page
function init_edit_employee_details() {

    // Hide or show the conditional fields
    hide_show_conditional_employee_details();
}


// Initialize Javascript for the edit_office_hours page
function init_edit_office_hours() {

    // Hide or show the conditional fields
    hide_show_conditional_office_hours();
}


// Initialize Javascript for the edit_employee_work page
function init_edit_employee_work() {

    // Hide or show the conditional fields
    hide_show_conditional_employee_work();
}


// Confirm deletion of an item
// --itemtype: the type of item to delete
function confirm_delete(itemtype) {
    return confirm ("Are you sure you wish to delete this " + itemtype + "?");
}


// Hide/show conditionals on the edit_employee_details page
function hide_show_conditional_employee_details() {

   // Decide whether to show problem reporting textarea
   if (document.getElementById("ReportProblem1").checked) {
      document.getElementById("field_Problem").style.display = "";
   } else {
      document.getElementById("field_Problem").style.display = "none";
   }

   // Decide whether to show the file upload control
   if (!document.getElementById("field_ChangeCV") || document.getElementById("ChangeCV2").checked) {
      document.getElementById("field_CV").style.display = "";
   } else {
      document.getElementById("field_CV").style.display = "none";
   }
}


// Hide/show conditionals on the edit_office_hours page
function hide_show_conditional_office_hours() {

    // Decide whether to show the SpecificClass field
    if (document.getElementById("Category1").checked) {
        document.getElementById("field_SpecificClass").style.display = "";
    } else {
        document.getElementById("field_SpecificClass").style.display = "none";
    }

    // Decide whether to show the Room field
    if (document.getElementById("Location0").checked) {
        document.getElementById("field_Room").style.display = "";
    } else {
        document.getElementById("field_Room").style.display = "none";
    }
}


// Hide/show conditionals on the edit_employee_work page
function hide_show_conditional_employee_work() {

    // Decide whether to show the EndYear field
    if (document.getElementById("StartYear").value == "0") {
        document.getElementById("field_EndYear").style.display = "none";
    } else {
        document.getElementById("field_EndYear").style.display = "";
    }
}


// Switch the tabs on the administration page
// --TabName: tab to switch to
function switch_tab_admin(TabName) {

    // Call the regular switch_tab() function
    switch_tab("admin", TabName);

    // Hide all the toolbar items for this page
    document.getElementById("tool_add_user").style.display = "none";

    // Show the appropriate toolbar item if one is present
    if (document.getElementById("tool_add_" + TabName)) {
        document.getElementById("tool_add_" + TabName).style.display = "inline";
    }

    return false;
}


// Validate the edit_employee_details page
function validate_employee_details() {

    // Set the message array to hold unanswered items
    var missing = [];

    // Problem reporting
    if (document.getElementById("ReportProblem1").checked && !document.getElementById("Problem").value) {
        missing.push("\u2022 " + "Please describe which piece of data you believe is incorrect");
        document.getElementById("Problem").style.backgroundColor="#FFDDDD";
    } else {
        document.getElementById("Problem").style.backgroundColor="#FFFFFF";
    }

    // CV Upload
    if (document.getElementById("ChangeCV2") && document.getElementById("ChangeCV2").checked && !document.getElementById("CV").value) {
        missing.push("\u2022 " + "Please Select a PDF File to Upload for your CV");
    }   

    // Define a variable to hold the alert message
    var alert_message = "";

    // If missing fields were found, add them to the alert message
    if (missing.length > 0) {
        alert_message += "You must answer the following items:\r\n" + missing.join("\r\n");
    }

    // If an alert message is present, display it and return false
    if (alert_message) {
        alert(alert_message);
        return false;
    }

    return true;
}


// Validate the office hours page
function validate_office_hours() {

    // Set the message array to hold unanswered items
    var message = [];

    // Check SpecificClass field
    if (document.getElementById("Category1").checked && !document.getElementById("SpecificClass").value.replace(/^\s*|\s*$/g, "")) {
        message.push("\u2022 " + "You must enter a specific course or section.");
        document.getElementById("SpecificClass").style.backgroundColor="#FFDDDD";
    } else {
        document.getElementById("SpecificClass").style.backgroundColor="#FFFFFF";
    }

    // Check Room field
    if (document.getElementById("Location0").checked && !document.getElementById("Room").value.replace(/^\s*|\s*$/g, "")) {
        message.push("\u2022 " + "You must enter a room number.");
        document.getElementById("Room").style.backgroundColor="#FFDDDD";
    } else {
        document.getElementById("Room").style.backgroundColor="#FFFFFF";
    }

    // Do not allow General with ANGEL Live Office Hours
    if (document.getElementById("Category0").checked && document.getElementById("Location1").checked) {
        message.push("\u2022 " + "ANGEL Live Office Hours must be associated with a specific course.");
    }

    // Check day
    if (!document.getElementById("Sunday").checked && !document.getElementById("Monday").checked
      && !document.getElementById("Tuesday").checked && !document.getElementById("Wednesday").checked
      && !document.getElementById("Thursday").checked && !document.getElementById("Friday").checked
      && !document.getElementById("Saturday").checked ) {
        message.push("\u2022 " + "You must choose at least one day of the week.");
    }

    // Check times
    if ((document.getElementById("StartHour").value * 60 + document.getElementById("StartMinute").value * 1
      - document.getElementById("EndHour").value * 60 - document.getElementById("EndMinute").value * 1) >= 0) {
        message.push("\u2022 " + "The end time must be later than the start time.");
    }

    // If any message are set to be displayed, do so and return false
    if (message.length > 0) {
        alert(message.join("\r\n") + "\r\n");
        return false;
    }

    return true;
}


// Make sure all required fields and one-of fields are filled out
// --type: type of form to validate
function validate(type) {

    // Define required parameters for user submission
    if (type == "edit_user") {
        var required = {
          "all" : ["Name", "Username"]
        };
    }

    // Define required parameters for education submission
    else if (type == "edit_employee_education") {
        var required = {
          "all" : ["Degree", "School", "Year"]
        };
    }

    // Define required parameters for employment submission
    else if (type == "edit_employee_work") {

        // Set the end year to 0 if the start year was unspecified
        if (document.getElementById("StartYear").value == "0") {
            document.getElementById("EndYear").value = "0";
        }

        var required = {
          "all" : ["Employer", "Title", "StartYear", "EndYear"]
        };
    }

    // Define required parameters for certification/award submission
    else if (type == "edit_employee_certification") {
        var required = {
          "all" : ["Certification", "Year"]
        };
    }

    // Define required parameters for link submission
    else if (type == "edit_employee_link") {
        var required = {
          "all" : ["Label", "Href"]
        };
    }

    // Define required parameters for biography submission
    else if (type == "edit_employee_biography") {
        var required = {};
    }

    // Define required parameters for detail submission
    else if (type == "edit_employee_details") {
        var required = {
          "oneof" : ["HidePicture0", "HidePicture1"]
        };
    }

    // Otherwise, don't let the submission go
    else {
        return false;
    }

    // Start with an empty alert message
    var alert_message = "";

    // Process "only one required" fields
    if (required["oneof"]) {

        // Assume no fields are present
        var present = false;

        // Set up a parameter label list
        var parameter_labels = [];

        // Step through the parameters
        for (index in required["oneof"]) {

            // Add to the parameter label list
            parameter_labels.push("\u2022 " + document.getElementById("label_" + required["oneof"][index]).firstChild.nodeValue);

            // If this parameter is specified, then mark it as such
            if (document.getElementById(required["oneof"][index]).value.replace(/^\s*|\s*$/g, "")) {
                present = true;
            }
        }

        // If nothing was specified, then alert the user
        if (!present) {
            alert_message += "You must enter at least one of the following:\r\n" + parameter_labels.join("\r\n") + "\r\n";
        }
    }

    // Process "all required" fields
    if (required["all"]) {

        // Set an array to hold the missing fields
        var missing = [];

        // Step through the parameters
        for (index in required["all"]) {

            // Check to make sure parameter is specified
            if (!(document.getElementById(required["all"][index]).value.replace(/^\s*|\s*$/g, ""))) {

                // Make note of the missing parameter
                missing.push("\u2022 " + document.getElementById("label_" + required["all"][index]).firstChild.nodeValue);

                // Change its background color to pink
                document.getElementById(required["all"][index]).style.backgroundColor="#FFDDDD";
            }

            else {

                // Change its background color to white
                document.getElementById(required["all"][index]).style.backgroundColor="#FFFFFF";
            }
        }

        // If missing fields were found, add them to the alert message
        if (missing.length > 0) {
            alert_message += "You must answer the following items:\r\n" + missing.join("\r\n") + "\r\n\r\n";
        }
    }

    // Date check for the work page
    if (type == "edit_employee_work") {

        // If an end date is specified, then it must be later than the start date
        if ((document.getElementById("EndYear").value > 0 )
          && (document.getElementById("EndYear").value < document.getElementById("StartYear").value)) {
            alert_message += "The End Year cannot be earlier than the Start Year.\r\n";
        }
    }

    // If an alert message is present, display it and return false
    if (alert_message) {
        alert(alert_message);
        return false;
    }

    return true;
}

