Ext.onReady(function() {

	var fedStore = new Ext.data.ArrayStore({
				data : [['Vlaamse Judofederatie', 'VJF'], ['OTHER', 'OTH']],
				fields : ['name', 'code']
			})

	this.recoverForm = new Ext.FormPanel({
				border : false,
				bodyStyle : 'background:#E2E7F6;',
				url : BASE_URL + 'user/ext_recover_password',
				method : 'POST',
				items : [{
							xtype : 'textfield',
							allowblank : false,
							name : 'username',
							fieldLabel : 'Username',
							labelWidth : 120,
							width : 250
						}]
			})

	this.logoPanel = new Ext.Panel({
				baseCls : 'x-plain',
				id : 'login-logo',
				region : 'center'
			});

	this.formLogin = new Ext.FormPanel({
		region : 'south',
		border : false,
		frame : false,
		url : BASE_URL + 'user/ext_login',
		method : 'POST',
		id : 'frmLogin',
		bodyStyle : 'background:#E2E7F6;padding:10;',
		labelWidth : 120,
		autoHeight : true,
		defaults : {
			width : 250,
			allowBlank : false,
			xtype : 'textfield'
		},
		items : [{
					xtype : 'combo',
					fieldLabel : 'Federation',
					name : 'federation',
					store : fedStore,
					mode : 'local',
					triggerAction : 'all',
					foreceSelection : true,
					editable : false,
					displayField : 'name',
					waitMsg : 'Logging in, please wait...',
					valueField : 'code',
					hiddenName : 'code',
					emptyText : 'Select VJF or OTHER...',
					id : 'logFederation'
				}, {
					fieldLabel : 'Username',
					name : 'username',
					id : 'logUsername',
					listeners : {
						render : function(c) {
							Ext.QuickTips.register({
								target : c.getEl(),
								text : 'VJF Leden: gebruik uw vergunningsnummer als username!',
								dismissDelay : 7000
							});
						}
					}
				}, {
					fieldLabel : 'Password',
					name : 'password',
					id : 'logPassword',
					inputType : 'password'
				}],
		buttons : [{
					text : 'Login',
					handler : fnLogin
				}, {
					text : 'Recover password',
					handler : fnRecoverPassword
				}]
	});

	function fnLogin() {
		Ext.getCmp('frmLogin').on({
					beforeaction : function() {
						if (formLogin.getForm().isValid()) {
							Ext.getCmp('winLogin').body.mask();
							Ext.getCmp('sbWinLogin').showBusy();
						}
					}
				});
		formLogin.getForm().submit({
			success : function() {
				window.location.replace(BASE_URL);
			},
			failure : function(form, action) {
				Ext.getCmp('winLogin').body.unmask();
				if (action.failureType == 'server') {
					obj = Ext.util.JSON.decode(action.response.responseText);
					Ext.getCmp('sbWinLogin').setStatus({
								text : obj.errors.reason,
								iconCls : 'x-status-error'
							});
				} else {
					if (formLogin.getForm().isValid()) {
						Ext.getCmp('sbWinLogin').setStatus({
									text : 'Authentication server is unreachable',
									iconCls : 'x-status-error'
								});
					} else {
						Ext.getCmp('sbWinLogin').setStatus({
							text : 'You have errors in the form, please correct and try again!',
							iconCls : 'x-status-error'
						});
					}
				}
			}
		});
	}

	function fnRecoverPassword() {
		recoverWin = new Ext.Window({
			layout : 'fit',
			modal : true,
			width : 495,
			autoHeight: true,
			y: 200,
			draggable : false,
			closable : false,
			title : 'Password recovery',
			bodyStyle : 'padding:10px; background:#E2E7F6;',
			items : [recoverForm],
			buttons : [{
						text : 'Submit',
						handler : function() {
							recoverForm.getForm().submit({
										success : function() {
											this.recoverForm.getForm().reset();
											recoverWin.hide();
										},
										failure : function(form, action) {
                      obj = Ext.util.JSON.decode(action.response.responseText);
                      Ext.getCmp('sbWinRecover').setStatus({
                      text : obj.errors.reason,
                      iconCls : 'x-status-error'
                    });
									}})
					}},{
						text : 'Cancel',
						handler : function() {
							recoverWin.hide();
						}}
					],
			bbar : new Ext.ux.StatusBar({
				text : 'VJF Leden moeten de <a href="http://www.infserv.net/ontw/judo/f?p=101:1">ledenadministratie</a> applicatie gebruiken om hun paswoord te resetten!',
				id : 'sbWinRecover',
				iconCls : 'x-status-error'
			})
		})
		recoverWin.show();
	}

	this.winLogin = new Ext.Window({
		title : 'Login to the VJF Competition Administration System',
		id : 'winLogin',
		iconCls : 'ux-auth-header-icon',
		modal : this.modal,
		width : 429,
		height : 300,
		y : 120,
		resizable : false,
		closable : false,
		modal : true,
		layout : 'border',
		plain : false,
		bodyStyle : 'padding:5px',
		items : [this.logoPanel, this.formLogin],
		bbar : new Ext.ux.StatusBar({
			text : 'Not yet registered? <a href="createAccount">Create a new user account.</a>',
			id : 'sbWinLogin',
			iconCls : 'x-status-error'
		}),
		tools : [{
			id : 'help',
			qtip : 'Help me, please!',
			handler : function(event, toolEl, panel) {
				Ext.Msg
						.alert('HELP INFORMATION',
								'VJF Leden moeten hun vergunningsnummer gebruiken om in te loggen')
			}
		}]
	});

	Ext.QuickTips.init();
	winLogin.show();
});
