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: 'Bitte geben Sie eine PLZ ein!',
        hideTrigger: true,
        typeAhead: false,
        forceSelection: true,
        triggerAction: 'all',
        emptyText:'PLZ eingeben',
        selectOnFocus:true,
        loadingText:'Lädt PLZ Liste...',
        applyTo: 'inputSearchOffer',
    	queryDelay: 100,
    	width: 200,
    	minChars: 1,
    	valueNotFoundText: 'PLZ oder Ort nicht auffindbar'
    });
    

   zipcity_combobox.store.on('load', function(obj) {
	   if (obj.getCount() == 1) {
		   zipcity_combobox.setValue(obj.data.items[0].data.zipcode_id);
	   }
   });
    
//   zipcity_combobox.on('select', function(comboBox, selected) { 
//	   AbfallScout_Shop_Service_Zipcode.setSessionZipcode({
//			zipcode_id: selected.data.zipcode_id
//			
//		}, function(response, request) {
//			
//			if (response.success) {
//				//ZipCode-Auswahl wurde erfolgreich in der Session gespeichert
//			}
//		},
//		this);
//   });
}


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

