<?php require_once('Connections/conn.php'); ?>
<?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_postgrado -- cohortes</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 { 
    background-color: #FFFFFF01;
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    background-color: #FFFFFF01;
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    background-color: #FFFFFF01;
    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 Cohortes</tr></table>
<?php
   require_once('Connections/conn.php');
  mysql_select_db("siga_postgrado");
  mysql_query ("SET NAMES 'utf8'");
  
  $showrecs = 20;
  $pagerange = 20;

  $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">!SIGA LA ENAHP!</td></tr></table>
</body>
</html>

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


  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>Cohortes: <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="cohortes.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 "num_cohorte" ?>"<?php if ($filterfield == "num_cohorte") { echo "selected"; } ?>>N&uacute;mero</option>
<option value="<?php echo "programa" ?>"<?php if ($filterfield == "programa") { echo "selected"; } ?>><?php echo htmlspecialchars("Programa") ?></option>
<option value="<?php echo "sede" ?>"<?php if ($filterfield == "sede") { echo "selected"; } ?>><?php echo htmlspecialchars("Sede") ?></option>
<option value="<?php echo "turno" ?>"<?php if ($filterfield == "turno") { echo "selected"; } ?>><?php echo htmlspecialchars("Turno") ?></option>
<option value="<?php echo "periodo_inicial" ?>"<?php if ($filterfield == "periodo_inicial") { echo "selected"; } ?>><?php echo htmlspecialchars("Lapso Inicial") ?></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="cohortes.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"><a class="hr" href="cohortes.php?order=<?php echo "num_cohorte" ?>&type=<?php echo $ordtypestr ?>">N&uacute;mero</a></td>
<td class="hr"><a class="hr" href="cohortes.php?order=<?php echo "programa" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Programa") ?></a></td>
<td class="hr"><a class="hr" href="cohortes.php?order=<?php echo "sede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Sede") ?></a></td>
<td class="hr"><a class="hr" href="cohortes.php?order=<?php echo "turno" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Turno") ?></a></td>
<td class="hr"><a class="hr" href="cohortes.php?order=<?php echo "periodo_inicial" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Lapso de Inicio") ?></a></td>
</tr>
<?php
  mysql_select_db("siga_postgrado");
  mysql_query ("SET NAMES 'utf8'");
  
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
  $id_periodo=$row["periodo_inicial"];
  $id_sede=$row["sede"];
  $id_turno=$row["turno"];
  
  $sql_periodo = "SELECT codigo FROM periodos where id_periodo='$id_periodo'";
  $rst_periodo = mysql_query($sql_periodo, $conn) or die(mysql_error());
  $row_periodo = mysql_fetch_assoc($rst_periodo);
  $periodo = $row_periodo["codigo"];
  
  $sql_turnos = "SELECT turno FROM turnos where codigo='$id_turno'";

  $rst_turnos = mysql_query($sql_turnos, $conn) or die(mysql_error());
  $row_turnos = mysql_fetch_assoc($rst_turnos);
  $turno = $row_turnos["turno"];
  
  $sql_sede = "SELECT nombre_sede FROM sedes where codigo='$id_sede'";
  $rst_sede = mysql_query($sql_sede, $conn) or die(mysql_error());
  $row_sede = mysql_fetch_assoc($rst_sede);
  $sede = $row_sede["nombre_sede"];
  
  	
?>
<tr>
<td class="<?php echo $style ?>" align="center"><a href="cohortes.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="cohortes.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="cohortes.php?a=del&recid=<?php echo $i ?>"><img src="imagenes/delete.png" border="0" ></a></td>

