  function IsNumeric(inputVal,sErrorMsg) {
    if (isNaN(parseFloat(inputVal))) {
      return false;
    }
    return true
  }

     
      function tabGrade(initOptions){

        var _settings = {};
        var _self = this;
        Ext.apply(_settings, initOptions);
        _self._settings = _settings;
        this.gradingEnabled = true;
  
        if (!canEdit)
          return false;
          
        if (!this.gradingEnabled)
          return new Ext.Panel({
              frame: false,
              border: false,
              bodyStyle: 'text-align: center; padding: 50px 20px 20px;',
              html: 'To enable grading please contact our <a href="http://www.iescentral.com/" target="_blank">sales department</a>.'
            });

        /*
        var theTemplates = new Ext.data.JsonStore({
                                            fields: ['valID','texty'],
                                            root: 'templates',
                                            url: '/ajax/navigation/templateList.cfm'});
        theTemplates.load();
        function saveSchoolForm(){
          //alert(Ext.getCmp('winSchoolSettingsEditor').getForm().getValues().toSource());
          //return false;
          var thisForm = Ext.getCmp('winSchoolSettingsEditor').getForm();
          thisForm.submit({
            clientValidation: true,            
            url:'/ajax/siteconfig/site/submitConfig.cfm?s='+schoolID, 
            waitMsg:'Saving settings...',
            params: {},
            errorReader : new Ext.data.JsonReader({
                root:'errors',
                successProperty:'success'
            }, ['id', 'msg']),
            success: function(form, action) {
              //if(action.result.data_id)
            
              if (treenode)
                treenode.setText(thisForm.getValues().school_title);
              winSchoolSettingsEdit.close();              
              return true;
            },
            failure: function(form, action) {
              switch (action.failureType) {
                case Ext.form.Action.CLIENT_INVALID:
                  Ext.Msg.alert('Failure', 'Please verify all form fields are correct.');
                  break;
                case Ext.form.Action.CONNECT_FAILURE:
                  Ext.Msg.alert('Failure', 'Ajax communication failed');
                  break;
                case Ext.form.Action.SERVER_INVALID:
                default:
                  Ext.Msg.alert('Failure', action.result.msg);
              }
              Ext.Msg.hide();
              return false;
            }
          });

        }
        */

        var xmlCols = [];
        xmlCols.push({name: 'school_title', mapping: 'title/title'});
        xmlCols.push({name: 'school_shortTitle', mapping: 'title/short'});

        var gradeType = 'letter';
        var cm = new Ext.grid.ColumnModel({
              // specify any defaults for each column
              defaults: {
                  sortable: true // columns are not sortable by default           
              },
              columns: [
                  {
                      id: 'student',
                      header: 'Student Name',
                      dataIndex: 'student',
                      width: 220
                  },{
                      header: 'Files',
                      dataIndex: 'filelink',
                      width: 150
                  },{
                      header: 'Grade',
                      dataIndex: 'grade',
                      width: 50,
                      editor: new Ext.form.ComboBox({
                          //xtype: 'combo',
                          typeAhead: true,
                          forceSelection: true,
                          triggerAction: 'all',
                          displayField: 'val',
                          store: new Ext.data.Store({
                            proxy: new Ext.data.MemoryProxy(Ext.grading.letter),
                            reader: new Ext.data.ArrayReader({},[{name: 'key', mapping:0},{name: 'val', mapping:1}])
                          }),
                          listeners: {
                            expand: function(c){
                              switch (gradeType){
                                case 'percent':
                                  c.getStore().loadData(Ext.grading.percent);
                                  break;

                                case 'total':
                                  var tmpArray=[];
                                  var max = parseInt(Ext.getCmp('total_field').getValue(), 10);
                                  if (IsNumeric(max) && max > 0){
                                    for(n=max;n>=0;n--)
                                      tmpArray.push([n,n]);
                                    c.getStore().loadData(tmpArray);
                                  }else{
                                    c.collapse();
                                    Ext.getCmp('editPanel_gradetab_'+_settings.winName).activate(0);
                                    Ext.Msg.alert('Error', 'Please specify the number of total possible points.');
                                  }
                                  break;
                                  
                                case 'letter':
                                default:
                                  c.getStore().loadData(Ext.grading.letter);
                                  break;
                              }
                            }
                          },
                          emptyText:'...',
                          selectOnFocus:true,
                          listClass: 'x-combo-list-small'
                      })
                  },{
                      header: 'Bonus Pts.',
                      dataIndex: 'bonus',
                      width: 70,
                      editor: {
                          xtype: 'numberfield',
                          typeAhead: true,
                          triggerAction: 'all',
                          lazyRender: true
                      }
                  }
              ]
          });

        var gradeStore = new Ext.data.Store({
            proxy : new Ext.data.HttpProxy({url: '/ajax/editor/getGradeFiles.cfm'}),
            reader : new Ext.data.XmlReader({
                record : 'file',
                id: 'id',
                success : '@success'
              },[
                  {name: 'id', mapping: 'id'},
                  {name: 'student', mapping: 'student'},
                  {name: 'grade', mapping: 'grade'},
                  {name: 'bonus', mapping: 'bonus'},
                  {name: 'filelink', mapping: 'filelink'},
                  {name: 'filetitle', mapping: 'filetitle'},
                  {name: 'filedesc', mapping: 'filedesc'},
                  {name: 'filename', mapping: 'filename'},
                  {name: 'filetype', mapping: 'filetype'},
                  {name: 'filesize', mapping: 'filesize', convert: Ext.util.Format.fileSize},
                  {name: 'priority', mapping: 'priority', type: 'int'}
                ]
            ),
            listeners: {
              update : function(store, record, operation ){
                switch (operation){
                  case 'edit':
                    Ext.Ajax.request({
                        url: '/ajax/editor/updateGrade.cfm',
                        method: 'GET',
                        'params': {
                          data: _self._settings.editorDataID,
                          user: record.data.id,
                          grade: record.data.grade,
                          bonus: record.data.bonus
                        },
                        reader : new Ext.data.JsonReader({
                          success : '@success'
                        }),
                        success: function(response, opts) {
                          record.commit();
                        },
                        failure: function(response, opts) {
                          record.cancelEdit();
                        }
                      });
                    break;
                }
              }
            },
            sortInfo: {field: 'priority', direction: 'ASC'}
        });
        
        var gradePeriodPicker = {
                                  xtype:'fieldset',
                                  region: 'north',
                                  height: 50,
                                  border: false,
                                  labelWidth: 50,
                                  frame:false,
                                  bodyStyle:'padding: 0 5px',
                                  defaults: {anchor: '0'},
                                  items: new Ext.form.ComboBox({
                                            id: 'periodSelectorGrade',
                                            store: new Ext.data.Store({
                                              autoLoad: true,
                                              url: '/ajax/siteconfig/gcd/getGCDPeriodsJson.cfm?id='+_settings.editorGCDID,
                                              reader : new Ext.data.JsonReader({
                                                  root : 'periods'                           
                                                },[
                                                  {name: 'id', mapping: 'id'},
                                                  {name: 'name', mapping: 'name'}
                                                ]
                                              ),
                                              listeners: {
                                                load: function(s,r,o){
                                                  if (r[0].data.id == "error_no_periods"){
                                                    Ext.getCmp('periodSelectorGrade').setValue(r[0].data.name);
                                                    /*
                                                      var n = _self._settings;
                                                      var winName = 'gcd_edit';
                                                      var switchTabToPeriod = function(){
                                                        Ext.getCmp('editPanel_tabs_'+winName).activate(1);
                                                      }
                                                      Ext.Msg.show({
                                                         title:'Setup Error',
                                                         msg: 'There are no periods defined. The properties for this location will now open so you can specify them.',
                                                         buttons: Ext.Msg.OK,
                                                         fn: function(){
                                                          _self.winNavEdit.close();
                                                          if (n.parentNode) {
                                                            if (n.attributes.data_type=='department'){
                                                                  setTimeout(function(){
                                                                    callEditor({navigationContextNode: n, afterFn: switchTabToPeriod, 'winName': winName, editorDataID: n.attributes.gcd_id, editorType: 'Department', editorModal: true});
                                                                  }, 100);
                                                            }else if (n.attributes.data_type=='class'){
                                                                setTimeout(function(){
                                                                  callEditor({navigationContextNode: n, afterFn: switchTabToPeriod, 'winName': winName, editorDataID: n.attributes.gcd_id, editorType: 'Class', editorModal: true});
                                                                }, 100);
                                                            }
                                                          }
                                                         },
                                                         icon: Ext.MessageBox.ALERT
                                                      });
                                                    */
                                                    return false;
                                                  }
                                                  //Ext.Msg.wait('Loading users...', 'Please wait');
                                                  var rID = 0;
                                                  _self.period=r[rID].data.id;
                                                  Ext.getCmp('periodSelectorGrade').setValue(r[rID].data.name);
                                                  //gradeStore.proxy.getConnection().url = '/ajax/editor/getGradeFiles.cfm?id='+_self._settings.editorDataID+'&pt='+_self.period;
                                                  gradeStore.proxy.setUrl('/ajax/editor/getGradeFiles.cfm?id='+_self._settings.editorDataID+'&pt='+_self.period, true);
                                                  gradeStore.reload();
                                                  
                                                  //gradeStore.proxy.setUrl('/ajax/editor/getGradeFiles.cfm?id='+_self._settings.editorDataID+'&pt='+_self.period, true);
                                                  
                                                  //_self.tree.getLoader().dataUrl = '/ajax/siteconfig/gcd/getGCDUsersJson.cfm?id='+_self.gcdID+'&pt='+r[rID].data.id;
                                                  //_self.tree.getRootNode().reload();
                                                }
                                              }
                                            }),
                                            displayField:'name',
                                            typeAhead: true,
                                            mode: 'local',
                                            fieldLabel:'Period',
                                            forceSelection: true,
                                            triggerAction: 'all',
                                            selectOnFocus:true,
                                            listeners: {
                                              select: function(c,r,i){
                                                _self.period=r.data.id;
                                                //gradeStore.proxy.getConnection().url = '/ajax/editor/getGradeFiles.cfm?id='+_self._settings.editorDataID+'&pt='+_self.period;
                                                //gradeStore.proxy.getConnection().url = '/ajax/editor/getGradeFiles.cfm?id='+_self._settings.editorDataID+'&pt='+_self.period;
                                                gradeStore.proxy.setUrl('/ajax/editor/getGradeFiles.cfm?id='+_self._settings.editorDataID+'&pt='+_self.period, true);
                                                gradeStore.reload();
                                                //Ext.Msg.wait('Loading users...', 'Please wait');
                                                //_self.tree.getLoader().dataUrl = '/ajax/siteconfig/gcd/getGCDUsersJson.cfm?id='+_self.gcdID+'&pt='+r.data.id;
                                                //_self.tree.getRootNode().reload();
                                              }
                                            }
                                          })
                                        };
        

        var schoolSiteConfigTab = new Ext.Panel({
          frame: false,
          layout: 'fit',
          border: false,
          padding: 5,
          items: 
            [
              new Ext.TabPanel({
                id:'editPanel_gradetab_'+_settings.winName,
                plain:true,
                activeItem: 0,
                items: [new Ext.Panel({
                  title: 'Details',
                  padding: 10,
                  items: {
                    xtype:'fieldset',
                    id: 'grading_enabled',
                    checkboxName: 'grading_enabled',
                    checkboxToggle:true,
                    collapsed: true,
                    layout : 'form',
                    //onCheckClick: Ext.emptyFn,
                    title: 'Grade this Assignment',
                    autoHeight:true,
                    defaults: {width: 300},
                    defaultType: 'textfield',
                    items :[{
                          xtype: 'radiogroup',
                          fieldLabel: 'Grading Style',
                          itemCls: 'x-check-group-alt',
                          name: 'grading_style',
                          columns: 3,
                          items: [
                            {boxLabel: 'Percentage', name: 'grading_style', inputValue: 'percent',
                              listeners: {
                                check: function(cb,c){
                                  if (c) gradeType = 'percent';
                                }
                              }
                            },
                            {boxLabel: 'Letter Grade', name: 'grading_style', inputValue: 'letter',
                              listeners: {
                                check: function(cb,c){
                                  if (c) gradeType = 'letter';
                                }
                              }
                            },
                            {boxLabel: 'Total Score', name: 'grading_style', inputValue: 'total',
                              listeners: {
                                check: function(cb,c){
                                  if (c) gradeType = 'total';
                                  Ext.getCmp('total_field').setDisabled(!c);
                                }
                              }
                            }
                          ]
                        },{
                          id: 'total_field',
                          xtype: 'numberfield',
                          fieldLabel: 'Total Points',
                          width: 60,
                          maxLength: 3,
                          name: 'grading_total_points',
                          disabled: true
                        },{
                          xtype: 'checkbox',
                          fieldLabel: 'Bonus Points Possible',
                          name: 'grading_bonus_points'
                        }
                      ]
                  }
                }),{
                    title: 'Assignment Grades',
                    layout: 'border',
                    items: [gradePeriodPicker, {
                      region: 'center',
                      layout: 'fit',
                      border: false,
                      items: new Ext.grid.EditorGridPanel({
                          border: false,
                          store: gradeStore,
                          /*view: new Ext.grid.GroupingView({
                            forceFit:true,
                            groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
                          }),*/
                          cm: cm,
                          autoExpandColumn: 'student',
                          clicksToEdit: 1
                        })
                      }
                    ]
                }]
              })
            ]
        });

        return schoolSiteConfigTab;
        
      }