var AppLogon = {

	formSystemLogon: null,
	
	init: function()
	{
	
		Ext.QuickTips.init();
    	Ext.form.Field.prototype.msgTarget = 'side';
		
		var fieldLogin = new Ext.form.TextField({
			id: 'Login',
		    name: 'Login',
		    fieldLabel: 'Login',
		    maxLength: 15,
		    allowBlank:false,
		    width: 320
		});
		
		var fieldSenha = new Ext.form.TextField({
			inputType: 'password',
			id: 'Senha',
		    name: 'Senha',
		    fieldLabel: 'Senha',
		    maxLength: 15,		    
		    allowBlank:false,
		    width: 320
		});
		
		var buttonAcessar = new Ext.Button({
			type: 'submit',
			text: 'Acessar',
    		cls:'x-btn-text-icon',
    		icon: APPURL + 'Modules/Application/Images/Icons/checkmark.png',
    		handler: AppLogon.actionForm
		});
		
		var buttonEsqueci = new Ext.Button({
			type: 'submit',
			text: 'Esqueci minha senha',
    		cls:'x-btn-text-icon',
    		icon: APPURL + 'Modules/Application/Images/Icons/hprio_tsk.gif',
    		handler: function() {
                AppLembrarSenha.show();
            }
		});
		
		AppLogon.formSystemLogon = new Ext.form.FormPanel({
			renderTo: 'FormLogon',
			bodyStyle: 'background: transparent',
			url: 'VerifyLogon.php',
			buttonAlign: 'right',
    		labelAlign: 'top',
    		method: 'post',
    		border: false,
    		fileUpload: true,
    		items: [
    			new Ext.form.FieldSet({
    				title: 'Entre com seus dados',
    				border: true,
    				collapsible: false,
					height: 160,
    				items: [
    					fieldLogin,
    					fieldSenha
    				],
    				buttons: [
    					buttonAcessar,
    					buttonEsqueci   					
    				]
    			})
    		]    		
		});
	
	},
	
	actionForm: function(form, action)
	{
	
		if (AppLogon.formSystemLogon.form.isValid())
		{
		
			AppLogon.formSystemLogon.form.submit({
				waitTitle: 'Processando',
				waitMsg: 'Aguarde enquanto a autenticação é realizada',
				failure: function(form, action)
				{
					
					var texto;
							
					if (action.result != null)
						texto = action.result.data.text;
					else
						texto = '<b>Erro 404:</b> Arquivo não encontrado ou não disponível.<br><b>Arquivo:</b> ' + AppLogon.formSystemLogon.url;
						
					Ext.Msg.show({
					   title:'Mensagem',
					   msg: texto,
					   buttons: Ext.Msg.OK,
					   icon: Ext.MessageBox.ERROR
					});
					
					AppLogon.formSystemLogon.form.reset();
					
				},
				success: function(form, action)
				{
					
					var texto;
					
					if (action.result != null)
					{
						
						texto = action.result.data.text;
						
						AppProgressBar.init();
						window.location = action.result.data.path;
						
					}
					else
					{
						
						texto = '<b>Erro 404:</b> Arquivo não encontrado ou não disponível.<br><b>Arquivo:</b> ' + AppLogon.formSystemLogon.url;
						
						Ext.Msg.show({
						   title:'Mensagem',
						   msg: texto,
						   buttons: Ext.Msg.OK,
						   icon: Ext.MessageBox.ERROR
						});
						
					}
								
				}
			});
		
		}
		else
		{
		
			// Show a dialog using config options:
			Ext.Msg.show({
			   title:'Erro no processo de autenticação!!!',
			   msg: 'Preencha corretamente os campos<br>para acessar o sistema.',
			   buttons: Ext.Msg.OK,
			   animEl: 'FormButton_Acessar',
			   icon: Ext.MessageBox.ERROR
			});
			
			return false;
		
		}
	
	}

}
