function toggleChildren(children, input)
{
    var districts = document.getElementById(children + '_' + input.value);

    if (districts) {
        var aDistrict = $('#' + children + '_' + input.value + ' input');

        if (input.checked) {
            var checked   = false;

            aDistrict.each(function () { this.disabled = false;   });
            aDistrict.each(function () { checked |= this.checked; });

            if (!checked) { aDistrict.each(function () { this.checked = true; }); }

            districts.style.display = 'block';
        } else {
            districts.style.display = 'none';

            aDistrict.each(function () { this.disabled = true; });
        }
    }
}

function toggleParent(parent, level, input, idParent)
{
    var aDistrict = $('#' + level + '_' + idParent + ' input');
    var checked   = false;

    aDistrict.each(function () { checked |= this.checked; });

    if (!checked) {
        var canton    = document.getElementById(parent + '_' +  idParent);
        var districts = document.getElementById(level  + '_' +  idParent);

        districts.style.display = 'none';
        canton.checked          = false;

        aDistrict.each(function () { this.disabled = true; });
    }
}

function changeBounds(base, indice, unique)
{
    var aBound;

    var from    = '';
    var to      = '';
    var current = document.getElementById(base + indice );

    if (unique) {
        $('.' + base).each(function() { if (this.id != current.id) { this.checked = false; } });

        if (current.checked) {
            aBound = current.value.split('|');
            from   = aBound[0];
            to     = aBound[1];
        }
    } else {
        var lower;
        var upper;

        var state   = 0;
        var toCheck = new Array();

        $('.' + base).each(function() {
            if (this.checked) {
                if (state == 0) {
                    state = 1;
                    lower = this;
                    upper = this;
                } else if (state == 1) {
                    upper = this;
                } else if (state == 2 || state == 3) {
                    state = 3;
                    upper = this;
                } else {
                    this.checked = false;
                }
            } else if (state == 1 || state == 2) {
                if (this.id == current.id) {
                    state = 4;
                } else {
                    state = 2;
                    toCheck.push(this);
                }
            } else if (state == 3){
                state = 4;
            }
        });

        if (state > 2) { for (var id in toCheck) { toCheck[id].checked = true; } }

        if (state > 0) {
            aBound = lower.value.split('|');
            from   = aBound[0];

            aBound = upper.value.split('|');
            to     = aBound[1];
        }
    }
    document.getElementById(base + '_from').value = from;
    document.getElementById(base + '_to'  ).value = to;
}

$('#filters .filter_canton').each(function () { this.onclick(); });
