var CodigoNumberExpr = /^\d{10}$|^\d{8}$|^\d{5}$/;
var storeWinForgotCorreos;

Ext.onReady(function(){
	
	function PasoSgt( direction ) {
		var lay = Ext.getCmp('layoutCard').getLayout();
		var idant = lay.activeItem.id;
        switch (direction){
        	case -1:
				Ext.Ajax.request({
					url			: 'xmlprocess/control_access.php',
					method 		: 'GET',
					success		: function(response, options){
						obj = Ext.util.JSON.decode(response.responseText);
						if (obj.proceso==false){
							Ext.Msg.alert('Error',obj.errors.reason);
							return ;
						}
						Ext.getCmp('winForgotPassword').hide();
					},
					failure: function(){
						Ext.Msg.alert('Error','Error en la consulta al Servidor');
						return ;
				   },
				   params: { accion: '5',  step : -1}
				});
			break;
			case 0:
				lay.setActiveItem(direction);
				break;
			case 1:
				Ext.Ajax.request({
				   url: 'xmlprocess/control_access.php',
				   method : 'GET',
				   success: function(response, options){
				   		obj = Ext.util.JSON.decode(response.responseText); 
		                if (obj.proceso==false){
							Ext.Msg.alert('Error',obj.errors.reason);
							return ;
						}
						Ext.getCmp('txtWinForgotCodPreg').setValue( obj.data.id_pregunta );
						Ext.getCmp('txtWinForgotPregunta').setValue( obj.data.desc_pregunta );
						//Ext.getCmp('txtWinForgotRespuesta').setValue( obj.data.desc_pregunta );
						
                        lay.setActiveItem(direction);
                        
						if (obj.data.correopersonal !=''){							
							Ext.getCmp('lblWinForgotCorreoPersonal').setText(  obj.data.correopersonal);
						//	Ext.getCmp('rdWinForgotMailReturn_1').setRawValue( obj.data.correousp);
							Ext.getCmp('rdWinForgotMailReturn_1').setDisabled( false );
						}else{
							Ext.getCmp('cmbWinForgotMailReturn_1').setDisabled( true );
						}
						if (obj.data.correousp !=''){
							Ext.getCmp('lblWinForgotCorreoUSP').setText( obj.data.correousp);
						//	Ext.getCmp('rdWinForgotMailReturn_2').setRawValue( obj.data.correousp);
							Ext.getCmp('rdWinForgotMailReturn_2').setDisabled( false );
						}else{
							Ext.getCmp('rdWinForgotMailReturn_2').setDisabled( true );
						}
						
						 
				   },
				   failure: function(){
				   		Ext.Msg.alert('Error','Error en la consulta al Servidor');
				   		return ;
				   },
				   params: { accion: '5',  step : 1,  coduser : Ext.getCmp('txtWinForgotUser').getValue(),  validaimg : Ext.getCmp('txtWinForgotImagen').getValue()}
				});
				break;
			case 2:
				Ext.Ajax.request({
				   url: 'xmlprocess/control_access.php',
				   method : 'GET',
				   success: function(response, options){
				   		obj = Ext.util.JSON.decode(response.responseText); 
		                if (obj.proceso==false){
							Ext.Msg.alert('Error',obj.errors.reason);
							return ;
						}
						
						if (Ext.getDom('txtWinForgotNewClave').nextSibling!=null){
							var ObjMeter = Ext.getDom('txtWinForgotNewClave').nextSibling;
							if (ObjMeter.className=='x-form-password-strengthMeter'){
								ObjMeter.style.width = 115;
							}
						}
						
						lay.setActiveItem(direction);
				   },
				   failure: function(){
				   		Ext.Msg.alert('Error','Error en la consulta al Servidor');
				   		return ;
				   },
				   params: { 
				   		accion		: '5',  
						step 		: 2,  
						metodo 		: (Ext.getCmp('rdRespuesta').getValue()?'A':'B'),  
				   		parametro 	: (Ext.getCmp('rdRespuesta').getValue()?Ext.getCmp('txtWinForgotRespuesta').getValue():( Ext.getCmp('rdWinForgotMailReturn_1').getValue()?'P':'U' ) ) }
				});
				break;
			case 3:
				Ext.Ajax.request({
				   url: 'xmlprocess/control_access.php',
				   method : 'GET',
				   success: function(response, options){
				   		obj = Ext.util.JSON.decode(response.responseText); 
		                if (obj.proceso==false){
							Ext.Msg.alert('Error',obj.errors.reason);
							return ;
						}
						
						lay.setActiveItem(direction);
				   },
				   failure: function(){
				   		Ext.Msg.alert('Error','Error en la consulta al Servidor');
				   		return ;
				   },
				   params: { 
				   		accion		: '5',  
						step 		: 3,  
						clave 		: Ext.getCmp('txtWinForgotNewClave').getValue()}				   		
				});	
				break;
			case 4:
				Ext.Ajax.request({
				   url: 'xmlprocess/control_access.php',
				   method : 'GET',
				   success: function(response, options){
				   		obj = Ext.util.JSON.decode(response.responseText); 
		                if (obj.proceso==false){
							Ext.Msg.alert('Error',obj.errors.reason);
							return ;
						}
						
						lay.setActiveItem(direction);
				   },
				   failure: function(){
				   		Ext.Msg.alert('Error','Error en la consulta al Servidor');
				   		return ;
				   },
				   params: { 
				   		accion		: '5',  
						step 		: 2,  
						metodo 		: (Ext.getCmp('rdRespuesta').getValue()?'A':'B'),    
						correo	 	: (Ext.getCmp('rdWinForgotMailReturn_1').getValue()?'P':'U') }
				});	
				break;
		}
		                   
    };
	

	var winForgotPassword = new Ext.Window({
        id          : 'winForgotPassword',
		layout      : 'fit',
		width       : 410,
		height      : 300,
		closable	: false,
		closeAction :'hide',
		plain       : true,
		modal		: true,
		title		: 'Universidad San Pedro',
		listeners   : {
			'show'  : function(){
				Ext.getDom('imgPict').src="getImage.php?id="+ new Date().getTime();	
				PasoSgt(0);
			}
		},
		items 		: [			
			{
				id	   : 'layoutCard',
				layout : "card",
				activeItem : 0,
				items: [
					{
						id			: 'card-0',
						bodyStyle 	: 'padding: 10 10 5 10;',
						items 		: [
							{
								xtype 		: 'label',
								style 		: 'font-size: 16px; font-weight:bold;',
								id	 		: 'lblTituloCard0',
								name 		: 'lblTituloCard0',
								html 		: 'Restaurar su clave'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								style 		: 'font-size: 12px; ',
								html 		: 'Para poder restablecer la contrase&ntilde;a ',
								formBind: true
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},new Ext.form.FormPanel({
								id 			: 'frmStep1',
								labelwidth	: 140,
								border 		: false,
								hideBorders : true,
								buttons 	: [
									{
										xtype		: 'button',
										text		: 'Continuar',
										onClick		: function(){
											if ( Ext.getCmp('frmStep1').getForm().isValid()){
												PasoSgt(1);
											}else{
												Ext.Msg.alert('Error','Rellene los campos requeridos');
											}
											
										}
									},{
										xtype		: 'button',
										text		: 'Cancelar',
										handler 	: function(){
											Ext.getCmp('frmStep1').getForm().reset();
											PasoSgt(-1);											
										}
									}
								],
								items  		: [
									{
										xtype 		: 'textfield',
										fieldLabel 	: 'Codigo Usuario',
					        			allowBlank	: false,
					        			name		: 'txtWinForgotUser',
										id			: 'txtWinForgotUser',
										blankText	: 'Codigo de Usuario Requerido',
										emptyText	: 'Codigo de Usuario',
										regex		: CodigoNumberExpr,
										regexText	: 'El codigo ingresado no es valido'
												
									},
									new Ext.Panel({
										layout		: 'column',
										width 		: 330,
										border 		: false,
										hideBorders : true,
										items 		: [
											{
												width 	: 105,
												items 	: [
													{
														xtype	: 'label',
														text	: 'Caracteres'
													}
												]
											},{
												witdh : 190,
												items : [
													{
														id		: 'lblPict' ,
														xtype	: 'label', 
														html 	: '<img id=\'imgPict\' height=\'30\' witdh=\'140\' />'
													}
												]
											},{
												width : 40,
												height: 35,
												items : [
													{
														xtype	: 'button', 
														iconCls	: 'btn_reload', 
														onClick	: function(){
															Ext.getDom('imgPict').src="getImage.php?id="+ new Date().getTime();
														}
													}
												]
											}]
									}),
						       		{
						        		xtype 		: 'textfield',
					        			fieldLabel 	: 'Validar Imagen',
										name		: 'txtWinForgotImagen',
										id			: 'txtWinForgotImagen',
										allowBlank	: false,
										regex		: /^\w{5}$/,	
										regexText 	: 'Debe ingresar los 5 caracteres',
										blankText	: 'Es neceario validar la imagen',
										emptyText	: 'Codigo de validacion'			
						        	}
						    	]
							})
						]				  
					},{
						id			: 'card-1',
						bodyStyle 	: 'padding: 10 10 5 10;',
						items 		: [
							{					    
						    	xtype 		: 'label',
								style 		: 'font-size: 16px; font-weight:bold;',
								id	 		: 'lblTituloCard1',
								name 		: 'lblTituloCard1',
								html 		: 'Restaurar su clave'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								style 		: 'font-size: 12px; ',
								html 		: 'Selecciona una opci&oacute;n para restablecer tu contrase&ntilde;a ',
								formBind	: true
							},{
								xtype 		: 'label',
								html 		: '<br />' 
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								layout 		: 'form',
								id 			: 'frmStep2',
								border 		: false,
								hideBorders : true,
								labelWidth 	: 1,
								buttons 	: [
									{
										xtype		: 'button',
										text		: 'Continuar',
										onClick		: function(){
											if (Ext.getCmp('rdRespuesta').getValue()==true){
												if ( Ext.getCmp('frmStep2a').getForm().isValid()){
													PasoSgt(2);
												}else{
													Ext.Msg.alert('Error','Debe indicar su respuesta');
												}
											}else if (Ext.getCmp('rdCorreo').getValue()==true){
												if ( Ext.getCmp('frmStep2b').getForm().isValid()){
													PasoSgt(4);
												}else{
													Ext.Msg.alert('Error','Debe seleccionar el correo de retorno');
												}
												
											}else{
												Ext.Msg.alert('Error','Debe Seleccionar una opcion');
											}
										}
									},{
										xtype		: 'button',
										text		: 'Cancelar',
										handler 	: function(){
											Ext.getCmp('frmStep1').getForm().reset();
											Ext.getCmp('frmStep2a').getForm().reset();
											Ext.getCmp('frmStep2b').getForm().reset();
											Ext.getCmp('winForgotPassword').hide();
										}
									}
								],
								items 		: [
									{
										xtype		: 'radio',
										name 		: 'rdRespuesta',
										id 			: 'rdRespuesta',
										boxLabel	: 'Utilizar mi pregunta secreta para comprobar mi identidad',
										value		: 'A',
										hideLabel 	: true,
										onClick 	: function (){
											Ext.getCmp('rdRespuesta').setValue(true);
											Ext.getCmp('rdCorreo').setValue(false);
											Ext.getCmp('leyRpta').setVisible(true);
											Ext.getCmp('leySendMail').setVisible(false);
										}
									},{
										xtype  		: 'fieldset',
										id     		: 'leyRpta',
										autoHeight 	: true,
										hidden 		: true ,
										width		: 360,
										items  		: [
											new Ext.form.FormPanel({
												id 				: 'frmStep2a',
											  	labelwidth 		: 160,
												border 			: false,
												hideBorders 	: true,
												items  			: [
													{
														xtype 		: 'hidden',
										        		id			: 'txtWinForgotCodPreg',
														name		: 'txtWinForgotCodPreg'
														
													},{
														xtype 		: 'textfield',
										        		fieldLabel 	: 'Pregunta',
														readOnly	: true,
														clearCls    : 'x-form-clear-left',
														id			: 'txtWinForgotPregunta',
														name		: 'txtWinForgotPregunta',
														width		: 200
														
													},{
														xtype 		: 'textfield',
										        		fieldLabel 	: 'Respuesta Secreta',
														id			: 'txtWinForgotRespuesta',
														name		: 'txtWinForgotRespuesta',
														allowBlank	: false,
														minLength	: 4,
														maxLength	: 20,
														maxLengthText : 'Debe ingresar 4 caracteres como minimo',
														minLengthText : 'Debe ingresar 20 caracteres como minimo',
														blankText	: 'Es neceario validar la imagen',
														emptyText	: 'Codigo de validacion'
													}
												]
											})
										]
									},{
						  				xtype		: 'radio',
						  				name 		: 'rdCorreo',
						  				boxLabel 	: 'Envieme instrucciones por correo para restablecer la contrase&ntilde;a',
						  				id 			: 'rdCorreo',
						  				value		: 'B',
						  				hideLabel 	: true,
						  				onClick  	: function (){
							  				Ext.getCmp('rdRespuesta').setValue(false);
											Ext.getCmp('rdCorreo').setValue(true);
											Ext.getCmp('leyRpta').setVisible( false);
											Ext.getCmp('leySendMail').setVisible(true);	
							  			}
									},{
										xtype		: 'fieldset',
										id 			: 'leySendMail',
										autoHeight 	: true,
										hidden 		: true,
										width		: 360,
										items  		: [
											new Ext.form.FormPanel({
												id 			:'frmStep2b',
											  	labelwidth 	: 160,
											  	border 		: false,
											  	hideBorders : true,										  	
											  	items  		: [
													{
														layout			: 'column',
														hideBorders		: true,
														border			: false, 
														items			: [
															{
																width 	: 160,
																items	: [
																	{	xtype			: 'radio',
														                id				: 'rdWinForgotMailReturn_1',
														                name			: 'rdWinForgotMailReturn',
														                itemCls			: 'x-check-group-alt',
				                										boxLabel		: 'Correo Personal' ,
				                										checked			: true,
				                										width 			: 155
																	}															
																]
															},{
																width 	: 150,
																bodyStyle : 'padding-top: 5px;',
																items	: [
																	{
																		xtype			: 'label',
														                fieldLabel		: 'ralolive@hotmail.com',
														                id				: 'lblWinForgotCorreoPersonal',
														                name			: 'lblWinForgotCorreoPersonal',
														                style			: 'font-size: 12px;' 
																	}
																]
															}														
														]
													},{
														layout			: 'column',
														hideBorders		: true,
														border			: false, 
														items			: [
															{
																witdh 	: 160,
																items	: [
																	{	xtype			: 'radio',
														                id				: 'rdWinForgotMailReturn_2',
														                name			: 'rdWinForgotMailReturn',
														                itemCls			: 'x-check-group-alt',
														                hideLabel		: true,
				                										boxLabel		: 'Correo U San Pedro',
				                										width 			: 155
																	}															
																]
															},{
																witdh 	: 150,
																bodyStyle : 'padding-top: 5px;',
																items	: [
																	{
																		xtype			: 'label',
														                fieldLabel		: 'ralolive@hotmail.com',
														                id				: 'lblWinForgotCorreoUSP',
														                name			: 'lblWinForgotCorreoUSP',
														                style			: 'font-size: 12px;'
																	}
																]
															}														
														]
													}
												]
											})
										]
									}
								]
							}
						]
					},{
						id			: 'card-2',
						bodyStyle 	: 'padding: 10 10 5 10;',
						items 		: [
							{
								xtype 		: 'label',
								style 		: 'font-size: 16px; font-weight:bold;',
								name 		: "lblTituloCard2",
								html 		:'Restaurar su clave'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							}, new Ext.form.FormPanel({
								id 				: 'frmStep3',
						  		//border 		: false,
						  		//hideBorders 	: true,
						  		standardSubmit	: true,
						        labelAlign		: 'left',
						        defaultType		: 'textfield',
								width			: 310,
						        bodyStyle		: 'padding:5px 5px 0',
								labelWidth 		: 140,
								default 		: {
									width 		: 120
								},
								buttons 	: [
									{
						  				xtype		:'button',
										text		:'Continuar',
										onClick		: function(){
											if ( Ext.getCmp('frmStep3').getForm().isValid()){
												PasoSgt(3);
											}else{
												Ext.Msg.alert('Error','Debe indicar su respuesta');
											}																						
											
										}
									},{
										xtype		: 'button',
										text		: 'Cancelar',
										handler 	: function(){
											Ext.getCmp('frmStep1').getForm().reset();
											Ext.getCmp('frmStep2a').getForm().reset();
											Ext.getCmp('frmStep2b').getForm().reset();
											Ext.getCmp('frmStep3').getForm().reset();
											PasoSgt(-1);
										}
									}
								],
								items  : [
									{
										xtype 				: 'uxpassword',
										width 				: 120,
										fieldLabel 			: 'Nueva Contrase&ntilde;a',
										name 				: 'txtWinForgotNewClave',
										id 					: 'txtWinForgotNewClave',
										minLengthText 		: 'La Contrase&ntilde;a no puede ser menor de 5 caracteres',
										minLength 			: 5,
										maxLengthText 		: 'La Contrase&ntilde;a no puede ser mayor de 20 caracteres',
										maxLength 			: 20,
										style 				: 'width: 120px;', 
										allowBlank			: false,
										emptyText			: 'Nueva Clave',
										showCapsWarning		: false,
										showStrengthMeter	: true,
										pwStrengthTest		: function(pw) {
											return (pw.length * 10);
										}
									},{
										xtype 				: 'textfield',
										fieldLabel 			: 'Vuelva a ingresar la Contrase&ntilde;a',
										name 				: 'txtWinForgotNewClaveVerifi',
										id 					: 'txtWinForgotNewClaveVerifi',
										inputType 			: 'password',
										vtype				: 'password',
										allowBlank			: false,
										emptyText			: 'Rescribir la nueva clave',
										width				: 120,
										initialPassField	: 'txtWinForgotNewClave'
									}
								]
							})
						]
					},{
						id			: 'card-3',
						bodyStyle 	: 'padding: 10 10 5 10;',
						items 		: [
							{					    
						    	xtype 		: 'label',
								style 		: 'font-size: 16px; font-weight:bold;',
								name 		: 'lblTitulo',
								html 		: 'Restaurar su clave'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: 'La constrase&ntilde;a ha sido actualizada, ahora puede acceder al Sistema.'
							}, new Ext.form.FormPanel({
								id 			: 'frmStep4',
								border 		: false,
								hideBorders : true,
								labelWidth 	: 140,
								default 	: {
									width : 120
								},
								buttons : [
									{
										xtype	: 'button',
										text	: 'Continuar',
										onClick	: function(){
											Ext.getCmp('frmStep1').getForm().reset();
											Ext.getCmp('frmStep2a').getForm().reset();
											Ext.getCmp('frmStep2b').getForm().reset();
											PasoSgt(-1);
										}
									}
								],
								items : [
									{
										xtype	: 'label'
									}
								]
							})
						]
					},{
						id			: 'card-4',
						bodyStyle 	: 'padding: 10 10 5 10;',
						items 		: [
							{					    
						    	xtype 		: 'label',
								style 		: 'font-size: 16px; font-weight:bold;',
								name 		: 'lblTitulo',
								html 		: 'Envio de nueva clave'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: '<br />'
							},{
								xtype 		: 'label',
								html 		: 'Se ha enviado un mensaje al correo seleccionado, por favor verifique el envio del correo.'
							}, new Ext.form.FormPanel({
								id 			: 'frmStep5',
								border 		: false,
								hideBorders : true,
								labelWidth 	: 140,
								default 	: {
									width 	: 120
								},
								buttons 	: [
									{
										xtype	: 'button',
										text	: 'Cerrar',
										onClick	: function(){
											Ext.getCmp('frmStep1').getForm().reset();
											Ext.getCmp('frmStep2a').getForm().reset();
											Ext.getCmp('frmStep2b').getForm().reset();
											Ext.getCmp('frmStep3').getForm().reset();
											PasoSgt(-1);
										}
									}
								],
								items : [
									{
										xtype:'label'
									}
								]
							})
						]
					}
				]
			}
		]
	})
});