// shorthand to document.getElementById
function _$(id) { return document.getElementById(id); }


/**
 * Builds the sub category list
 * @param [selectBox] the calling select box
 */
function buildSubCategoryList(selectBox)
{
    // get the sub category select box
    var subSelectBox = _$("search-multisearch-sub_categories");
    var selValue = selectBox.options[selectBox.selectedIndex].value;
    // get all child nodes of the selected category
    var subNodes = _$("cat_"+selValue).getElementsByTagName("span");
    
    // clear the previous list
    subSelectBox.options.length = 0;
    for (var i = 0 ; i < subNodes.length; ++i)
    {
        var subNode = subNodes[i];
        var text = subNode.getAttribute("sub_name");
        var value = subNode.getAttribute("sub_id");
        // add the new options
        subSelectBox.options[subSelectBox.options.length] = // below
            new Option(text, value);
    }
}

/**
 * Sets up autocomplete boxes
 */
Ext.onReady(function(){
    var store_cities = new Ext.data.JsonStore({
        url: 'get_cities.php',
        baseParams: {type:'city'},
        root: 'items',
        fields: ['name', 'id']
    });
    var store_zips = new Ext.data.JsonStore({
        url: 'get_cities.php',
        baseParams: {type:'zip'},
        root: 'items',
        fields: ['name', 'id']
    });
    
    var cities_search = new Ext.form.ComboBox({
        store: store_cities,
        applyTo: 'search-multisearch-city',
        minChars: 1,
        displayField: 'name',
        typeAhead: false,
        loadingText: 'Searching...',
        hideTrigger: true,
        queryDelay: 250
    });
    cities_search.addListener('beforeselect', function(combo, record, index){
        console.log(record);
        return (record.data.id != '');
    });

    var zips_search = new Ext.form.ComboBox({
        store: store_zips,
        applyTo: 'search-multisearch-zip',
        minChars: 2,
        displayField: 'name',
        typeAhead: false,
        loadingText: 'Searching...',
        hideTrigger: true,
        queryDelay: 250
    });
    zips_search.addListener('beforeselect', function(combo, record, index){
        return (record.data.id != '');
    });

})
