/*!
 * Ext JS Library 3.0+
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){

    var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/crm_localities.php',
            method : 'POST'
        }),
        baseParams: {
             limit: 25,
        },
        reader: new Ext.data.JsonReader({
            root: 'localities',
            totalProperty: 'totalCount',
            id: 'loc_id'
        }, [
            {name: 'loc_name',  mapping: 'loc_name'},
            {name: 'loc_id', mapping: 'loc_id'}
        ])
    });

    // Custom rendering Template
/*    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item">',
            '<h3><span>{lastPost:date("M j, Y")}<br />by {author}</span>{title}</h3>',
            '{excerpt}',
        '</div></tpl>'
    );*/
    
    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item">',
            '{loc_name}',
        '</div></tpl>'
    ); 
    
    var search = new Ext.form.ComboBox({
        store: ds,
        displayField:'loc_name',
        typeAhead: false,
        loadingText: 'Пошук...',
        width: 400,
        pageSize:10,
        hideTrigger:true,
        tpl: resultTpl,
        applyTo: 'search',
      //  minChars: 2,
        itemSelector: 'div.search-item',
        onSelect: function(record){ 
                if(this.isExpanded()){
                    this.collapse();
                }
                document.getElementById("search").value=record.data.loc_name;
                document.getElementById("loc_id").value=record.data.loc_id;   
        }
    });
});