<?php use_helper('UrlFunctions');?>
<?php use_helper('UtilProyecto'); ?>
<?php
$s_User = myUser::getUser()->getUserId();
$s_RoleTmp = myUser::getUser()->getRoleId();
if(isset($control))
{
    try 
    {
        switch ( $s_RoleTmp )
        {
            case "sys.admgen":
            case "sys.adm":
                $s_RoleId = "sys.admins";
                break;
            case "sys.admins":
                $o_Proy_usuarioFN = new Proy_usuarioFN();
                $o_Proy_usuarioFN->setUser_id( myUser::getUser()->getUserId() );
                $a_Proy_usuario = $o_Proy_usuarioFN->select( 1 );
                if( $a_Proy_usuario )
                {
                    $idinstitucion = $a_Proy_usuario[0]->getIdinstitucion();
                }
                $s_RoleId = "sys.reg";
                break;
        }
        $o_Mnu_userFN = new Mnu_userFN();
        $o_Mnu_userBN = $o_Mnu_userFN->selectByPK( $user_id );
        if( $o_Mnu_userBN )
        {
            throw new Exception( "El usuario seleccionado ya existe." );
        }
        
        $o_Mnu_userDP = new Mnu_userDP();
        $o_Mnu_userDP->setUser_id( $user_id );
        $o_Mnu_userDP->setUser_name( $user_name );
        $o_Mnu_userDP->setUser_password( md5( $txtPass ) );
        $o_Mnu_userDP->setRole_id( $s_RoleId );
        $o_Mnu_userDP->setIn_use( "Y" );
        $o_Mnu_userDP->setIs_disabled( "N" );
        $o_Mnu_userDP->setStart_date( date( "Y-m-d" ) );
        $o_Mnu_userDP->setEnd_date( date( "Y-m-d" ) );
        
        $o_Mnu_userDP->setCreated_date( date( "Y-m-d H:i:s" ) );
        $o_Mnu_userDP->setCreated_user( $s_User );
        
        //$o_Mnu_userDP->setRevised_date( date( "Y-m-d" ) );
        //$o_Mnu_userDP->setRevised_userr( $s_User );
        
        $b_Result = $o_Mnu_userDP->save();
        
        if( $b_Result )
        {
            $o_Proy_usuarioDP = new Proy_usuarioDP();
            $o_Proy_usuarioDP->setUser_id( $user_id );
            $o_Proy_usuarioDP->setIdinstitucion( ( int ) $idinstitucion );
            $b_Result = $o_Proy_usuarioDP->save();
            ?>
            <script language="JavaScript">
            parent.objMainTab.reqBody( '<?php echo buildUrl( 'user/list' ) ?>', 'block=1' );
            parent.objMainTab.windowsCloseAll();
            </script>
            <?php
        }
    }
    catch ( Exception $o_Exception )
    {
        ?>
        <script>
        alert( "<?php echo $o_Exception->getMessage() ?>" );
        //parent.objMainTab.windowsCloseAll();
        </script>
        <?php
    }
}
?>
<?php
if( !isset( $control ) )
{
    $idinstitucion = "";
    $user_name = "";
    $user_id = "";
}
?>
<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>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">
      <form id="form1" name="form1" method="POST" onsubmit="">
        <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">
                  <?php if( $s_RoleTmp == "sys.adm" || $s_RoleTmp == "sys.admgen" ){ ?>
                   <tr>
                       <td width="14%" nowrap="nowrap"><label class="label req" for="idinstitucion">Instituci&oacute;n</label></td>
                       <td width="86%" nowrap="nowrap">
                        <select class="combo required" name="idinstitucion" id="idinstitucion" title="Seleccione." style="width:100%">
                          <option value="" > - Seleccionar - </option>
                          <?php
                          $o_Proy_v_institucionFN = new Proy_v_institucionFN();
                          $a_Proy_v_institucion = $o_Proy_v_institucionFN->select();
                          ?>
                          <?php foreach ( $a_Proy_v_institucion as $iKey => $o_Proy_v_institucionBN ){ ?>
                          <option value="<?php echo $o_Proy_v_institucionBN->getIdinstitucion()?>" <?php if( $idinstitucion == $o_Proy_v_institucionBN->getIdinstitucion() ){ echo "SELECTED"; } ?> ><?php echo $o_Proy_v_institucionBN->getNombre()?></option>
                          <?php } ?>
                        </select>
                      </td>
                      <td width="8%" nowrap="nowrap">&nbsp;</td> 
                     </tr>
                     <?php } ?>
                     <tr>
                      <td nowrap="nowrap"><label class="label req" for="user_name">Nombre Completo</label></td>
                      <td nowrap="nowrap"><div class="cusField-label"><input class="text required" name="user_name" id="user_name" type="text" maxlength="60" value="<?php echo $user_name ?>" style="width:100%" title="Campo requerido"></div></td>
                       <td nowrap="nowrap" class="cabecera">&nbsp;</td>
                    </tr>   
                    <tr>
                      <td nowrap="nowrap"><label class="label req" for="user_id">Usuario</label></td>
                      <td nowrap="nowrap"><div class="cusField-label"><input class="text required" name="user_id" id="user_id" value="" type="text" maxlength="15" size="11" title="Campo requerido."></div></td>
                      <td nowrap="nowrap" class="cabecera">&nbsp;</td>
                    </tr>
                     <tr>
                        <td nowrap="nowrap"><div class="field-label"><label class="label" for="txtPass"><b>Ingrese contrase&ntilde;a</b></label></div></td>
                        <td nowrap="nowrap"><div class="cusField-label"><input class="text required" name="txtPass" id="txtPass" type="password" maxlength="40" size="20" ></div></td>															        
                        <td nowrap="nowrap" class="cabecera">&nbsp;</td>
                     </tr>
                     <tr>
                        <td width="" nowrap="nowrap"><div class="field-label"><label class="label" for="txtPass"><b>Repita la contrase&ntilde;a </b></label></div></td>
                        <td nowrap="nowrap"><div class="cusField-label"><input class="text required" name="txtPass2" id="txtPass2" type="password" maxlength="40" size="20" onblur="" ></div></td>															        
                        <td nowrap="nowrap" class="cabecera">&nbsp;</td>
                     </tr>
                     <!--
                     <tr>
                        <td nowrap="nowrap"><label class="label req" for="is_disabled">Estado</label></td>
                        <td>
                            <select class="combo" name="is_disabled" id="is_disabled" title="Seleccione el estado.">
                                <option value="N">Activo</option>
                                <option value="Y">Inactivo</option>
                            </select>
                        </td>
                        <td nowrap="nowrap" 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" onclick="">
                  <input class="button" type="button" value="Cancelar" onclick="parent.objMainTab.windowsCloseAll();"/>
                </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>
$( "#form1" ).validate
(
    {
    	rules: 
    	{
    		
    		txtPass: 
    		{
    			required: true,
    			minlength: 5
    		},
    		txtPass2: 
    		{
    			required: true,
    			minlength: 5,
    			equalTo: "#txtPass"
    		}
    	},
    	messages: 
    	{
    		txtPass: 
    		{
    			required: "Campo requerido.",
    			minlength: "Como minimo es 5 caracteres"
    		},
    		txtPass2: 
    		{
    			required: "Campo requerido.",
    			minlength: "Como minimo es 5 caracteres",
    			equalTo: "No coinciden los caracteres."
    		}
    	}
    }
);
</script>