var calendarSchedulePopup = new Ext.Window({
    height: 590, 
    width: 620,
    title: 'Calendar Event',
    modal: true,
    layout: 'fit',
    id: 'calendarCatPopup',
    closeAction: 'hide', 
    tools: [
        {
            id:'help',
            handler: function(btn){
                helppopup.loadHelpFile("Calendar Event", 500, 455);
            }  
        }
    ],
    items:[
    {
		xtype: 'form',
		border: false,
		layout: 'fit',
		items: [
        {
            xtype: 'tabpanel',
            deferredRender: false,
            border: false,
            id: 'schedEventTab',
            defaults: { 
                padding: 5
            },
            layoutOnTabChange: true,
            activeTab: 0,
            items: [
                /****** Scheduled Event tab ******/
                {
                    xtype: 'panel',
                    title: 'Schedule Event',
                    layout: 'hbox',
                    items: [
                        {
                            layout: 'form',
                            border: false,
                            flex: 1,
                            margins: '0 8 0 0',
                            labelWidth: 80,
                            height: 475, 
                            defaults: {
                                anchor: '100%'
                            },
                            items: [
                                {
										xtype: 'textfield',
										hidden: true,
										id: 'schedIDField',
										name: 'itemID',
										itemCls: 'displayNone'
										
								},   
                                {
										xtype: 'textfield',
										hidden: true,
										id: 'schedHiddenShowAs',
										name: 'hiddenShowAs',
										itemCls: 'displayNone'
										
								},   								
                                {
										xtype: 'textfield',
										hidden: true,
										id: 'schedCatID',
										submitValue: false,
										itemCls: 'displayNone'
										
								},   								                         
                                {
                                    xtype: 'textfield',
                                    fieldLabel: 'Title',
                                    name: 'title',
                                    allowBlank: false
                                },
                                new SchoolDynamics.locationCombo({hiddenName: 'location', fieldLabel: 'Location', forceSelection: false,
                                listeners: {
									select: function(combo, rec, index) {
										this.nextSibling().setValue(rec.data.id);
									},
									change: function(combo, newValue, oldValue){ 
										var hidden = this.nextSibling(),
											record = this.store.getAt(this.store.find('id', hidden.getValue()));
										if (!record || record.get('location') != newValue) {
											record = this.store.getAt(this.store.find('location', newValue));
											hidden.setValue(record ? record.get('id') : '');
										}
									}
								}
                                
                                
                                
                                }),
                                {
										xtype: 'textfield',
										hidden: true,
										name: 'locationID',
										itemCls: 'displayNone'
										
								},
                                {
                                    xtype: 'dollarfield',
                                    fieldLabel: 'Cost',
                                    name: 'cost',
                                    anchor: '60%'
                                },
                                {

                                        
                                            xtype: 'container',
                                            flex: 1,
                                            layout: 'form',
                                            items: [
                                                {
                                                    xtype: 'checkbox',
                                                    boxLabel: 'All-day event',
                                                    name: 'allDayEvent',
                                                    listeners: {
                                                        check : function(cbox, checked){
                                                            fromTime = Ext.getCmp('calEventFromTime');
                                                            fromAt = Ext.getCmp('calEventFromAt');
                                                            toComposite = Ext.getCmp('calEventToComposite');
                                                            //toAt = Ext.getCmp('calEventToAt');
                                                            if (checked){
                                                                fromTime.hide();
                                                                fromAt.hide();
                                                                toComposite.hide();
                                                                //toAt.hide();
                                                            } else {
                                                                fromTime.show();
                                                                fromAt.show();
                                                                toComposite.show();
                                                                //toAt.show();
                                                            }
                                                        }
                                                    }
                                                }
                                           ]
                                        
                                    
                                },
                                {
                                    xtype: 'compositefield',
                                    fieldLabel: 'From',
                                    items: [
                                        {
                                            xtype: 'datefield',
                                            flex: 1,
                                            fieldLabel: 'From',
                                            id: 'calEventFromDate',
                                            name: 'fromDate',
                                            allowBlank: 'false'
                                        },
                                        {
                                            xtype: 'label',
                                            flex: 1,
                                            width: 15,
                                            id: 'calEventFromAt'
                                        },
                                        {
                                            xtype: 'timespinner',
                                            flex: 1,
                                            id: 'calEventFromTime',
                                            name: 'fromTime'                                            
                                        }
                                    ]
                                },
                                {
                                    xtype: 'compositefield',
                                    fieldLabel: 'To',
                                    id: 'calEventToComposite',
                                    items: [
                                        {
                                            xtype: 'timespinner',
                                            flex: 1,
                                            id: 'calEventToTime',
                                            name: 'toTime'
                                        }
                                    ]
                                },
                                {
                                    xtype: 'fieldset',
                                    collapsible: true,
                                    collapsed: true,
                                    title: 'Additional Times',
                                    id: 'additionalTimes',
                                    labelWidth: 90,
                                    //animCollapse: true, /* normally cool but some weirdness with composite fields */
                                    items: [
                                        {
                                            xtype: 'compositefield',
                                            fieldLabel: 'Early departure',
                                            items: [
                                                {
                                                    xtype: 'timefield',
                                                    flex: 1,
                                                    width: 104,
                                                    name: 'departTime'
                                                }
                                            ]
                                        },
                                        {
                                            xtype: 'compositefield',
                                            fieldLabel: 'Set up',
                                            items: [
                                                {
                                                    xtype: 'timefield',
                                                    flex: 1,
                                                    width: 104,
                                                    name: 'setupTime'
                                                },
                                                {
                                                    xtype: 'displayfield',
                                                    flex: 1,
                                                    value: 'for',
                                                    width: 15
                                                },
                                                {
                                                    xtype: 'textfield',
                                                    flex: 1,
                                                    width: 40,
                                                    name: 'setupHours'
                                                },
                                                {
                                                    xtype: 'displayfield',
                                                    flex: 1,
                                                    value: 'hours',
                                                    width: 35
                                                }
                                            ]
                                        },
                                        {
                                            xtype: 'compositefield',
                                            fieldLabel: 'Clean up',
                                            items: [
                                                {
                                                    xtype: 'timefield',
                                                    flex: 1,
                                                    width: 104,
                                                    name: 'cleanupTime'
                                                },
                                                {
                                                    xtype: 'displayfield',
                                                    flex: 1,
                                                    value: 'for',
                                                    width: 15
                                                },
                                                {
                                                    xtype: 'textfield',
                                                    flex: 1,
                                                    width: 40,
                                                    name: 'cleanupHours'
                                                },
                                                {
                                                    xtype: 'displayfield',
                                                    flex: 1,
                                                    value: 'hours',
                                                    width: 35
                                                }
                                            ]
                                        }
                                    ]
                                },
								{
									xtype: 'combo',
									mode: 'local',
									typeAhead: true,
									triggerAction: 'all',
									store: new Ext.data.ArrayStore({
										fields: ['type'],
										data: [['Public'],['Private'],['Personal']]
									}),
									displayField: 'type',
									fieldLabel: 'Show as',
									id: 'schedShowAs',
									flex: 1,
									name: 'showAs',
									doChange: function(newValue) {
										schedEvent = Ext.getCmp('calendarCatPopup');
										fieldsDontTouch = ['hiddenShowAs', 'itemID', 'title', 'location', 'showAs', 'fromDate', 'fromTime', 'toTime', 'reminderTime', 'rdDateEndOn', 'rwEndDate', 'rmEndDate', 'rmNumDay', 'rmNumWeek'];
										if (newValue == "Personal") {
											Ext.each(schedEvent.items.items[0].getForm().getExtFields(), function(field) {
												name = (field.hiddenName === undefined) ? field.name : field.hiddenName;
												if (name !== undefined && fieldsDontTouch.indexOf(name) == -1) {		//field is not in array
													field.disable();
												}
											});	
											schedEvent.items.items[0].items.items[0].hideTabStripItem(1);
											schedEvent.items.items[0].items.items[0].hideTabStripItem(2);
											Ext.getCmp('schedEventCats').items.items[0].disable();
											Ext.getCmp('schedDivision').allowBlank = true;
										} else {
											Ext.each(schedEvent.items.items[0].getForm().getExtFields(), function(field) {
												name = (field.hiddenName === undefined) ? field.name : field.hiddenName;
												if (name !== undefined && fieldsDontTouch.indexOf(name) == -1) {		//field is not in array
													field.enable();
												}
											});
										schedEvent.items.items[0].items.items[0].unhideTabStripItem(1);
										schedEvent.items.items[0].items.items[0].unhideTabStripItem(2);					
										Ext.getCmp('schedEventCats').items.items[0].enable();		
										Ext.getCmp('schedDivision').allowBlank = false;				
										}										
									},
									listeners: {
										
										select: function(combo, record, index){
											this.doChange(record.data.type);
										},
										change: function(combo, newValue, oldValue){
											this.doChange(newValue);
										}
									}                
								},                                
                                {
                                    xtype: 'compositefield',
                                    fieldLabel: 'Reminder',
                                    items: [
                                        {
                                            xtype: 'datefield',
                                            flex: 1,
                                            name: 'reminderDate'
                                        },
                                        {
                                            xtype: 'displayfield',
                                            flex: 1,
                                            value: '@',
                                            width: 15
                                        },
                                        {
                                            xtype: 'timefield',
                                            flex: 1,
                                            name: 'reminderTime'
                                        }
                                    ]
                                },
                                {
                                    xtype: 'textarea',
                                    fieldLabel: 'Description',
                                    name: 'description'
                                },
                                {
                                    xtype: 'textarea',
                                    fieldLabel: 'Special instructions',
                                    name: 'instructions'
                                }
                            ]
                        },
                        {
                            xtype: 'container',
                            flex: 1,
                            width: 240,
                            margins: '0 0 0 8',
                            layout: 'form',
                            border: false,
                            items: [
                                {
                                    xtype: 'panel',
                                    flex: 1,
                                    style: 'margin-bottom: 10px;',
                                    height: 270,
                                    autoScroll: true,
                                    title: 'Categories',
                                    layout: 'fit',
                                    id: 'schedEventCats',
                                    items: [
                                        new calendarCategoryGrid([0,1], false, 'schedEventCategories')
                                    ]
                                },
                                {
                                    xtype: 'panel',
                                    title: 'Audience',
                                    layout: 'form',
                                    //labelAlign: 'top',
                                    padding: 5,
                                    frame: true,
                                    items: [
                                        {
                                            xtype: 'container',
                                            layout: 'form',
                                            labelAlign: 'top',
                                            items: [
												new SchoolDynamics.divisionCombo({flex: 1, hiddenName: 'division', fieldLabel: 'Division', allowBlank: false, id: 'schedDivision'}),                                          
                                                {
                                                    xtype: 'compositefield',
                                                    fieldLabel: 'Home room range',
                                                    items: [
                                                        new SchoolDynamics.homeroomCombo({flex: 1, hiddenName: 'hrRangeFrom'}),
                                                        new SchoolDynamics.homeroomCombo({flex: 1, hiddenName: 'hrRangeTo'})
                                                    ]
                                                }
                                            ]
                                        },
                                        {
                                            xtype: 'panel',
                                            layout: 'form',
                                            labelAlign: 'left',
                                            labelWidth: 1,
                                            items: [
                                                {
                                                    xtype: 'checkbox',
                                                    boxLabel: 'Show in infodirect calendar',
                                                    name: 'showInID'
                                                },                                                   
                                                {
                                                    xtype: 'checkbox',
                                                    boxLabel: 'Create an InfoDirect news item',
                                                    name: 'createNewsItem'
                                                }                                        
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
          /****** Repeat tab ******/
          {
            title: 'Repeat',
            bodyStyle: 'padding: 10px',
            items: [
              {
                xtype:'fieldset',
                title: 'Repeat event',
                cls: 'my-fieldset',
                items: [
                  {
					layout: 'form',
					border: false,
					items: [
					{
					
							id:'repeatForm',
							layout: 'card',
							activeItem: 0,
							border: false,
							defaults: {autoHeight: true},
							cls: 'font12',
							items:[ 
							  {
								border: false,
								layout: 'form',
								items:[
								  {
									xtype: 'compositefield',
									border: false,
									hideLabel: true,
									items: [
									  {
										xtype: 'label',
										text:'Every',
										style: {
										  paddingRight: 10
										}
									  },
									  {
										xtype: 'textfield',
										width: 50,
										name: 'numDays'
									  },
									  {
										xtype: 'label',
										text: 'day(s)',
										style: {
										  paddingLeft: 10
										}                            
									  }
									]
								  },
								  {
									xtype: 'compositefield',
									border: false,
									hideLabel: true,
									style: {
									  marginTop: '5px'
									},                            
									items: [
									  {
										xtype: 'checkbox',
										boxLabel: 'End on',
										name: 'rdEndOn',
										cls: 'font12',
										listeners: {
										  check : function(checkbox,checked){
											endOnDate = this.ownerCt.items.items[1];
											if (checked){
											  endOnDate.enable();
											} else {
											  endOnDate.disable();
											}        
										   }
										}                                
									  },
									  {
										xtype: 'datefield',
										disabled: true,
										name: 'rdDateEndOn',
										width: 100
									  }
									]
								  }
								]
							  },
							  {
								border: false,
								items:[
								  {
									xtype: 'label',
									text:'Every',
									style: {
									  paddingRight: 10
									}
								  },
								  {
									xtype: 'numberfield',
									name: 'numWeeks',
									width: 50
								  },
								  {
									xtype: 'label',
									text: 'week(s)',
									style: {
									  paddingLeft: 10
									}                            
								  },
								  {
									xtype:'panel',
									border: false,
									style: {
										marginTop: 10
									},
									defaults: {
									  xtype:'button',
									  enableToggle: true,
									  width: 30,
									  height: 30,
									  cls: 'floatLeft',
									  toggleHandler: function(btn, state){
										rwDay = Ext.getCmp('rwDay');
										if (state) {
											rwDay.setValue(rwDay.getValue() + "_" + btn.value + "_");
										} else {
											rwDay.setValue(rwDay.getValue().replace(new RegExp('_' + btn.value + '_','ig'),""));
										}
									  }
									},
									items: [
									  {text: 'S', value: '0'},{text: 'M', value: '1'},{text: 'T', value: '2'},{text: 'W', value: '3'},{text: 'T', value: '4'},{text: 'F', value: '5'},{text: 'S', value: '6'}
									]
								  },
								  {
										id: 'rwDay',
										xtype: 'textfield',
										name: 'rwDay',
										hidden: true
								  },
								  {
									xtype: 'compositefield',
									border: false,
									style: {
									  marginTop: '10px'
									},
									hideLabel: true,
									items: [                            
									  {
										xtype: 'checkbox',
										boxLabel: 'End on',
										name: 'rwEndOn',								
										listeners: {
										  check : function(checkbox,checked){
											endOnDate = this.ownerCt.items.items[1];
											if (checked){
											  endOnDate.enable();
											} else {
											  endOnDate.disable();
											}        
										   }
										}                                  
									  },
									  {
										xtype: 'datefield',
										disabled: true,
										name: 'rwEndDate',
										width: 100										
									  }
									]
								  }
								]
							  },
							  {
								border: false,
								items:[
								  {
									border: false,
									xtype: 'compositefield',
									hideLabel: true,
									defaults: {cls: 'font12'},
									items:[
									  {
										xtype: 'label',
										text: 'Every'          
									  },                            
									  {
										xtype: 'textfield',
										width: 50,
										name: 'numMonths'
									  },
									  {
										xtype: 'label',
										text: 'month(s)'                    
									  }
									]
								  },
								  {
									html:'<br/>',
									border: false
								  },
								  {
									xtype: 'radio',
									itemCls: 'displayBlock',
									boxLabel: 'Each',
									hideLabel: true,
									name: 'rmRepeatType',
									inputValue: 'each',
									listeners: {
									  check : function(radio,checked){
										dayNumPanel = Ext.getCmp('calPopupDayNum');
										if (checked){
										  dayNumPanel.items.each(function(el){el.enable();});
										} else {
										  dayNumPanel.items.each(function(el){el.disable();});
										}        
									   }
									}
								  },                        
								  {
									xtype:'panel',
									id: 'calPopupDayNum',
									border: false,
									width: 210,
									cls: 'floatLeft',
									defaults: {
									  xtype:'button',
									  enableToggle: true,
									  width: 30,
									  height: 30,
									  cls: 'floatLeft',
									  disabled: true,
									  toggleHandler: function(btn, state) {
										  rmDays = Ext.getCmp('rmDays');
										if (state) {
											rmDays.setValue(rmDays.getValue() + "_" + btn.text + "_");
										} else {
											rmDays.setValue(rmDays.getValue().replace(new RegExp('_' + btn.text + '_','ig'),""));
										}										  
									  }
									},
									listeners: {
									  render: function(){
										for (i=1; i <= 31; i++){
										  this.add({text:i})
										}
									  }
									}
								  },
								  {
									id: 'rmDays',
									xtype: 'textfield',
									name: 'rmDays',
									hidden: true									  
								  },
								  {
									  xtype: 'compositefield',
									  hideLabel: true,
									  cls: 'clearLeft',
									  style: {
										 width: "100%",
										 paddingTop: 10,
										 paddingBottom: 10 
									  },
									  items: [
									  {
										xtype: 'radio',
										boxLabel: 'On the',
										name: 'rmRepeatType',
										hideLabel: true,
										inputValue: 'on',
										listeners: {
										  check : function(radio,checked){
											if (checked){
											  this.ownerCt.items.each(function(el){el.enable()});
											} else {
											  this.ownerCt.items.each(function(el){if (el.xtype != 'radio') { el.disable() }});
											}
											
										  }
										}                            
									  },
									  {
										xtype: 'combo',
										width: 100,
										disabled: true,
										hiddenName: 'rmNumWeek',
										mode: 'local',
										typeAhead: false,
										triggerAction: 'all',
										store: new Ext.data.ArrayStore({
											fields: ['num','text'],
											data: [[1, 'First'],[2, 'Second'],[3, 'Third'],[4, 'Fourth'],[5, 'Last']]
										}),
										displayField: 'text',
										valueField: 'num'										
									  },
									  {
										xtype: 'combo',
										width: 100,
										disabled: true,
										flex: 1,
										style: {
										  marginLeft: 5
										},
										hiddenName: 'rmNumDay',
										mode: 'local',
										typeAhead: false,
										triggerAction: 'all',
										store: new Ext.data.ArrayStore({
											fields: ['num','text'],
											data: [[0, 'Sunday'],[1, 'Monday'],[2, 'Tuesday'],[3, 'Wednesday'],[4, 'Thursday'],[5, 'Friday'],[6, 'Saturday']]
										}),
										displayField: 'text',
										valueField: 'num'	
										                 
									  }

									] 
								  },
								  {
									xtype: 'compositefield',
									border: false,
									hideLabel: true,
									items:[
									  {
										xtype: 'checkbox',
										boxLabel: 'End on',
										name: 'rmEndOn',								
										hideLabel: true,
										listeners: {
										  check : function(checkbox,checked){
											endOnDate = this.ownerCt.items.items[1];
											if (checked){
											  endOnDate.enable();
											} else {
											  endOnDate.disable();
											}        
										   }
										}                                
									  },
									  {
										xtype: 'datefield',
										disabled: true,
										name: 'rmEndDate',
										width: 100										
									  }
									]
								  }
								]
							  }
							  /*** Implement yearly backend in phase 2 ***/
							  /*** Working code, do not delete ***/
							  /*{        
								border: false,
								items:[
								  {
									border: false,
									xtype: 'compositefield',
									hideLabel: true,
									defaults: {cls: 'font12'},
									items:[
									  {
										xtype: 'label',
										text: 'Every',
										style: {
										  marginRight: 10
										}                         
									  },                            
									  {
										xtype: 'textfield',
										width: 50,
										name: 'numYears'
									  },
									  {
										xtype: 'label',
										text: 'year(s) in:',
										style: {
										  paddingLeft: 10
										}                            
									  }
									]
								  },
								  {
									html:'<br/>',
									border: false
								  },                       
								  {
									xtype:'panel',
									id: 'calPopupMonths',
									border: false,
									width: 120,
									defaults: {
									  xtype:'button',
									  enableToggle: true,
									  width: 30,
									  height: 30,
									  toggleGroup: 'yearToggle',
									  cls: 'floatLeft'
									},
									items: [
									  {text:'Jan'},{text:'Feb'},{text:'Mar'},{text:'Apr'},{text:'May'},{text:'Jun'},{text:'Jul'},{text:'Aug'},{text:'Sep'},{text:'Oct'},{text:'Nov'},{text:'Dec'}
									
									]
								  },
								  {
									html:'<br/>',
									border: false
								  },
								  {
									xtype: 'checkbox',
									boxLabel: 'On the',
									name: 'ryEndOn',
									hideLabel: true,
									inputValue: 'on',
									listeners: {
									  check : function(radio,checked){
										yearlyDayOfMonth = Ext.getCmp('yearlyDayOfMonth');
										if (checked){
										  yearlyDayOfMonth.items.each(function(el){el.enable()});
										} else {
										  yearlyDayOfMonth.items.each(function(el){el.disable()});
										}        
									   }
									}
									
																							
								  },
								  {
									xtype: 'compositefield',
									id: 'yearlyDayOfMonth',
									border: false,
									hideLabel: true,
									defaults: {disabled: true},
									items: [                          
									  {
										xtype: 'combo',
										width: 100
									  },
									  {
										xtype: 'combo',
										width: 100,
										style: {
										  marginLeft: 5
										}                           
									  }
									]
								  },                       
								  {
									xtype: 'compositefield',
									border: false,
									hideLabel: true,
									style: {
										marginTop: 10
									},
									items:[
									  {
										xtype: 'checkbox',
										boxLabel: 'End on',
										hideLabel: true,
										listeners: {
										  check : function(checkbox,checked){
											endOnDate = this.ownerCt.items.items[1];
											if (checked){
											  endOnDate.enable();
											} else {
											  endOnDate.disable();
											}        
										   }
										}                                
									  },
									  {
										xtype: 'datefield',
										disabled: true
									  }
									]
								  }
								]
							  }*/                                                            
							]
						}]
					}
                ],
                listeners: {
                	render: function(c){
                		var combo = new Ext.form.ComboBox({
                			store: ['Daily', 'Weekly', 'Monthly'],
                			triggerAction: 'all',
                			name:'repeatType',
                			editable: false,
                			width: 80,
                			value: 'Daily',
                			listeners: {
                			   select : function(combo, record, index ) {
								  form = Ext.getCmp('repeatForm');
								  formLayout = form.getLayout();
								  formLayout.setActiveItem(index);
                			   }
                			}
                		});
                		combo.render(c.header, 1);
                		c.header.child('div').setWidth(80)
                		c.on('destroy', function(){
                			combo.destroy();
                		}, c, {single: true});
                	}
                }
              }
            ]
          },
          
          
          /****** Transportation tab ******/
          {
            title: 'Transportation',
           /* tbar: [
              {
                xtype: 'button',
                text: 'Print transportation request',
                iconCls: 'print'
              }
            ],  */
            items: [
              {
                xtype: 'fieldset',
                id: 'transpFieldset',
                checkboxToggle:true,
                checkboxName: 'needTransp',
                collapsed: true,
                title: 'Request transportation for this event',
                items: [
                  {
                    layout: 'form',
                    border: false,
                    items: [
                      {
                        border: false,
                        layout: 'form',
                        labelWidth: 200,
                        items:[
                          {
                            xtype: 'numberfield',
                            width: 50,
                            fieldLabel: 'Number of students to transport',
                            name: 'numStudents'
                          }
                        ]
                      },
                      {
                        xtype: 'checkboxgroup',
                        fieldLabel: 'Other needs',
                        columns: 1,
                        items: [
                          {
                            boxLabel: 'Bus driver',
                            name: 'needBusDriver',
                            id: 'needBusDriver'
                          },
                          {
                            boxLabel: 'Mobile phone',
                            name: 'needPhone'
                          },
                          {
                            boxLabel: 'Keys',
                            name: 'needKeys'
                          },
                          {
                            boxLabel: 'Credit card',
                            name: 'needCreditCard'
                          }
                        ]
                      },
                      {
                        xtype: 'textarea',
                        fieldLabel: 'Note',
                        anchor: '100%',
                        name: 'transpNote'
                      }
                    ]
                  } 
                ]
              }
            ]
          }                    
        ]           
     }]
	}
   ],   
   buttons: [
   {
      text: 'Submit',
      handler: function(btn){
		selectedCat = Ext.getCmp('schedEventCats').items.items[0].getSelectionModel().getSelected();
		catID = (selectedCat !== undefined) ? selectedCat.data.catid : '';
		schedEventPopup = Ext.getCmp('calendarCatPopup');
		schedEventForm = schedEventPopup.items.items[0].getForm();
		if (schedEventForm.isValid()) {
			if (catID != '' || Ext.getCmp('schedShowAs').getValue() == 'Personal') {
				
				if (schedEventPopup.fromIsOnRepeat()) {
					Ext.Msg.show({ 
					   title:'', 
					   msg: "The fromDate does not fall on a repeat day, would you like to add an event on the fromDate as well?", 
					   buttons: Ext.Msg.YESNO, 
					   icon: Ext.MessageBox.WARNING,
					   fn: function(btn) {
						  ignoreFrom = (btn == 'no');
						  schedEventPopup.submitEvent(catID,ignoreFrom);
					   }
					}); 
				} else {			
					schedEventPopup.submitEvent(catID, false);
				}
				
			} else {
				Ext.Msg.alert('ERROR','Failure: Event failed to be submitted.  Please select a category');
			}
		} else {
			Ext.Msg.alert('ERROR','Failure: Event failed to be submitted.  Please fill in all required fields');
		}
      }  
   },   
   {
      text: 'Close',
      handler: function(btn){
         this.ownerCt.ownerCt.hide();
      }  
   }],
   fromIsOnRepeat: function(){
		onRepeat = false;
		values = Ext.getCmp('calendarCatPopup').items.items[0].getForm().getValues();
		fromDate = new Date(values.fromDate);		
		
		if (values.repeatType == 'Weekly'  && values.numWeeks != '' && values.numWeeks != '0') {
			fromDOW = fromDate.format('w');
			if (values.rwDay.indexOf("_" + fromDOW + "_") == -1){
				onRepeat = true;
			}
		} else if (values.repeatType == 'Monthly' && values.numMonths != '' && values.numMonths != '0') {
			if (values.rmRepeatType == 'each') {
				fromDay = fromDate.format('d');
				if (values.rmDays.indexOf("_" + fromDay + "_") == -1){
					onRepeat = true;
				}
			} else if (values.rmRepeatType == 'on' && values.numMonths != '' && values.numMonths != '0'){
				fullWeeksInMonth = 4;
				daysInWeek = 7;
				weekLater = 1 + daysInWeek;  		//The same day as the first day in the second week (eg the first day is Wednesday so the second Wednesday of the month)
				
				dt = new Date(fromDate.format('m/1/Y'));   //set to the very first day of the month
				
				numFirstDay = dt.format('w');
				
				if (values.rmNumDay < numFirstDay) {
					firstOfChosenDay = weekLater - (numFirstDay - values.rmNumDay);
				} else {
					firstOfChosenDay = 1 + (values.rmNumDay - numFirstDay);  //1 is for the first day of the month
				}
				
				if (values.rmNumWeek <= fullWeeksInMonth) {
					numDay = firstOfChosenDay + daysInWeek * (values.rmNumWeek - 1);
					if (numDay != fromDate.format('d')){
						onRepeat = true;
					}			
				} else {  //get last instance of day
					numMonth = dt.format('m');
					dt.setFullYear(dt.format('Y'), dt.format('m'), firstOfChosenDay + (daysInWeek * (values.rmNumWeek - 1)));
					if (dt.format('m') != numMonth) {	//The month changed so it was not the last day
						dt.setFullYear(dt.format('Y'), dt.format('m'),firstOfChosenDay + (daysInWeek * (values.rmNumWeek - 2)));
					}
					if (dt.format('d') != fromDate.format('d')){
						onRepeat = true;
					}			
				}							
			}
		}	   
	   return onRepeat;
   },
   submitEvent: function(catID, ignoreFrom){
		schedEventForm = Ext.getCmp('calendarCatPopup').items.items[0].getForm();
			schedEventForm.submit({
				url: 'main_menu/calendar_data.json.php',
				params: {command: 'submitEvent', catID: catID, ignoreFrom: ignoreFrom},
				success: function(form, obj){
					Ext.Msg.alert('Success');
					if (obj.result.approved == '1') {
						calendarstore.reload();
					}
					Ext.getCmp('schedEventTab').setActiveTab(0);
					Ext.getCmp('calendarCatPopup').hide();
				},
				failure: function(){
					Ext.Msg.alert('ERROR','Failure: Event failed to be submitted');
				}
			}); 
   },
   listeners: {
	  render: function(){
		schedEventCatGrid = Ext.getCmp('schedEventCats').items.items[0];
		sm = Ext.getCmp('schedEventCats').items.items[0].getSelectionModel();
		schedEventCatGrid.store.getCatID = function(){
			catID = Ext.getCmp('schedCatID').getValue();
			if (catID != '') {
				rowNum = this.find('catid', catID);
				if (rowNum != -1) {
					rec = this.getAt(rowNum);
					sm.selectRecords([rec]);
				}		
			} else {
					sm.clearSelections();
			}
		};		
		schedEventCatGrid.store.addListener('load', function(catStore) {
			catStore.getCatID();
		}); 
	  },
      show : function(){
		schedEventCatGrid = Ext.getCmp('schedEventCats').items.items[0];
		if (schedEventCatGrid.store.data.items.length == 0) {
			schedEventCatGrid.store.load();
		}
		schedEventCatGrid.store.getCatID();
		
        selDate = Ext.getCmp('test_calx').viewmonth.selectedDate;
        if (selDate != 'undefined'){
           Ext.getCmp('calEventFromDate').setValue(selDate);
        }
        Ext.getCmp('schedEventTab').setActiveTab(0);
        showAs = Ext.getCmp('schedShowAs');
        if (showAs.getValue() == '') {
			showAs.setValue('Public');
		}
      },	   
	   hide: function(){
		   this.items.items[0].getForm().reset();	
		   Ext.getCmp('transpFieldset').collapse();	
		   showAs = Ext.getCmp('schedShowAs');  
		   showAs.enable();
		   showAs.doChange('Public');
	   }
   }   
});