<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["num_cohorte"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["programa"]) ?></td>
<td class="<?php echo $style ?>"><?php echo utf8_decode($sede) ?></td>
<td class="<?php echo $style ?>"><?php echo utf8_decode($turno) ?></td>
<td class="<?php echo $style ?>"><?php echo $periodo; ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function showrow($row, $recid)
  {
  global $conn;
  
  $id_periodo=$row["periodo_inicial"];
  $id_sede=$row["sede"];
  $id_turno=$row["turno"];
  
  $sql_periodo = "SELECT codigo FROM periodos where id_periodo='$id_periodo'";
  $rst_periodo = mysql_query($sql_periodo, $conn) or die(mysql_error());
  $row_periodo = mysql_fetch_assoc($rst_periodo);
  $periodo = $row_periodo["codigo"];
  
  $sql_turnos = "SELECT turno FROM turnos where codigo='$id_turno'";
  $rst_turnos = mysql_query($sql_turnos, $conn) or die(mysql_error());
  $row_turnos = mysql_fetch_assoc($rst_turnos);
  $turno = $row_turnos["turno"];
  
  $sql_sede = "SELECT nombre_sede FROM sedes where codigo='$id_sede'";
  $rst_sede = mysql_query($sql_sede, $conn) or die(mysql_error());
  $row_sede = mysql_fetch_assoc($rst_sede);
  $sede = $row_sede["nombre_sede"];
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr">N&uacute;mero&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["num_cohorte"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Programa")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["programa"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Sede")."&nbsp;" ?></td>
<td class="dr"><?php echo utf8_decode($sede) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Turno")."&nbsp;" ?></td>
<td class="dr"><?php echo utf8_decode($turno) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Lapso de Inicio")."&nbsp;" ?></td>
<td class="dr"><?php echo $periodo ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
  
  $id_periodo=$row["periodo_inicial"];
  $id_sede=$row["sede"];
  $id_turno=$row["turno"];
  
  $sql_periodo = "SELECT codigo FROM periodos where id_periodo='$id_periodo'";
  $rst_periodo = mysql_query($sql_periodo, $conn) or die(mysql_error());
  $row_periodo = mysql_fetch_assoc($rst_periodo);
  $codigo_periodo = $row_periodo["codigo"];
  
  $sql_turnos = "SELECT turno FROM turnos where codigo='$id_turno'";
  $rst_turnos = mysql_query($sql_turnos, $conn) or die(mysql_error());
  $row_turnos = mysql_fetch_assoc($rst_turnos);
  $turno = $row_turnos["turno"];
  
  $sql_sede = "SELECT ubicacion_sede,nombre_sede FROM sedes where codigo='$id_sede'";
  $rst_sede = mysql_query($sql_sede, $conn) or die(mysql_error());
  $row_sede = mysql_fetch_assoc($rst_sede);
  $sede = $row_sede["nombre_sede"].' - '.$row_sede["nombre_sede"];
  
  $id_programa=$row["programa"];
  $sql_programa = "SELECT nombre_carrera FROM carreras where id_carrera='$id_programa'";
  $rst_programa = mysql_query($sql_programa, $conn) or die(mysql_error());
  $row_programa = mysql_fetch_assoc($rst_programa);
  $nombre_carrera = $row_programa["nombre_carrera"];

   
  $sql_periodo = "SELECT id_periodo, codigo FROM periodos";
  $rst_periodo = mysql_query($sql_periodo, $conn) or die(mysql_error());
  $row_periodo = mysql_fetch_assoc($rst_periodo);
  $totalRows_periodo = mysql_num_rows($rst_periodo);

   
  $sql_turnos = "SELECT codigo, turno FROM turnos";
  $rst_turnos = mysql_query($sql_turnos, $conn) or die(mysql_error());
  $row_turnos = mysql_fetch_assoc($rst_turnos);
  $totalRows_turno = mysql_num_rows($rst_turnos);
	 
	 
  $sql_sede = "SELECT codigo, ubicacion_sede, nombre_sede FROM sedes order by ubicacion_sede,nombre_sede";
  $rst_sede = mysql_query($sql_sede, $conn) or die(mysql_error());
  $row_sede = mysql_fetch_assoc($rst_sede);
  $totalRows_sede = mysql_num_rows($rst_sede);
  
  
  
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr">N&uacute;mero</td>
<td class="dr"><input name="num_cohorte" type="text" disabled="disabled" value="<?php echo str_replace('"', '&quot;', trim($row["num_cohorte"])) ?>" size="2" maxlength="3"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Programa")."&nbsp;" ?></td>
<td class="dr">
<select id="programa" name="programa" >
<option value="<?php echo $row["programa"]; ?>"><?php echo $nombre_carrera ?></option>
</select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Sede")."&nbsp;" ?></td>
<td class="dr">
<select id="sede" name="sede" >
<option value="<?php echo $row["sede"]; ?>"><?php echo $sede ?></option>
  <?php
do {  
?>
  <option value="<?php echo $row_sede['codigo_sede']?>"><?php echo $row_sede['ubicacion_sede'].' - '.$row_sede['nombre_sede']?></option>
  <?php
} while ($row_sede = mysql_fetch_assoc($rst_sede));
  $rows = mysql_num_rows($rst_sede);
  if($rows > 0) {
      mysql_data_seek($rst_sede, 0);
	  $row_sede = mysql_fetch_assoc($rst_sede);
  }
?>
</select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Turno")."&nbsp;" ?></td>
<td class="dr">
<select id="turno" name="turno" >
<option value="<?php echo $row["turno"]; ?>"><?php echo $turno ?></option>
  <?php
do {  
?>
  <option value="<?php echo $row_turnos['codigo']?>"><?php echo $row_turnos['turno']?></option>
  <?php
} while ($row_turnos = mysql_fetch_assoc($rst_turnos));
  $rows = mysql_num_rows($rst_turnos);
  if($rows > 0) {
      mysql_data_seek($rst_turnos, 0);
	  $row_turnos = mysql_fetch_assoc($rst_turnos);
  }
?>
</select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Lapso de Inicio")."&nbsp;" ?></td>
<td class="dr">
<select id="periodo_inicial" name="periodo_inicial" >
<option value="<?php echo $row["periodo_inicial"]; ?>"><?php echo $codigo_periodo ?></option>
  <?php
do {  
?>
  <option value="<?php echo $row_periodo['id_periodo']?>"><?php echo $row_periodo['codigo']?></option>
  <?php
} while ($row_periodo = mysql_fetch_assoc($rst_periodo));
  $rows = mysql_num_rows($rst_periodo);
  if($rows > 0) {
      mysql_data_seek($rst_periodo, 0);
	  $row_periodo = mysql_fetch_assoc($rst_periodo);
  }
?>
</select>
</td>
</tr>
</table>
<?php } ?>


