var TOiR_outlets_Window = null;
var TOiR_outlets_WindowAddEdit = null;
var TTPointObj = {};
var pressed = false;
var allOutlets = true;

Ext.define('TOiR_RequestModel', {
    extend: 'Ext.data.Model',
    fields: ['id_outlet','client_id', 'name', 'region', 'city', 'address',
	'contact_name', 'id_code_PSD', 'ID_PSD', 'contract_number', 'note', 'type_TT',
	'sales_contact_number', 'name_TT', 'model', 'geoX', 'geoY', 'equipment_id', 'client_name', 'model_equipment', 
	{name:'check',type: 'bool',defaultValue:'false'}]
});

var TOiR_outletsStore = new Ext.data.Store(
  {
        //autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_RequestModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=show',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        },
        listeners: {
        	load: function(store,records) {
        		//console.log(records);
        		store.each(function(rec) {
        			var checkRec = rec.get('id_outlet');
        			for (var key in TTPointObj) {
            			if (checkRec == key) {
            				rec.set('check', true);
            				break;
            			}
            		}
        		})
        		/*for (var key in TTPointObj) {
        			console.log(key);
        		}*/
        	}
        }
});

Ext.define('TOiR_SelectRegion', {
    extend: 'Ext.data.Model',
    fields: ['region']
});

var TOiR_SelectRegionStore = new Ext.data.Store(
  {     
  	autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_SelectRegion',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=selectRegion',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_SelectCity', {
    extend: 'Ext.data.Model',
    fields: ['city']
});

