<?php session_start();
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

?>

<html>
<head>
<title>siga -- promociones</title>
<meta name="generator" http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>

</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Gesti&oacute;n de Promociones</h2></td></tr></table>
<?php
  require_once('Connections/conn.php');
  mysql_select_db("pregrado_pregrado");
  mysql_query ("SET NAMES 'utf8'");
  
  $showrecs = 20;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>N&uacute;mero de Promociones <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="promociones.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Filtrar por</b>&nbsp;</td>
<td><input type="text" name="filter" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">Todos los Campos</option>
<option value="<?php echo "id_promocion" ?>"<?php if ($filterfield == "Id") { echo "selected"; } ?>><?php echo htmlspecialchars("Id") ?></option>
<option value="<?php echo "numero_promocion" ?>"<?php if ($filterfield == "numero_promocion") { echo "selected"; } ?>><?php echo htmlspecialchars("Número") ?></option>
<option value="<?php echo "fecha" ?>"<?php if ($filterfield == "fecha") { echo "selected"; } ?>><?php echo htmlspecialchars("Fecha") ?></option>
<option value="<?php echo "annio" ?>"<?php if ($filterfield == "annio") { echo "selected"; } ?>><?php echo htmlspecialchars("Año") ?></option>
<option value="<?php echo "libro" ?>"<?php if ($filterfield == "libro") { echo "selected"; } ?>><?php echo htmlspecialchars("Libro") ?></option>
<option value="<?php echo "acta" ?>"<?php if ($filterfield == "acta") { echo "selected"; } ?>><?php echo htmlspecialchars("Acta") ?></option>
<option value="<?php echo "folios" ?>"<?php if ($filterfield == "folios") { echo "selected"; } ?>><?php echo htmlspecialchars("Folios") ?></option>
<option value="<?php echo "lugar" ?>"<?php if ($filterfield == "lugar") { echo "selected"; } ?>><?php echo htmlspecialchars("Lugar") ?></option>
</select></td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Palabra Completa</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Aplicar Filtro"></td>
<td><a href="promociones.php?a=reset">Quitar Filtro</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr" align="center">Ver</td>
<td class="hr" align="center">Editar</td>
<td class="hr" align="center">Borrar</td>
<td class="hr" align="right" colspan="2"><a class="hr" href="promociones.php?order=<?php echo "id_promocion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Número") ?></a></td>

<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "decreto" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Decreto") ?></a></td>
<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "fecha" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Fecha") ?></a></td>
<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "annio" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Año") ?></a></td>
<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "libro" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Tomo") ?></a></td>
<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "acta" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Acta") ?></a></td>
<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "folios" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Folios") ?></a></td>
<td class="hr"><a class="hr" href="promociones.php?order=<?php echo "lugar" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Lugar") ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>

