Ext.namespace('beheer.login');

beheer.login = function(config){
    Ext.QuickTips.init(); 
	
    var login = new Ext.FormPanel({ 
        labelWidth:80,
        url:'classes/class.login.php', 
        frame:true, 
        title:'Please Login', 
        defaultType:'textfield',
		monitorValid:true,

        items:[{ 
                fieldLabel:'Username', 
                name:'Username',
				width:'175',
                allowBlank:false
            },{ 
                fieldLabel:'Password', 
                name:'Password', 
                inputType:'password', 
				width:'175',
                allowBlank:false 
            }],
 
        buttons:[{ 
                text:'Login',
                formBind: true,	 
                handler:function(){ 
                    login.getForm().submit({ 
                        method:'POST', 
                        waitTitle:'Connecting', 
                        waitMsg:'Sending data...',
 
                        success:function(){ 
		                        var redirect = 'index.php'; 
		                        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({
        layout:'fit',
        width:300,
        height:150,
        closable: false,
        resizable: false,
        plain: true,
        border: false,
        items: [login]
	});
	win.show();

	this.init(config);

	var baseConfig = {
		layout:'fit',
		width:300,
		height:150,
		closable: false,
		resizable: true,
		plain: true,
		border: false,
		items: [
			this.loginForm
		]
	};

	Ext.apply(baseConfig,config || {});
	beheer.login.superclass.constructor.call(this, baseConfig);
};

Ext.extend(beheer.login, Ext.Window, {
	init:function(fConfig){
		if(fConfig && fConfig.msg){
			this.msgField.setValue(fConfig.msg);
		}
		this.show();
	}
});
