function getSchoolImage(id){
  return '/images/getSchoolPicture.cfm?id='+id+'&t=' + new Date().getTime();
}
CS2.School = {};
CS2.School.UploadPhoto = function(initOptions){
    var _self = this;
    this.settings = {};
    Ext.apply(this.settings, initOptions);
    
    var upThumbPanel = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        clientValidation: true,
        standardSubmit:false, 
        fileUpload: true,
        isUpload: true,
        method: 'POST',
        border: false,
        items: [{
            border: false,
            xtype:'fieldset',
            defaults: {
                anchor: '-20',
                msgTarget: 'side'
            },
            defaultType: 'textfield',
            items :[{
                      xtype: 'displayfield',
                      hideLabel: true,
                      html: '<strong>We currently support the following image types:</strong><br /> jpg, png, gif, bmp (<a href="mailto:luis@iescentral.com?subject=Request%20for%20new%20thumbnail%20type%20in%20Editor">Suggest more</a>)<br /><br />'
                    },{
                      xtype: 'fileuploadfield',
                      emptyText: 'Select a photo',
                      fieldLabel: 'Image File',
                      name: 'eventFile_1',
                      allowBlank: false,
                      emptyText: 'Select a photo',
                      buttonText: '',
                      buttonCfg: {
                          iconCls: 'upload-icon'
                      }
                    },{
                      xtype: 'button',
                      anchor: '0',
                      cls: 'bigPrint1',
                      text: 'Upload '+CS2.schoolVerbage()+' Photo',
                      style: 'margin-top: 10px',
                      handler: function(){
                        Ext.Msg.wait('Please wait...', 'Uploading Photo');
                        upThumbPanel.getForm().submit({
                          clientValidation: true, 
                          standardSubmit:false, 
                          fileUpload:true,
                          isUpload: true,
                          method: 'POST',
                          url:'/ajax/editor/uploadFile.cfm?t=_',
                          params: {
                            id: _self.settings.schoolID,
                            uimg: 1,
                            action: "school",
                            max:1
                          },
                          reader : new Ext.data.JsonReader({
                            success : '@success'
                          }),
                          success: function(form, action) {
                              winUploadThumbnail.hide();
                              Ext.Msg.hide();
                              setTimeout(function(){
                                var thumber = Ext.get('imgSchoolPhoto_'+ _self.settings.schoolID);
                                thumber.dom.src = getSchoolImage(_self.settings.schoolID);
                                var thumber2 = Ext.get('imgSchoolPhotoPage_'+ _self.settings.schoolID);
                                thumber2.dom.src = getSchoolImage(_self.settings.schoolID);
                              }, 100);
                          },
                          failure: function(form, action) {
                            Ext.Msg.hide();
                            switch (action.failureType) {
                                case Ext.form.Action.CLIENT_INVALID:
                                    Ext.Msg.alert('Failure', 'Please verify 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);
                            }
                          }
                        });
                      }
                    }
            ]
        }]
    });
    var winUploadThumbnail = new Ext.Window({title: 'Select a '+CS2.schoolStoreVerbage()+' Photo', constrainHeader: true, width: 400, height: 200, modal: true, resizable: false, closeAction:'close', items:upThumbPanel,
        buttons: [{
                    text: 'Cancel',
                    handler: function(){
                      winUploadThumbnail.close();
                    }
                  }]
    });
    winUploadThumbnail.show(this);
};



        var theTemplates = new Ext.data.JsonStore({
                                            fields: ['valID','texty'],
                                            root: 'templates',
                                            url: '/ajax/siteConfig/templateList.cfm'});

      function windowSchoolSettingsEditor(initOptions){
        var _self = this;
        this._settings = {};
        Ext.apply(this._settings, initOptions);
        var loadMode = this._settings.loadMode;
        var schoolID = this._settings.schoolID;
        var treenode = this._settings.treenode;

        if (!canEdit)
          return false;       
        
        function saveSchoolForm(){
          //alert(Ext.getCmp('winSchoolSettingsEditor').getForm().getValues().toSource());
          //return false;
          var thisForm = this.formPanel.getForm();
          thisForm.submit({
            clientValidation: true,            
            url:'/ajax/siteconfig/site/submitConfig.cfm',
            waitMsg:'Saving settings...',
            params: {schoolID: schoolID},
            errorReader : new Ext.data.JsonReader({
                root:'errors',
                successProperty:'success'
            }, ['id', 'msg']),
            success: function(form, action) {
              //if(action.result.data_id)

              this._settings.sc.triggerReload();
              this.win.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'});
        xmlCols.push({name: 'school_address', mapping: 'mailing_addr/address'});
        xmlCols.push({name: 'school_city', mapping: 'mailing_addr/city'});
        xmlCols.push({name: 'school_state', mapping: 'mailing_addr/state'});
        xmlCols.push({name: 'school_zip', mapping: 'mailing_addr/zip'});
        xmlCols.push({name: 'school_business', mapping: 'phone/business'});
        xmlCols.push({name: 'school_fax', mapping: 'phone/fax'});
        xmlCols.push({name: 'school_email', mapping: 'email/general'});
        xmlCols.push({name: 'school_webmaster', mapping: 'email/webmaster'});
        xmlCols.push({name: 'school_webmail', mapping: 'config/webmail'});
        xmlCols.push({name: 'school_analytics', mapping: 'config/analytics'});
        xmlCols.push({name: 'school_swearfilter', mapping: 'config/swearfilter'});
        xmlCols.push({name: 'school_holidaymode', mapping: 'config/holidaymode'});
        xmlCols.push({name: 'school_usechat', mapping: 'config/showchat'});
        xmlCols.push({name: 'school_quickbarbottom', mapping: 'config/quickbarbottom '});
        xmlCols.push({name: 'school_usetranslator', mapping: 'config/showtranslator'});
        xmlCols.push({name: 'school_useadobereader', mapping: 'config/showadobereader'});
        xmlCols.push({name: 'school_externalwarn', mapping: 'config/externalwarn'});
        xmlCols.push({name: 'school_meta_keywords', mapping: 'meta/keywords'});
        xmlCols.push({name: 'school_meta_desc', mapping: 'meta/desc'});
        xmlCols.push({name: 'school_template', mapping: 'config/template'});
        xmlCols.push({name: 'schoool_legal_terms', mapping: 'config/legal/terms'});
        xmlCols.push({name: 'schoool_legal_privacy', mapping: 'config/legal/privacy'});
        xmlCols.push({name: 'schoool_custom_css', mapping: 'config/custom/css'});
        xmlCols.push({name: 'schoool_custom_js', mapping: 'config/custom/js'});
        
        xmlCols.push({name: 'share_enabled', mapping: 'config/sharing/enabled', type: 'int'});
        for(n=0;n<=6;n++) 
          xmlCols.push({name: 'share_ext_'+n, mapping: 'config/sharing/sharing_ext_'+n, type: 'bool'});
    
        var navInfo = 'Changes will be applied when you click \'Save\' below.';
        
        var ies_Tab = new Ext.Panel({ 
                      title: '* IES *',
                      padding: 5,
                      items: {
                          title: 'Site Template',
                          autoHeight:true,
                          xtype: 'fieldset',
                          defaultType: 'textfield',
                          labelWidth: 85, // label settings here cascade unless overridden
                          items :[new Ext.form.ComboBox({
                                    fieldLabel: 'Site Layout',
                                    hiddenName:'school_template',
                                    store: theTemplates,
                                    valueField:'valID',
                                    anchor: '0',
                                    displayField:'texty',
                                    forceSelection: true,
                                    triggerAction: 'all',
                                    selectOnFocus:true,
                                    allowBlank:false
                                  })]
                      }
                    });
        var info_Tab = new Ext.Panel({ 
                      title: 'General',
                      padding: 5,
                      items: {
                                        title: 'Site Description',
                                        autoHeight:true,
                                        xtype: 'fieldset',
                                        defaultType: 'textfield',
                                        labelWidth: 85, // label settings here cascade unless overridden
                                        waitMsgTarget: !Ext.isIE,
                                        items :[{
                                          fieldLabel: 'Title',
                                          anchor: '0',
                                          width: 300,
                                          name: 'school_title',
                                          allowBlank:false
                                        },{
                                          fieldLabel: 'Short Title',
                                          name: 'school_shortTitle',
                                          width: 140,
                                          allowBlank:false
                                        },new Ext.form.DisplayField({html: ''}),{
                                          fieldLabel: 'Keywords',
                                          anchor: '0',
                                          width: 300,
                                          name: 'school_meta_keywords'
                                        },{
                                          xtype:'textarea',
                                          height:100,
                                          width: 300,
                                          anchor: '0',
                                          fieldLabel: 'Description',
                                          name: 'school_meta_desc'
                                        }]
                                    }
                        });                        
        var contact_Tab = new Ext.Panel({ 
                      title: 'Contact',
                      padding: 5,
                      items: {
                                        title: 'Physical Contact Info',
                                        xtype: 'fieldset',
                                        labelWidth: 85, // label settings here cascade unless overridden
                                        autoHeight:true,
                                        defaultType: 'textfield',
                                        labelWidth: 120,
                                        items :[{
                                                  fieldLabel: 'Business Phone',
                                                  name: 'school_business',
                                                  vtype: !Ext.isIE ? 'phone' : '',
                                                  width: 200,
                                                  allowBlank:false
                                                },{
                                                  fieldLabel: 'Business Fax',
                                                  name: 'school_fax',
                                                  vtype: !Ext.isIE ? 'phone' : '',
                                                  width: 200,
                                                  allowBlank:false
                                                },new Ext.form.DisplayField({html: ''}),{
                                                  fieldLabel: 'Business Address',
                                                  name: 'school_address',
                                                  anchor: '0',
                                                  allowBlank:false
                                                },{
                                                  fieldLabel: 'City',
                                                  name: 'school_city',
                                                  width: 200,
                                                  allowBlank:false
                                                },new Ext.form.ComboBox({
                                                  fieldLabel: 'State',
                                                  hiddenName:'school_state',
                                                  store: new Ext.data.ArrayStore({
                                                    fields: ['abbr', 'state'],
                                                    data : Ext.exampledata.states // from states.js
                                                  }),
                                                  valueField:'abbr',
                                                  displayField:'state',
                                                  forceSelection: true,
                                                  mode: 'local',
                                                  triggerAction: 'all',
                                                  emptyText:'Select a state...',
                                                  width: 200,
                                                  selectOnFocus:true,
                                                  allowBlank:false
                                                }),{
                                                  fieldLabel: 'ZIP',
                                                  name: 'school_zip',
                                                  width: 80,
                                                  minLength:5,
                                                  maxLength:5,
                                                  maskRe: /[0-9]/,
                                                  allowBlank:false
                                                },new Ext.form.DisplayField({html: ''}),{
                                                  fieldLabel: 'General Email',
                                                  name: 'school_email',
                                                  width: 200,
                                                  vtype:'email',
                                                  allowBlank:false
                                                },{
                                                  fieldLabel: 'Webmaster Email',
                                                  name: 'school_webmaster',
                                                  width: 200,
                                                  vtype:'email',
                                                  allowBlank:false
                                                },{
                                                  fieldLabel: 'Web Mail URL',
                                                  anchor: '0',
                                                  name: 'school_webmail'
                                                }]
                                    }
                      });
                      
        var accountPhoto = new Ext.Panel({
                title: 'Photo',
                tbar: [{
                        xtype: 'button',
                        text: 'Upload Photo...',
                        handler: function(){ CS2.School.UploadPhoto({schoolID: _self._settings.schoolID}); }
                      }],
                autoScroll:true,
                border: false,
                padding: 10,
                html: '<img src="'+getSchoolImage(_self._settings.schoolID)+'" width="100%" id="imgSchoolPhoto_'+_self._settings.schoolID+'" />'
              });
              
        var google_Tab = new Ext.Panel({ 
                      title: 'Google',
                      padding: 5,
                      items: [{
                                xtype: 'fieldset',
                                hidden: true, //also, comment login below
                                title: 'Google Services',
                                autoHeight:true,
                                defaultType: 'textfield',
                                items :[
                                          new Ext.form.DisplayField({
                                            hideLabel: true,
                                            html: 'Google Services require access to Google Servers from within your local network to function properly.<br /><br />'
                                          }),
                                          {
                                            xtype: 'checkbox',
                                            hideLabel: true,
                                            boxLabel: 'Enable Google Services',
                                            name: 'school_usegoogleservices'
                                          }
                                        ]
                            },{
                                xtype: 'fieldset',
                                title: 'Google Analytics',
                                autoHeight:true,
                                defaultType: 'textfield',
                                items :[
                                          new Ext.form.DisplayField({
                                            hideLabel: true,
                                            html: '<strong>Google Analytics requires an API Key to work.</strong><br />You can register your key at <a href="http://www.google.com/analytics/" target="_blank"><img src="/common/images/google_analytics.png" style="position: relative; top: 10px; left: 2px; border="0" /></a><br /><br />'
                                          }),
                                          {
                                            fieldLabel: 'API Key',
                                            width: 200,
                                            name: 'school_analytics'
                                          }
                                        ]
                            },{
                                xtype: 'fieldset',
                                title: 'Google Translator',
                                autoHeight:true,
                                defaultType: 'textfield',
                                items :[
                                          new Ext.form.DisplayField({
                                            hideLabel: true,
                                            html: 'This service is provided by Google and we cannot control any errors or layout breaking caused by it\'s usage.<br /><br />'
                                          }),
                                          {
                                            xtype: 'checkbox',
                                            hideLabel: true,
                                            boxLabel: 'Enable Google Site Translator',
                                            name: 'school_usetranslator'
                                          }
                                        ]
                            },{
                                xtype: 'fieldset',
                                hidden: true,
                                title: 'Google Login',
                                autoHeight:true,
                                defaultType: 'textfield',
                                items :[
                                          new Ext.form.DisplayField({
                                            hideLabel: true,
                                            html: 'This feature allows users to link a Google/Gmail account to their '+CS2.cyberschoolVerbage()+' login for instant login.<br /><br />'
                                          }),
                                          {
                                            xtype: 'checkbox',
                                            hideLabel: true,
                                            boxLabel: 'Enable Google Login',
                                            name: 'school_usegooglelogin'
                                          }
                                        ]
                            }]
                          });
                          
        var extra_Tab = new Ext.Panel({ 
                      title: 'Misc',
                      padding: 5,
                      items: {
                                        xtype: 'fieldset',
                                        title: 'Miscellaneous Settings',
                                        autoHeight:true,
                                        defaultType: 'textfield',
                                        defaults: {anchor: '0'},
                                        labelWidth: 1,
                                        items :[{
                                                  xtype: 'checkbox',
                                                  fieldLabel: '',
                                                  boxLabel: 'Enable Chat Window',
                                                  name: 'school_usechat'
                                                },{
                                                  xtype: 'checkbox',
                                                  fieldLabel: '',
                                                  boxLabel: 'Enable Swear-word Filtering',
                                                  name: 'school_swearfilter'
                                                },{
                                                  xtype: 'checkbox',
                                                  fieldLabel: '',
                                                  boxLabel: 'Warn about following External Links',
                                                  name: 'school_externalwarn'
                                                },{
                                                  xtype: 'checkbox',
                                                  fieldLabel: '',
                                                  disabled: true,
                                                  value: false,
                                                  boxLabel: 'Place QuickBar at Bottom of Page',
                                                  name: 'school_quickbarbottom'
                                                },{
                                                  xtype: 'checkbox',
                                                  fieldLabel: '',
                                                  boxLabel: 'Show Adobe Reader Download',
                                                  name: 'school_useadobereader'
                                                },{
                                                  xtype: 'checkbox',
                                                  fieldLabel: '',
                                                  boxLabel: 'Enable Holiday Mode (we sometimes provide extra graphics and effects durring holidays)',
                                                  name: 'school_holidaymode'
                                                }]
                                    }
                        });
                                    
        function showShareHelper(cb,c){
          if (cb.inputValue == '1' && c){
            Ext.getCmp('sharing_defined_list').setDisabled(false);
          }else if(cb.inputValue == '1' && !c){
            Ext.getCmp('sharing_defined_list').setDisabled(true);
          }
        }
        var sharing_Tab = new Ext.Panel({ 
                      title: 'Sharing',
                      padding: 5,
                      items: {
                                        xtype: 'fieldset',
                                        title: 'Sharing Options',
                                        autoHeight:true,
                                        labelWidth: 1,
                                        layout: 'form',
                                        items :[{
                                                    xtype: 'radiogroup',
                                                    columns: 1,
                                                    name: 'share_enabled',
                                                    items: [
                                                        {boxLabel: 'Disable', name: 'share_enabled', inputValue: '0', listeners: { check : showShareHelper }},
                                                        {boxLabel: 'Enable', name: 'share_enabled', inputValue: '1', listeners: { check : showShareHelper }}
                                                    ]
                                                },{
                                                    xtype: 'checkboxgroup',
                                                    id: 'sharing_defined_list',
                                                    columns: 1,
                                                    disabled: true,
                                                    items: [
                                                        {boxLabel: 'Share on Facebook', name: 'share_ext_0'},
                                                        {boxLabel: 'Share on Delicious', name: 'share_ext_1'},
                                                        {boxLabel: 'Share on StumbleUpon', name: 'share_ext_2'},
                                                        {boxLabel: 'Share on Digg', name: 'share_ext_3'},
                                                        {boxLabel: 'Share on Reddit', name: 'share_ext_4'},
                                                        {boxLabel: 'Share on Newsvine', name: 'share_ext_5'},
                                                        {boxLabel: 'Share on Magnolia', name: 'share_ext_6'}
                                                    ]
                                                }]
                                    }
                              });
                                    
        var terms_Tab = new Ext.Panel({ 
                      title: 'Terms &amp; Conditions',
                      padding: 5,
                      layout: 'fit',
                      items: {
                                xtype: 'htmleditor',
                                name: 'schoool_legal_terms'
                              }
                      });
                                    
        var privacy_Tab = new Ext.Panel({ 
                      title: 'Privacy Policy',
                      padding: 5,
                      layout: 'fit',
                      items: {
                                xtype: 'htmleditor',
                                name: 'schoool_legal_privacy'
                              }
                        });
                                    
        var customCSS_Tab = new Ext.Panel({ 
                      title: 'Custom CSS',
                      padding: 5,
                      layout: 'fit',
                      items: {
                                xtype: 'textarea',
                                name: 'schoool_custom_css'
                              }
                      });
                                    
        var customJS_Tab = new Ext.Panel({ 
                      title: 'Custom JS',
                      padding: 5,
                      layout: 'fit',
                      items: {
                                xtype: 'textarea',
                                name: 'schoool_custom_js'
                              }
                        });

        var tabItems = isSpecial ? [ies_Tab,info_Tab,contact_Tab,accountPhoto,google_Tab,sharing_Tab,extra_Tab,terms_Tab,privacy_Tab,customCSS_Tab,customJS_Tab] : [info_Tab,contact_Tab,accountPhoto,google_Tab,sharing_Tab,extra_Tab,terms_Tab,privacy_Tab];
        
        this.formPanel = new Ext.FormPanel({
                    waitMsgTarget: !Ext.isIE,
                    border: false,
                    reader : new Ext.data.XmlReader({
                          record : 'result',
                          success : '@success'
                      }, xmlCols),
                    plain: true,
                    autoScroll: true,
                    listeners: {
                      render: function(fp){
                        theTemplates.load();
                      }
                    },
                    layout: 'fit',
                    items: new Ext.TabPanel({
                      plain: true,
                      activeItem: 0,
                      deferredRender:false,
                      enableTabScroll:true,
                      defaults: {autoScroll:true},
                      items: tabItems
                    })/*
                    items: tabItems new Ext.Panel({
                          layout: 'border',
                          border: false,
                          items: [
                            new Ext.Panel({
                              region: 'north',
                              border: true,
                              html: '<div class="siteConfigInfo"><div class="siteConfigHeader">Here you can configure your site!</div><div class="siteConfigText">'+navInfo+'</div></div>'
                            }),
                            new Ext.Panel({
                              region: 'center',
                              activeTab: 0,
                              margins: '5 0 0 0',
                              bodyStyle:'padding: 5px 10px 0',
                              border: true,
                              plain: true,
                              autoScroll: true,
                              defaults: {autoScroll:true, xtype: 'fieldset', bodyStyle:'padding:15px 10px 0'},
                              items: tabItems
                                  })
                                ]
                              })*/
                  });
          
        this.win = new Ext.Window({
              width:550,
              height:500,
              resizable: false,
              title: CS2.schoolVerbage()+' Settings',
            	iconCls: CS2.isCyberSchool() ? 'cpi_schoolInfo' : 'cpi_ebusInfo',
            	constrainHeader: true,
              layout:'fit',
              closeAction:'close',              
              modal:true,
              closable:true,
              border:true,
              padding: 5,
              items: this.formPanel,
              buttons: [{
                text: 'Save',
                handler: saveSchoolForm.createDelegate(_self, [], true)
              },{ text: 'Cancel', handler: function(){ _self.win.close(); } }],
              listeners: {
                afterRender: function(win){
                  _self.formPanel.getForm().load({url:'/ajax/siteconfig/site/getSiteConfig.cfm?s='+schoolID, waitMsg:'Loading Setting...'});
                }
              }
          });
        this.win.show(this);
      }

      
      
reportingIn('generalTab');
