function getSelect(url, obj, default_value, force_default)
{
    nocache = new Date();
    url += (/\?/i.test(url) ? '&' : '?') + 'nocache=' + nocache.getTime();
    JsHttpRequest.query(url, {}, function(result, text) { fillSelect(obj, result, default_value, force_default); });
}

function fillSelect(obj, fill, default_value, force_default)
{
    var myOPTION;
    var significant = 0;
    var auto_def = 0;
    obj.innerHTML = '';

    if (obj.options) {
        //alert('123');
        //obj.options = {};
        for(var i=0;obj.options.length;i++){
            obj.remove(i);
        }
        
    }
    /*while (obj.options.length > 0) {
        obj.removeChild(obj.options[0]);
    }*/
    //-------------------------------------
    if (fill['']) {
        myOPTION = document.createElement('option');
        myOPTION.value = '';
        myOPTION.innerHTML = fill[''];
        obj.appendChild(myOPTION);
    }
    
    //-------------------------------------
    for (key in fill) {
        myOPTION = document.createElement('option');
        myOPTION.value = key;
        myOPTION.innerHTML = fill[key];
        if (key == '') continue; // Разкоментироватеь если надо не на показывать всякие направления
        if (key) {
            significant++;
            auto_def = key;
        }
        obj.appendChild(myOPTION);
    }
    if (default_value) {
        obj.value = default_value;
        if (obj.onchange) {
            obj.onchange();
        }
    } else if (force_default && significant == 1 && auto_def) {
        obj.value = auto_def;
        if (obj.onchange) {
            obj.onchange();
        }
    }
}

function getSelect2(url, obj, default_value, force_default)
{
    nocache = new Date();
    url += (/\?/i.test(url) ? '&' : '?') + 'nocache=' + nocache.getTime();
    JsHttpRequest.query(url, {}, function(result, text) { fillSelect2(obj, result, default_value, force_default); });
}

function fillSelect2(obj, fill, default_value, force_default)
{
    var myOPTION;
    var significant = 0;
    var auto_def = 0;
    var i, key, val, sel;
    obj.innerHTML = '';
    for (i = 0; i < fill.length; i++) {
        key = fill[i].key;
        val = fill[i].val;
        sel = parseInt(fill[i].sel ? fill[i].sel : 0);
        myOPTION = document.createElement('option');
        myOPTION.value = key;
        myOPTION.innerHTML = val;
        if (key) {
            significant++;
            auto_def = key;
        }
        if (!default_value && sel) {
            default_value = key;
        }
        obj.appendChild(myOPTION);
    }
    if (default_value) {
        obj.value = default_value;
        if (obj.onchange) {
            obj.onchange();
        }
    } else if (force_default && significant == 1 && auto_def) {
        obj.value = auto_def;
        if (obj.onchange) {
            obj.onchange();
        }
    }
}

clearSelect = function(obj)
{
    var i = 0;
    while (i < obj.options.length) {
        if (obj.options[i].value == '' || obj.options[i].value == '0') {
            obj.selectedIndex = i;
            i++;
        } else {
            obj.removeChild(obj.options[i]);
        }
    }
}


function autocompleter(completers)
{
    $(document).ready(function() {
        for (obj_id in completers) {
            $(document.getElementById(obj_id)).autocomplete(
                '/ajax/autocompleter.php', 
                {
                    minChars:1,
                    timeout:100,
                    validSelection:false,
                    parameters:{type:completers[obj_id]},
                    before : function(input,text) {},
                    after : function(input,text) {}
                }
            );
        }
    }); 

}
