                                                                                                                                                                                                                               
if(flgDelete == "true") flgDelete1 = true;
else flgDelete1 = false;
if(flgAdd == "true") flgAdd1 = true;
else flgAdd1 = false;

var secFields = [{
        name: 'designation_id',
        type: 'int'
    }, {
        name: 'designation_nameeng',
        type: 'string'
    },{
        name: 'designation_nameguj',
        type: 'string'
    },{
        name: 'designation_priority',
        type: 'string'
    },{
        name: 'designation_type',
        type: 'string'
    }]

var Section = Ext.data.Record.create(secFields);
var designationtype = new Ext.data.ArrayStore({
           fields: ['distype'],
	       data: [['Teaching Staff'],['Non-Teaching Staff']]
        }); 
var Store = new Ext.data.JsonStore({
    url:'data/designation-grid-filter.php',
    root: 'rowe',
		remoteSort: true,
    sortInfo: {
    	field: 'designation_id',
        direction: 'ASC'
   	},
    fields: secFields,
    idProperty: 'designation_id',
		autoLoad: true,
    writer:new Ext.data.JsonWriter()
});

Ext.onReady(function(){
	
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
		
		{ 
			header: "Designation ID", 
			dataIndex: 'designation_id',
			sortable: true, 
			hidden: true 
		},
		{ 
			id: 'designation_nameeng',
			header: "Designation English",
			width: 100,
			dataIndex: 'designation_nameeng',
			align: 'left',
			sortable: true
    },
		{
			dataIndex: 'designation_nameguj',
			header: 'Designation Gujarati',
			sortable: true,
			align: 'left',
			hidden:true
			,width: 100
		},
		{
			dataIndex: 'designation_priority',
			header: 'Designation Priority',
			sortable: true,
			align: 'left'
			,width: 100
		},
		{
			dataIndex: 'designation_type',
			header: 'Type',
			sortable: true,
			align: 'left'
			,width: 100
		}
    ]);
	var flgalert = 0;
	 var grid = new Ext.grid.GridPanel({
					ds: Store,
					cm: colModel,
					sm: new Ext.grid.RowSelectionModel({
						singleSelect: false,
						listeners: {
							rowselect: function(sm, row, rec) {
								Ext.getCmp("company-form").getForm().loadRecord(rec);
								if(flgEdit == "true"){
									gridForm.getForm().findField('designation_nameeng').enable();
									//gridForm.getForm().findField('designation_nameguj').enable();
									gridForm.getForm().findField('designation_priority').enable();
									gridForm.getForm().findField('designation_type').enable();
									Ext.getCmp('btnSave').enable();
								}
								else{
										if(flgalert == 0){
											alert("You do not have access to edit the record.");
											flgalert = 1;
										}
								}
								Ext.getCmp('btnAdd').disable();
								
								if(flgDelete == "true"){
									Ext.getCmp('btnDel').enable();
								}
								
								
							}
						}
					}),
					autoExpandColumn: 'designation_nameeng',
					height: 250,
					border: true
				});
	
