Ext.override(Ext.layout.FormLayout, {
    renderItem : function(c, position, target){
        if(c && !c.rendered && c.isFormField && c.inputType != 'hidden'){
            var args = [
                   c.id, c.fieldLabel,
                   c.labelStyle||this.labelStyle||'',
                   this.elementStyle||'',
                   typeof c.labelSeparator == 'undefined' ? this.labelSeparator : c.labelSeparator,
                   (c.itemCls||this.container.itemCls||'') + (c.hideLabel ? ' x-hide-label' : ''),
                   c.clearCls || 'x-form-clear-left'
            ];
            if(typeof position == 'number'){
                position = target.dom.childNodes[position] || null;
            }
            if(position){
                c.formItem = this.fieldTpl.insertBefore(position, args, true);
            }else{
                c.formItem = this.fieldTpl.append(target, args, true);
            }

//          Remove the form layout wrapper on Field destroy.
            c.on('destroy', c.formItem.remove, c.formItem, {single: true});
            c.render('x-form-el-'+c.id);
        }else {
            Ext.layout.FormLayout.superclass.renderItem.apply(this, arguments);
        }
    }
});

function BuildAddresses() {
	dest_arr = new Array();
	Ext.form.AddressComboBox = Ext.extend(Ext.form.ComboBox, {
		valueField: 'id',
		displayField: 'name',
		typeAhead: true,
		mode: 'local',
		fieldLabel: 'Выбрать объект',
		emptyText: 'Быстрый адрес',
		//forceSelection : true,
		store: refPlaceStore,
		selectOnFocus: true,
		triggerAction: 'all',
		width: 250
	});

	Ext.form.StreetComboBox = Ext.extend(Ext.form.ComboBox, {
		valueField: 'street',
		displayField: 'street',
		typeAhead: true,
		mode: 'local',
		fieldLabel: 'Улица',
		emptyText: 'Улица куда ехать',
		store: this.store_street,
		selectOnFocus: true,
		autoLoad: true,
		width: 250
	});

	combo_refplace_dest_blur = function (f) {
		if(f.selectedIndex == -1) {
		  f.street.setValue('');
		  f.house.reset();
		}
	}

	combo_street_dest_select = function (f) {
		if (f.getValue() == '&nbsp;') {	//если код=о, значит очистка поля!!!
			f.setValue('');
			//combo_refplace_dest.house.setValue('');
			return;
		}
	}

	combo_refplace_dest_select = function (f) {
		f.street.reset();
		if (f.getValue() == 0) {	//если код=о, значит очистка поля!!!
			f.street.setValue('');
			f.house.setValue('');
			f.setValue('');
			return;
		}

		//если  вначале символ *, значит ищем адрес в постоянных адресах
		displayValue = f.getRawValue();//Ext.get('refplace_dest1').getValue();
		if (displayValue.substr(displayValue.length-1, 1) == SYMBOL_REFPLACE_ADDR) {
			Ext.Ajax.request({
				url: common_url + 'action.php?action=getaddress1&id=' + f.getValue() /*Ext.get('refplace_dest').getValue()*/,
				method: 'GET',
				success: function(result, request) {
					srcresult = Ext.util.JSON.decode(result.responseText);
					f.street.setValue(srcresult.street);
					f.house.setValue(srcresult.house);
				}
			});
			return false;
		};
		//* если  вначале символ *, значит ищем адрес в постоянных адресах

		if (!this.store_street) {
			Ext.Ajax.request({
				url: common_url + 'action.php?action=getadress&id=' + f.getValue(),
				method: 'GET',
				success: function(result, request) {
					srcresult = Ext.util.JSON.decode(result.responseText);
					f.street.setValue(srcresult.street);
					f.house.setValue(srcresult.house);
				}
			});
		}
	}

	AddAddress = function(butt,e) {
		var n = dest_arr.length + 1;
		var i = -1;
		for (var pos=0; pos<addressto_fieldset.items.length; pos++) {
			if (addressto_fieldset.items.items[pos].getXType() == 'panel')
				if (addressto_fieldset.items.items[pos].buttons[1].id == butt.id)
					{i = Math.floor((pos-3)/4); break; }
		}
		pos++;

		var r,s,h,m,a,d,p;
		addressto_fieldset.insert(pos++,
			r = new Ext.form.AddressComboBox(Ext.apply(/*combo_refplace_dest.initialConfig, */
				{id:'refplace_dest1_'+n}))
		);
		addressto_fieldset.insert(pos++,
			s = new Ext.form.StreetComboBox(Ext.apply(/*combo_street_dest.initialConfig, */
				{id:'id_street_dest_'+n}))
		);
		addressto_fieldset.insert(pos++,
			h = new Ext.form.TextField(Ext.apply(house_dest.initialConfig,
				{id:'id_house_dest_'+n, name:'house_dest_'+n}))
		);
		m = new Ext.Button(Ext.apply(refPlaceToButton.initialConfig,
			{id:'id_mark_dest'+n}));
		a = new Ext.Button(Ext.apply(addressto_add.initialConfig,
			{id:'id_add_dest'+n}));
		d = new Ext.Button(Ext.apply(addressto_del.initialConfig,
			{id:'id_del_dest'+n, hidden:false}));
		addressto_fieldset.insert(pos++,
			p = new Ext.Panel(Ext.apply(addressto_panel.initialConfig,
				{id:'id_panel_dest_'+n, buttons:[m,a,d]}))
		);
		addressto_fieldset.doLayout();
		r.street = s;
		r.house = h;
		s.house = h;
		r.on('blur', combo_refplace_dest_blur);
		s.on('select', combo_street_dest_select);
		r.on('select', combo_refplace_dest_select);
		r.focus();
		dest_arr.push(dest_arr.length/*{refplace:f, street:s, house:h, button:b}*/);
	}

	DeleteAddress = function(butt) {
		if (dest_arr.length == 0) return;

		var n = dest_arr.length + 1;
		var i = -1;
		for (var pos=0; pos<addressto_fieldset.items.length; pos++) {
			if (addressto_fieldset.items.items[pos].getXType() == 'panel')
				if (addressto_fieldset.items.items[pos].buttons[2].id == butt.id)
					{i = Math.floor((pos-3)/4); break; }
		}
		pos-=3;

		addressto_fieldset.remove(pos, true);
		addressto_fieldset.remove(pos, true);
		addressto_fieldset.remove(pos, true);
		addressto_fieldset.remove(pos, true);
		addressto_fieldset.doLayout();
		dest_arr.pop();
	}


	CalcAddresses = function() {
		var res, item, item_tmp, pos;
		res = new Array();
		for(var i=0; i<dest_arr.length; i++) {
			pos = i*4 + 4;
			item_tmp = new Array(
				addressto_fieldset.items.items[pos++],
				addressto_fieldset.items.items[pos++],
				addressto_fieldset.items.items[pos++]
			);
			item = new Array(
				item_tmp[0].getValue()+'^^'+item_tmp[0].getRawValue(),
				item_tmp[1].getValue()+'^^'+item_tmp[1].getRawValue(),
				item_tmp[2].getRawValue()
			);
			res.push(item.join('#'));
		}
		return(res.join('%'));
	}
}