Ext.onReady(function() {
	Ext.QuickTips.init();
	
	var fieldset1 = {
	xtype       : 'fieldset',
	title       : 'Credit',
	flex        : 1,
	border      : false,
	labelWidth  : 50,
	defaultType : 'field',
	defaults    : {
		anchor     : '-10',
		allowBlank : false
	},
	items : [
		{
			fieldLabel : 'First',
			name       : 'firstName'
		}
	]
	}

var fieldset2 = Ext.apply({}, {
	flex       : 1,
	labelWidth : 50,
	title      : 'Debit',
	items      : [
		{
			fieldLabel : 'Address',
			name       : 'address'
		}
		
	]

}, fieldset1);

var fieldsetContainer = new Ext.Container({
	//xtype  : 'container',
	layout  : 'hbox',
	height  : 120,
	layoutConfig : {
		align : 'stretch',
	},
	items  : [
		fieldset1,
		fieldset2
	]
});

 	
 var account= new Ext.FormPanel({
								url:'save-employee.php',
								id: 'myFormPanel',
								method:'post',
								border      : false,
								layout:'fit',
								 frame:true,
								items:[fieldsetContainer ]
								});
	
   
  


	var onSuccessOrFail = function(form, action) {
	
		var formPanel = Ext.getCmp('myFormPanel');
		formPanel.el.unmask();
		var result = action.result;
		if (result.success) {
			accountgroupliststore.load();
			account.getForm().findField('groupcode').reset();	
			account.getForm().findField('groupname').reset();	
			account.getForm().findField('grouptype').reset();
		
			account.getForm().findField('groupcode').disable();	
			account.getForm().findField('groupname').disable();	
			account.getForm().findField('grouptype').disable();
			
			
		}
	}

	function deleteAccount(btn){
		if(btn=='yes'){
			
				var formPanel = Ext.getCmp('myFormPanel');
					formPanel.getForm().submit({
					url     : 'save-acgroup.php',
					params  : { action: 'delete'},
					success : onSuccessOrFail,
					failure : onSuccessOrFail
				});
			
			
			
		}
	}

 if(!win){	
var  win=new Ext.Window({
	title      : 'Account group ',
	height     : 265,
	border: false,
	url:'saveAccountgroup.php',
	layout     : 'form',
	labelWidth : 100,
	width      : 650,
    closable:false,
	modal:true,
	bodyStyle  : 'padding: 5px',
	items      :account,
	 buttonAlign: 'right',
	buttons: [{
					   text:'Add New',
						handler: function(){
							
							
                          
							var txtaccountgroupCode = Ext.getCmp('groupcode');
							var txtaccountgroupName = Ext.getCmp('groupname');
							var txtaccountgrouptype = Ext.getCmp('grouptype');
							txtaccountgroupCode.enable();
							txtaccountgroupName.enable();
							txtaccountgrouptype.enable();
							 
							  account.getForm().findField('groupname').focus(); 
                			txtaccountgroupName.reset();
							txtaccountgrouptype.reset();
							Ext.Ajax.request({
									  loadMask: true,
									  sync : true,
									  url: 'data/getAccountCode.php',
									  params: {action:'group'},
									  waitMsg:'Loading',
									  success: function(resp) {
										varcodeno=Ext.decode(resp.responseText).idno;
										txtaccountgroupCode.setValue(varcodeno); 			
									  }				
							});	
						}
				   },
			  {
					   text: 'Save',
					   handler: function(){
							var txtaccountgroupName = Ext.getCmp('groupname');
							var txtaccountgrouptype = Ext.getCmp('grouptype');
							if(txtaccountgroupName.getValue()!="" && txtaccountgrouptype.getValue()!=""){
								totalstore = accountgroupliststore.getCount()-1;
								if(totalstore>0){
									var first = parseInt(accountgroupliststore.getAt(0).data.account_groupid);
									var last = parseInt(accountgroupliststore.getAt(totalstore).data.account_groupid);
									var txtaccountgroupCode = Ext.getCmp('groupcode').getValue();
									if(txtaccountgroupCode>Math.max(first,last)){
										var formPanel = Ext.getCmp('myFormPanel');
											formPanel.getForm().submit({
											url     : 'save-acgroup.php',
											params  : { action: 'save'},
											success : onSuccessOrFail,
											failure : onSuccessOrFail
										});
											//account.getForm().findField('subgroupcode').reset();	
			                                //account.getForm().findField('subgroupname').reset();
											//account.getForm().findField('subgroupname').reset();	
									}
									else{
										var formPanel = Ext.getCmp('myFormPanel');
											formPanel.getForm().submit({
											url     : 'save-acgroup.php',
											params  : { action: 'edit'},
											success : onSuccessOrFail,
											failure : onSuccessOrFail
										});
									}
								}
								else{
									var formPanel = Ext.getCmp('myFormPanel');
										formPanel.getForm().submit({
										url     : 'save-acgroup.php',
										params  : { action: 'save'},
										success : onSuccessOrFail,
										failure : onSuccessOrFail
									});
								}
							}
							else{
								Ext.MessageBox.show({
									title: 'Error',
									   msg:'Please fill complete form ',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.ERROR
										
								});
							}
						}  
						
				   },
		      {
					   text: 'Reset',
					   handler: function(){
							var txtaccountgroupCode = Ext.getCmp('groupcode');
							var txtaccountgroupName = Ext.getCmp('groupname');
							var txtaccountgrouptype = Ext.getCmp('grouptype');
							txtaccountgroupCode.reset();
                			txtaccountgroupName.reset();
							txtaccountgrouptype.reset();
						}
						  
				   },
			  {
					   text: 'Delete',
					   handler: function(){
						   
						   
						    var txtaccountgroupCode = Ext.getCmp('groupcode');
						   if(txtaccountgroupCode.getValue()!=""){
								Ext.MessageBox.confirm('Confirmation','You are about to delete a Account. Continue?',deleteAccount);
						   }
						   else
						   {
							   Ext.MessageBox.show({
									title: 'Error',
									   msg:'Please select any group ',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.ERROR
										
								});
							   
							   }
						   
						   
						        
						}
						  
				   },
			  {
					   text: 'Close',
					   handler: function(){
						   win.hide();
					}
				   }
	]
})
}
win.show();

});
