// Javascript from Moodle modules
/**
 * This function checks that the selected action is valid, via checking that the
 * selected action is valid and that there is at least one checked course
 * (eg: checkchecked())
 *
 * @param object    form        Document form object
 * @return boolean
 **/
function course_management_checksubmit(form) {
    var destination = form.courseaction.options[form.courseaction.selectedIndex].value;
    if (destination == "" || !course_management_checkchecked(form)) {
        form.courseaction.selectedIndex = 0;
        return false;
    } else {
        // Modifies the forms action value to submit to selected page
        return course_management_form_action(form, destination);
    }
}

/**
 * This function checks all inputs on the form that are of type checkbox to
 * ensure that at least one checkbox is selected
 *
 * @param object    form    Document form object
 * @return boolean
 **/
function course_management_checkchecked(form) {
    var inputs = document.getElementsByTagName("INPUT");
    var checked = false;
    inputs = filterByParent(inputs, function() {return form;});
    for(var i = 0; i < inputs.length; ++i) {
        if (inputs[i].type == "checkbox" && inputs[i].checked) {
            checked = true;
            // Only needs to check for one checked value
            break;
        }
    }
    return checked;
}

/**
 * This function modifies the form's action to passed destination url and
 * optionally submits.
 *
 * @param object    form        Document form object
 * @param string    destination URL of destination
 * @param boolean   submit      Submits form if set to true...returns bool otherwise
 **/
function course_management_form_action(form, destination, submit) {
    form.action = destination;
    if (submit) {
        form.submit();
    } else {
        return true;
    }
}
