Ext.BLANK_IMAGE_URL='images/default/s.gif';
Ext.BLANK_IMAGE_URL='images/default/s.gif';

var DNINumberExpr = /^\d{8}$|^\d(11)$/;
var CodigoNumberExpr = /^\d{10}$|^\d{8}$|^\d{5}$/;
var winRegNewUser;

Ext.onReady(function(){
	winRegNewUser = new Ext.Window({
		layout      	: 'fit',
		width       	: 645,
		height      	: 290,
		closeAction 	: 'hide',
		plain       	: true,
		modal			: true,
		title			: 'Registro Nuevo Usuario',
		listeners		:{
			'show'  	: function(Obj){
				Ext.getCmp('frmRegNewUser').getForm().reset();
				Ext.getDom('imgPictReg').src='getImage.php?id='+ new Date().getTime();
			}	
		}, 
		buttons			: [ 
			{
			    xtype 	: 'button',
			    text 	: 'Ok',
				onClick : function(){
					if (!Ext.getCmp('frmRegNewUser').getForm().isValid()){
						Ext.Msg.alert('Error em validaci&oacute;n de formulario','Los datos ingresados no son v&aacute;lidos');	
					}else{
						if (Ext.getCmp('chkWinRegMailUSP').getValue()==true){
							if (Ext.getCmp('txtWinRegCorreoUSP').getValue()==''){
								Ext.Msg.alert('Error','Ha seleccionado la casilla de correo usanpedro, pero no ingreso el par&aacute;metro')
							}
						}
						Ext.Ajax.request({
						   url: 'xmlprocess/control_access.php',
						   method : 'GET',
						   success: function(response, options){
						   		try {o = Ext.util.JSON.decode(response.responseText);}
								catch(e) {
									//error occurred, act accordingly
									Ext.MessageBox.alert('Error en Decodificacion', 'El Documento no puede decodificarse : ' + response.responseText);
								}
								if(true !== o.proceso) {
									//error occurred, act accordingly
									Ext.Msg.alert('Error Interno', o.errors.reason);
								}else {
									Ext.Msg.alert('Informacion', o.mensaje);
									Ext.getCmp('loginUsername').setValue(o.data.coduser);
									winRegNewUser.hide();
									Ext.getCmp('frmRegNewUser').getForm().reset();
								}
						   },
						   failure: function(){
						   		Ext.Msg.alert("Error en la consulta al Servidor");
						   },
						   params: { 
						   		accion: '4',  
								coduser : Ext.getCmp('txtWinRegCodigo').getValue(),  
								pwduser : Ext.getCmp('txtWinRegClave').getValue(),
								datos	: escape(Ext.getCmp('txtWinRegDatos').getValue()),
								dni		: Ext.getCmp('txtWinRegDNI').getValue(),
								fechan  : Ext.getCmp('txtWinRegFechaNac').getValue().dateFormat('d-m-Y'),
								vimagen : Ext.getCmp('txtWinRegValidaImg').getValue(),
								emailp 	: Ext.getCmp('txtWinRegCorreoPersonal').getValue(),
								flageusp: (Ext.getCmp('chkWinRegMailUSP').getValue()==true?'S':'N'),
								emailusp: Ext.getCmp('txtWinRegCorreoUSP').getValue(),
								pregunta: Ext.getCmp('cmbWinRegPregunta').getValue(),
								rtpa	: Ext.getCmp('txtWinRegRespuesta').getValue()
							}
						});
						
					}
				}
			},{
				xtype 	: 'button',
				text 	: 'Cancelar',
				onClick : function(){
					Ext.getCmp('frmRegNewUser').getForm().reset();
					winRegNewUser.hide();
				}
			}		
		],
		items       : [ 
			{
				id 				: 'frmRegNewUser',
				xtype 			: 'form',
				bodyStyle 		: 'padding-left:5Px; padding-top:10Px; ',
				items 			: [
					{
						layout 		: 'column',
						border 		: false,
						hideBorders : true,
						items 		: [
							{
								witdh 		: 345,
								items 		: [
									{
										xtype 		: 'fieldset',
										title 		: 'Validaci&oacute;n de Usuario',
										autoHeight 	: true,
										labelWidth 	: 140,
										width 		: 315,
										items 		: [
											{
												xtype 		: 'textfield',
												fieldLabel 	: 'Codigo',
												id			: 'txtWinRegCodigo',
												name 		: 'txtWinRegCodigo',
												regex		: CodigoNumberExpr,
												regexText	: 'El codigo ingresado no es valido',
												blankText	: 'Codigo de Usuario Requerido',
												allowBlank 	: false,
												emptyText	: 'Ingrese su codigo'
											},{
												xtype 		: 'textfield',
								        		fieldLabel 	: 'Apellidos y Nombres',
								        		id	 		: 'txtWinRegDatos',
												name 		: 'txtWinRegDatos',
												blankText	: 'Apellidos y Nombres Requerido',
												allowBlank 	: false,
												emptyText	: 'Ingrese sus apellidos y nombres',
												style		: 'text-transform: uppercase;'
											},{
												xtype 		: 'textfield',
												fieldLabel 	: 'Doc. Identidad',
												regex		: '',
												id			: 'txtWinRegDNI',
												name 		: 'txtWinRegDNI',
												blankText	: 'DNI Requerido',
												regex		: DNINumberExpr,
												regexText	: 'El formato es XXXXXXXX',
												allowBlank 	: false,
                                                value       : '00000000',
												emptyText	: 'DNI'
											},{
												xtype 		: 'datefield',
												fieldLabel 	: 'Fecha Nac',
												id	 		: 'txtWinRegFechaNac',
												name 		: 'txtWinRegFechaNac',
												format 		: 'd/m/Y',
												blankText	: 'Fecha Nacimiento Requerido',
												allowBlank 	: false,
												emptyText	: '01/01/1990'
											},new Ext.Panel({
												layout		: 'column',
												width 		: 330,
												border 		: false,
												hideBorders : true,
												items 		: [
													{
														width : 145,
														items : [
															{
																xtype		:'label', 
																text:'Caracteres'
															}
														]
													},{
														witdh : 160,
														items : [
															{
																id			: 'lblPictReg' ,
																xtype		: 'label', 
																html 		: '<img id=\'imgPictReg\' height=\'30\' witdh=\'140\' />'
															}
														]
													},{
														width : 40,
														height: 35,
														items : [
															{
																xtype		: 'button', 
																iconCls		: 'btn_reload', 
																onClick		: function(){
																	Ext.getDom('imgPictReg').src='getImage.php?id='+ new Date().getTime();
																}
															}
														]
													}
												]
											}),{
												xtype 		: 'textfield',
												fieldLabel 	: 'Validar Imagen',
												id			: 'txtWinRegValidaImg',
												name		: 'txtWinRegValidaImg',
												blankText	: 'Es Necesario validar la imagen',
												allowBlank 	: false,
												emptyText	: 'Ingrese los caracteres de la imagen'
											}
										]
									}
								]
							},{
								width 		: 5,
								items 		: [
									{
										xtype		: 'label', 
										html 		: '&nbsp;'
									}
								]
							},{
								witdh 		: 305,
								items 		: [
									{
										xtype 		: 'fieldset',
										title 		: 'Registro de Usuario',
										autoHeight 	: true,
										labelWidth 	: 120,
										width 		: 295,
										items 		: [
											{
												xtype 		: 'uxpassword',
												fieldLabel 	: 'Contrase&ntilde;a',
												name 		: 'txtWinRegClave',
												id 			: 'txtWinRegClave',
												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,
												blankText	: 'Es Necesario registra su clave',
												allowBlank 	: false,
												showCapsWarning: true,
												showStrengthMeter: true,
												pwStrengthTest: function(pw) {
													return (pw.length * 10);
												}
											},{
												xtype 		: 'textfield',
												fieldLabel 	: 'Verifica Contrase&ntilde;a',
												name 		: 'txtWinRegClaveVerifi',
												id 			: 'txtWinRegClaveVerifi',
												inputType 	: 'password',
												vtype		: 'password',
												initialPassField : 'txtWinRegClave'
											},{
												xtype 		: 'textfield',
												fieldLabel 	: 'Correo Personal',
								        		id	 		: 'txtWinRegCorreoPersonal',
												name 		: 'txtWinRegCorreoPersonal',
												vtype		: 'email',
												blankText	: 'Correo Personal Requerido',
												allowBlank 	: false,
												emptyText	: 'usuario@servidor.com'
											},{
												layout 		: 'column',
												border 		: false,
												hideBorders : true,
												items 		: [
													{
														width : 125,
														items : [
															{
																xtype 		: 'label',
																text 		: 'Correo USP'
															}
														]
													},{
														width : 110,
														items : [
															{
																xtype 		: 'textfield',
																name 		: 'txtWinRegCorreoUSP',
																id 			: 'txtWinRegCorreoUSP',
																disabled 	: true,
																width 		: 100,
																vtype		: 'email',
																allowBlank 	: true,
																emptyText	: 'usuario@usanpedro.edu.pe'
															}
														]
													},{
														width : 20,
														items : [
															{
																xtype 		: 'checkbox',
																hideLabel 	: true,
																name 		: 'chkWinRegMailUSP',
																id 			: 'chkWinRegMailUSP',
																checked		: false,
																vtype		: 'email',
																onClick 	: function(){
																	Ext.getCmp('txtWinRegCorreoUSP').setDisabled(this.getValue());
																	this.setValue(!this.getValue());
																}
															}
														]
													}
												]
											},{
												id				: 'cmbWinRegPregunta',
												name			: 'cmbWinRegPregunta',
												xtype			: 'combo',
												fieldLabel 		: 'Pregunta Secreta',
												emptyText 		: 'Seleccione la pregunta secreta'	,
												width			: 130,
												forceSelection	: true,
												typeAhead		: false,
												mode			: 'local',
												triggerAction	: 'all',
												selectOnFocus	: true,
												allowBlank		: false,
												editable		: true,
												transform		: 'cmbPreguntas',
												lazyRender		: true,
												listClass		: 'x-combo-list-small',
												onSelect		: function(Record) {
													Ext.getCmp('cmbWinRegPregunta').setValue(Record.data.value);
													Ext.getCmp('cmbWinRegPregunta').collapse();
													Ext.getCmp('txtWinRegRespuesta').setValue('');
												}
											},{
												xtype			: 'textfield',
												id				: 'txtWinRegRespuesta',
												name			: 'txtWinRegRespuesta',
												fieldLabel 		: 'Respuesta',
												minLengthText 	: 'La respuesta no puede ser menor de 4 caracteres',
												minLength 		: 4,
												maxLengthText 	: 'La respuesta no puede ser mayor de 20 caracteres',
												maxLength 		: 20,
												blankText		: 'El campo respuesta es requerido',
												allowBlank 		: false,
												emptyText		: 'Respuesta'
											}
										]
									}
								]
							}
						]
					}
				]
			}
		]
	});
});	