﻿var LoginForm = function(post_url, success_url) {
  this.success_url = success_url;
  
  this.login_user_name = new Ext.form.TextField({
    fieldLabel: 'User Name',
    name: 'user_name',
    id: 'user_name',
    allowBlank: false
  });
  
  this.login_password = new Ext.form.TextField({
    fieldLabel: 'Password',
    name: 'password',
    inputType: 'password',
    allowBlank: false
  });
        
  LoginForm.superclass.constructor.call(this, {
    // constructor goes here
    title: 'Please Log In - iCenter',
    frame: true,
    url: post_url,
    labelWidth: 80,
    monitorValid: true,
    keys: {
      key: [10, 13],
      fn: this.do_login,
      scope: this
    },
    items: [
      this.login_user_name,
      this.login_password
    ],
    buttons: [{
      text: 'Log In',
      formBind: true,
      handler: this.do_login,
      scope: this
    }, {
      text: 'Forgot Password',
      handler: this.forgot_password,
      scope: this
    }]
  });

  // events go here
  this.on('afterrender', this.do_focus, this, { delay: 500 });
};

Ext.extend(LoginForm, Ext.form.FormPanel, {
    // member functions go here
    forgot_password: function() {
        var fp = new ForgotPasswordWindow();
        
        fp.show();
    },
    
    do_login: function() {
        var that = this;
        var form = this.getForm();

        if (!form.isValid()) {
            return;
        }

        form.submit({
            method: 'POST',
            params: { doLogin: '1' },
            waitTitle: 'Connecting',
            waitMsg: 'Sending login credentials...',
            success: function() {
                form.reset();
                Ext.Msg.wait('Loading iCenter. Please Wait...', 'Login Success');
                var redirect = this.success_url;
                window.location = redirect;
            },
            failure: function(form, action) {
                if (action.failureType == 'server') {
                    var obj = Ext.util.JSON.decode(action.response.responseText);

                    if (obj.expired) {
                        this.change_password();
                    }
                    else {
                        Ext.Msg.alert('Warning', 'Login failed: ' + obj.reason, this.do_focus, this);
                    }
                }
                else {
                    Ext.Msg.alert('Warning', 'Authentication server is unreachable: ' + action.response.responseText, this.do_focus, this);
                }

                form.reset();
                this.do_focus();
            },
            scope: that
        });
    },

    do_focus: function() {
        if (this.login_user_name.getValue().length == 0 && this.login_password.getValue().length == 0) {
            this.login_user_name.focus();
            this.getForm().clearInvalid();
        }
    },

    change_password: function() {
        var cp = new ChangePasswordWindow(true, this.login_user_name.getValue());
        
        cp.on('password_changed', function() {
            Ext.Msg.alert('Information', 'Password changed.', this.do_focus, this);
        }, this);
        
        cp.show();
    }
});
//======================================================================
var LoginWindow = function(post_url, success_url) {
    // member data goes here
    this.form = new LoginForm(post_url, success_url);

	LoginWindow.superclass.constructor.call(this, {
		// constructor goes here
		layout: 'fit',
        width: 300,
        height: 170,
        closable: false,
        resizable: false,
        plain: true,
        border: false,
        draggable: false,
        items: [this.form]
	});

	// events go here
	this.on('render', this.setup_task, this);
};

Ext.extend(LoginWindow, Ext.Window, {
    // member functions go here
    setup_task: function() {
        this.task = Ext.TaskMgr.start({
            run: this.center,
            scope: this,
            interval: 1000 // 1 seconds
        });
    },

    do_focus: function() {
        this.form.do_focus();
    },
    
    show_password_recovery: function(token, user_id, is_new) {
        var rp = new RecoverPasswordWindow(token, user_id, is_new);
        rp.show();
    }
});