<?php function showrowadd($row, $iseditmode)
  {
  global $conn;
  
  $sql_programa = "SELECT id_carrera, nombre_carrera FROM carreras order by nombre_carrera";
  $rst_programa = mysql_query($sql_programa, $conn) or die(mysql_error());
  $row_programa = mysql_fetch_assoc($rst_programa);
  

   
  $sql_periodo = "SELECT id_periodo, codigo FROM periodos where status='ABIERTO' order by id_periodo desc ";
  $rst_periodo = mysql_query($sql_periodo, $conn) or die(mysql_error());
  $row_periodo = mysql_fetch_assoc($rst_periodo);
  $totalRows_periodo = mysql_num_rows($rst_periodo);

   
  $sql_turnos = "SELECT codigo, turno FROM turnos";
  $rst_turnos = mysql_query($sql_turnos, $conn) or die(mysql_error());
  $row_turnos = mysql_fetch_assoc($rst_turnos);
  $totalRows_turno = mysql_num_rows($rst_turnos);
	 
	 
  $sql_sede = "SELECT codigo, ubicacion_sede, nombre_sede FROM sedes order by ubicacion_sede,nombre_sede";
  $rst_sede = mysql_query($sql_sede, $conn) or die(mysql_error());
  $row_sede = mysql_fetch_assoc($rst_sede);
  $totalRows_sede = mysql_num_rows($rst_sede);
  
  
  
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr">N&uacute;mero</td>
<td class="dr"><input name="num_cohorte" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["num_cohorte"])) ?>" size="2" maxlength="3" onKeyPress="return soloNum(event)" ></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Programa")."&nbsp;" ?></td>
<td class="dr">
<select id="programa" name="programa" onBlur="verificar_cohorte(this.value, document.form1.num_cohorte.value)">
<option value="0">Seleccione...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_programa['id_carrera']?>"><?php echo $row_programa['nombre_carrera'] ?></option>
  <?php
} while ($row_programa = mysql_fetch_assoc($rst_programa));
  $rows = mysql_num_rows($rst_programa);
  if($rows > 0) {
      mysql_data_seek($rst_programa, 0);
	  $row_programa = mysql_fetch_assoc($rst_programa);
  }
?>
</select>
<iframe name="frame_cohorte" id="frame_cohorte" with="0" height="0" frameborder="0" ></iframe>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Sede")."&nbsp;" ?></td>
<td class="dr">
<select id="sede" name="sede" >
<option value="0">Seleccione...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_sede['codigo']?>"><?php echo $row_sede['ubicacion_sede'].' - '.$row_sede['nombre_sede']?></option>
  <?php
} while ($row_sede = mysql_fetch_assoc($rst_sede));
  $rows = mysql_num_rows($rst_sede);
  if($rows > 0) {
      mysql_data_seek($rst_sede, 0);
	  $row_sede = mysql_fetch_assoc($rst_sede);
  }
?>
</select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Turno")."&nbsp;" ?></td>
<td class="dr">
<select id="turno" name="turno" >
<option value="0">Seleccione...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_turnos['codigo']?>"><?php echo $row_turnos['turno']?></option>
  <?php
} while ($row_turnos = mysql_fetch_assoc($rst_turnos));
  $rows = mysql_num_rows($rst_turnos);
  if($rows > 0) {
      mysql_data_seek($rst_turnos, 0);
	  $row_turnos = mysql_fetch_assoc($rst_turnos);
  }
?>
</select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Lapso de Inicio")."&nbsp;" ?></td>
<td class="dr">
<select id="periodo_inicial" name="periodo_inicial" >
<option value="0">Seleccione...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_periodo['id_periodo']?>"><?php echo $row_periodo['codigo']?></option>
  <?php
} while ($row_periodo = mysql_fetch_assoc($rst_periodo));
  $rows = mysql_num_rows($rst_periodo);
  if($rows > 0) {
      mysql_data_seek($rst_periodo, 0);
	  $row_periodo = mysql_fetch_assoc($rst_periodo);
  }
