Ext.ux.autocompleter = Ext.extend(Ext.util.Observable, {
	els : {},
	constructor : function(config) {
		config = config || {};
		Ext.apply(this, config);

		Ext.ux.autocompleter.superclass.constructor.call(this, config);
		this.initMarkup();
		this.initEvents();
	},

	initMarkup : function() {
		var dh = Ext.DomHelper;
		this.els.observerfield = Ext.get(this.observerfield);
		this.els.updatefield = Ext.get(this.updatefield);
	},

	initEvents : function() {
		if (this.els.observerfield != null) {
			this.els.observerfield.on('blur', function(ev, t) {
				var el = Ext.get(Ext.get(t));

				var toUpdate = this.els.updatefield;

				Ext.Ajax.request({
							url : '/plzs/getCities.json',
							methode : 'POST',
							params : {
								"data[Plz][id]" : el.getValue(),
								"data[Plz][country_id]" : 'DE'
							},
							success : function(response, opts) {
								var obj = Ext.decode(response.responseText);
								if (obj.results) {
									toUpdate.dom.value = (obj.results[0].value);
								}
							},
							failure : function(response, opts) {

							}
						});

			}, this);

		}
	}
});


Ext.ux.plzautocompleter = Ext.extend(Ext.util.Observable, {
	els : {},
	constructor : function(config) {
		config = config || {};
		Ext.apply(this, config);

		Ext.ux.plzautocompleter.superclass.constructor.call(this, config);
		this.initMarkup();
		this.initEvents();
	},

	initMarkup : function() {
		var dh = Ext.DomHelper;
		this.els.observerfield = Ext.get(this.observerfield);
		this.els.updatefield = this.updatefield;
	},

	initEvents : function() {
		if (this.els.observerfield != null) {
			this.els.observerfield.on('blur', function(ev, t) {
				var el = Ext.get(Ext.get(t));

				var toUpdate = this.els.updatefield;
				toUpdate.disable();
				Ext.Ajax.request({
							url : '/plzs/getCities.json',
							methode : 'POST',
							params : {
								"data[Plz][id]" : el.getValue(),
								"data[Plz][country_id]" : 'DE'
							},
							success : function(response, opts) {
								var obj = Ext.decode(response.responseText);
								if (obj.results) {
								//	toUpdate.dom.value = (obj.results[0].value);
									toUpdate.setValue(obj.results[0].value);
									toUpdate.enable();
								}
							},
							failure : function(response, opts) {
									toUpdate.enable();
							}
						});

			}, this);

		}
	}
});
