/**
 * @author Konstantin
 */
App.LoginDlg = function(config)
{
	config = config || {};
	this.Fields = {
		Login:new Ext.form.TextField({
			fieldName:'Login',
			fieldLabel:txt.Login,
			allowBlank:false
		}),
		Password:new Ext.form.TextField({
			fieldName:'Password',
			fieldLabel:txt.Password,
			inputType:'password',
			allowBlank:false
		})
	};
	this.Form = new Ext.form.FormPanel({
		bodyStyle:'padding:5px;',
		border:false,
		items:[this.Fields.Login, this.Fields.Password],
		buttons:[{text:txt.LogIn, handler:this.handleLogin, scope:this}, {text:txt.ForgotPassword, handler:this.handleSendPassword, scope:this}]
	});
	config = Ext.applyIf(config, {
		closable:false,
		mimimizabe:false,
		maximizable:false,
		title: txt.LoginTitle,
		items:[{
			layout:"border",
			border:false,
			items:[{
				region:'north',
				border:false,
				items:[
				{
					border:false,
					bodyStyle:'padding:5px 0 0 5px',
					html:txt.Welcome
				}],
				height:35
			},
			{
				region:"center",
				border:false,
				items:[this.Form]
			},
			{
				region:'south',
				height:50,
				frame:true,
				html:'<div class="btmLogo"></div>',
				border:false
			}
			]
		}],
		layout:"fit",
		width:325,
		height:225,
		closeAction:'hide'
	});
	App.LoginDlg.superclass.constructor.call(this, config);
}

Ext.extend(App.LoginDlg, Ext.Window, {
	handleLoginResponse:function(o, success, response)
	{
		Ext.Msg.hide();
		if (!success)
		{
			Ext.Msg.alert(txt.RequestError, txt.RequestErrorMessage);
			return;
		}
		if (response.responseText)
		{
			var res = Ext.util.JSON.decode(response.responseText);
			if (!res.Result)
			{
				Ext.Msg.alert(txt.LoginError, res.Message);
				return;
			}
			else
			{
				window.location.reload();
			}
		}
	},
	handleLogin:function()
	{
		if (!this.Fields.Login.isValid() || !this.Fields.Password.isValid())
		{
			Ext.Msg.alert(txt.Error, txt.FieldsInvalid);
			return;
		}
		Ext.Msg.wait(txt.ProcessingLogin, txt.LoggingIn);
		this.Conn = new Ext.data.Connection({
			method:'POST',
			url:App.url.Login
		});
		this.Conn.request({
			callback:this.handleLoginResponse,
			scope:this,
			params:{
				Login: this.Fields.Login.getValue(),
				Password: this.Fields.Password.getValue()
			}
		});
	},
	handleSendPassword:function()
	{
		if (!this.recoverPanel)
		{
			this.recoverPanel = new App.ForgotPasswordDlg();
		}
		this.recoverPanel.show();
	},
	show:function()
	{
		App.LoginDlg.superclass.show.call(this);
		this.Fields.Login.setRawValue('');
		this.Fields.Password.setRawValue('');
	}
});
