// $Id: rebuild_selectboxes.js

function func_rebuild_selectbox(availObj, avail) {
    min_avail = 0;
    if (availObj && availObj.tagName.toUpperCase() == 'SELECT') {
        var cnt = availObj.options.length;
        if (cnt > avail) {
            for (var x = (avail < 0 ? 0 : avail); x < cnt; x++)
                availObj.options[availObj.options.length-1] = null;

        } else {
            for (var x = cnt; x < avail; x++)
                availObj.options[cnt++] = new Option(x, x);
        }

    }
}


function func_rebuild_selectboxes(optionid, classid) {
    if (!container_options) return false;
    //    if (!container_options[classid]) return false;

    selected_cells = 0

        if (classid != 0) {

            for (i in container_options[classid]) {
                if (document.getElementById("selectbox_"+container_options[classid][i])) {
                    selectboxObj = document.getElementById("selectbox_"+container_options[classid][i]);
                    if (selectboxObj.value != "")
                        selected_cells = parseInt(selected_cells) + parseInt(selectboxObj.value); 

                }
            }


            for (i in container_options[classid]) {
                if (document.getElementById("selectbox_"+container_options[classid][i])) {
                    selectboxObj = document.getElementById("selectbox_"+container_options[classid][i]);
                    if (selectboxObj.value == "")
                        new_max= parseInt(container_size)-parseInt(selected_cells) + 1;
                    else 
                        new_max =  parseInt(container_size)-parseInt(selected_cells)+parseInt(selectboxObj.value)+1;
                    func_rebuild_selectbox(selectboxObj, new_max);

                }

            }
        } else {
            for (classid in container_options) {
                for (i in container_options[classid]) {
                    if (document.getElementById("selectbox_"+container_options[classid][i])) {
                        selectboxObj = document.getElementById("selectbox_"+container_options[classid][i]);
                        if (selectboxObj.value)
                            selected_cells = parseInt(selected_cells) + parseInt(selectboxObj.value); 

                    }
                }
            }

            for (classid in container_options) {
                for (i in container_options[classid]) {
                    if (document.getElementById("selectbox_"+container_options[classid][i])) {
                        selectboxObj = document.getElementById("selectbox_"+container_options[classid][i]);
                        if (selectboxObj.value == "")
                            new_max= parseInt(container_size)-parseInt(selected_cells) + 1;
                        else 
                            new_max =  parseInt(container_size)-parseInt(selected_cells)+parseInt(selectboxObj.value)+1;

                        func_rebuild_selectbox(selectboxObj, new_max);

                    }

                }

            }
        }
}

function func_check_box() {

    if (!container_options) return true;

    for(classid in container_options) {
        selected_cells = 0;
        for (i in container_options[classid]) {
            selectboxObj = document.getElementById("selectbox_"+container_options[classid][i]);
            if (selectboxObj && selectboxObj.value != "") {
                selected_cells = parseInt(selected_cells) + parseInt(selectboxObj.value);

            }
        }

        if (parseInt(selected_cells) != parseInt(container_size)) {
            alert(error_fill_container);
            return false;
        }
    }

    return true;
}


