<?php use_helper('UrlFunctions');?>
<?php

if(isset($control)){

  $o_UsuarioDP = new MnuUserDP();
  $o_UsuarioDP->setUserId($txtLogin);
  $o_UsuarioDP->setUserName($txtNombre);
  $o_UsuarioDP->setUserPassword($txtPass);
  $o_UsuarioDP->setRoleId("sys.reg");
  $o_UsuarioDP->setIsDisabled('N');
  $o_UsuarioDP->setStartDate(date("Y-m-d"));
  $o_UsuarioDP->setCreatedDate(date("Y-m-d H:i:s"));
  $o_UsuarioDP->setCreatedUser(myUser::getUser()->getUserId());
 
  if($cboRol=="sys.emp") $o_UsuarioDP->setIsFirstTime('1');
  
  $bResult = $o_UsuarioDP->save();
    
  if($bResult){
    ?>
    <script language="JavaScript">
    parent.objMainTab.reqBody('<?=buildUrl('user/list')?>','block=1&update=1&Descripcion=<?=rawurlencode($txtNombre)?>');
	parent.objMainTab.windowsCloseAll();
    </script>
    <?php
  }else{
    $url = buildUrl('user/add', "block=".$block."&error=1");
  }
}else{
	
	$o_UserRol = new MnuRoleFN();
	$a_roles[] = 'sys.eva';
  	$a_roles[] = 'sys.master';
  	$a_roles[] = 'sys.emp';
  	$a_roles[] = 'sys.web';
  	$o_UserRol->setRoleIds($a_roles);
	$a_Rol = $o_UserRol->select();
	
	
  	 	
 	 
  	
  	
}
?>

<img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="10">
<table class="cuerpo" width="98%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="0">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="titleBody">
            <li>Adicionar usuario</li>
            <span class="subtitle"><I>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:::&nbsp;Dato</I></span></td>
             <td align="right" valign="baseline">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td valign="top">
      <script type="text/javascript">
        validatePass = function(){
          var elmPass  = $('txtPass');
          var elmPass2 = $('txtPass2');
          if(elmPass.value == elmPass2.value){
            valid.onSubmit = true;
          }else{
            alert("Las contraseñas son diferentes.");
            elmPass.value  = "";
            elmPass2.value = "";
            elmPass.focus();
            valid.onSubmit = false;
          }
        }
      </script>
      <form id="form1" name="form1" method="POST" onsubmit="validatePass();">
        <input type="hidden" name="control" value="1">
      <table hspace="5px" class="marco" width="100%">
        <tr>
          <td valign="top">
          
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="50%" valign="top">
                  <table class="SubCabecera" width="100%" border="0" cellpadding="0" cellspacing="2">
                   <tr>
                    </tr>
                       <td width="14%" nowrap="nowrap"><label class="label req" for="">Instituci&oacute;n</label></td>
                       <td width="86%" nowrap="nowrap">
                        <select class="combo validate-selection" name="cboRol" id="cboRol" title="Seleccione un tipo de usuario.">
                          <option value="" > - Seleccionar - </option>
                          <?php foreach ($a_Rol as $iKey => $o_RolBean){ ?>
                          <option value="<?=$o_RolBean->getRoleId()?>"><?=$o_RolBean->getRoleDesc()?></option>
                          <?php } ?>
                        </select>
                      </td>
                      <td nowrap="nowrap">&nbsp;</td> 
                      </tr>
                     <tr>
                      <td nowrap="nowrap"><label class="label req" for="txtNombre">Nombre completo</label></td>
                      <td nowrap="nowrap"><input class="text required" name="txtNombre" id="txtNombre" type="text" maxlength="60" size="50" ></td>
                       <td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td>
                    </tr>   
                    <tr>
                      <td nowrap="nowrap" width="14%"><label class="label req" for="txtLogin">Usuario</label></td>
                      <td nowrap="nowrap" width="86%"><input class="text required validate-alpha-pto" name="txtLogin" id="txtLogin" type="text" maxlength="15" size="11" ></td>
                      <td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td>
                    </tr>
                     <tr>
                        <td width="20%" nowrap="nowrap"><div class="field-label"><label class="label" for="txtPass"><b>Ingrese contraseña</b></label></div></td>
                        <td nowrap="nowrap"><div class="cusField-label"><input class="text required validate-password" name="txtPass" id="txtPass" type="password" maxlength="40" size="20" ></div></td>															        
                        <td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td>
                     </tr>
                     <tr>
                        <td width="" nowrap="nowrap"><div class="field-label"><label class="label" for="txtPass"><b>Vuelva a ingresar su contraseña </b></label></div></td>
                        <td nowrap="nowrap"><div class="cusField-label"><input class="text required validate-password-confirm" name="txtPass2" id="txtPass2" type="password" maxlength="40" size="20" ></div></td>															        
                        <td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td>
                       </tr>
                                      
                  </table>
                </td>
               
              </tr>            
            </table>
                               
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr><td><img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="10"></td></tr>
              <tr>
                <td align="center">
                  <input type="hidden" name="typeSubmit" id="typeSubmit">
                  <!--<input class="button" type="submit" value="Grabar y Nuevo" onclick="$('typeSubmit').value='add';"/>-->
                  <input class="button" type="button" value="Grabar" onclick="formCallback();$('typeSubmit').value='list';">
                  <input class="button" type="button" value="Cancelar" onclick="parent.objMainTab.windowsCloseAll();//parent.objMainTab.reqBody('<?=buildUrl('user/list')?>','001')"/>
                </td>
              </tr>
            </table>
            <tr><td><img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="10"></td></tr>
          </td>
        </tr>
      </table>
      </form>
      
    </td>
  </tr>
  
</table>

<script type="text/javascript">
		function formCallback() {
			var valid = new Validation('form1', {immediate : true,useTitles : true});
			Validation.addAllThese([
				['validate-password', 'Su contrase&ntilde;a debe tener por lo menos 7 caracteres <br>y como m&aacute;ximo 12 caracteres.', {
					minLength : 7,
					maxLength: 12,
					notOneOf : ['password','PASSWORD','1234567','0123456']
				}],
				['validate-password-confirm', 'Tu confirmaci&oacute;n de contrase&ntilde;a no es igual a la nueva <br>contrase&ntilde;a, por favor intente de nuevo.', {
					equalToField : 'txtPass'
				}]
			]);
		    var result = valid.validate();
		   if(result){
		   	   var pars = $('form1').serialize();	
		   	   var pars = 'operacion=validaAddUsuario&'+ pars;
			   new Ajax.Request('<?=buildUrlComplet('mnt.php/ajax/request', "block=001")?>',{
			    method:'get',
			    parameters: pars, 
			    onSuccess: function(transport){
			      var response = transport.responseText || "no response text";
			      var a_resultado = response.split('|');
			      var permitir = a_resultado[0];
			      var mensaje  = a_resultado[1];
			      if(permitir == "si"){
			      	$('form1').submit();
			      }else{
		      		if(permitir == "no"){
		   	    		alert(mensaje); 
		      		}
			      }
			    },
			    onFailure: function(){ alert('Se encontro un fallo al validar...') }
			  });
		   }
		}
	</script>