// JS functions for the selectListTag
// by Daniel Lichtenberger, UCS

// selectIndex global
var selectedIndexGlobal = null;

function stOnSelectListChanged(event, selectName, listId, selectLists, selectListParents, selectElementParents, isRadioGroup, emptyElement) {
    var elementId = null;
    var editSelectList = false;
    if (!isRadioGroup) {
        // get value from standard select list
        var select = document.getElementById(selectName);
        if(select.value == "null") {
            editSelectList = true;
            select.selectedIndex = selectedIndexGlobal;
        }
        elementId = (select.options.length > 0) ? select.options[select.selectedIndex].value : -1;
    } else {
        // get value from radio button group
        var radios = document.getElementsByName(selectName);
        // find selected radio button
        var select = null;
        for (var i = 0; i < radios.length; i++) {
            var elem = radios[i];
            if (elem.checked) {
                select = elem;
                break;
            }
        }
        elementId = select != null ? select.value : -1;
    }
    if(!editSelectList) {
        var childSelectName = select.getAttribute("childSelectName");
        var childListId = select.getAttribute("childListId");
        if (childSelectName == null || childSelectName.length == 0)
            return;     // no child select list to be updated
        var childSelect = document.getElementById(childSelectName);
        // remove old child options
        while (childSelect.childNodes.length > 0)
            childSelect.removeChild(childSelect.childNodes[0]);
        // insert empty element if requested
        if (emptyElement) {
            var option = document.createElement("option");
            option.setAttribute("value", -1);
            option.innerHTML = "-";
            childSelect.appendChild(option);
        }
        // insert new values
        var list = selectLists[childListId];
        for (var i = 0; i < list.length; i++) {
            var curId = list[i][0];
            //alert(curId + " " + selectElementParents[curId] + " " + elementId);
            if (selectElementParents[curId] == elementId) {
                var option = document.createElement("option");
                option.setAttribute("value", curId);
                option.innerHTML = list[i][1];
                childSelect.appendChild(option);
            }
        }
        try {
            if (isRadioGroup && childSelect) {
                childSelect.focus();        // IE refresh fix
            }
        } catch(e) {
            // Do Nothing  --> If display: none
        }
        // propagate event
        stOnSelectListChanged(event, childSelectName, childListId, selectLists, selectListParents, selectElementParents);
    } else {
        top.top_openEditSelectList(listId, editSelectListTitle);
    }
}

