Ext.override(Ext.form.ComboBox, {
    setValue : function(v){
        var text = v;
        if(this.valueField){
            if(this.mode == 'remote' && !Ext.isDefined(this.store.totalLength)){
                this.store.on('load', this.setValue.createDelegate(this, arguments), null, {single: true});
                if(this.store.lastOptions === null){
                    var params = {};
                    if(this.valueParam){
                        params[this.valueParam] = v;
                    }else{
                        params[this.queryParam] = this.allQuery;
                    }
                    this.store.load({params: params});
                }
                return;
            }
            var r = this.findRecord(this.valueField, v);
            if(r){
                text = r.data[this.displayField];
            }else if(this.valueNotFoundText !== undefined){
                text = this.valueNotFoundText;
            }
        }
        this.lastSelectionText = text;
        if(this.hiddenField){
            this.hiddenField.value = v;
        }
        Ext.form.ComboBox.superclass.setValue.call(this, text);
        this.value = v;
    }
});

/**
 * Registriert die ExtJs ZipComboBox
 * @return void
 */
function registerZipCityComboBox() {
	
	if (!Ext.get('inputSearchOffer')) {
		return;
	}
	
    var direct_store = new Ext.data.DirectStore({
        directFn: AbfallScout_Shop_Service_Zipcode.getZipCityList,
        root: '',
        fields: ['zipcode_city', 'zipcode_id']
    });

    zipcity_combobox = new Ext.form.ComboBox({
        store: direct_store,
        displayField: 'zipcode_city', //welches "field" aus dem Storage anzeigen?
        valueField: 'zipcode_id', // welches Feld wird by getValue() zurückgegeben
        hiddenName: 'zipcode_id',
        allowBlank: false,
        blankText: 'Ihre PLZ / Ort eingeben',
        hideTrigger: true,
        typeAhead: false,
        forceSelection: true,
        triggerAction: 'all',
        emptyText:'Ihre PLZ / Ort eingeben',
        selectOnFocus:true,
        loadingText:'Lädt PLZ Liste...',
        applyTo: 'inputSearchOffer',
    	queryDelay: 50,
    	width: 245,
    	minChars: 3,
    	valueNotFoundText: 'Ihre PLZ / Ort eingeben'
    });
    

   zipcity_combobox.store.on('load', function(obj) {
	   if (obj.getCount() == 1) {
		   zipcity_combobox.setValue(obj.data.items[0].data.zipcode_id);

           var form = document.getElementById('locationForm');
           if (form) {
               form.submit();
           }
	   }
   });
    
   zipcity_combobox.on('select', function(comboBox, selected) {
       //zipcity_combobox.setValue(obj.data.items[0].data.zipcode_id);
       zipcity_combobox.setValue(selected.json.zipcode_id);
       var form = document.getElementById('locationForm');
       if (form) {
           form.submit();
       }
   });
}


Ext.onReady(function() {
	registerZipCityComboBox();
});