?>
</select>
</td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="cohortes.php?a=add">Agregar </a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="cohortes.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="cohortes.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="cohortes.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="cohortes.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="cohortes.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="cohortes.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="cohortes.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="cohortes.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>

<form name="form1" id="form1" enctype="multipart/form-data" action="cohortes.php" method="post" onSubmit="return validar_datos()">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "num_cohorte" => "",
  "programa" => "",
  "sede" => "",
  "turno" => "",
  "periodo_inicial" => "");
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>

<?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="cohortes.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xnum_cohorte" value="<?php echo $row["num_cohorte"] ?>">
<input type="hidden" name="xprograma" value="<?php echo $row["programa"] ?>">
<input type="hidden" name="xsede" value="<?php echo $row["sede"] ?>">
<input type="hidden" name="xturno" value="<?php echo $row["turno"] ?>">
<input type="hidden" name="xperiodo_inicial" value="<?php echo $row["periodo_inicial"] ?>">
<?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="cohortes.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xnum_cohorte" value="<?php echo $row["num_cohorte"] ?>">
<input type="hidden" name="xprograma" value="<?php echo $row["programa"] ?>">
<input type="hidden" name="xsede" value="<?php echo $row["sede"] ?>">
<input type="hidden" name="xturno" value="<?php echo $row["turno"] ?>">
<input type="hidden" name="xperiodo_inicial" value="<?php echo $row["periodo_inicial"] ?>">
<?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 `num_cohorte`, `programa`, `sede`, `turno`, `periodo_inicial` FROM `cohortes`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`num_cohorte` like '" .$filterstr ."') or (`programa` like '" .$filterstr ."') or (`sede` like '" .$filterstr ."') or (`turno` like '" .$filterstr ."') or (`periodo_inicial` like '" .$filterstr ."')";
  }
  $sql .= " order by programa, num_cohorte";
  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 `cohortes`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`num_cohorte` like '" .$filterstr ."') or (`programa` like '" .$filterstr ."') or (`sede` like '" .$filterstr ."') or (`turno` like '" .$filterstr ."') or (`periodo_inicial` like '" .$filterstr ."')";
  }
  $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;

  $sql = "insert into `cohortes` (`num_cohorte`, `programa`, `sede`, `turno`, `periodo_inicial`) values (" .sqlvalue(@$_POST["num_cohorte"], false).", " .sqlvalue(@$_POST["programa"], true).", " .sqlvalue(@$_POST["sede"], true).", " .sqlvalue(@$_POST["turno"], true).", " .sqlvalue(@$_POST["periodo_inicial"], false).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update `cohortes` set `num_cohorte`=" .sqlvalue(@$_POST["num_cohorte"], false).", `programa`=" .sqlvalue(@$_POST["programa"], true).", `sede`=" .sqlvalue(@$_POST["sede"], true).", `turno`=" .sqlvalue(@$_POST["turno"], true).", `periodo_inicial`=" .sqlvalue(@$_POST["periodo_inicial"], false) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `cohortes` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`num_cohorte`";
  if (@$_POST["xnum_cohorte"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xnum_cohorte"], false);
  };
  $pk .= ") and ";
  $pk .= "(`programa`";
  if (@$_POST["xprograma"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xprograma"], true);
  };
  
  $pk .= ")";
  return $pk;
}
 ?>
 
 <script type="text/javascript">
	function validar_datos()
	{
	    
	 			
		if(document.form1.num_cohorte.value==""){
			alert("El campo <<Número de Cohorte>> es obligatorio");
			document.form1.num_cohorte.focus();
			return false;
			}
		if(document.form1.programa.value==0){
			alert("El campo <<Programa>> es obligatorio");
			document.form1.programa.focus();
			return false;
			}
	    if(document.form1.sede.value==0){
			alert("El campo <<Sede>> es obligatorio");
			document.form1.sede.focus();
			return false;
			}
		if(document.form1.turno.value==0){
			alert("El campo <<Turno>> es obligatorio");
			document.form1.turno.focus();
			return false;
			}
		if(document.form1.periodo_inicial.value==0){
			alert("El campo <<Lapso de Inicio>> es obligatorio");
			document.form1.periodo_inicial.focus();
			return false;
			}	
	    return true;
	}
	
</script>	

<script type="text/javascript">
	var nav4 = window.Event ? true : false;
	function soloNum(evt)
	{	
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, ','= 44, '.'= 46
		var key = nav4 ? evt.which : evt.keyCode;	
		//var str=obj.value;
	
		return (key == 8 || key == 13 || (key >= 48 && key <= 57));
	}
</script>	

<script type="text/javascript">
function verificar_cohorte(programa, numero)
{
  url="validar_cohorte.php?numero="+numero+"&programa="+programa;
  frame_cohorte.location=url;
}
</script>
