function Search(fields) {
    function updateCount(count){$$('.nbrFonds').each(function(i){i.update(count);});showResetLink()};
    function showResetLink(){if(!$('resetLink').visible())$('resetLink').show();}
    function updateAmountsStyle(filter) {
        $('aggregator').getInputs('checkbox', filter).each(function(s){
            if(s.checked) {$(s.id+"_nbr").setStyle({color:"#000"});}
            else {$(s.id+"_nbr").setStyle({color:"#aaa"});}
        });
    }

    this.reset = function(filter) {
        new Ajax.Request(Search.serviceUrl, {
            method:'post',
            postBody:Search.AWDW+'=1&'+Search.DW+'[]='+filter,
            onCreate: function(){$('frstGoLnk').addClassName('loading');},
            onSuccess: function(){$('frstGoLnk').removeClassName('loading');},
            onComplete: function(t) {
                fields.each(function(criteria) {
                    Search.prototype.updateCriteria(criteria);
                });
                updateCount(t.responseText);
            }
        });
    };

    this.filter = function(filter) {
        var addWhere = "";
        $('aggregator').getInputs('checkbox', filter).each(function(i){
            if(i.checked) addWhere += Search.AW+'['+filter+'][]=' + escape(i.value) + '&';
        });
        var postBody = Search.AWDW+'=1&'+Search.DW+'[]=' + filter + '&' + addWhere;
        new Ajax.Request(Search.serviceUrl, {
            method:'post',
            postBody:postBody,
            onCreate: function(){$('frstGoLnk').addClassName('loading');},
            onSuccess: function(){$('frstGoLnk').removeClassName('loading');},
            onComplete: function(t) {
                updateAmountsStyle(filter);
                fields.each(function(criteria) {
                    if(filter != criteria) Search.prototype.updateCriteria(criteria);
                    else {if(!$('reset_'+filter).visible()) $('reset_'+filter).show();}
                });
                updateCount(t.responseText);
            }
        });
    };

    this.easyResetAll = function(){
        new Ajax.Request(Search.serviceUrl, {
            method:'post',
            postBody:'es=1&'+Search.AWDW+'=1&'+Search.DW+'=',
            onComplete: function(t) {
                fields.each(function(criteria){
                    Search.prototype.easyUpdateCriteria(criteria);
                });
            }
        });
    };

    this.easyReset = function(filter){
        new Ajax.Request(Search.serviceUrl, {
            method:'post',
            postBody:'es=1&'+Search.AWDW+'=1&'+Search.DW+'[]='+filter,
            onComplete: function(t) {
                fields.each(function(criteria){
                    Search.prototype.easyUpdateCriteria(criteria);
                });
            }
        });
    };

    this.easyFilter = function(filter, addWhere) {
        if(addWhere == -2) return;
        else if(addWhere == -1) this.easyReset(filter);
        else {
            var postBody = 'es=1&'+Search.AWDW+'=1&'+Search.DW+'[]=' + filter + '&' + Search.AW+'['+filter+'][]=' + escape(addWhere);
            new Ajax.Request(Search.serviceUrl, {
                method:'post',
                postBody:postBody,
                onCreate: function(){$('select_'+filter).addClassName('loading');},
                onSuccess: function(){$('select_'+filter).removeClassName('loading');},
                onComplete: function(t) {
                    fields.each(function(criteria){
                        if(criteria != filter) {Search.prototype.easyUpdateCriteria(criteria)};
                    });
                }
            });
        }
    };

}

Search.serviceUrl = '/services/fonds/grpby.php';
Search.AWDW = "awdw";
Search.DW = "dw";
Search.AW = "aw";
Search.prototype.updateCriteria = function(criteria) {
    new Ajax.Updater('box_'+criteria, Search.serviceUrl, {
        method:'post',
        parameters:{f: criteria},
        onLoading: function(){$('box_'+criteria).addClassName('loading');},
        onSuccess: function(){$('box_'+criteria).removeClassName('loading');}
    });
};
Search.prototype.easyUpdateCriteria = function(criteria) {
    new Ajax.Updater('select_'+criteria, Search.serviceUrl, {
        method:'post',
        parameters:{f: criteria, es: 1},
        onLoading: function(){$('select_'+criteria).addClassName('loading');},
        onSuccess: function(){$('select_'+criteria).removeClassName('loading');}
    });
};