
var cascadeData = [];

function comboChange(value, table, dep) {
    jsonrpc.nszForm.comboChange(processResults, value, table, dep);
}

function init(table, maxDepth) {
    combos = new Array(maxDepth + 1);
    for (var i = 0; i <= maxDepth; i++) {
        combos[i] = document.getElementById(table+'_'+i);
    }
    data = {
        "combos": combos,
        "maxDepth": maxDepth
    }
    cascadeData[table] = data;
    jsonrpc.nszForm.getComboValues(initProcessing, table);
}

function processResults(values, exception) {
    if (values) {
        combos = cascadeData[values.map.table].combos;
        selectItems = values.map.values;
        maxDepth = cascadeData[values.map.table].maxDepth;
        depth = values.map.depth;
        if (depth < maxDepth) {
            for (var i = depth+1; i <= maxDepth; i++) {
                combos[i].innerHTML='';
                combos[i].style.display='none';
            }
            if (selectItems.length > 1) {
                fillOptions(selectItems, combos[depth+1], 0);
                combos[depth+1].style.display='block';
            }
        }
        
    }
}

function initProcessing(values, exception) {
    if (values) {
        combos = cascadeData[values.map.table].combos;
        var length = values.map.values.list.length;
        for (var i = 0; i < length; i++) {
            fillOptions(values.map.items.list[i], combos[i], values.map.values.list[i]);
            combos[i].style.display='block';
        }
    }
}