var timeclockAdjustmentPopup = new Ext.Window({
  layout: 'fit',
  title: 'Request a time adjustment',
  width: 300,
  height: 322,
  closeAction: 'hide',
  modal: true,
  plain: true,
  items:[
    {
      xtype: 'form',
      id: 'tcAdjustmentForm',
      cls: 'formpadding',
      border: false,
      defaults: {style: {marginBottom: '5px'}},
      items: [
        {
          xtype: 'datefield',
          id: 'tcAdjustmentDate',
          name: 'date',
          fieldLabel: 'Date',
          width: 150,
          listeners : {
            select: function(){
              tcForm = Ext.getCmp('tcForm').getForm().getValues();
              adjustmentGrid = Ext.getCmp('timeclock_adjustment_grid');
              adjustmentGrid.store.baseParams.date = this.getValue().format('m/d/Y');
              adjustmentGrid.store.baseParams.staffid = tcForm.staffid;
              adjustmentGrid.store.load();
            }
          }
        },
        new timeclockAdjustmentGrid(),
        {
          id: 'tcAdjustmentStart',
          name: 'tcAdjustmentStart', 
          xtype: 'timespinner',
          fieldLabel: 'Time In'
        },
        {
          id: 'tcAdjustmentEnd',
          name: 'tcAdjustmentEnd',
          xtype: 'timespinner',
          fieldLabel: 'Time Out'
        },
        {
          xtype: 'textfield',
          fieldLabel: 'Reason',
          name: 'reason',
          width: 150,
          autoCreate: { tag: 'input', type: 'text', maxlength: 30 }
        },
        {
          xtype: 'textfield',
          hidden: true,
          name: 'affectedID',
          id: 'tcAdjustmentRecId'
        }
      ]
    }
  ],
  buttons: [
    {
      text: 'Submit',
      handler: function(){
        Ext.getCmp('tcAdjustmentForm').getForm().submit({
          url: 'main_menu/timeclock_data.json.php',
          params: {command: 'submitAdjustment', staffid: Ext.getCmp('tcStaffMember').getValue(), tcPassword: Ext.getCmp('tcPassword').getValue()},
          success: function(){
            Ext.Msg.alert('Success');
          },
          failure: function(){
            Ext.Msg.alert('ERROR','Failure: Absence failed to be reported');
          }
        });      
      }
    },
    {
      text: 'Close',
      handler: function(btn){ 
         timeclockAdjustmentPopup.hide();
      }  
    }
  ],
  listeners: {
    beforeShow: function(){
      if (SchoolDynamics.TeacherID != 'KIOSK') {return true;}
      var showPopup;
      Ext.Ajax.request({
        url: 'main_menu/timeclock_data.json.php',
        async: false,
        success: function(r){
            if (r.responseText.trim() == 'true') {
              showPopup = true;
            }  else {
              Ext.Msg.alert('ERROR','Wrong password');
              showPopup = false;            
            }
        },
        params: { command: 'isValidPass', staffid: Ext.getCmp('tcStaffMember').getValue(), tcPassword: Ext.getCmp('tcPassword').getValue()}
      });
      
      return showPopup;
    }  
  }
  
})