/**
 * @author TsMtsUser
 */
function addKozter(feature){
	
	//Need to get highest id from database
	//webservice connected datastore
	var kozterStore=new Ext.data.JsonStore ({
		url: 'querySQL.php',
		baseParams: {
			'mode':'GETMAXKOZTERID',
			'query':'dummy'
		},
		fields:['MAXID'],
		root: 'data',
		autoLoad: true,
		listeners: {
			'load': function(){
				var maxKozterId=kozterStore.getAt(0).data.MAXID;
				maxKozterId++;
				//Create form for input data and save
		        var kozterForm = new Ext.FormPanel({
					feature: feature,
		            url: 'querySQL.php',
					baseParams:{
						'mode':'submitkozter',
						'query':'dummy'
					},
		            title: '',
		            width: 350,
		            items: [{
		                xtype: 'textfield',
		                fieldLabel: 'Igénylő',
		                name: 'igenylo',
		                allowBlank: false
		            }, {
		                xtype: 'numberfield',
		                fieldLabel: 'Méret (m2)',
		                name: 'meret',
		                allowBlank: false
		            }, {
		                xtype: 'textfield',
		                fieldLabel: 'Cél',
		                name: 'cel',
		                allowBlank: false
		            }, {
		                xtype: 'datefield',
		                fieldLabel: 'Kiadás ideje',
		                name: 'kiadas',
		                allowBlank: false
		            }, {
		                xtype: 'datefield',
		                fieldLabel: 'Lejárat',
		                name: 'lejarat',
		                allowBlank: false
		            }, {
		                xtype: 'numberfield',
		                fieldLabel: 'Azonosító',
		                name: 'id',
		                allowBlank: false,
		                readOnly: true,
		                value: maxKozterId
		            }, {
		                xtype: 'numberfield',
		                fieldLabel: 'X',
		                name: 'X',
		                allowBlank: false,
		                readOnly: true,
		                value: feature.feature.geometry.x
		            }, {
		                xtype: 'numberfield',
		                fieldLabel: 'Y',
		                name: 'Y',
		                allowBlank: false,
		                readOnly: true,
		                value: feature.feature.geometry.y
		            }, {
		                xtype: 'tbbutton',
		                text: 'Mentés',
		                handler: function(){
		                    kozterForm.getForm().submit({
		                        success: function(f, a){
									var kiadas=f.items.items[3].getValue().format("Y-m-d");
									var lejarat=f.items.items[4].getValue().format("Y-m-d");
									f.feature.feature.attributes={
										'fillColor':"#00ff00",
										'igenylo':f.items.items[0].getValue(),
										'cel':f.items.items[1].getValue(),
										'meret':f.items.items[2].getValue(),
										'kiadas':kiadas,
										'lejarat':lejarat,
										'id':f.items.items[5].getValue()	
									};
		                            Ext.Msg.alert('Siker', 'Sikeres mentés');
									//ToDo: actually add feature attributes to the kozterLayer with attributes
									kozterWindow.close();
									
		                        },
		                        failure: function(f, a){
		                            Ext.Msg.alert('Hiba', 'Hiba történt a mentéskor');
		                        }
		                        
		                    })
		                }
		            },{
						xtype:'tbbutton',
						text:'Mégse',
						handler: function(){
							kozterLayer.removeFeatures([feature.feature]);
							kozterWindow.close();
						}
					}]
		        });
    
		        var kozterWindow = new Ext.Window({
		            title: 'Kérem adja meg a közterület foglalás adatait!',
		            width: 350,
		            height: 400,
		            x: 500,
		            y: 100,
		            layout: 'fit',
		            plain: true,
		            closable: true,
		            collapsible: false,
		            bodyStyle: 'padding:5px;',
		            modal: true,
		            items: [kozterForm]
		        });
		        kozterWindow.show();
   
			}
		}

	});
}

function onKozterSelect(feature){
    selectedFeature = feature;
	if (perm == 'admin') {
    popup = new OpenLayers.Popup.FramedCloud("chicken", feature.geometry.getBounds().getCenterLonLat(), null, "<div style='font-size:.8em'>Azonosító: " + feature.attributes.id + "<br />\
									 Igénylő: " +feature.attributes.igenylo+"<br />\
									 Cél: " +feature.attributes.cel+"<br />\
									 Méret: " +feature.attributes.meret +"<br />\
									 Kiadás dátuma: " +feature.attributes.kiadas.substr(0,10) +"<br />\
									 Lejárat dátuma: " +feature.attributes.lejarat.substr(0,10) +"<br />\
									 <a href='javascript:deleteKozter(selectedFeature)'>Törlés</a>\
									 </div>", null, true, onPopupClose);
	} else {
	 popup = new OpenLayers.Popup.FramedCloud("chicken", feature.geometry.getBounds().getCenterLonLat(), null, "<div style='font-size:.8em'>Azonosító: " + feature.attributes.id + "<br />\
									 Igénylő: " +feature.attributes.igenylo+"<br />\
									 Cél: " +feature.attributes.cel+"<br />\
									 Méret: " +feature.attributes.meret +"<br />\
									 Kiadás dátuma: " +feature.attributes.kiadas.substr(0,10) +"<br />\
									 Lejárat dátuma: " +feature.attributes.lejarat.substr(0,10) +
									 "</div>", null, true, onPopupClose);
	}
    feature.popup = popup;
    map.addPopup(popup);
}

function deleteKozter(feature){
	var numPopups=map.popups.length
	for(j=numPopups-1;j>=0;j--){
		map.removePopup(map.popups[j]);
	}
	kozterLayer.removeFeatures([feature]);
	var conn = new Ext.data.Connection();
	conn.request({
	    url: 'querySQL.php',
	    method: 'POST',
	    params: {'mode': 'DELKOZTER', 'query':feature.attributes.id},
	    success: function(responseObject) {
	        Ext.Msg.alert('Status', 'Sikeres törlés!');
	    },
	     failure: function() {
	         Ext.Msg.alert('Status', 'Törlés sikertelen!');
	     }
	});
}

