var ajax = {
    filter: function(current_id) {
        var selects = getAllFilterFields();

        var current = document.getElementById(current_id);
        var current_value = current.value;

        var data = {};
        for (p in selects) data[p] = selects[p].value;

        data['pid'] = document.getElementById('filter_pid').value;
        data['application_area'] = document.getElementById('application_area').value;
//        data['hydraulic_section'] = document.getElementById('hydraulic_section').value;
/*
        ajaxQuery('filter', data, function(data) {
            if (data.status == 'error') alert('error!');
            else {
                document.getElementById('listData').innerHTML = data.html;
                document.getElementById('specialSpace').innerHTML = '';

                selects = getAllFilterFields();

                for (p in allFilters) {
                    for (key in allFilters[p]) {
                        ajax.common.addOptionIfNotExist(selects[p], key, allFilters[p][key], selects[p].getAttribute('rev'));
                    }
                }

                cuSel_init();

                if (current_value) {
                    var number = current_id.charAt(current_id.length-1);
                    $('#filterContent').find('td').parents('tr').find('TD:eq('+ number +')').addClass('activeColumn');
                }
            }
        });
*/

        data['col'] = current.getAttribute('name');
        data['filter'] = 1;

        var query = [];
        for (key in data) {
            if (data[key] != '') query[query.length] = key + '=' + data[key];
        }
        window.location.href = window.location.href.split(window.location.search).join('') + '?' + query.join('&');
    },

    searchData: function() {
        var selects = getAllSearchFilterFields();

        var data = {};
        for (p in selects) data[p] = selects[p].value;
        data['article'] = document.getElementById('article').value;

        ajaxQuery('searchData', data, function(data) {
            if (data.status == 'error') alert('error!');
            else {
                document.getElementById('catalogFilter').innerHTML = data.html;

                selects = getAllSearchFilterFields();

                for (p in allSearchFields) {
                    for (key in allSearchFields[p]) {
                        ajax.common.addOptionIfNotExist(selects[p], key, allSearchFields[p][key], selects[p].getAttribute('rev'));
                    }
                }

                cuSel_init();
            }
        });
    },

    common: {
        addOptionIfNotExist: function(select, key, val, selected) {
            for (var i = 0; i < select.options.length; i++) {
                if (select.options[i].value == key) return;
            }

            var option = document.createElement("option");
            var test = $(option);

            option.appendChild(document.createTextNode(val));
            option.setAttribute("value", key);

//            option.disabled = 'disabled';
            option.setAttribute("dis", 'dis');
            option.className = "disabledSpan";

            if (selected == key) option.setAttribute("selected", 'selected');

            select.appendChild(option);
        }
    }
}


function reQuery(func){setTimeout(func,1000);}
function refreshPage(){window.location.href=window.location.href.replace(/#.*$/,'');}
function ajaxQuery(query,params,callback) {
    if (typeof(query)=='undefined') return false;
    params['rnd'] = Math.random();
    $.getJSON('/ajax/'+query+'/',params,function(data){
        if (data==null || data.status=='waiting'){reQuery(function(){ajaxQuery(params,callback)}); return false;}
        if (typeof(callback)=='function'){callback(data);}
    });
}