<td class="<?php echo $style ?>" align="center"><a href="promociones.php?a=view&recid=<?php echo $i ?>"><img src="imagenes/search.gif" border="0" ></a></td>
<td class="<?php echo $style ?>" align="center"><a href="promociones.php?a=edit&recid=<?php echo $i ?>"><img src="imagenes/edit.gif" border="0" ></a></td>
<td class="<?php echo $style ?>" align="center"><a href="promociones.php?a=del&recid=<?php echo $i ?>"><img src="imagenes/delete.png" border="0" ></a></td>
<td class="<?php echo $style ?>" align="right"><?php echo htmlspecialchars($row["id_promocion"]) ?></td>
<td class="<?php echo $style ?>" align="right"><?php echo htmlspecialchars($row["numero_promocion"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["decreto"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars(join ("-",(array_reverse(split("-",$row["fecha"]))))) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["annio"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["libro"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["acta"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["folios"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["lugar"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="90%">
<tr>
<td >Datos Generales</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_promocion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número (Romanos)")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["numero_promocion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Decreto del Consejo Académico")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["decreto"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha del Acto Académico")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars(join ("-",(array_reverse(split("-",$row["fecha"]))))) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Año")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["annio"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Nombre de Promoción")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombre_promocion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Padrinos")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["padrinos"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Lugar")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["lugar"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["status"]) ?></td>
</tr>

<tr>
<td >Datos del Libro de Actas</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número del Libro (Tomo)")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["libro"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número del Acta")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["acta"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Folios")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["folios"]) ?></td>
</tr>

</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="90%">
<tr>
<td >Datos Generales</td>
</tr>

<tr>
<td class="hr" rowspan="2"><?php echo htmlspecialchars("Número de Promoción")."&nbsp;" ?></td>
<td class="dr"><input name="numero_promocion" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["id_promocion"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>

<td class="dr"><input name="numero_promocion" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["numero_promocion"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Decreto del Consejo Académico")."&nbsp;" ?></td>
<td class="dr"><input name="decreto" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["decreto"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha del Acto Académico")."&nbsp;" ?></td>
<td class="dr"><input name="fecha" type="text" size="10" maxlength="10" value="<?php echo htmlspecialchars(join ("-",(array_reverse(split("-",$row["fecha"]))))) ?>" onKeyUp="mascara(this,'-',patron,true)" onBlur="return esFechaValida(this.value)">&nbsp;&nbsp;(dd-mm-aaaa)
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Año de la Independencia")."&nbsp;" ?></td>
<td class="dr"><input name="annio_inde" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["annio_inde"])) ?>" size="6" maxlength="6"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Año de la Federación")."&nbsp;" ?></td>
<td class="dr"><input name="annio_fede" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["annio_fede"])) ?>" size="6" maxlength="6"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Año")."&nbsp;" ?></td>
<td class="dr"><input disabled="disabled" name="annio" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["annio"])) ?>" size="4" maxlength="4"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Nombre de Promoción")."&nbsp;" ?></td>
<td class="dr"><input onKeyUp="this.value=this.value.toUpperCase();" name="nombre_promocion" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["nombre_promocion"])) ?>" size="50" maxlength="50"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Padrinos")."&nbsp;" ?></td>
<td class="dr"><textarea onKeyUp="this.value=this.value.toUpperCase();" cols="35" rows="4" name="padrinos" maxlength="160"><?php echo str_replace('"', '&quot;', trim($row["padrinos"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Lugar del Acto")."&nbsp;" ?></td>
<td class="dr"><input name="lugar" onKeyUp="this.value=this.value.toUpperCase();" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["lugar"])) ?>" size="40" maxlength="40"></td>
</tr>

<tr>
<td class="hr"><?php echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr"><select name="status" id="status">
  <option value="<?php echo str_replace('"', '&quot;', trim($row["status"])) ?>"><?php echo str_replace('"', '&quot;', trim($row["status"])) ?></option>
  <option value="ACTIVA">ACTIVA</option>
  <option value="CERRADA">CERRADA</option>
</select></td>
</tr>
<td >Datos del Libro de Actas</td>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número del Libro (Tomo)")."&nbsp;" ?></td>
<td class="dr"><input name="libro" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["libro"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número del Acta")."&nbsp;" ?></td>
<td class="dr"><input name="acta" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["acta"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Folios")."&nbsp;" ?></td>
<td class="dr"><input name="folios" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["folios"])) ?>" size="10" maxlength="10"></td>
</tr>

</table>
<?php } ?>