var gridForm = new Ext.FormPanel({	
	id: 'company-form',
  frame: true,
	url:'save-designation.php',
  labelAlign: 'left',
    
    bodyStyle:'padding:5px',
    
    layout: 'column',
    items: [{
				columnWidth: 1,
				layout: 'fit',
				items:[grid]
			},
			{
		    	columnWidth: 1,
    	    xtype: 'fieldset',
        	labelWidth: 150 ,
	        defaults: {border:false},
          defaultType: 'textfield',
					height:160,
					bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
            border: false,
            style: {
                "margin-left": "10px", 
                "margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0" 
            },
            items: [				
				{
					xtype: 'textfield',
					name: 'designation_nameeng',
					id:'designation_nameeng',
					fieldLabel: 'Designation Name English',
					width:150,
				
					disabled:true,
					allowBlank:true,
					autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
            	},
				{ 
					xtype: 'textfield',
	        fieldLabel: 'Designation Name Gujarati',
    	    name: 'designation_nameguj',
    	    hidden:true,
					width:150,
					disabled:true
         },
			     {				
									xtype: 'combo',
									name: 'designation_type',
									id:'designation_type',
									fieldLabel: 'Designation Type',
									forceSelection:true,				
									displayField:'distype',
									valueField: 'distype',
									width: 80,
									emptyText:'Type',
									allowBlank:true,
									triggerAction:'all',
									mode:'local',
									editable:false,disabled:true,												
									store: designationtype
									
    		      },{
									xtype: 'textfield',
	                fieldLabel: 'Designation Priority',
    	            name: 'designation_priority',
									disabled:true,
									id:'designation_priority',
									width:150,
									allowBlank:true
    		      },
			 				{
									xtype: 'numberfield',
									name: 'designation_id',
									id:'designation_id',
									hidden:true
			
            	},
			 	{
					xtype: 'textfield',
					name: 'mode',
					id:'mode',
					value:'edit',
					hidden:true
			
            	}
			]
			}
        ]
    });


	var DesignationWindow = new Ext.Window({
//		tbar: languageToolbar,
    title: 'Designation Information',
		width:420,		
		height:480,		
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		
		items: gridForm,
		buttonAlign: 'center',
	    buttons: [
					{
						text: 'Add New',
						name:'btnAdd',
						iconCls: 'icon-add',
						id:'btnAdd',/*disabled : !flgAdd1,*/
						handler: function (){
									
								gridForm.getForm().findField('designation_id').setValue(0);
								gridForm.getForm().findField('designation_nameeng').reset();
							//	gridForm.getForm().findField('designation_nameguj').reset();
								gridForm.getForm().findField('designation_priority').reset();
								Ext.getCmp('btnAdd').disable();
								Ext.getCmp('btnDel').disable();
								Ext.getCmp('btnSave').enable();
								gridForm.getForm().findField('designation_nameeng').enable();
							//	gridForm.getForm().findField('designation_nameguj').enable();
								gridForm.getForm().findField('designation_priority').enable();
								gridForm.getForm().findField('designation_type').enable();
								gridForm.getForm().findField('designation_nameeng').focus(true,500);
							}
						},
						{
							text: 'Save', name:'btnSave',id:'btnSave',disabled:true,
						 	handler: function (){
							var designation_nameeng = Ext.getCmp('designation_nameeng').getValue();
							//var designation_nameguj = Ext.getCmp('designation_nameguj').getValue();
							var designation_priority = Ext.getCmp('designation_priority').getValue();
							//var designation_nameguj = Ext.getCmp('designation_nameguj').getValue();
							if(designation_nameeng!=''){
								gridForm.getForm().submit({
									waitMsg: 'Saving...',
									success: function () {
										Ext.MessageBox.show({
									   title: 'Message',
									   msg: 'Data has been saved.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.INFO
				                      });
								if(flgAdd == "true"){		
									Ext.getCmp('btnAdd').enable();
								}
								Ext.getCmp('btnDel').disable();
								Ext.getCmp('btnSave').disable();
								gridForm.getForm().findField('designation_nameeng').disable();
							
								gridForm.getForm().findField('designation_priority').disable();
								gridForm.getForm().findField('designation_type').disable();
								Store.load();	
								gridForm.getForm().reset();
								grid.getSelectionModel().clearSelections();
									}
									
								});			
								
							}else{
								    Ext.MessageBox.show({
                                                   title: 'Error',
                                                   msg: 'Please enter Designation Name.',
                                                   buttons: Ext.MessageBox.OK,
                                                   icon:Ext.MessageBox.ERROR
                                                  });    
                                }
							}
												
							}						 
						,
						{
						text: 'Delete',
						name:'btnDel',
						iconCls:'remove',
						id:'btnDel',
			        	disabled: true,
					    	handler: function (){
								gridForm.getForm().findField('mode').setValue('delete');
								if(gridForm.getForm().findField('designation_nameeng').getValue() =='')
								{
									 Ext.MessageBox.show({
												   title: 'Error',
												   msg: 'Please select a record.',
												   buttons: Ext.MessageBox.OK,
												   icon:Ext.MessageBox.ERROR
												  });	
								}
								else
								{
								Ext.MessageBox.confirm('Confirmation', 'Do u want to Delete this record?',deletesubject);
								}
							}	
						},{text: 'Reset', name:'btnreset',id:'btnreset',
						handler: function (){
										gridForm.getForm().reset();
										gridForm.getForm().findField('designation_nameeng').disable();
								//gridForm.getForm().findField('designation_nameguj').disable();
								gridForm.getForm().findField('designation_priority').disable();
								gridForm.getForm().findField('designation_type').disable();
										Ext.getCmp('btnSave').disable();
										Ext.getCmp('btnDel').disable();
									if(flgAdd == "true"){
										Ext.getCmp('btnAdd').enable();
									}
										grid.getSelectionModel().clearSelections();
									}
						},
						{text: 'Close', name:'btnclose',id:'btnclose',
						handler: function (){
										
										DesignationWindow.close();
									}
						}]

	});
	var varWidth =  (screen.width - DesignationWindow.width)/2;
    DesignationWindow.setPosition(varWidth,((screen.height/4)-110));
	DesignationWindow.show();
	initIndicIME();

	function deletesubject(btn){
									if(btn=='yes')
									{			  			
										gridForm.getForm().submit({
										waitMsg: 'Loading...',
										success: function () {
											
												gridForm.getForm().reset();
												gridForm.getForm().findField('designation_nameeng').disable();
												//gridForm.getForm().findField('designation_nameguj').disable();
												gridForm.getForm().findField('designation_priority').disable();
												gridForm.getForm().findField('designation_type').disable();
												Ext.getCmp('btnSave').disable();
												Ext.getCmp('btnDel').disable();
												if(flgAdd == "true"){
													Ext.getCmp('btnAdd').enable();
												}
												Store.load();
										},
										failure: function () {
												 Ext.MessageBox.show({
												   title: 'Error',
												   msg: 'Please fill up complete form.',
												   buttons: Ext.MessageBox.OK,
												   icon:Ext.MessageBox.ERROR
												  });									
										}
									});
									}
									else
									{
										//gridForm.getForm().reset();
									}

								}

