var login = function(){
    Ext.QuickTips.init();

	// Create a variable to hold our EXT Form Panel.
	// Assign various config options as seen.
    var login = new Ext.FormPanel({
        labelWidth  : 80,
        url         : baseUrl + '/session/login',
        frame       : true,
        defaultType : 'textfield',
        monitorValid: true,
        items       : [{
                fieldLabel  : 'Username',
                name        : 'username',
                allowBlank  : false
            },{
                fieldLabel  : 'Password',
                name        : 'password',
                inputType   : 'password',
                allowBlank  : false
            }],
        buttons     : [{
                text    : 'Login',
                formBind: true,
                // Function that fires when user clicks the button
                handler : function()
                {
                    login.getForm().submit(
                    {
                        method      : 'POST',
                        waitTitle   : 'Connecting',
                        waitMsg     : 'Sending data...',

                        success : function()
                        {
                            var redirect = baseUrl + '/';
                            window.location = redirect;
                        },

                        failure : function(form, action){
                            if(action.failureType == 'server'){
                                obj = Ext.util.JSON.decode(action.response.responseText);
                                Ext.Msg.alert('Login Failed!', obj.errors.reason);
                            }else{
                                Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText);
                            }
                            login.getForm().reset();
                        }
                    });
                }
            }]
    });


	var win = new Ext.Window({
        title       : 'Please Login',
        layout      : 'fit',
        width       : 300,
        height      : 150,
        closable    : true,
        resizable   : false,
        plain       : true,
        border      : false,
        items       : [login]
	});
	win.show();
};