var winLogin;
var frmLogin = new Ext.FormPanel({ 
        labelWidth:80,
        url:'php/login.php', 
        frame:true, 
		autoHeight: true,
        defaultType:'textfield',
		monitorValid:true,
		items:[{ 
                fieldLabel:'Usuario', 
                name:'loginUsername', 
				id: 'txtLogin',
				selectOnFocus: true,
                enableKeyEvents: true,
				listeners: {
                	keydown: function(t, e){
						if (e.getKey() == 13) {							
                        		Ext.getCmp('txtLogin2').focus();
                    	}
                	}
            	}//listeners
            },{ 
                fieldLabel:'Contraseña', 
                name:'loginPassword',
				id: 'txtLogin2', 
                inputType:'password', 
				selectOnFocus: true,
                enableKeyEvents: true,
				listeners: {
                	keydown: function(t, e){
						if (e.getKey() == 13) {
							if(!Ext.getCmp('accesar2').disabled){
                        		Ext.getCmp('accesar2').focus();
							}
                    	}
                	}
            	}//listeners
            }],
 
	 buttons:[{ 
                text:'Accesar',
				id: 'accesar2',
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:function(){ 
				
				 	if(!validaCamposLogin())return;
                    
					frmLogin.getForm().submit({ 
                        method:'POST', 
                        waitTitle:'Conectando', 
                        waitMsg:'Enviando datos...',
 						success:function(form, action){ 
								obj = Ext.util.JSON.decode(action.response.responseText); 
								var redirect='';
								window.location = 'manager.php';                 
                        },
 						 failure:function(form, action){ 
                            if(action.failureType == 'server'){ 
                                obj = Ext.util.JSON.decode(action.response.responseText); 
                                Ext.Msg.alert('Conexión Fallida', obj.errors.reason); 
                            }else{ 
                                Ext.Msg.alert('Error!', 'El servidor de autenticación es inalcanzable : ' + action.response.responseText); 
                            } 
                            frmLogin.getForm().reset(); 
                        } 
                    }); 
                } 
            }] 
    });

function validaCamposLogin(){
	var objeto = '';
	var band = true;
	
	if(Ext.getCmp('txtLogin').getValue() == ''){ Ext.getCmp('txtLogin').markInvalid('Introduzca Usuario previamente.');		band=false;   objeto == '' ? objeto = Ext.getCmp('txtLogin')	: objeto = objeto }
	if(Ext.getCmp('txtLogin2').getValue() == ''){Ext.getCmp('txtLogin2').markInvalid('Introduzca Contraseña previamente.');	band=false;   objeto == '' ? objeto = Ext.getCmp('txtLogin2')	: objeto = objeto }
	if(band == false){objeto.focus(true,100); return band;}
	return band;
}

	
function login(){
	if (!winLogin) {
		var winLogin = new Ext.Window({
			layout: 'fit',
			width: 300,
			autoHeight: true,
			title: 'Acceso a Sistemas',
			resizable: false,
			closeAction: 'hide',
			plain: true,
			border: false,
			modal: true,
			items: [frmLogin],
			listeners: {
				show: function(){
					frmLogin.getForm().reset();
					Ext.getCmp('txtLogin').focus(true,300);
				}
			}
		});
	}
	winLogin.show();
}