gridForm.getForm().findField('designation_priority').on('blur',function (){
	
	        pphText.setGlobalScript(document.getElementById('language').value);
    });
	gridForm.getForm().findField('designation_priority').on('focus',function (){
	
		pphText.setGlobalScript("english");
    });
	
	
	
	gridForm.getForm().findField('designation_priority').on('blur',function (){
		
	var vardesignation_priority=gridForm.getForm().findField('designation_priority').getValue();	
	var vardesignation_id=gridForm.getForm().findField('designation_id').getValue();			
	var type=gridForm.getForm().findField('designation_type').getValue();
	
	if(vardesignation_id == '' )
		{
			vardesignation_id = 0;
		}
		var resbool="";		
				
		if(vardesignation_priority!=""  && type!='')
		{			
					Ext.Ajax.request({
							  loadMask: true,
							  sync : true,
							  url: 'data/checkdesignation.php',
							  params: {designation_priority: vardesignation_priority,type:type, designation_id: vardesignation_id},
							  success: function(resp) {
								resbool=Ext.decode(resp.responseText).success;			                     
										if(resbool=='false')
										{
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'Designation Priority already exist.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});
											gridForm.getForm().findField('designation_priority').reset();
										}	 			
							  }				
							});		
		}
	}); //designation_priority blur event
/*if (flgDelete=='true') {
                  Ext.getCmp('btnDel').enable();
               }
*/
});