<?php function showrowadd($row, $iseditmode)
  {
  global $conn;
  
  mysql_select_db("pregrado_pregrado");
  mysql_query ("SET NAMES 'utf8'");
  
  $rs_id = mysql_query("SELECT MAX(id_promocion) AS id FROM promociones");
  if ($row_id = mysql_fetch_row($rs_id)) $id = trim($row_id[0])+1;
  $romano=to_roman($id);
  
  
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="90%">
<tr>
<td >Datos Generales</td>
</tr>
<tr>
<td class="hr" rowspan="2"><?php echo htmlspecialchars("Número de Promocion")."&nbsp;" ?></td>

<td class="dr"><input type="text" name="id_promocion" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($id)) ?>"></td>
</tr>
<tr>
<td class="dr"><input type="text" name="numero_promocion" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($romano)) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Decreto del Consejo Académico")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="decreto" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["decreto"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha del Acto Académico")."&nbsp;" ?></td>
<td class="dr"><input name="fecha" type="text" size="10" maxlength="10" onKeyUp="mascara(this,'-',patron,true)" onBlur="return esFechaValida(this.value)">&nbsp;&nbsp;(dd-mm-aaaa)</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Año de la Independencia")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="annio_inde" maxlength="6" value="<?php echo str_replace('"', '&quot;', trim($row["annio_inde"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Año de la Federación")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="annio_fede" maxlength="6" value="<?php echo str_replace('"', '&quot;', trim($row["annio_fede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Nombre de la Promoción")."&nbsp;" ?></td>
<td class="dr"><input name="nombre_promocion" onKeyUp="this.value=this.value.toUpperCase();" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["nombre_promocion"])) ?>" size="50" maxlength="50"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Padrinos")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" onKeyUp="this.value=this.value.toUpperCase();" name="padrinos" maxlength="160"><?php echo str_replace('"', '&quot;', trim($row["padrinos"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Lugar del Acto Académico")."&nbsp;" ?></td>
<td class="dr"><input name="lugar" onKeyUp="this.value=this.value.toUpperCase();" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["lugar"])) ?>" size="40" maxlength="40"></td>
</tr>
<tr>
<td >Datos del Libro de Actas</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número del Libro (Tomo)")."&nbsp;" ?></td>
<td class="dr"><input onKeyUp="this.value=this.value.toUpperCase();" name="libro" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["libro"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número del Acta")."&nbsp;" ?></td>
<td class="dr"><input name="acta" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["acta"])) ?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Folios")."&nbsp;" ?></td>
<td class="dr"><input name="folios" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["folios"])) ?>" size="10" maxlength="10"></td>
</tr>

</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="promociones.php?a=add">Agregar </a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="promociones.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Anterior</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="promociones.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="promociones.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="promociones.php?page=<?php echo $page + 1 ?>">Siguiente&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="promociones.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="promociones.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="promociones.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Siguiente Registro</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="promociones.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="promociones.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id_promocion" => "",
  "numero_promocion" => "",
  "decreto" => "",
  "fecha" => "",
  "annio" => "",
  "nombre_promocion" => "",
  "padrinos" => "",
  "libro" => "",
  "acta" => "",
  "folios" => "",
  "lugar" => "",
  "annio_fede" => "",
  "annio_inde" => "");
