var tfKTWcaption;

function showKerTabWin() {
	var cbSor=getKTWcomboSor();
	var cbOszl = getKTWcomboOszlop();
	var cbCella = getKTWcomboCell();
	var chbNewPanel=getKTWPanelTypeCB(); 
	tfKTWcaption = getKTWCaptionField();
	
	var p = new Ext.Panel({
		layout: 'form',
		defaults: {width: 350, anchor : '100%'},
		bodyStyle : 'padding: 5px;',
		baseCls: 'x-plain',
		labelWidth : 100,
    defaultType: 'textfield',
		items: [tfKTWcaption, cbSor, cbOszl, cbCella, chbNewPanel]		
	});
	
	var btnOK = new Ext.Button({
		text : 'Rendben',
		minWidth : 120,
		iconCls : 'ok',
		handler : function() {

			ktonnewtab = chbNewPanel.getValue();
			if (cbSor.getValue() == '') {
				Ext.Msg.alert('Hiba', 'A "Sorok" mező nem maradhat üres.');
				return;
			}
			else if (cbOszl.getValue() == '') {
				Ext.Msg.alert('Hiba', 'Az "Oszlopok" mező nem maradhat üres.');
				return;
			}		
			var id = cbSor.getValue().toLowerCase()+'!' +cbOszl.getValue().toLowerCase()+ cbCella.getValue();
//			alert(tfKTWcaption.getValue() +", "+ cbSor.getValue()+", "+cbOszl.getValue()+", "+cbCella.getValue()+", "+chbNewPanel.getValue() + "\n"+id);
			var caption = tfKTWcaption.getValue();
			if (tfKTWcaption.getValue() == '') {
				Ext.Msg.alert('Hiba', 'A "Kimutatás neve" mező nem maradhat üres.');
				return;
			}
			else {
				closeWin();
				
				waitwin = Ext.MessageBox.wait("'" + caption + "' kimutatás készítése...", "Egységes Hídnyilvántartási Rendszer");
				
				createKerTabPanel(id, caption, cbSor.getValue(), cbOszl.getValue(), cbCella.getValue());
			}
		}
	});
  var btnCancel = new Ext.Button({
		text : 'Mégsem',
		minWidth : 120,
		iconCls : 'cancel',
		handler : function() {
			closeWin();
		}
	});

	var win = new Ext.Window({
		title : 'Kimutatás összeállítása',
		height: 200,
		width: 500,
		iconCls : 'defptab',
		modal: true,
		resizable : false,
		layout : 'fit',
		constrainHeader : true,
		items: [p],
		buttonAlign: 'right',
		buttons: [btnCancel, btnOK]
	});
	win.show();
	
	function closeWin() {
			win.hide();
			win.destroy();
	}
}
function getKTWCaptionField() {
	return new Ext.form.TextField({
		fieldLabel : 'Kimutatás neve',
		emptyText: 'Adja  meg a kimutatás nevét (címkéjét)',
		name : 'caption',
		allowBlank : false
	});
}

function getKTWcomboSor() {
	var cb = new Ext.form.ComboBox({
                        fieldLabel: 'Sorok',
                        hiddenName:'rowfield',
												allowBlank : false,
                        store: new Ext.data.JsonStore({
											    url: 'phpk/tab/kertabdefcb.php',
													params : {t:'r'},
											    root: 'r',
													id : 'kod',
													fields: ['kod', 'leir']
                        }),
                        valueField:'kod',
                        displayField:'leir',
												typeAhead: true,
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText:'A sorok generálásához használandó mező...',
                        selectOnFocus:true,
												listeners : { change : function(cb, newValue, oldValue){
													var c =tfKTWcaption.getValue();
													var p = c.indexOf('/');
													var r = cb.store.getById(newValue);
													if (p>-1)
															c = '/'+c.substr(p+1, c.length-p-1);
													else
													    c ='/';
												
													if (r!=undefined) 
														c = r.get('leir') + ' ' + c;
													tfKTWcaption.setValue(c);	
												}},
                        width:140
                    });
	cb.store.load({params:{t:'r'}});
	return cb;
}

function getKTWcomboOszlop() {
	var cb = new Ext.form.ComboBox({
                        fieldLabel: 'Oszlopok',
                        hiddenName:'colfield',
												allowBlank : false,
                        store: new Ext.data.JsonStore({
											    url: 'phpk/tab/kertabdefcb.php',
													params : {t:'c'}, 
											    root: 'r',
													id : 'kod',
													fields: ['kod', 'leir']
                        }),
                        valueField:'kod',
                        displayField:'leir',
                        typeAhead: true,
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText:'Az oszlopok generálásához használandó mező...',
                        selectOnFocus:true,
												listeners : { change : function(cb, newValue, oldValue){
													var c =tfKTWcaption.getValue();
													var p = c.indexOf('/');
													var r = cb.store.getById(newValue);
													if (p>-1)
															c = c.substr(0, p) +'/';
													else
													    c ='/';
												
													if (r!=undefined) 
														c += ' ' +r.get('leir');
													tfKTWcaption.setValue(c);	
												}},
                        width:90
                    });
	cb.store.load({params:{t:'c'}});
	return cb;
}

function getKTWcomboCell() {
	return new Ext.form.ComboBox({
                        fieldLabel: 'Cellaérték',
                        hiddenName:'ktcella',
												allowBlank : false,
                        store: new Ext.data.SimpleStore({
                            fields: ['kod', 'felirat'],
                            data : [[1,'Híd darabszám [db]'],
														        [2,'Híd szerkezeti hossz [m]'],
																		[3,'Híd felület [m2]']]
                        }),
                        valueField:'kod',
												value : 1,
                        displayField:'felirat',
                        typeAhead: true,
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText:'A táblázat celláiban megjelenítendő érték...',
                        selectOnFocus:true,
                        width:190
                    });
}

function getKTWPanelTypeCB() {
	return new Ext.form.Checkbox({
												fieldLabel: '',
                        labelSeparator: '',
                        boxLabel: 'Új fülön kérem a táblázatot',
                        name:'ktpanel'
                    });
}
