var LoginDialog = function(){
  Ext.QuickTips.init();
  Ext.form.Field.prototype.msgTarget = 'side';
}
LoginDialog.prototype.window = null;
LoginDialog.prototype.open = function() {
  if (!this.window) {
    var win = new Ext.Window({
      animateTarget: 'menu',
      title: 'ログイン',
      layout:'fit',
      width:430,
      height:250,
      modal:true,
      resizable:false,
      closeAction:'hide',
      plain: true
    });

    var loginAction = new Ext.Action({
      text: 'ログイン',
      formBind: true,
      handler: function(){
        login.getForm().submit({
          waitMsg:'ログインしています...',
          success: function() {
            window.location = path_prefix + '/user';
            //win.hide();
          },
          failure:function(form, action){ 
            obj = Ext.util.JSON.decode(action.response.responseText); 
            Ext.MessageBox.show({
              title:'ログイン',
              icon:Ext.MessageBox.WARNING,
              msg:obj.errors.reason,
              buttons:Ext.MessageBox.OK
            }); 
          } 
        });
      }
    });

    var login = new Ext.FormPanel({
      //baseCls: 'x-plain',
      url: path_prefix + '/session/login',
      frame: true,
      bodyStyle:'padding:5px 5px 0',
      items: [{
          xtype:'label',
          style:'font-size:13px',
          text:'IDとパスワードを入力して、「ログイン」ボタンを押してください。'
        }, {
          xtype:'fieldset',
          defaultType: 'textfield',
          defaults: {width: 220},
          labelWidth: 130, 
          autoHeight:true,
          monitorValid: true,
          items:[{
              fieldLabel: 'ID',
              name: 'account'
            }, {
              inputType:'password',
              fieldLabel: 'パスワード',
              name: 'password',
              listeners:{
                specialkey: function(field, e) {
                  if (e.getKey() == Ext.EventObject.ENTER) {
                    loginAction.execute();
                  }
                }
              }
            }, {
              xtype:'checkbox',
              fieldLabel: '次回から自動でログイン',
              name: 'remember_me',
              checked:true,
              inputValue: '1'
            }
          ]
        }, {
          xtype:'label',
          html:'<div id="forgot_password">※<a href="javascript:void(0)" onclick="new ResetPasswordDialog().open();return false;">パスワードを忘れた方はこちら</a></div>'
        }, {
          xtype:'label',
          html:'<div id="for_mobile_user">※<a href="javascript:void(0)" onclick="new MobileHelpDialog().open();return false;">モバイルユーザーの方はこちら</a></div>'
        }
      ],
      buttons:[loginAction]
    });

    win.add(login);
/*
    win.add(new Ext.Panel({
      baseCls:'x-panel-mc',
      height:50,
      bodyStyle:'padding:5px 5px 0',
      anchor:'right 30%',
      html:'<div><a href="user/signup">まだ会員登録されていない方はこちら。</a></div>'
    }));
*/
    this.window = win;
  }
  this.window.show();
}

var ResetPasswordDialog = function(){
  Ext.QuickTips.init();
  Ext.form.Field.prototype.msgTarget = 'side';
}
ResetPasswordDialog.prototype.window = null;
ResetPasswordDialog.prototype.open = function() {
  if (!this.window) {
    var win = new Ext.Window({
      //animateTarget: 'menu',
      title: 'パスワード再発行',
      layout:'fit',
      width:480,
      height:180,
      modal:true,
      resizable:false,
      closeAction:'hide',
      plain: true
    });

    var resetPasswordAction = new Ext.Action({
      text: 'パスワード再発行',
      formBind: true,
      handler: function(){
        resetPasswordForm.getForm().submit({
          waitMsg:'パスワードを再発行しています...',
          success: function() {
            //window.location = path_prefix + '/user';
            Ext.MessageBox.show({
              title:'パスワード再発行',
              icon:Ext.MessageBox.INFO,
              msg:'入力されたメールアドレスに新しいパスワードを送信しました。',
              buttons:Ext.MessageBox.OK
            }); 
            win.hide();
          },
          failure:function(form, action){ 
            if (action.response) {
              obj = Ext.util.JSON.decode(action.response.responseText); 
              Ext.MessageBox.show({
                title:'パスワード再発行',
                icon:Ext.MessageBox.WARNING,
                msg:obj.errors.reason,
                buttons:Ext.MessageBox.OK
              }); 
            }
          } 
        });
      }
    });

    var resetPasswordForm = new Ext.FormPanel({
      //baseCls: 'x-plain',
      url: path_prefix + '/session/reset_password',
      frame: true,
      bodyStyle:'padding:5px 5px 0',
      items: [{
          xtype:'label',
          style:'font-size:13px',
          html:'<p>本人確認のため、登録済みのメールアドレスを入力してください。<br/>入力されたメールアドレス宛てに新しいパスワードをお送りします。</p>'
        }, {
          xtype:'fieldset',
          defaultType: 'textfield',
          defaults: {width: 220},
          labelWidth: 150, 
          autoHeight:true,
          monitorValid: true,
          items:[{
            fieldLabel: '登録済みメールアドレス',
            name: 'registered_mail_address',
            allowBlank:false,
            //emptyText:'Emailを入力...',
            listeners:{
              specialkey: function(field, e) {
                if (e.getKey() == Ext.EventObject.ENTER) {
                  resetPasswordAction.execute();
                }
              }
            }
          }]
        }
      ],
      buttons:[resetPasswordAction]
    });

    win.add(resetPasswordForm);

    this.window = win;
  }
  this.window.show();
}

var MobileHelpDialog = function(){
  Ext.QuickTips.init();
  Ext.form.Field.prototype.msgTarget = 'side';
}
MobileHelpDialog.prototype.window = null;
MobileHelpDialog.prototype.open = function() {
  if (!this.window) {
    var win = new Ext.Window({
      title: 'モバイルユーザーのログイン方法',
      layout:'fit',
      width:650,
      height:480,
      modal:true,
      //resizable:false,
      closeAction:'hide',
      plain: true,
      items:{
        layout:'fit',
        autoLoad:path_prefix + '/help/for_mobile_user'
      },
      buttons:[{text:'閉じる', handler:function() {win.hide();}}]
    });
    this.window = win;
  }
  this.window.show();
}