showrowadd($row, false);
?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="promociones.php?a=add">Agregar </a></td>
<td><a href="promociones.php?a=edit&recid=<?php echo $recid ?>">Editar </a></td>
<td><a href="promociones.php?a=del&recid=<?php echo $recid ?>">Borrar </a></td>
</tr>
</table>
<?php
  mysql_free_result($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="promociones.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid_promocion" value="<?php echo $row["id_promocion"] ?>">
<input type="hidden" name="xnumero_promocion" value="<?php echo $row["numero_promocion"] ?>">
<input type="hidden" name="xdecreto" value="<?php echo $row["decreto"] ?>">
<input type="hidden" name="xfecha" value="<?php echo $row["fecha"] ?>">
<input type="hidden" name="xannio" value="<?php echo $row["annio"] ?>">
<input type="hidden" name="xnombre_promocion" value="<?php echo $row["nombre_promocion"] ?>">
<input type="hidden" name="xlibro" value="<?php echo $row["libro"] ?>">
<input type="hidden" name="xacta" value="<?php echo $row["acta"] ?>">
<input type="hidden" name="xfolios" value="<?php echo $row["folios"] ?>">
<input type="hidden" name="xlugar" value="<?php echo $row["lugar"] ?>">
<input type="hidden" name="xannio_fede" value="<?php echo $row["annio_fede"] ?>">
<input type="hidden" name="xannio_inde" value="<?php echo $row["annio_inde"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="promociones.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid_promocion" value="<?php echo $row["id_promocion"] ?>">
<input type="hidden" name="xnumero_promocion" value="<?php echo $row["numero_promocion"] ?>">
<input type="hidden" name="xdecreto" value="<?php echo $row["decreto"] ?>">
<input type="hidden" name="xfecha" value="<?php echo $row["fecha"] ?>">
<input type="hidden" name="xannio" value="<?php echo $row["annio"] ?>">
<input type="hidden" name="xnombre_promocion" value="<?php echo $row["nombre_promocion"] ?>">
<input type="hidden" name="xlibro" value="<?php echo $row["libro"] ?>">
<input type="hidden" name="xacta" value="<?php echo $row["acta"] ?>">
<input type="hidden" name="xfolios" value="<?php echo $row["folios"] ?>">
<input type="hidden" name="xlugar" value="<?php echo $row["lugar"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirmar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
 
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT `id_promocion`, `numero_promocion`, `decreto`, `fecha`, `annio`, `nombre_promocion`, `padrinos`, `libro`, `acta`, `folios`, `lugar`, `annio_inde`, `annio_fede`,`status` FROM `promociones`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id_promocion` like '" .$filterstr ."') or (`numero_promocion` like '" .$filterstr ."') or (`fecha` like '" .$filterstr ."') or (`annio` like '" .$filterstr ."') or (`nombre_promocion` like '" .$filterstr ."') or (`padrinos` like '" .$filterstr ."') or (`libro` like '" .$filterstr ."') or (`acta` like '" .$filterstr ."') or (`folios` like '" .$filterstr ."') or (`lugar` like '" .$filterstr ."')";
  }
  $sql .= " order by id_promocion desc";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `promociones`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id_promocion` like '" .$filterstr ."') or (`numero_promocion` like '" .$filterstr ."') or (`decreto` like '" .$filterstr ."')  or (`fecha` like '" .$filterstr ."') or (`annio` like '" .$filterstr ."') or (`nombre_promocion` like '" .$filterstr ."') or (`padrinos` like '" .$filterstr ."') or (`libro` like '" .$filterstr ."') or (`acta` like '" .$filterstr ."') or (`folios` like '" .$filterstr ."') or (`lugar` like '" .$filterstr ."')";
  }
  $sql .= " order by id_promocion desc";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;
  
  $fecha=join ("-",(array_reverse(split("-",@$_POST["fecha"]))));
 
 
  $creditos_aprobados=0;
  $annio=substr($fecha,0,4);
  $status="ACTIVA";
  $sql = "insert into `promociones` (`numero_promocion`, `decreto`, `fecha`, `annio`, `nombre_promocion`, `padrinos`, `libro`, `acta`, `folios`, `lugar`, `annio_fede`,`annio_inde`,`status`) values (" .sqlvalue(@$_POST["numero_promocion"], true).", " .sqlvalue(@$_POST["decreto"], true).", '$fecha', '$annio', " .sqlvalue(@$_POST["nombre_promocion"], true).", " .sqlvalue(@$_POST["padrinos"], true).", " .sqlvalue(@$_POST["libro"], true).", " .sqlvalue(@$_POST["acta"], true).", " .sqlvalue(@$_POST["folios"], true).", " .sqlvalue(@$_POST["lugar"], true)."," .sqlvalue(@$_POST["annio_fede"], true)."," .sqlvalue(@$_POST["annio_inde"], true).",'$status')";
  mysql_query($sql, $conn) or die(mysql_error());
  
}

