function dbrl_list(listArr, connection) {
    function setListOptions(i, list, select) {
        var e = $('[id="' + listArr[i].id + '"]'),
            opt = '<option>' + empty_option[i] + '</option>';
        $.each(list, function () {
            opt += '<option' + ((this.value) ? ' value="' + this.value + '"' : '') + ((this.filter) ? ' data="' + this.filter + '"' : '') + '>' + this.text + '</option>';
        });
        e.html(opt);
        if (select) {
            $('option[value="' + select + '"]', e).attr('selected', true);
            e.change();
        }
    };

    function getContent(i, filter) {
        var e = $('[id="' + listArr[i].id + '"]');
        e.html('<option>' + loading_text + '</option>');
        $.getJSON(connection + '?get=' + listArr[i].id + ((filter) ? '&id=' + filter : '') + ((listArr[i].additional) ? '&additional=' + listArr[i].additional : ''), function (json) {
            if (json.results) {
                setListOptions(i, json.results, listArr[i].default_value);
                listArr[i].default_value = null;
            } else if (json.error) {
                alert(json.error.message);
            }
            for (j = i + 1, h = listArr.length; j < h; j++) {
                setListOptions(j, []);
            }
        });
    };
    $.each(listArr, function (i, data) {
        var e = $('[id="' + data['id'] + '"]');
        if (e.length) {
            setListOptions(i, []);
            if (i == 0) {
                getContent(i);
            }
            if (i != listArr.length - 1) e.change(function (evt) {
                if (this.selectedIndex) {
                    var s = $(this.options[this.selectedIndex]);
                    var selected = (s.attr('data')) ? s.attr('data') : s.attr('value');
                    getContent(i + 1, selected)
                } else {
                    for (j = i + 1, h = listArr.length; j < h; j++) {
                        setListOptions(j, []);
                    }
                }
            });
        }
    });
}
