window.ondom(function() {
    document.getElementById('project_select_all').onclick = function() {
        var inputs = document.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == 'checkbox' && inputs[i].id.match(/^project\[\d+\]$/)) {
                inputs[i].checked = this.checked;
            }
        }
    }
    document.getElementById('dest_select_all').onclick = function() {
        var inputs = document.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == 'checkbox' && inputs[i].id.match(/^destination\[\d+\]$/)) {
                inputs[i].checked = this.checked;
            }
        }
    }

    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'checkbox') {
            if (inputs[i].id.match(/^project\[\d+\]$/)) {
                inputs[i].onclick = function() {
                    verifyCheckboxes('project');
                }
            } else if (inputs[i].id.match(/^destination\[\d+\]$/)) {
                inputs[i].onclick = function() {
                    verifyCheckboxes('destination');
                }
            }
        }
    }
});

function verifyCheckboxes(type) {
    var all_checked = true;
    var regex = new RegExp('^' + type + '\\[\\d+\\]$');
    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'checkbox' && inputs[i].id.match(regex) && !inputs[i].checked) {
            all_checked = false;
            break;
        }
    }
    if (type == 'project') {
        var check_all = 'project_select_all';
    } else if (type == 'destination') {
        var check_all = 'dest_select_all';
    }
    document.getElementById(check_all).checked = all_checked;
}

function doSubmit(a) {
	
	a.submit();
}

