var Cms$Login = {
  
  loginFormPanel : null,
  
  initialize : function() {
    this.loginFormPanel = new Ext.FormPanel({
          id : 'loginFormPanel',
          frame : true,
          labelAlign : 'left',
          labelWidth : 64,
          width : 360,
          waitMsgTarget : true,
          title : '欢迎使用内容管理系统',
          renderTo : Ext.get('loginFormPanelContainer'),
          // 增加按键事件，回车键提交
          keys : [{
                key : Ext.EventObject.ENTER,
                fn : function() {
                  Cms$Login.submitAction();
                },
                scope : this
              }],
          buttons : [{
                text : '登录',
                handler : function() {
                  Cms$Login.submitAction();
                },
                scope : this
              }, {
                text : '重置',
                handler : function() {
                  this.loginFormPanel.getForm().reset();
                },
                scope : this
              }],
          items : [new Ext.form.FieldSet({
                title : '请登录',
                autoHeight : true,
                defaultType : 'textfield',
                items : [{
                      id : 'j_username',
                      name : 'j_username',
                      fieldLabel : '登录名称',
                      allowBlank : false,
                      width : 240
                    }, {
                      inputType : 'password',
                      id : 'j_password',
                      name : 'j_password',
                      fieldLabel : '登录密码',
                      allowBlank : false,
                      width : 240
                    }, {
                      cls : 'key',
                      name : 'randCode',
                      id : 'randCode',
                      fieldLabel : '验证码',
                      width : 80,
                      allowBlank : false,
                      blankText : '验证码不能为空'
                    }]
              })]
        });
    var bd = Ext.getDom('randCode');
    var bd2 = Ext.get(bd.parentNode);
    bd2.createChild([{
          tag : 'span',
          html : '<a href="javascript:Cms$Login.reloadcode();">'
        }, {
          tag : 'img',
          id : 'safecode',
          src : webrootWithoutBackslash + '/validationCodeServlet?timestamp=' + new Date().getTime(),
          align : 'absbottom'
        }, {
          tag : 'span',
          html : '</a> <b>点击图片可刷新</b>'
        }]);
    
    this.loginFormPanel.header.setStyle('text-align', 'center');
  },
  
  // 提交表单
  submitAction : function() {
    var validationCode = this.loginFormPanel.findById('randCode').getValue();
    // 阻塞用户操作
    Ext.Ajax.request({
          url : String.format(//
              '{0}/loginValidationServlet'//
              , webrootWithoutBackslash//
              ),
          method : 'POST',
          // timeout set to 1 minute
          // timeout : CMS$Globals.MILLISECONDS_PER_MINUTE,
          params : {
            timestamp : new Date().getTime(),
            validationCode : validationCode
          },
          // 成功后的响应
          success : function(response, options) {
            var responseText = response.responseText;
            var result = Ext.decode(responseText);
            // 验证成功则提交
            if (result.success) {
              try {
                Ext.Ajax.request({
                      url : String.format(//
                          '{0}/editor_login_check'//
                          , webrootWithoutBackslash//
                          ),
                      method : 'POST',
                      // timeout set to 1 minute
                      params : {
                        timestamp : new Date().getTime(),
                        j_username : this.loginFormPanel.getForm().getEl().dom.j_username.value,
                        j_password : this.loginFormPanel.getForm().getEl().dom.j_password.value
                      },
                      // 成功后的响应
                      success : function(response, options) {
                        var responseText = response.responseText;
                        var result = Ext.decode(responseText);
                        // 验证成功则提交
                        if (result.success) {
                          window.location.href = webrootWithoutBackslash + '/index.jsp';
                        } else {
                          Ext.MessageBox.buttonText.ok = '确定';
                          Ext.MessageBox.alert('提示', '输入的用户名密码错误,请重新输入');
                          this.reloadcode();
                        }
                      },
                      // scope
                      scope : this
                    });
              } catch (e) {
                alert( //
                    String.format( //
                        '提交异常:{0}', //
                        e.message ? e.message : e//
                        )//
                );
              }
            } else {
              Ext.MessageBox.buttonText.ok = '确定';
              Ext.MessageBox.alert('提示', '验证码输入有误，请重新输入');
              this.reloadcode();
            }
          },
          // 失败后的响应
          failure : function() {
            // 提示用户操作失败
            Ext.MessageBox.buttonText.ok = '确定';
            Ext.MessageBox.alert('提示', '服务器异常,请联系维护人员.');
          },
          // scope
          scope : this
        });
    
  },
  
  // 刷新验证码
  reloadcode : function() {
    var verify = document.getElementById('safecode');
    verify.setAttribute('src', webrootWithoutBackslash + '/validationCodeServlet?timestamp=' + new Date().getTime());
  }
  
};
Ext.onReady(function() {
      Ext.QuickTips.init();
      Ext.form.Field.prototype.msgTarget = 'under';
      Cms$Login.initialize();
    });

