SD.teacherTools.lessonPlans.addPopup('masterPlans', new Ext.Window({
	title: 'Manage Master Plans',
	width: 460,
	height: 380,
	modal: true,
	plain: false,
	closeAction: 'hide',
	bodyStyle: 'background-color: white;',
	id: 'manageMasterPlan',
	items: [{
		xtype: 'tabpanel',
		id: 'masterPlansTabPanel',
		activeTab: 0,
		border: false,
		style: '',
		items: [{
			xtype: 'form',
			monitorValid: true,
			id: 'masterPlanForm',
			title: 'Create Master Plan',
			autoHeight: true,
			items: [{
				labelWidth: 100,
				labelAlign: 'left',
				layout: 'form',
				border: false,
				titleCollapse: true,
				bodyStyle: 'background-color: #D5E1F0;',
				padding: 15,
				id: 'masterPlanHeader',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Title',
					allowBlank: false,
					anchor: '100%',
					id: 'masterPlanTitle',
					name: 'title'
				},
				new SchoolDynamics.coursesCombo({
					id: 'masterPlanCourse',
					allowBlank: false,
					hiddenName: 'courseid'
				})]
			},
			{
				labelWidth: 100,
				labelAlign: 'left',
				layout: 'form',
				border: false,
				titleCollapse: true,
				height: 210,
				items: [{
					xtype: 'textarea',
					id: 'masterPlanBody',
					anchor: '100% 100%',
					allowBlank: false,
					hideLabel: true,
					name: 'plan'
				},
				{
					xtype: 'textfield',
					name: 'id',
					id: 'masterPlanID'
				}]
			}],
			listeners: {
				activate: function(tab) {
					if (SchoolDynamics.currentCourseID != undefined && SchoolDynamics.currentCourseID != '') {
						Ext.getCmp('masterPlanCourse').setValue(SchoolDynamics.currentCourseID);
					}
				},
				clientvalidation: function(form, valid) {
					if (valid) {
						Ext.getCmp('lp_master_save').enable();
					}
					else {
						Ext.getCmp('lp_master_save').disable();
					}
				}
			}
		},
		{
			xtype: 'panel',
			title: 'Saved Master Plans',
			autoHeight: true,
			border: false,
			tbar: {
				xtype: 'toolbar',
				items: [{
					xtype: 'button',
					text: 'Edit master plan',
					iconCls: 'blueprint_edit',
					style: 'margin-left: 5px;',
					handler: function() {
						mySavedPlansGrid = this.ownerCt.ownerCt.items.items[0];
						selection = mySavedPlansGrid.getSelectionModel().getSelected();
						Ext.getCmp('masterPlanBody').setValue(selection.data.plan);
						Ext.getCmp('masterPlanCourse').setValue(selection.data.courseid);
						Ext.getCmp('masterPlanTitle').setValue(selection.data.title);
						Ext.getCmp('masterPlanID').setValue(selection.data.id);
						Ext.getCmp('masterPlansTabPanel').setActiveTab(0);
						mySavedPlansGrid.getSelectionModel().clearSelections();
					}
				},
				{
					xtype: 'button',
					text: 'Delete',
					iconCls: 'delete',
					style: 'margin-left: 5px;',
					handler: function() {
						mySavedPlansGrid = this.ownerCt.ownerCt.items.items[0];
						selections = mySavedPlansGrid.getSelectionModel().getSelections();
						mySavedPlansGrid.store.remove(selections);
						ids = [];
						for (i = 0; i < selections.length; i++) {
							ids.push(selections[i].data.id)
						}
						Ext.Ajax.request({
							url: 'teacher_tools/lessonplans_data.json.php',
							params: {
								command: 'masterPlan',
								xaction: 'destroy',
								ids: Ext.encode(ids)
							}
						});
						Ext.getCmp('masterplanList').store.load();
					}
				}]
			},
			items: [
			new lessonplansManageSavedGrid()],
			listeners: {
				activate: function() {
					Ext.getCmp('lp_master_save').disable();
					Ext.getCmp('savedPlansGrid').store.load();
				}
			}
		}]
	}],
	clearAll: function() {
		Ext.getCmp('masterPlanBody').setValue('');
		Ext.getCmp('masterPlanCourse').clearValue();
		Ext.getCmp('masterPlanTitle').setValue('');
		Ext.getCmp('masterPlanID').setValue('');
	},
	fbar: {
		xtype: 'toolbar',
		items: [{
			xtype: 'button',
			text: 'Save',
			id: 'lp_master_save',
			handler: function() {
				id = Ext.getCmp('masterPlanID').getValue();
				Ext.getCmp('masterPlanForm').getForm().submit({
					url: 'teacher_tools/lessonplans_data.json.php',
					params: {
						command: 'masterPlan',
						xaction: ((id == '') ? 'create' : 'update')
					},
					success: function(form, action) {
						Ext.getCmp('manageMasterPlan').clearAll();
						Ext.getCmp('masterplanList').store.load();
						Ext.Msg.alert('Success', "Master plan saved successfully");
					},
					failure: function(form, action) {}
				});
			}
		},
		{
			xtype: 'button',
			text: 'Close',
			handler: function() {
				this.ownerCt.ownerCt.hide();
			}
		}]
	},
	listeners: {
		hide: function() {
			this.clearAll();
		}
	}
})
);

