/**
 * @author  Isaac Peraza León.
 * Fecha 	Julio 22 de 2009.
 * publicado en: www.extjsmexico.com
 * 
 * Todas las cuestiones de uso de la libreria quedan sujetas a los terminos de ExtJs Library
 * para mayor información visite http://extjs.com/license
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 *
 */
  var winLogin;
	// Cajas de Textos	
  var txtUsuario = new Ext.form.TextField({
				name: 'usr',
				hideLabel: true,		
				width: 180,
				x: 70,
				y: 5,
				allowBlank: false,
				blankText: 'documento de usuario requerido.',
				enableKeyEvents: true,
				selectOnFocus: true,
				listeners: {
					keypress: function(t,e){
						if(e.getKey()==13){
							txtClave.focus();
						}
					}
				}
		});
		
		var txtClave = new Ext.form.TextField({
				name: 'clave',
				hideLabel: true,
				inputType:'password', 
				width: 180,
				x: 70,
				y: 35,
				allowBlank: false,
				blankText: 'Clave de acceso requerida.',
				enableKeyEvents: true,
				selectOnFocus: true,
				listeners: {
					keypress: function(t,e){
						if(e.getKey()==13){
							btnAceptar.focus();
						}
					}
				}
		});
		
		// Labels
		var lblUsuario = new Ext.form.Label({
			text: 'No Cedula :',
			x: 10,
			y: 10,
			height: 20,
			cls: 'x-label'
		});
		
		var lblClave = new Ext.form.Label({
			text: 'Clave :',
			x: 10,
			y: 40,
			height: 20,
			cls: 'x-label'
		});

		// botones
		var btnAceptar = new Ext.Button({
		    id: 'btnAceptar',
			x: 85,
			y: 75,
			text: 'Aceptar',
			icon: 'entrar.png',
			iconCls: 'x-btn-text-icon',
			minWidth: 80,
			handler:function(){
				frmLogin.validarAcceso();
			} 
		});
		
		var btnLimpiar = new Ext.Button({
		    id: 'btnLimpiar',
			x: 170,
			y: 75,
			text: 'Limpiar',
			icon: 'limpiar.png',
			iconCls: 'x-btn-text-icon',
			minWidth: 80,
			handler:function(){
				var frm = frmLogin.getForm();
				frm.reset();
				frm.clearInvalid();
				txtUsuario.focus(true, 100);
			} 
		});
		
		var frmLogin = new Ext.FormPanel({ 
			frame:true, 		
			layout: 'absolute',
			items:[lblUsuario, lblClave, txtUsuario, txtClave, btnAceptar, btnLimpiar],
			validarAcceso: function(){
				
				if (this.getForm().isValid()) {
					this.getForm().submit({
						url: 'login.php',
						method: 'POST',
						waitTitle: 'Conectando',
						waitMsg: 'Validando login..',
						success: function(form, action){
							window.location = 'titulariza.php?nit='+action.result.msg;

							//Ext.Msg.alert('Conexión Exitosa', action.result.msg, function(){
							//	txtUsuario.focus(true);
							//});
							/* En lugar del mensaje puede sustituir la pagina de login 
							 * por la pagina principal de tu aplicación o abrir otra ventna.
							 * 
							 * window.location = 'tuaplicacion.php';
							 * window.open('tuaplicacion.php');
							 */
						},
						failure: function(form, action){
							if (action.failureType == 'server') {
								var data = Ext.util.JSON.decode(action.response.responseText);
								Ext.Msg.alert('Conexión Fallida', data.errors.reason, function(){
									txtUsuario.focus(true, 100);
								});
							}
							else {
								Ext.Msg.alert('Error!', 'El servidor de autenticacion es inalcanzable : ' + action.response.responseText);
							}
							frmLogin.getForm().reset();
						}
					});
				}
			}
		});
				
		function abrirLogin(){		
			if (!winLogin) {
				winLogin = new Ext.Window({
					layout: 'fit',
					width: 280,
					height: 150,		
					title: 'Login',			
					resizable: false,
					closeAction: 'hide',
					closable: true,
					draggable: false,
					plain: true,
					border: false,
					//modal: true,
					items: [frmLogin],
					listeners: {
						hide: function(){
							var frm = frmLogin.getForm();
							frm.reset();
							frm.clearInvalid();
						},
						show: function(){
							txtUsuario.focus(true, 300);
						}
					}
				});
			}
			
			winLogin.show();
		}
		
		Ext.onReady(function(){
			
			Ext.BLANK_IMAGE_URL = '../imagenes/s.gif';
			Ext.QuickTips.init();


			//Evento onclick del boton Abrir login.
			var button = Ext.get('mostrar-btn');
			button.on('click', function(){
				abrirLogin();
			});
			
		});

