<?php 
	require "_header.php"; 
	require "data/combo_semester.php";
?>
<div id="main-content" >
<script type="text/javascript">
var Semester_Fields = [
	{name:'branchsemester_id',type: 'int'},
	{name:'branch_id',type: 'int'},
	{name:'branch_name'},
	{name:'semester_id',type: 'int'},
	{name:'semester_name', type:'string'},
	{name:'noofdiv'}	
]

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

var labelArray;
labelArray = [['Semester Name:<span class="comp">*</span>']];

var SemesterStore = new Ext.data.JsonStore({
    url: 'data/branchsemester-grid-data.php',
    root: 'data',
    fields: Semester_Fields,
    autoLoad: true
});

Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	{ 
		header: "Branch Name",
		width: 170,
		dataIndex: 'branch_name',
		sortable: true
  	},{ 
		header: "Semester Name",
		width: 150,
		dataIndex: 'semester_name',
		sortable: true
  	},
	{ 
		header: "No Of Div",
		width: 80,
		dataIndex: 'noofdiv',
		sortable: true
  	}
	]);

	
	var grid = new Ext.grid.GridPanel({
   		store: SemesterStore,
        cm: colModel,
		columnLines:true,
		stripeRows:true,
		loadMask:true,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                	Ext.getCmp("semesterform").getForm().loadRecord(rec);
					
					Ext.getCmp('branch_name').setRawValue(rec.data.branch_name);
					Ext.getCmp('branch_name').setValue(rec.data.branch_id);
					
					//alert(rec.data.semester_name);
					Ext.getCmp('semester_name').setRawValue(rec.data.semester_name);
					Ext.getCmp('semester_name').setValue(rec.data.semester_id);
					
					
					if(flgEdit == "true"){
									Ext.getCmp('btnSave').enable();
					 }
					else{
						   Ext.getCmp('btnSave').disable();
						   alert('You do not have access to edit the record.');
					   }
					  	
						if(flgDelete == "true"){
									Ext.getCmp('btnDelete').enable();
					 }
				}
         	}
   		}),
		height: 380,
		border: true
 	});
	
	grid.on('render', function(){
		grid.tip = new Ext.ToolTip({
    		view: grid.getView(),
    		target: grid.getView().mainBody,
    		delegate: '.x-grid3-row',
    		trackMouse: true,
    		renderTo: document.body,
    		listeners: {
     			beforeshow: function updateTipBody(tip) {
      				tip.body.dom.innerHTML = "Please click here to edit record.";
     			}
    		}
   		});
  	});

