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

var batchyearstore = new Ext.data.ArrayStore({
	fields: ['year_id', 'year_name'],
	data: Ext.semester.yearname
});

var semesterstore = new Ext.data.ArrayStore({
	fields: ['branch_id', 'semester_id', 'semester_name'],
	data: Ext.semester.semester
});

var yearstore = new Ext.data.ArrayStore({
	fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
	data: Ext.year.year
});

	  var branch = new Ext.form.ComboBox({
		name: 'branch_name',
		id: 'branch_name',
		hiddenName: 'branch_hidden',
		fieldLabel: 'Course <span class="comp"></span>',
		displayField: 'branch_name',
		valueField: 'branch_id',
		editable: true,
		triggerAction: 'all',
		tabIndex: 1,
		width: 163,
		mode: 'local',
		allowBlank: true,
		store: branchstore,
		listeners: {
			'select': function (cmb, rec, idx) {
				Ext.getCmp('semester_name').reset();
				Ext.getCmp('year_name').reset();
				Ext.getCmp('branchyear_name').reset();
				semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
				yearstore.filter('branch_id', cmb.getValue(), false, true, true);
				var semesterCombo = Ext.getCmp('semester_name');
				if (cmb.getValue() == 3 || cmb.getValue() == 5) {
					semesterCombo.enable();
				} else {
					semesterCombo.disable();
					semesterCombo.reset();
				}
			}
		}
	});

	var year = new Ext.form.ComboBox({
		name: 'year_name',
		id: 'year_name',
		hiddenName: 'year_hidden',
		fieldLabel: 'Batch Year <span class="comp"></span>',
		displayField: 'year_name',
		valueField: 'year_id',
		editable: true,
		triggerAction: 'all',
		tabIndex: 2,
		width: 163,
		mode: 'local',
		allowBlank: true,
		store: batchyearstore,
		listeners: {
			'select': function (cmb, rec, idx) {

			}
		}
	});


	var branchyear = new Ext.form.ComboBox({
		name: 'branchyear_name',
		id: 'branchyear_name',
		hiddenName: 'branchyear_hidden',
		fieldLabel: 'Course Year <span class="comp"></span>',
		displayField: 'branchyear_name',
		valueField: 'branchyear_id',
		editable: true,
		triggerAction: 'all',
		tabIndex: 3,
		width: 163,
		mode: 'local',
		allowBlank: true,
		store: yearstore,
		listeners: {
			'select': function (cmb, rec, idx) {
				var branch = Ext.getCmp('branch_name').getValue();
			}
		}
	});

	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: 4,
		width: 100,
		lastQuery: '',
		triggerAction: 'all',
		mode: 'local',
		allowBlank: true,
		store: semesterstore,
		listeners: {
			'select': function (cmb, rec, idx) {
				var branch = Ext.getCmp('branch_name').getValue();
			}
		}
	});
var studentstore = new Ext.data.JsonStore({
	url:'data/getstudent-message.php',
    root:'data',
	storeId:'student_id',
	totalProperty:'count',
	fields:[{
		name:'student_id',
		type:'int'
	},{
		name:'branch_name',
		type:'string'
	},
	{
		name:'year_name',
		type:'string'
	},
	{
		name:'branchyear_name',
		type:'string'
	},{
		name:'semester_name',
		type:'string'
	},{
		name:'message',
		type:'string'
	},{
		name:'student_name',
		type:'string'
	},{
		name:'student_gturegno',
		type:'int'
	},{
		name:'student_mobilestudents'
	},{
		name:'student_mobileparents'
	}] 
});

studentstore.on({'load':{fn: function(){
	if(studentstore.getCount() <= 0){
		Ext.MessageBox.show({
			title: 'Error',
			msg: 'There is no Data found for this Selection.',
			buttons: Ext.MessageBox.OK,
			icon:Ext.MessageBox.ERROR
		});
	} else{
		sm1.selectAll();
	}
}}});

var sm1 = new Ext.grid.CheckboxSelectionModel();

var colModel = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer(),
	{
		dataIndex: 'student_id',
		header: 'Student ID', 
		id: 'student_id',
		hidden: true
	},{
		dataIndex: 'branch_name',
		header: 'Branch',
		sortable:true,
		width:80
	},{
		dataIndex: 'student_gturegno',
		header: 'Enrollment No',
		sortable:true,
		width:100
	},{
		dataIndex: 'student_name',
		header: 'Student Name',
		sortable:true,
		width:145,
		id: 'SurName'  
	},{
		dataIndex: 'student_mobilestudents',
		header: 'Mobile Student',
		sortable:true,
		width:80
	},{
		dataIndex: 'student_mobileparents',
		header: 'Mobile Parents',
		sortable:true,
		width:80,
		id: 'student_mobileparents'  
	},sm1
]);