function sql_update()
{
  global $conn;
  global $_POST;
   $fecha=join ("-",(array_reverse(split("-",@$_POST["fecha"]))));
  $annio=substr($fecha,0,4);
  $promocion=@$_POST["numero_promocion"];	
  $sql = "update `promociones` set `numero_promocion`=" .sqlvalue(@$_POST["numero_promocion"], true).", `decreto`=" .sqlvalue(@$_POST["decreto"], true).", `fecha`='$fecha', `annio`='$annio', `nombre_promocion`=" .sqlvalue(@$_POST["nombre_promocion"], true).", `padrinos`=" .sqlvalue(@$_POST["padrinos"], true).", `status`=" .sqlvalue(@$_POST["status"], true).", `libro`=" .sqlvalue(@$_POST["libro"], true).", `acta`=" .sqlvalue(@$_POST["acta"], true).", `folios`=" .sqlvalue(@$_POST["folios"], true).", `annio_fede`=" .sqlvalue(@$_POST["annio_fede"], true).",`annio_inde`=" .sqlvalue(@$_POST["annio_inde"], true).",`lugar`=" .sqlvalue(@$_POST["lugar"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
  
  // Cambia la fecha de los graduandos.
  $sql = "update `egresados` set `fecha_egreso`='$fecha', `annio`='$annio' where promocion='$promocion'";
  mysql_query($sql, $conn) or die(mysql_error());
  
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `promociones` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id_promocion`";
  if (@$_POST["xid_promocion"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_promocion"], false);
  };
  $pk .= ")";
  return $pk;
}

function to_roman($num) {
if ($num <0 || $num >9999) {return -1;}
$r_ones = array(1=>"I", 2=>"II", 3=>"III", 4=>"IV", 5=>"V", 6=>"VI", 7=>"VII", 8=>"VIII",
9=>"IX");
$r_tens = array(1=>"X", 2=>"XX", 3=>"XXX", 4=>"XL", 5=>"L", 6=>"LX", 7=>"LXX",
8=>"LXXX", 9=>"XC");
$r_hund = array(1=>"C", 2=>"CC", 3=>"CCC", 4=>"CD", 5=>"D", 6=>"DC", 7=>"DCC",
8=>"DCCC", 9=>"CM");
$r_thou = array(1=>"M", 2=>"MM", 3=>"MMM", 4=>"MMMM", 5=>"MMMMM", 6=>"MMMMMM",
7=>"MMMMMMM", 8=>"MMMMMMMM", 9=>"MMMMMMMMM");
$ones = $num % 10;
$tens = ($num - $ones) % 100;
$hundreds = ($num - $tens - $ones) % 1000;
$thou = ($num - $hundreds - $tens - $ones) % 10000;
$tens = $tens / 10;
$hundreds = $hundreds / 100;
$thou = $thou / 1000;
if ($thou) {$rnum .= $r_thou[$thou];}
if ($hundreds) {$rnum .= $r_hund[$hundreds];}
if ($tens) {$rnum .= $r_tens[$tens];}
if ($ones) {$rnum .= $r_ones[$ones];}
return $rnum;
}
 ?>
 
 
 <script>
function esFechaValida(fecha){
    if (fecha != undefined && fecha != "" ){
       
    	var dia  =  parseInt(fecha.substring(0,2),10);
		var mes  =  parseInt(fecha.substring(3,5),10);
		var anio =  parseInt(fecha.substring(6,10),10);
	
		if (isNaN(dia) || isNaN(mes) || isNaN(anio)) {
		   alert("Formato de fecha no v�lido");
		   document.addAlumno.fechanac.focus();
       return false;
	 }
	
    switch(mes){
        case 1:
			numDias=31;
            break;
        case 3:
			numDias=31;
            break;
        case 5:
			numDias=31;
            break;
        case 7:
			numDias=31;
            break;
        case 8: 
			numDias=31;
            break;
        case 10:
			numDias=31;
            break;
        case 12:
            numDias=31;
            break;
        case 4: case 6: case 9: case 11:
            numDias=30;
            break;
        case 2:
           if (comprobarSiBisisesto(anio)){ numDias=29 }else{ numDias=28};
            break;
        default:
            alert("Error en fecha introducida (Mes Inv�lido)");
			document.addAlumno.fechanac.focus();
            return false;
    }
 
        if (dia>numDias || dia==0){
            alert("Error en fecha introducida (D�a Inv�lido)");
            document.addAlumno.fechanac.focus();
			return false;
        }
        return true;
    }
}
 
function comprobarSiBisisesto(anio){
if ( ( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {
    return true;
    }
else {
    return false;
    }
}
</script>

<script type="text/javascript">
var patron = new Array(2,2,4)
var patron1= new Array(1,2)
var patron2 = new Array(1,3,3,3,3)
function mascara(d,sep,pat,nums){
if(d.valant != d.value){
	val = d.value
	largo = val.length
	val = val.split(sep)
	val2 = ''
	for(r=0;r<val.length;r++){
		val2 += val[r]	
	}
	if(nums){
		for(z=0;z<val2.length;z++){
			if(isNaN(val2.charAt(z))){
				letra = new RegExp(val2.charAt(z),"g")
				val2 = val2.replace(letra,"")
			}
		}
	}
	val = ''
	val3 = new Array()
	for(s=0; s<pat.length; s++){
		val3[s] = val2.substring(0,pat[s])
		val2 = val2.substr(pat[s])
	}
	for(q=0;q<val3.length; q++){
		if(q ==0){
			val = val3[q]
		}
		else{
			if(val3[q] != ""){
				val += sep + val3[q]
				}
		}
	}
	d.value = val
	d.valant = val
	}
}

</script>


