<?php 
	require "_header.php";
	require "includeMultiSelect.php";
	require "data/combo_semester.php";
?>
<div id="main-content">
<style>
	label.myBold{font-weight: bold;color:#006;font-size: 14px;}
</style>



<script language="javascript" type="text/javascript">
Ext.onReady(function() {
	Ext.QuickTips.init();
	
	var labelArray;
	labelArray = [  
		['Branch:<span class="comp">*</span>'],
		['Semester:<span class="comp">*</span>']
	];
	
	var branchstore = new Ext.data.ArrayStore({		
		fields: ['branch_id','branch_name'],
		data:Ext.semester.branch
	});
	
	var semesterstoreleft = new Ext.data.ArrayStore({		
		fields: ['branch_id','semester_id', 'semester_name'],
		data:Ext.semester.semester
	});
		
	var divisionstore_left = new Ext.data.ArrayStore({
			fields:['division_id','division_name'],
			data: Ext.semester.division
		});	
		

	
	var branch_left = new Ext.form.ComboBox({
		name: 'branch_left',
		id:'branch_left',
		hiddenName:'branch_hidden_left',
		labelSeparator:'',	
		fieldLabel :labelArray[0] ,
		displayField:'branch_name',
		valueField: 'branch_id',
		editable:true,
		triggerAction:'all',
		mode:'local',
		allowBlank:false,
		store: branchstore,
		listeners: {'select': function(cmb, rec, idx){
			Ext.getCmp('semester_left').reset();
			studentstore_left.removeAll();
			studentstore_right.removeAll();
			semesterstoreleft.filter('branch_id',cmb.getValue(),false,true,true);
		}}
	});

	
	var semester_left = new Ext.form.ComboBox({
		name: 'semester_left',
		id:'semester_left',
		hiddenName:'semester_hidden_left',
		labelSeparator:'',	
		fieldLabel :labelArray[1] ,
		displayField:'semester_name',
		valueField: 'semester_id',
		editable:true,
		triggerAction:'all',
		mode:'local',
		allowBlank:false,
		lastQuery:'',
		store: semesterstoreleft,
		listeners: {'select': function(cmb, rec, idx) {
		Ext.getCmp('division_left').reset();
		var branch=Ext.getCmp('branch_left').getValue();
		divisionstore_left.filter('division_id',branch+cmb.getValue(),false,true,true);
		var cnt=divisionstore_left.getCount();
		if(cnt==1)
		{
			Ext.getCmp('division_left').setValue(divisionstore_left.getAt(0).data.division_name);
			Ext.getCmp('division_right').setValue(divisionstore_left.getAt(0).data.division_name);
		}
				
        }
		}
	});


	
	var division_left = {		
						xtype: 'combo',
						name: 'division_left',
						id:'division_left',
						labelSeparator:'',	
						fieldLabel :'Division :<span class="comp">*</span>' ,
						forceSelection:true,				
						displayField:'division_name',
						valueField: 'division_name'
						,lastQuery:''
						,allowBlank:false
						,triggerAction:'all'
						,mode:'local'
						,store:divisionstore_left
						,selectOnFocus: true,
						listeners: {'select': function(cmb, rec, idx) {}
					    }
				  }
	
	var division_right = {		
						xtype: 'combo',
						name: 'division_right',
						id:'division_right',
						labelSeparator:'',	
						fieldLabel :'Division :<span class="comp">*</span>' ,
						forceSelection:true,				
						displayField:'division_name',
						valueField: 'division_name'
						,lastQuery:''
						,allowBlank:false
						,triggerAction:'all'
						,mode:'local'
						,store:divisionstore_left
						,selectOnFocus: true,
						listeners: {'select': function(cmb, rec, idx) {}
     }}




var studentstore_left = new Ext.data.Store({
	proxy: new Ext.data.HttpProxy({url: 'data/getstudentdata.php'}),
	reader: new Ext.data.JsonReader({id: 'student_id',root: 'data'},
	[{name:'value',mapping:'student_id'},{name:'text',mapping: 'student_name'}])
});	

	
	studentstore_right = new Ext.data.JsonStore({
		url:'data/getstudentdata.php',
		root: 'data',
		totalProperty: 'total',
		fields: [{name: 'value', mapping:'student_id'}, {name: 'text', mapping: 'student_name'}] 
	});

    var SemTransferForm = new Ext.FormPanel({
        id:'SemTransferForm',
        frame: true,
        labelAlign: 'left',
		bodyStyle: 'padding:5px',
        url:'save-divtransfer.php',
		layout:'column',
		items:[{columnWidth:1,
		    labelWidth:75,  
			layout:'form',
			items:[branch_left,semester_left]
			},{ 
			columnWidth:.5,
		    labelWidth:75,  
			layout:'form',
			items:[{
				xtype: 'label',
				text: 'From Division',
				style: 'padding:0px 0px',
				cls: 'x-form-item myBold'	
			},division_left]
		},{ 
			columnWidth:.5,
		    labelWidth:75,  
			layout:'form',
			items:[{
				xtype: 'label',
				text: 'To Division',
				style: 'padding:0px 0px',
				cls: 'x-form-item myBold'	
			},division_right]
		},{ 
			columnWidth:.5,
			layout:'form',
			items:[{   
				xtype:'button',
				text: 'Show',
				style: 'padding:0px 0px 10px 235px',	
				handler:function(){	 
					var branch_left	  = Ext.getCmp('branch_left').getValue();
					var sem_left	  = Ext.getCmp('semester_left').getValue();
					var div_left	  = Ext.getCmp('division_left').getValue();
					var div_right	  = Ext.getCmp('division_right').getValue();
					
					if(branch_left!="" && sem_left!="" && div_left!="" && div_right!="" ){   
						if(div_left == div_right){  
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Invalid Division Selection',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.ERROR
							});
						}				     
						else{
							Ext.getCmp('branch_left').disable();
							Ext.getCmp('semester_left').disable();
							Ext.getCmp('division_left').disable();
							Ext.getCmp('division_right').disable();
																				 
							studentstore_left.load({
								params:{
								branch_id:Ext.getCmp('branch_left').getValue(),
								semester_id:Ext.getCmp('semester_left').getValue(),
								division_id:Ext.getCmp('division_left').getValue()
								}});
							studentstore_right.load({
								params:{branch_id:Ext.getCmp('branch_left').getValue(),
								semester_id:Ext.getCmp('semester_left').getValue(),
								division_id:Ext.getCmp('division_right').getValue()
								}});
						}  
					} 
	               	else{
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Fill up all of the above Fields',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.ERROR
						});
					} 
				}
			}]
		},{ 
			columnWidth:.5,
			layout:'form',
			items:[{  
				xtype:'button',
				text: 'Reset',
				style: 'padding:0px 0px 10px 0px',	
				handler: function () {	 
					Ext.getCmp('branch_left').reset();
					Ext.getCmp('semester_left').reset();
					Ext.getCmp('division_left').reset();
					Ext.getCmp('division_right').reset();
					
							
					studentstore_left.removeAll();	
					studentstore_right.removeAll();	
					Ext.getCmp('branch_left').enable();
					Ext.getCmp('semester_left').enable();
					Ext.getCmp('division_left').enable();
					Ext.getCmp('division_right').enable();
					Ext.getCmp('branch_left').focus(true,500);
				}
			}]
		},{ 
			columnWidth: 1,
			layout: 'form',
			labelWidth:10,
			items:[{
				xtype: 'itemselector', 
				name: 'studentlist_left', 
				id:'studentlist_left', 
				fieldLabel: '', 
				multiselects:[{    
					name:'first', 
					id:'first', 
					width: 370, 
					height: 200, 
					store: studentstore_left, 
					displayField: 'text', 
					valueField: 'value',
					legend:''
                },{   
					width: 370, 
					height: 200, 
					store: studentstore_right, 
					displayField: 'text', 
					valueField: 'value', 
					legend:''
				}]
         	}]
		},{  
			columnWidth: 1,
			layout: 'form',
			labelWidth:10,
			items:[{
				xtype: 'itemselector', 
				name: 'studentlist_right', 
				id:'studentlist_right', 
				fieldLabel: '',
				hidden:true, 
				multiselects:[{  
					width: 225, 
					height: 200, 
					store: studentstore_right, 
					displayField: 'text', 
					valueField: 'value', 
					legend:''
				},{   
					width: 225, 
					height: 200, 
					store: studentstore_left, 
					displayField: 'text', 
					valueField: 'value', 
					legend:''
				}]
         	}]
		}]
    });

	studentstore_right.on({'afterload': {
		fn: function(store, options){ 
			if(studentstore_right.getCount() <= 0 && studentstore_left.getCount() <= 0){
				Ext.MessageBox.show({
					title: 'Error',
					msg: 'There is no Student Found in this Selection',
					buttons: Ext.MessageBox.OK,
					icon:Ext.MessageBox.ERROR
				});
			} 
		}
	}});

	var SemTransferWindow = new Ext.Window({
    	title:'Division Transfer',
		width:820,		
		height:460,	
		modal:true,	
		border: false,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: SemTransferForm,
		buttonAlign: 'center',
	    buttons:[{
			text: 'Save',
			handler:function(){  	
				Ext.getCmp('branch_left').enable();
				Ext.getCmp('semester_left').enable();
				Ext.getCmp('division_left').enable();
				Ext.getCmp('division_right').enable();
				
				
				SemTransferForm.getForm().submit({
					waitMsg: 'Saving...',
					success: function(resp)
					{
							Ext.MessageBox.show({
							title: 'Message',
							msg: 'Successfully saved',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.INFO
							});
					   		 SemTransferForm.getForm().reset();
							 studentstore_left.removeAll();	
							 studentstore_right.removeAll();
						
					},
					failure: function () 
					{
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Saving data failed!',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.ERROR
						});
						Ext.getCmp('branch_left').disable();
						Ext.getCmp('semester_left').disable();
						
					}
				});
			}
        },{
			text: 'Close',
			handler: function () {  
				SemTransferWindow.close();
			}
      	}]

	});
	var varWidth =  (screen.width - SemTransferWindow.width)/2;
	SemTransferWindow.setPosition(varWidth,((screen.height/4)-85));
	SemTransferWindow.show();
	Ext.getCmp('branch_left').focus(true,1000);
});
</script>

</div>
<?php require "_footer.php";?>
