// Updaten yleismetodi

var indent = "\xa0\xa0\xa0\xa0";
var empty_text = "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0";

// Päivittää selectin
function update(parent, child, childData, update_func) {
    // Save existing selection to array.
    var sChildren = new Array;
    
    for(var i = 0 ; i < child.options.length ; i++) {
        if(child.options[i].selected && child.options[i].value != "") sChildren[sChildren.length] = child.options[i].value;
    }
    // Clear children
    child.length = 0;
    
    // Update child box.
    var current = parent.selectedIndex;
    if(current >= 0) {
        for(var i = 0 ; i < parent.options.length ; i++) {
            if(parent.options[i].selected) {
                var id = parent.options[i].value;
                var data = childData[id];
                if(parent.options[i].value != "") {
                    var text = parent.options[i].text;
                    child.options[child.length] = new Option(text.substr(text.lastIndexOf("\xa0")+1, text.length), "", false, false);
                }
                if(data != null) {
                    for(var j = 0 ; j < data.length ; j++) {
                        var opt = new Option(indent + data[j][1], data[j][0], false, false);
                        child.options[child.length] = opt;
                    }
                }
            }
        }
    } else {
    	child.options[child.length] = new Option(empty_text, "", false, false);
    }

    // Restore selection.
    for(var i = 0 ; i < sChildren.length ; i++) {
        var j = 0;
        while(j < child.options.length) {
            if(child.options[j].value == sChildren[i]) {
                child.options[j].selected = true;
                break;
            }
            j++;
        }
    }
    // Recurse update to lower levels.
    if(update_func != null) update_func();
}


