 // vim: sw=4:ts=4:nu:nospell:fdc=4
 /*global Ext, EQP */
 /**
 * Componente que fornece os recursos de autentica��o
 *
 * @author Ot�vio Augusto
 * @copyright (c) 2009, by Ot�vio Augusto
 * @company Net On - Solu��es Tecnol�gicas
 * @date 12 de Setembro 2009
 *
 */

//Define o namespace
Ext.ns('App');

//Componente respons�vel por autentica��o
App.Login = Ext.extend(Ext.util.Observable,{
		/**
		 * Inicializa o componente
		 */
		initComponent : function(){
			//Chama o initComponent da super classe
			App.Login.superclass.initComponent.apply(this, arguments);
			
			//Registra os eventos do componente
			this.addEvents('authenticate');			
		},
		
		authenticate : function(sender){

                        Ext.ux.Loader.start({sender: sender, waitingText: 'Verificando...'});                        
			this.form.form.submit({
				url: App.sysURL + 'usuario_action/authenticate/',
				method: 'POST',
				success: function(form, action){

                                        Ext.ux.Loader.stop(sender);
                                        
					if (action.result.success == 'true'){
						this.form.disable();
						this.fireEvent('authenticate',this, action.result.data[0]);
					}else{
						//Exibe mensagem de erro
						Ext.Msg.show({
						   title: 'Falha de autentica&ccedil;&atilde;o',
						   msg: 'Usu&aacute;rio ou senha inv&aacute;lidos!',
						   buttons: Ext.MessageBox.OK,
						   icon: Ext.MessageBox.ERROR
						});    		
						
					}
					
				},
				scope: this
			});
			
		},
		
		/**
		 * Verifica se o usu�rio est� autenticado
		 * 
		 * @return {Boolean}
		 */
		isLogged : function(){
			//Request an action to verify if exists a session started
			Ext.Ajax.request({
				url: App.sysURL + 'usuario_action/isLogged/',
				method: 'POST',
				success : function(result){
					eval('var action = ' + result.responseText);					
					if (action.success == 'true'){
						if (this.form)
							this.form.disable();
						this.fireEvent('authenticate',this, action.data);
					}else{
						//Mostra o formul�rio de autentica��o
						this.createForm();						
						this.win.show();
						this.win.center();						
					}					
				},
				scope : this
			});

		},
	
		/**
		 * Fecha a janela de autentica��o
		 */
		closeWindow : function(){
			//Reseta o formul�rio
			if (this.form){
				this.form.form.reset();
				this.win.hide();
			}
		},
		
		/**
		 * Cria o formul�rio de autentica��o
		 * 
		 */
		createForm : function(){
			this.win = new Ext.Window({
				title: 'Acesso restrito',
				width: 260,		
				autoHeight: true,
				items: [
					this.form = new Ext.form.FormPanel({
						xtype: 'form',
						layout: 'form',
						maskDisabled: true,
						bodyStyle: 'padding:8px;',
						labelAlign: 'top',
						baseCls: 'x-plain',
						items:[
							{
								xtype: 'container',
								layout: 'column',
								defaults:{
									xtype: 'container'
								},
								items:[
									{
										columnWidth: .7,
										layout: 'form',
										items:[
											{
												xtype: 'textfield',
												fieldLabel: 'Usu&aacute;rio',
												name: 'login',
												allowBlank: false,
												anchor: '90%'
											},
											{
												xtype: 'textfield',
												fieldLabel: 'Senha',
												name: 'senha',
												inputType: 'password',
												allowBlank: false,
												anchor: '90%'
											}						
											
										]
									},
									{
										columnWidth: .3,
										items:[
											{
												baseCls: 'x-plain',
												html: '<img src="'+ App.uiURL + 'images/security.png" border="0" style="margin-top:10px"></img>'
											}
										]
									}
								]
							}						
						]
					})
					],
					buttons:[
						{
							text:'<b>Acessar</b>',
							id: 'access-btn',
							scope: this,
							handler: this.authenticate
						},
						{
							text: 'Limpar',
							scope: this,
							handler: this.cancel
						}
					
					]
				});								
			}
});

//Registra o xtype do componente
Ext.reg('app-login',App.Login);