var grid_student = new Ext.grid.GridPanel({
	store: studentstore,
	cm: colModel,			
	loadMask: true,
	enableColLock:false,
	height: 275,    
	width: 560,
	sm: sm1,
	clicksToEdit: 1, 
	layout: 'fit',
	columnLines:true
});

Ext.override(Ext.Editor,{
	onBlur: function(){
    	if(this.allowBlur === true && this.editing) {
        	this.completeEdit();
        }
    }
});

var sendto = new Ext.form.Label({
	name: 'sendto',
	id:'sendto',
	cls:'x-form-item-label',
	text:"Send To:" 
});

var checkbox_parents = new Ext.form.Checkbox({	
	fieldLabel:' Parent',
	name: 'checkbox_parents',
	id: 'checkbox_parents',
	inputValue: 'parents'
});

var checkbox_students = new Ext.form.Checkbox({	
	fieldLabel:'Student',
	name: 'checkbox_students',
	id: 'checkbox_students',
	inputValue: 'students'
});

var SMSForm = new Ext.FormPanel({
	labelWidth: 60,
	id:'SMSForm',
	frame:true,
	standardSubmit:false,
	title: '',
	bodyStyle:'padding:5px',
	layout:'column',
	items :[{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:1,
		items:[branch]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:1,
		items:[year]
	},
	{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:1,
		items:[branchyear]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:1,
		items:[semester]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.14,
		items:[sendto]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.15,
		labelWidth: 40,
		items:[checkbox_parents]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.4,
		labelWidth: 45,
		items:[checkbox_students]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.12,
		items:[{
			xtype:'button',
			width:50,
			text:'View',
			handler: function(b,e){	
				var branch_id = Ext.getCmp('branch_name').getValue();
				var year_id = Ext.getCmp('year_name').getValue();
				var branchyear_id = Ext.getCmp('branchyear_name').getValue();
				var semester_id = Ext.getCmp('semester_name').getValue();
			
				if (branch_id != '' && year_id!='' &&semester_id != '') {
					studentstore.load({
						params: {
							branch_id: branch_id,
							year_id: year_id,
							semester_id: semester_id,
							
						}
					});

				}
				else if (branch_id != '' && year_id!='' &&branchyear_id != '') {
					studentstore.load({
						params: {
							branch_id: branch_id,
							year_id: year_id,
							branchyear_id: branchyear_id,
							
						}
					});

				}
				else {
					alert("Select branch first!");
					studentstore.removeAll();
				}
				
			}
		}]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.15,
		items:[{
			xtype:'button',
			width:50,
			text:'Reset',
			handler: function(b,e){	
				SMSForm.getForm().reset();	
				Ext.getCmp('branch_name').focus(true,500);
			}
		}]
	},{
		xtype:'container',
		autoEl:{},
		style: 'padding:5px 0px 0px 0px',
		layout:'form',
		columnWidth:1,
		items:[grid_student]
	}]
});

var StudentContactWindow = new Ext.Window({
	title:'Import Student Contact',
	height:450,
	width:600,
	modal:true,
	layout:'fit',
	border:false,
	closeAction:'hide',
	collapsible:true,
	items:SMSForm,
	buttonAlign:'center',
	buttons: [{
		text:'Import Numbers',
		handler: function(b, e){	
			if(grid_student.store.getCount() > 0){
				var selections = grid_student.selModel.getSelections();
				var message = "";
				var i;				
				var stud_mobile,pare_mobile;
				for(i = 0; i< grid_student.selModel.getCount(); i++){
					stud_mobile = selections[i].json.student_mobilestudents;
					pare_mobile = selections[i].json.student_mobileparents;
					if(Ext.getCmp('checkbox_students').checked == true && stud_mobile != ""){
						message = message + stud_mobile + ",";
					}
					
					if(Ext.getCmp('checkbox_parents').checked == true && pare_mobile != ""){
						message = message + pare_mobile + ",";
					}
				}
				message = message.substr(0,message.length - 1);
				var mobile_number = Ext.getCmp('mobile_number').getValue();
				if(mobile_number != ""){
					Ext.getCmp('mobile_number').setValue(mobile_number+","+message);
				} else{
					Ext.getCmp('mobile_number').setValue(message);
				}
				StudentContactWindow.hide();		
			} else{
				Ext.MessageBox.show({
					title:'Message',
					msg: 'Please select atlist one person to send message.',
					buttons: Ext.MessageBox.OK,
					icon:Ext.MessageBox.INFO
				});
			}
		}
	},{
		text: 'Reset', 
		handler: function () {   
			SMSForm.getForm().reset();
			studentstore.removeAll();
			Ext.getCmp('branch_name').focus(true,500);
		}
	}]
});
var varWidth =  (screen.width - StudentContactWindow.width)/2;
StudentContactWindow.setPosition(varWidth,((screen.height/4)-85));	