var TOiR_SelectCityStore = new Ext.data.Store(
  {     
  	// autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_SelectCity',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=selectCity',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});


Ext.define('TOiR_JobTypeModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_JobTypeStore = new Ext.data.Store(
  {     autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_JobTypeModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showJobType',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_ClientModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_ClientStore = new Ext.data.Store(
  {     autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_ClientModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showClients',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_OutletsModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_OutletStore = new Ext.data.Store(
  {     
		//autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_OutletsModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showOutlets',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_EquipmentModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_EquipmentStoreOutlts = new Ext.data.Store(
  {     
		//autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_EquipmentModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showEquipment',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_UsersRequestModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_UsersRequestStore = new Ext.data.Store(
  {     
		autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_UsersRequestModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showUsers',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});Ext.define('TOiR_JobTypeModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_JobTypeStore = new Ext.data.Store(
  {     autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_JobTypeModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showJobType',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_ClientModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_ClientStore = new Ext.data.Store(
  {     autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_ClientModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showClients',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_OutletsModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_OutletStore = new Ext.data.Store(
  {     
		//autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_OutletsModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showOutlets',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_EquipmentModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_EquipmentStore = new Ext.data.Store(
  {     
		//autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_EquipmentModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showEquipment',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

Ext.define('TOiR_UsersRequestModel', {
    extend: 'Ext.data.Model',
    fields: ['id' , 'name']
});

var TOiR_UsersRequestStore = new Ext.data.Store(
  {     
		autoLoad: true,
		//autoDestroy: true,
        model: 'TOiR_UsersRequestModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_outlets.php?func=showUsers',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});




function show_TOiR_outlets_Window(Xcoord,Ycoord,Wwidth,Wheight) {
	TOiR_outletsStore.load();
	Xcoord = Xcoord ? Xcoord :40;
    Ycoord = Ycoord ? Ycoord :60;  
    Wwidth = Wwidth ? Wwidth :1100;  
    Wheight = Wheight ? Wheight :400;

    var outletsGrid = Ext.create('Ext.grid.Panel', {
    	selType: 'rowmodel',
	    columnLines: true,
	    id: 'TOiR_OutletsGridId',
	    store: TOiR_outletsStore,
	    loadMask: true,
	    border: false,
	    viewConfig: {
            //preserveScrollOnRefresh: true,
    		markDirty: false
        },
	    autoScroll: true,
		dockedItems: [{
			xtype: 'toolbar',
            items: [
            	{
            		//text: 'Добавить',
            		text: all_toir.add,
            		iconCls: 'add',
            		handler: function () {
            			show_TOiR_request_WindowAddEdit();
            		}
            	}, '-',
            	{
            		//text: "Удалить",
            		text: all_toir.del,
            		iconCls: 'delete',
            		handler: function (View,record,item,index,e,eOpts) {
            			// var id = record.get('id').getValue();
            			// alert(id);
            			// console.log(record);
            			remove_TOiR_request_Window();
            		}
            	},'-',{
                  //text: 'Редактировать',
                  text: all_toir.edit,
                  iconCls: 'edit',
                  handler: function (){
                    var select = Ext.getCmp('TOiR_OutletsGridId').getView().getSelectionModel().getSelection()[0];
                    // alert(select.get('id_client'));
                    console.log(select);
                     if (select) {
                        var id = select.get('id_outlet'); 	
			    		// alert(id);
			    		var name = select.get('name'); 	
			    		var client_id = select.get('client_id'); 	
			    		var region = select.get('region'); 	
			    		var city = select.get('city'); 	
			    		var address = select.get('address'); 	
			    		var contact_name = select.get('contact_name'); 	
			    		var id_code_PSD = select.get('id_code_PSD'); 	
			    		var ID_PSD = select.get('ID_PSD'); 	
			    		var contract_number = select.get('contract_number'); 	
			    		var note = select.get('note'); 	
			    		var type_TT = select.get('type_TT'); 	
			    		var sales_contact_number = select.get('sales_contact_number'); 	
			    		var name_TT = select.get('name_TT'); 	
			    		var equipment = select.get('model'); 	
			    		var geox = select.get('geoX'); 	
			    		var geoy = select.get('geoY');
			    		var equipment_id = select.get('equipment_id');

			    		show_TOiR_request_WindowAddEdit(id,name,client_id,region,city,address,contact_name,id_code_PSD,ID_PSD,
			    				contract_number,note,type_TT,sales_contact_number,name_TT,equipment,geox,geoy, equipment_id);    		
                      } else {
                        Ext.MessageBox.alert(all_toir.error, all_toir.select_row);
                      }
                    }
                }
            ]
		}],
		columns: [{
			xtype: 'checkcolumn',
			//text: 'На карте',
			text: outlet_toir.on_map,
			dataIndex: 'check',
			sortable: false,
			width: 70,
			listeners: {
				checkchange:function(CheckColumn, rowIndex, checked, eOpts ) {
					
					var toir_map_googl = Ext.getCmp('mymap');
					record = outletsGrid.store.getAt(rowIndex);
					console.log(record);
					//name
					if (checked) {
						if (record.get('geoY') && record.get('geoX')) {							
							var GoogleLatLng = new google.maps.LatLng(record.get('geoY'), record.get('geoX'));
							var equipment = record.get('model_equipment') ? record.get('model_equipment') : '';
							var tolltip = '<div class="grad"><p><span>'+record.get('name')+'</span></p><p><span><b>Адрес: </b> '
							+record.get('address')+'</span></p><p><b>Оборудование: </b>'+equipment+'</p></div>';
							//var tolltip = 'dddd';
							
							toir_map_googl.addMarker(GoogleLatLng,{cursor:'TT_point_'+record.get('id_outlet'),
								icon: 'images/record.png'},false,false,null,tolltip);
							TTPointObj[record.get('id_outlet')] = record.get('id_outlet');
						} else {
							record.set('check', false);
							Ext.Msg.alert(all_toir.attention, outlet_toir.at_no_point_geo_coordinates);
						}
					} else {
						toir_map_googl.deleteMarker('TT_point_'+record.get('id_outlet'));
					} 
				}, 
				headerclick: function(ct, column, e, t, eOpts) {
					if(column.id=='check'){
						alert('sfdf');
						/*if () {
							
						} else {
							
						}*/
					}
				}/*,
				itemdblclick: function(cmp, record) {
					console.log(record);
				}*/
			}
		}, {
        		xtype: 'hidden',
				id: 'id',
				name: 'id',
				dataIndex: 'id_outlet',
			},
			{
		        //text: 'Название',
		        text: outlet_toir.title,
		        dataIndex: 'name',
				id: 'name',
				name: 'name',
		        sortable: true
		      },
			{
		        //text: 'Клиент',
		        text: client_toir.client, 
		        dataIndex: 'client_name',
				id: 'client_id',
				name: 'client_id',
		        sortable: true
		      },
		       {
		        //text: 'Область',
		        text: client_toir.domain, 
		        dataIndex: 'region',
				id: 'region',
				name: 'region',
		        sortable: true
		      },
		      {
		        //text: 'Город',
		        text: client_toir.sity, 
		        dataIndex: 'city',
				id: 'city',
				name: 'city',
		        sortable: true
		      },
		      {
		        //text: 'Адрес',
		        text: client_toir.address, 
		        dataIndex: 'address',
				id: 'address',
				name: 'address',
		        sortable: true
		      },
		  //  	  {
		  //       text: 'Контактный телефон', 
		  //       dataIndex: 'contact_phone',
				// id: 'contact_phone',
				// name: 'contact_phone',
		  //       sortable: true
		  //     },
			  {
		        //text: 'Контактное лицо',
		        text: client_toir.contact_person, 
		        dataIndex: 'contact_name',
				id: 'contact_name',
				name: 'contact_name',
		        sortable: true
		      },
			 //  {
		  //       text: 'ID код PS&D', 
		  //       dataIndex: 'id_code_PSD',
				// id: 'id_code_PSD',
				// name: 'id_code_PSD',
		  //       sortable: true
		  //     },
			  {
		        //text: 'Номер контракта',
		        text: client_toir.contract_number, 
		        dataIndex: 'contract_number',
				id: 'contract_number',
				name: 'contract_number',
		        sortable: true
		      },	 
			 //  {
		  //       text: 'Примечание', 
				// id: 'description',
		  //       dataIndex: 'description',
		  //       sortable: true
		  //     },
			  {
		        //text: 'Тип',
		        text: equip_toir.type,
		        dataIndex: 'type_TT',
				id: 'type_TT',
				name: 'type_TT',
		        sortable: true
		      },
			  {
		        //text: 'Телефон ТП',
		        text: user_toir.phone,
		        dataIndex: 'sales_contact_number',
				id: 'sales_contact_number',
				name: 'sales_contact_number',
		        sortable: true,
		      }, {
		        text: 'Geo X', 
		        dataIndex: 'geoY',
		        name: 'geoy',
				id: 'geoy',
		        sortable: true
		      }, {
			        text: 'Geo Y', 
			        dataIndex: 'geoX',
					id: 'geox',
					name: 'geox',
			        sortable: true
			   }, {
		    	  //text: 'Оборудование',
		    	  text: TOiR.Equipment,
		    	  dataIndex: 'model_equipment'
		      }

		],
		listeners: {
	    	itemdblclick: function(View,record,item,index,e,eOpts) {
	    		var toir_map_googl = Ext.getCmp('mymap');
	    		if (record.get('check') == true) {
	    			var Latlng = new google.maps.LatLng(record.get('geoY'), record.get('geoX'));
	    			toir_map_googl.setNewCenter(Latlng,17);
	    		} else {
	    			Ext.Msg.alert(all_toir.attention, outlet_toir.to_zoom_point_set_checkbox);
	    		}
	    		/*var id = record.get('id_outlet'); 	
	    		// alert(id);
	    		var name = record.get('name'); 	
	    		var client_id = record.get('client_id'); 	
	    		var region = record.get('region'); 	
	    		var city = record.get('city'); 	
	    		var address = record.get('address'); 	
	    		var contact_name = record.get('contact_name'); 	
	    		var id_code_PSD = record.get('id_code_PSD'); 	
	    		var ID_PSD = record.get('ID_PSD'); 	
	    		var contract_number = record.get('contract_number'); 	
	    		var note = record.get('note'); 	
	    		var type_TT = record.get('type_TT'); 	
	    		var sales_contact_number = record.get('sales_contact_number'); 	
	    		var name_TT = record.get('name_TT'); 	
	    		var equipment = record.get('model'); 	
	    		var geox = record.get('geoX'); 	
	    		var geoy = record.get('geoY');
	    		var equipment_id = record.get('equipment_id');
	
	    		show_TOiR_request_WindowAddEdit(id,name,client_id,region,city,address,contact_name,id_code_PSD,ID_PSD,contract_number,note,
	    				type_TT,sales_contact_number,name_TT,equipment,geox,geoy, equipment_id);    		
	    	*/},
	    	headerclick: function( ct, column, e, t, eOpts ) {	    		
	    		if (column.dataIndex == 'check') {
	    			if (allOutlets) {
	    				console.log(outletsGrid);
	    				showAllOutlets(outletsGrid);
	    				allOutlets = false;	    				
	    			} else {	    				
	    				hideAllOutlets(outletsGrid);
	    				allOutlets = true;
	    			}	    			
	    		}	    			
	    	}
    }
    });

    if (!TOiR_outlets_Window) {
    	TOiR_outlets_Window = new Ext.Window({
    		layout : 'fit',
            //title : 'Торговые точки',
            title : TOiR.outlets,
            closeAction : 'destroy',
            width : parseInt(Wwidth),
            height : parseInt(Wheight),
            //minimizable: true,
			// constrainHeader: true,
            maximizable:true,
            //x : parseInt(Xcoord),
            //y : parseInt(Ycoord),
			listeners : {
            	destroy : function(){
					TOiR_outlets_Window = null;	
            	}
            },
            items : [outletsGrid]

    	});
    }
    function show_TOiR_request_WindowAddEdit(id,name,client_id,region,city,address,contact_name,id_code_PSD,ID_PSD,
    		contract_number,note,type_TT,sales_contact_number,name_TT,equipment,geox,geoy, equipment_id) {
    	// alert(id);
    	if (id) {
    		//Ext.getCmp('outlet_equipment_id').setValue(equipment_id);
    		
    		//title = 'Редактирование торговой точки';
    		title = outlet_toir.edit_outlet;
    		//textBtn = 'Сохранить';
    		textBtn = all_toir.save;
    	} else {
    		//title = 'Новая торговая точка';
    		title = outlet_toir.new_outlet;
    		textBtn = all_toir.create;
    		id = '';
	    	name = '';
	    	client_id = '';
	    	region = '';
	    	city = '';
	    	address = '';
	    	contact_name = '';
	    	id_code_PSD = '';
	    	ID_PSD = '';
	    	contract_number = '';
	    	note = '';
	    	type_TT = '';
	    	sales_contact_number = '';
	    	name_TT = '';
	    	equipment = '';
	    	geox = '';
	    	geoy = '';
	    	equipment_id = '';
    	}

    	if (!TOiR_outlets_WindowAddEdit) {
    		TOiR_outlets_WindowAddEdit = new Ext.Window ({
    			layout : 'fit',
	            //title : 'Торговые точки',
	            title : TOiR.outlets,
	            closeAction : 'destroy',
	            width : 450,
	            height : 500,
	            //minimizable: true,
				// constrainHeader: true,
	            maximizable:true,
	            modal:true,
	            //x : parseInt(Xcoord),
	            //y : parseInt(Ycoord),
				listeners : {
	            	destroy : function(){
						TOiR_outlets_WindowAddEdit = null;	
	            	}
	            },

	            items: [
	            	{
	            		xtype: 'form',
						autoScroll: true,
						bodyPadding: '5 5 5 5',
						id: "TOiR_OutletsFormId",
						border: false,
						frame: true,
						autoHeight: true,
						//fieldwidth:'width:150px',
						layout: {
							type: 'vbox',
							columns: 2
							//align: 'stretch'
						},

	            	
	            items: [{
		            	xtype: 'textfield',
						//fieldLabel: 'Название',
						fieldLabel: outlet_toir.title,
						id: 'request_name',
						name: 'request_name',
						value: name,
						width: 400,
					},
					{
	            		xtype: 'hidden',
						fieldLabel: 'id',
						id: 'request_id_outlet',
						name: 'request_id_outlet',
						value:id
					},
					{
							xtype: 'combo',
							//fieldLabel: 'Клиент',
							fieldLabel: client_toir.client,
							store: TOiR_ClientStore,
							valueField: 'id',
							displayField: 'name',

							name : 'client',
							editable: false,
							allowBlank: false,
							value: client_id,
							id: 'request_client_id',
							listeners: {
								select: function(combo, record) {
									var clientID = record[0].get('id');
									TOiR_EquipmentStoreOutlts.setProxy({
										type: 'ajax',
										url: 'app/TOiR_outlets.php?func=showEquipment&clientID='+clientID,
										reader: { type: 'json', root: 'rows'}
									});
									TOiR_EquipmentStoreOutlts.load();
								}
							},
							width: 400,
						},
					
					{
	            		xtype: 'combobox',
						//fieldLabel: 'Область',
						fieldLabel: client_toir.domain,	
						id: 'request_region',
						value: city,
						store:TOiR_SelectRegionStore,
						minChars: 0,
						queryMode: 'remote',
						queryParam: 'inpRegion',
			       		// displayField: 'id',
			        	valueField: 'region',
			        	// queryParam: 'name',
			        	name: 'request_region',
						displayField: 'region',
						// valueField: 'varGLN',
						hideTrigger: true, 
						typeAhead: true,
						triggerAction: 'query',
						//forceSelect: true,
						lazyRender: true,
						editable: true,
						value: region,
       				listeners: {
							
						}	,
						width: 400		
					},
					{
	            		xtype: 'combobox',
						//fieldLabel: 'Город',
						fieldLabel: client_toir.sity,
						id: 'request_city',
						name: 'request_city',
						value: city,
						store:TOiR_SelectCityStore,
						minChars: 0,
						queryMode: 'remote',
						queryParam: 'inpCity',
			       		// displayField: 'id',
			        	valueField: 'city',
			        	// queryParam: 'name',
						displayField: 'city',
						// valueField: 'varGLN',
						hideTrigger: true, 
						typeAhead: true,
						triggerAction: 'query',
						//forceSelect: true,
						lazyRender: true,
						editable: true,
						listeners: {
							focus: function() {
								var inpRegion = Ext.getCmp('request_region').getValue();
									// TOiR_SelectCityStore.setProxy({
									// 	type: 'ajax',
									// 	url: 'app/TOiR_outlets.php?func=selectCity&region='+inpRegion,
									// 	reader: { type: 'json', root: 'rows'}
									// });
								
								TOiR_SelectCityStore.load({
										params: {
											clientID: clientID
										}
								});
							}
						}	,
						width: 400	
					},
					{
	            		xtype: 'textfield',
						//fieldLabel: 'Адрес',
						fieldLabel: client_toir.address,	
						id: 'request_address',
						name: 'request_address',
						value: address	,
						width: 400			
					},
					// {
	    //         		xtype: 'textfield',
					// 	fieldLabel: 'Контактный телефон',	
					// 	id: 'request_contact_phone'		,
					// 	name: 'request_contact_phone',
					// 	value: contract_number		
					// },
					{
	            		xtype: 'textfield',
						//fieldLabel: 'Контакттное лицо',
						fieldLabel: client_toir.contact_person,
							name: 'request_contact_name',
						id: 'request_contact_name',
						value: contact_name	,
						width: 400				
					},
					
					{
	            		xtype: 'textfield',
						//fieldLabel: 'Номер контракта',
						fieldLabel: client_toir.contract_number,	
						id: 'request_contract_number'	,
						name: 'request_contract_number',
						value: contract_number	,
						width: 400			
					},
					
					{
	            		xtype: 'textfield',
						//fieldLabel: 'Тип',	
						fieldLabel: equip_toir.type,
						id: 'request_type_TT'	,
						name: 'request_type_TT',
						value: type_TT		,
						width: 400		
					},
					{
	            		xtype: 'textfield',
						//fieldLabel: 'Телефон менеджера',
						fieldLabel: outlet_toir.phone_manager,
						id: 'request_sales_contact_number'	,
						name: 'request_sales_contact_number',
						value: sales_contact_number		,
						width: 400		
					}, {
	            		xtype: 'textfield',
						fieldLabel: 'Geo X',	
						name: 'request_geoy',
						id: 'request_geoy'	,
						value: geoy		,
						width: 400		
					}, {
	            		xtype: 'textfield',
						fieldLabel: 'Geo Y',	
						name: 'request_geox',
						id: 'request_geox'	,
						value: geox			,
						width: 400	
					}, {
	            		xtype: 'combo',
						//fieldLabel: 'Оборудование',
						fieldLabel: TOiR.Equipment,
						// allowBlank: false,
						name : 'request_equipment',
						store: TOiR_EquipmentStoreOutlts,
						valueField: 'id',
						displayField: 'name',
						width: 400,
						//value: equipment_id
						id: 'outlet_equipment_id'
					}
	            
	            ]
	            ,
	            buttons: [
	            	{
	            		text: textBtn,
	            		handler: function () {
	            			if (textBtn == all_toir.save) {
	            				func = 'edit';
	            			} else {
	            				func = 'add';
	            			}
	            			var id = Ext.getCmp('request_id_outlet'); 	
				    		var name = Ext.getCmp('request_name'); 
				    		var client_id = Ext.getCmp('request_client_id');
				    		var region = Ext.getCmp('request_region'); 	
				    		var city = Ext.getCmp('request_city'); 	
				    		var contact_phone = Ext.getCmp('contact_phone'); 
				    		var address = Ext.getCmp('request_address'); 	
				    		var contact_name = Ext.getCmp('request_contact_name'); 	
				    		var contract_number = Ext.getCmp('request_contract_number'); 	
				    		var type_TT = Ext.getCmp('request_type_TT'); 	
				    		var sales_contact_number = Ext.getCmp('request_sales_contact_number'); 	
				    		var geox = Ext.getCmp('request_geox'); 	
				    		var geoy = Ext.getCmp('request_geoy'); 	

				  

								var form = this.up('form').getForm();
								// alert(form);
								if(form.isValid()) {
								form.submit({
									//waitMsg:'Идет отправка...',
									waitMsg: all_toir.loading,
                                    url: 'app/TOiR_outlets.php?func=addedit&f='+func,
									wait: true,
									// scope: this,
									method: 'post',
									// params: {func:'addedit'},
                                    success: function(fp, o){
                                    	TOiR_outlets_WindowAddEdit.destroy();
                                    	Ext.getCmp('TOiR_OutletsGridId').store.load();
										Ext.getCmp('TOiR_OutletsGridId').getView().refresh();
                                    }
	            			
	            					})
								} 
				    		}
	            	},
	            	{
	            		//text: 'Отмена',
	            		text: all_toir.cancel,
	            		handler: function() {
	            			TOiR_outlets_WindowAddEdit.destroy();
	            		}
	            	}
	            ]
	            }
	            ]
    		})
    	}
    	TOiR_outlets_WindowAddEdit.show();
    	if (id) {
    		var clientID = Ext.getCmp('request_client_id').getValue();    		
    		TOiR_EquipmentStoreOutlts.setProxy({
				type: 'ajax',
				url: 'app/TOiR_outlets.php?func=showEquipment&clientID='+clientID,
				reader: { type: 'json', root: 'rows'}
			});
    		TOiR_EquipmentStoreOutlts.load();
    		Ext.getCmp('outlet_equipment_id').setValue(equipment_id);
    	}	
    }
    function remove_TOiR_request_Window() {
		if (outletsGrid) {
          var sm = outletsGrid.getSelectionModel().getSelection()[0];
          console.log(sm);
          if (sm) {
	          var id = sm.get('id_outlet');
	          /*if (!rs.length) {
	              Ext.Msg.alert(all_toir.attention, outlet_toir.job_type_is_not_selected);
	              return;
	              }*/
	          Ext.Msg.confirm(all_toir.attention, outlet_toir.are_you_sure_want_to_delete_the_selected_type_of_work, function (button) {
	              if (button == 'yes') {
	                          Ext.Ajax.request({  
	                              url : 'app/TOiR_outlets.php?func=del',
	                              method: 'GET',
	                              params: {id: id}, 
	                              success: function(objServerResponse){
	                            	  outletsGrid.store.load(); 
	                              }
	                          });
	                           
	              }
	            });
          	  } else {
          		 Ext.MessageBox.alert(all_toir.error, all_toir.select_row);
          	  }
          }
    }

    TOiR_outlets_Window.show();
}

function showAllOutlets(outletsGrid) {	
	toir_map_googl =  Ext.getCmp('mymap');
	outletsGrid.store.each(function(record){
		if (record.get('geoY') && record.get('geoX')) {							
			var GoogleLatLng = new google.maps.LatLng(record.get('geoY'), record.get('geoX'));
			var equipment = record.get('model_equipment') ? record.get('model_equipment') : '';
			var tolltip = '<div class="grad"><p><span>'+record.get('name')+'</span></p><p><span><b>Адрес: </b> '
			+record.get('address')+'</span></p><p><b>Оборудование: </b>'+equipment+'</p></div>';
			toir_map_googl.addMarker(GoogleLatLng,{cursor:'TT_point_'+record.get('id_outlet'),
				icon: 'images/record.png'},false,false,null,tolltip);
			record.set('check', true);
		} 
	});
}

function hideAllOutlets(outletsGrid) {
	toir_map_googl =  Ext.getCmp('mymap');
	outletsGrid.store.each(function(record){
		if (record.get('check') == true) {			
			toir_map_googl.deleteMarker('TT_point_'+record.get('id_outlet')); //delMarker
			record.set('check', false);
		}
	});
}