Ext.onReady(function() {
	var formFields = [
	{
		id: 'usr', fieldLabel: labels.username, name: 'username', value: prefilledUsername,
		labelStyle: 'white-space:nowrap', width:150
	},
	{
		id: 'pass', fieldLabel: labels.password, labelStyle: 'white-space:nowrap',
		name: 'password', inputType: 'password', value: prefilledPassword, width:150
	},
	{
		xtype: 'hidden', name: 'redirectAfterLogin', value: redirectAfterLogin
	}];
	if (Settings.ui_display_language_menu)	{ 
		formFields.push({
			id: 'lang', xtype: 'combo',
			hidden: !Settings.ui_display_language_menu,
			hideParent: true, fieldLabel: '&nbsp;', labelSeparator: '',
			autoCreate:true, mode: 'local', displayField:'n',
			valueField:'k', hiddenName:'language', name: 'language',
			editable: false, triggerAction:'all', disableKeyFilter: true,
			forceSelection:true, value: selectedLang, width:130,
			store: new Ext.data.SimpleStore({
				fields: ['n', 'k'],
				data: Languages
			}),
			listeners: {
				select: function(el) {
					usrName = Ext.getCmp('usr').getValue();
					if (!usrName || usrName.length == 0) {
						document.location.href = URLRoot+'/?language='+encodeURIComponent(el.getValue());
					}
				}
			}
		});
	}
	if (allowPersistentLogins)	{ 
		formFields.push({
			xtype: 'checkbox', fieldLabel: '', labelSeparator: '',
			id: 'persistent', boxLabel: labels.keepLogged, 
			inputValue : '1', name: 'persistent'
		});
	}
	win = new Ext.Window({
		el: 'viewport', title: Settings.title,
		layout: 'fit', iconCls: 'headerIcons-appWindow', width:350, bodyStyle:'background-color:white',
		constrain: true, closable: false, resizable: false, draggable: false, autoHeight: true, hideBorders: true,
		items: [
			new Ext.Panel({
				autoHeight: true, autoWidth: true,
				html:'<div id="loginLogo"><img src="'+Settings.ui_login_logo+'" alt="" border="0"></div>',
				hidden: (Settings.ui_login_logo.length == 0)
			}),
			new Ext.Panel({
				autoHeight: true, autoWidth: true,
				html:'<div id="loginText">'+Settings.ui_login_text+'</div>',
				hidden: (Settings.ui_login_text.length == 0)
			}),
			loginForm = new Ext.form.FormPanel({
				baseCls: 'x-plain', labelAlign: 'right',
				defaultType: 'textfield', autoHeight: true,
				bodyStyle: 'padding-top:25px;padding-left:20px;padding-bottom:20px;',
				items: formFields
			})
		],
		buttons: [
			loginBtn = new Ext.Button({
				text: '<b>'+labels.login+'</b>', 
				handler: submitForm, scale: 'medium',
				icon: URLRoot+'/images/fileman/interface/icons/door_in.gif'
			})
		]
	});

	function submitForm() {
		Ext.get(win.body).mask(labels.waitMsg);
		if (Ext.getCmp('persistent')) {
			var pers = Ext.getCmp('persistent').getValue() ? 1 : 0;
		} else {
			var pers = 0;
		}
		Ext.Ajax.request({
		url: loginURL,
		success: function(resp) {
			Ext.get(win.body).unmask();
			try {
				var rs = Ext.util.JSON.decode(resp.responseText);
			} catch (er) {
				if (confirm('Unexpected server reply. Press "OK" to display it.')) {
					document.write(resp.responseText);
				}
			}
			if (rs.success) {
				document.location.href = URLRoot+(startFolder ? '/?folder='+encodeURIComponent(startFolder): '/');
			} else if (rs.success == false) {
				Ext.Msg.show({title: '', msg: rs.error, buttons: Ext.Msg.OK, modal: false});
			}
		},
		failure: function(frm, act) {
			document.write(resp.responseText);
		},
		params: loginForm.getForm().getFieldValues()
		});
	}
	win.show();
	if (allowPersistentLogins)	{
		new Ext.ToolTip({
			target: Ext.get(Ext.getCmp('persistent').container.dom).first(),
			anchor: 'top', showDelay: 100, dismissDelay: 0, width: 300,
			html: labels.keepLoggedInDescription
		});
	}
	
	leftSideBBarHTML = '';
	if (Settings.ui_display_language_menu) {
		langPar = '&language='+encodeURIComponent(Ext.getCmp('lang').getValue());
	} else {
		langPar = '';
	}
	if (signUpEnabled) {
		leftSideBBarHTML = '<a href="'+signUpURL+langPar+'">'+labels.signup+'</a>';
	}
	if (passwordRecoveryEnabled) {
		if (signUpEnabled) {
			leftSideBBarHTML += '<br>';
		}
		leftSideBBarHTML += '<a href="'+URLRoot+'/?module=fileman&page=password_recovery'+langPar+'">'+labels.forgotPassword+'</a>';
	}
	
	$$('.x-toolbar-left')[0].innerHTML = leftSideBBarHTML;
	
	win.anchorTo(Ext.get('theBODY'), 'c-c');
	new Ext.KeyMap("usr", {
		key: Ext.EventObject.ENTER,
		fn: submitForm
	});
	new Ext.KeyMap("pass", {
		key: Ext.EventObject.ENTER,
		fn: submitForm
	});
	window.setTimeout(function() {Ext.getCmp('usr').focus();}, 50);
	if (message) {
		Ext.Msg.show({title: '', msg: message, buttons: Ext.Msg.OK, modal: false});
	}
});
