var winLoginDemo;

var frmLoginDemo = new Ext.FormPanel({ 
        labelWidth:80,
        url:'php/login.php', 
        frame:true, 
		autoHeight: true,
        defaultType:'textfield',
		monitorValid:true,
		items:[{ 
                fieldLabel:'Usuario', 
                name:'loginUsername', 
				id: 'txtLoginDemo',
				selectOnFocus: true,
                enableKeyEvents: true,
				listeners: {
                	keydown: function(t, e){
						if (e.getKey() == 13) {							
                        		Ext.getCmp('txtLoginDemo2').focus();
                    	}
                	}
            	}//listeners
            },{ 
                fieldLabel:'Contraseña', 
                name:'loginPassword',
				id: 'txtLoginDemo2', 
                inputType:'password', 
				selectOnFocus: true,
                enableKeyEvents: true,
				listeners: {
                	keydown: function(t, e){
						if (e.getKey() == 13) {
							if(!Ext.getCmp('accesarDemo').disabled){
                        		Ext.getCmp('accesarDemo').focus();
							}
                    	}
                	}
            	}//listeners
            }],
 
	 buttons:[{ 
                text:'Accesar',
				id: 'accesarDemo',
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:function(){ 
				
				 	if(!validaCamposLoginDemo())return;
                    
					frmLoginDemo.getForm().submit({ 
                        method:'POST', 
                        waitTitle:'Conectando', 
                        waitMsg:'Enviando datos...',
 						success:function(form, action){ 
						//obj = Ext.util.JSON.decode(action.response.responseText); 
							if (action.result.demo == 1) {
								Ext.getCmp('winLoginDemo').close();
							}else{
								Ext.Msg.alert('Conexión Fallida', 'Usuario o contraseña incorrecta.');  
								frmLoginDemo.getForm().reset();
							}	 
								             
                        },
 						 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); 
                            } 
                            frmLoginDemo.getForm().reset(); 
                        } 
                    }); 
                } 
            },
			{ 
                text:'Cancelar',
				id: 'cancelDemo',
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:function(){ 
				 	window.location = '../'; 
                } 
            }] 
    });

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

	
function loginDemo(){
	if (!winLoginDemo) {
		var winLoginDemo = new Ext.Window({
			id:'winLoginDemo',
			layout: 'fit',
			width: 300,
			autoHeight: true,
			title: 'Acceso a Reactiva Demo',
			resizable: false,
			closeAction: 'hide',
			closable:false,
			plain: true,
			border: false,
			modal: true,
			items: [frmLoginDemo],
			listeners: {
				show: function(){
					frmLoginDemo.getForm().reset();
					Ext.getCmp('txtLoginDemo').focus(true,300);
				}
			}
		});
	}
	winLoginDemo.show();
	
}