var branchstore = new Ext.data.ArrayStore({		
		fields: ['branch_id','branch_name'],
		data:Ext.semester.branch
	});

	var semesterstore = new Ext.data.ArrayStore({		
	fields: ['branch_id','semester_id', 'semester_name'],
		data:Ext.semester.semester
	});
		
	var branch = new Ext.form.ComboBox({
		name: 'branch_name',
		id:'branch_name',
		hiddenName:'branch_hidden',
		labelSeparator:'',	
		fieldLabel :'Branch :<span class="comp">*</span>' ,
		displayField:'branch_name',
		valueField: 'branch_id',
		editable:true,
		triggerAction:'all',
		tabIndex:1,
		mode:'local',
		allowBlank:false,
		store: branchstore,
		listeners: {'select': function(cmb, rec, idx){
		semesterstore.filter('branch_id',cmb.getValue(),false,true,true);				
		}}
	});

	var semester = new Ext.form.ComboBox({
		name: 'semester_name',
		id:'semester_name',
		hiddenName:'semester_hidden',
		labelSeparator:'',	
		fieldLabel :'Semester: <span class="comp">*</span>' ,
		displayField:'semester_name',
		valueField:'semester_id',
		editable:true,
		tabIndex:2,
		triggerAction:'all',
		mode:'local',
		lastQuery:'',
		allowBlank:false,
		store: semesterstore,
		lastQuery:'',
		listeners: {'select': function(cmb, rec, idx){
			
		}}
	});
	
	var semesterform = new Ext.FormPanel({	
		id: 'semesterform',
		frame: true,
		url: 'save-branchsemester.php',
    	labelAlign: 'left',
    	 layout:'column',        
		items :[{
			columnWidth: 0.60,
			layout: 'form',
			labelWidth:85,
			items:[grid]},
		   {
			columnWidth: 0.40,
			layout: 'form',
			labelWidth:75,
			style:'padding:0px 5px',
			items:[branch,semester,
					{
						xtype: 'numberfield',
						labelSeparator:'',	
						fieldLabel :'No of Div:<span class="comp">*</span>' ,
						name: 'noofdiv',
						id: 'noofdiv',
						maxValue:4,
						tabIndex:2,
						allowBlank:false
					},
					{
						xtype: 'textfield',
						name: 'branchsemester_id',
						id:'branchsemester_id',
						value:'0',
						hidden:true
					}]}	]
    });
	

	var SemesterWindow = new Ext.Window({
		title: 'Division Master',
		width:730,		
		height:450,	
		frame: true,
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: semesterform,
		buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:2,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
				semesterform.getForm().submit({
										waitMsg: 'Saving...',
										success: function () {	
											grid.getSelectionModel().clearSelections();
											Ext.getCmp('btnDelete').disable();	
											SemesterStore.load();
											semesterform.getForm().reset();	
											Ext.getCmp('branch_name').focus(true,500);								
										},
										failure: function () {
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'Please fill up complete form.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});									
										}
									});		
				}	
			},
			{
				text: 'Delete',
				iconCls:'remove',
				disabled:true,
				id: 'btnDelete',
				tabIndex:3,
				name: 'btnDelete',
				handler: function(){
					
					var semester_id=Ext.getCmp('semester_name').getValue();
					Ext.Ajax.request({
						loadMask: true,
						sync : true,
						url: 'data/getRelatedRecord.php',
						params: {action:'semester',semester_id:semester_id},
						success: function(resp){
							var str = "";
							resbool=resp.responseText;
							if(resbool != 0){
								str = "It has related record in " + resbool +"Please Delete This Record First";
								Ext.MessageBox.show({
									title: 'Error',
									msg: str,
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});			
							}
							else{
								Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
							}
						}				
					});
				}
			},
			{
				text: 'Reset', 
				name:'btnreset',
				tabIndex:4,
				iconCls:'icon-reset',
				id:'btnreset',
				handler: function (){
					semesterform.getForm().reset();
					grid.getSelectionModel().clearSelections();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('branch_name').focus(true,500);
					if(flgAdd == "true"){
									Ext.getCmp('btnSave').enable();
					 }
				}
			},
			{
				text:'Close', 
				name:'btnclose',
				tabIndex:5,
				iconCls:'icon-close',
				id:'btnclose',
				handler: function (){
					SemesterWindow.close();
				}
			}
		]
	});
	
	function deleterecord(btn){
    	if(btn=='yes'){
	   		semesterform.getForm().submit({
				waitMsg:'Saving...',
				params:{act:'del'},
				success:function(){	
					semesterform.getForm().reset();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('semester_id').setValue('0');	
					SemesterStore.load();	
					Ext.getCmp('branch_name').focus(true,500);								
				}
			});
	 	}  
  	}
	
	var varWidth =  (screen.width - SemesterWindow.width)/2;
	SemesterWindow.setPosition(varWidth,((screen.height/4)-85));
	SemesterWindow.show();
	
if(flgAdd == "true"){
				Ext.getCmp('btnSave').enable();
 }else
{
				Ext.getCmp('btnSave').disable();
}

Ext.getCmp('branch_name').focus(true,1000);

});
</script>
<div id="abcd"></div>
</div>
<?php require "_footer.php";?>
