<?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>ceulac_sadce -- preinscripciones</title>
<meta name="generator" http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #000000;
    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ón de Docentes</h2></td></tr></table>
<?php
  $conn = connect();
  $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>Tabla: preinscripciones</td></tr>
<tr><td>Número de Registros <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="preinscripciones.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 "tipodoc" ?>"<?php if ($filterfield == "tipodoc") { echo "selected"; } ?>><?php echo htmlspecialchars("tipodoc") ?></option>
<option value="<?php echo "cedula" ?>"<?php if ($filterfield == "cedula") { echo "selected"; } ?>><?php echo htmlspecialchars("cedula") ?></option>
<option value="<?php echo "nacionalidad" ?>"<?php if ($filterfield == "nacionalidad") { echo "selected"; } ?>><?php echo htmlspecialchars("nacionalidad") ?></option>
<option value="<?php echo "fechanac" ?>"<?php if ($filterfield == "fechanac") { echo "selected"; } ?>><?php echo htmlspecialchars("fechanac") ?></option>
<option value="<?php echo "apellidos" ?>"<?php if ($filterfield == "apellidos") { echo "selected"; } ?>><?php echo htmlspecialchars("apellidos") ?></option>
<option value="<?php echo "nombres" ?>"<?php if ($filterfield == "nombres") { echo "selected"; } ?>><?php echo htmlspecialchars("nombres") ?></option>
<option value="<?php echo "genero" ?>"<?php if ($filterfield == "genero") { echo "selected"; } ?>><?php echo htmlspecialchars("genero") ?></option>
<option value="<?php echo "edocivil" ?>"<?php if ($filterfield == "edocivil") { echo "selected"; } ?>><?php echo htmlspecialchars("edocivil") ?></option>
<option value="<?php echo "tlfcasa" ?>"<?php if ($filterfield == "tlfcasa") { echo "selected"; } ?>><?php echo htmlspecialchars("tlfcasa") ?></option>
<option value="<?php echo "tlfoficina" ?>"<?php if ($filterfield == "tlfoficina") { echo "selected"; } ?>><?php echo htmlspecialchars("tlfoficina") ?></option>
<option value="<?php echo "tlfcelular" ?>"<?php if ($filterfield == "tlfcelular") { echo "selected"; } ?>><?php echo htmlspecialchars("tlfcelular") ?></option>
<option value="<?php echo "email" ?>"<?php if ($filterfield == "email") { echo "selected"; } ?>><?php echo htmlspecialchars("email") ?></option>
<option value="<?php echo "id_programa" ?>"<?php if ($filterfield == "id_programa") { echo "selected"; } ?>><?php echo htmlspecialchars("id_programa") ?></option>
<option value="<?php echo "nom_programa" ?>"<?php if ($filterfield == "nom_programa") { echo "selected"; } ?>><?php echo htmlspecialchars("nom_programa") ?></option>
<option value="<?php echo "id_sede" ?>"<?php if ($filterfield == "id_sede") { echo "selected"; } ?>><?php echo htmlspecialchars("id_sede") ?></option>
<option value="<?php echo "nom_sede" ?>"<?php if ($filterfield == "nom_sede") { echo "selected"; } ?>><?php echo htmlspecialchars("nom_sede") ?></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="preinscripciones.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">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "tipodoc" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("tipodoc") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "cedula" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("cedula") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "nacionalidad" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nacionalidad") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "fechanac" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("fechanac") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "apellidos" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("apellidos") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "nombres" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nombres") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "genero" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("genero") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "edocivil" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("edocivil") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "tlfcasa" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("tlfcasa") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "tlfoficina" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("tlfoficina") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "tlfcelular" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("tlfcelular") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "email" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("email") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "id_programa" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id_programa") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "nom_programa" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nom_programa") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "id_sede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id_sede") ?></a></td>
<td class="hr"><a class="hr" href="preinscripciones.php?order=<?php echo "nom_sede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nom_sede") ?></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 ?>"><a href="preinscripciones.php?a=view&recid=<?php echo $i ?>">Ver</a></td>
<td class="<?php echo $style ?>"><a href="preinscripciones.php?a=edit&recid=<?php echo $i ?>">Editar</a></td>
<td class="<?php echo $style ?>"><a href="preinscripciones.php?a=del&recid=<?php echo $i ?>">Borrar</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["tipodoc"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["cedula"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nacionalidad"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["fechanac"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["apellidos"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nombres"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["genero"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["edocivil"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["tlfcasa"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["tlfoficina"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["tlfcelular"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["email"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_programa"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nom_programa"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_sede"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nom_sede"]) ?></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="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("tipodoc")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["tipodoc"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cedula")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["cedula"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nacionalidad")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nacionalidad"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("fechanac")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["fechanac"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("apellidos")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["apellidos"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nombres")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombres"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("genero")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["genero"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("edocivil")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["edocivil"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("tlfcasa")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["tlfcasa"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("tlfoficina")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["tlfoficina"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("tlfcelular")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["tlfcelular"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("email")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["email"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_programa")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_programa"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nom_programa")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nom_programa"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_sede")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_sede"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nom_sede")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nom_sede"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("tipodoc")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="tipodoc" maxlength="1" value="<?php echo str_replace('"', '&quot;', trim($row["tipodoc"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cedula")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="cedula" maxlength="12" value="<?php echo str_replace('"', '&quot;', trim($row["cedula"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nacionalidad")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nacionalidad" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["nacionalidad"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("fechanac")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="fechanac" value="<?php echo str_replace('"', '&quot;', trim($row["fechanac"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("apellidos")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="apellidos" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["apellidos"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nombres")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nombres" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["nombres"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("genero")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="genero" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["genero"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("edocivil")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="edocivil" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["edocivil"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("tlfcasa")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="tlfcasa" maxlength="20" value="<?php echo str_replace('"', '&quot;', trim($row["tlfcasa"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("tlfoficina")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="tlfoficina" maxlength="20" value="<?php echo str_replace('"', '&quot;', trim($row["tlfoficina"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("tlfcelular")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="tlfcelular" maxlength="20" value="<?php echo str_replace('"', '&quot;', trim($row["tlfcelular"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("email")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="email" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["email"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_programa")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_programa" maxlength="5" value="<?php echo str_replace('"', '&quot;', trim($row["id_programa"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nom_programa")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="nom_programa" maxlength="90"><?php echo str_replace('"', '&quot;', trim($row["nom_programa"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_sede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_sede" maxlength="2" value="<?php echo str_replace('"', '&quot;', trim($row["id_sede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nom_sede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nom_sede" maxlength="40" value="<?php echo str_replace('"', '&quot;', trim($row["nom_sede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("fecha_preins")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="fecha_preins" value="<?php echo str_replace('"', '&quot;', trim($row["fecha_preins"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("observaciones")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="observaciones" maxlength="120"><?php echo str_replace('"', '&quot;', trim($row["observaciones"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("status")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="status" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["status"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="preinscripciones.php?a=add">Agregar Docente</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="preinscripciones.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="preinscripciones.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="preinscripciones.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="preinscripciones.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="preinscripciones.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="preinscripciones.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="preinscripciones.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="preinscripciones.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="preinscripciones.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "tipodoc" => "",
  "cedula" => "",
  "nacionalidad" => "",
  "fechanac" => "",
  "apellidos" => "",
  "nombres" => "",
  "genero" => "",
  "edocivil" => "",
  "tlfcasa" => "",
  "tlfoficina" => "",
  "tlfcelular" => "",
  "email" => "",
  "id_programa" => "",
  "nom_programa" => "",
  "id_sede" => "",
  "nom_sede" => "",
  "fecha_preins" => "",
  "observaciones" => "",
  "status" => "");
showroweditor($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="preinscripciones.php?a=add">Agregar Docente</a></td>
<td><a href="preinscripciones.php?a=edit&recid=<?php echo $recid ?>">Editar Docente</a></td>
<td><a href="preinscripciones.php?a=del&recid=<?php echo $recid ?>">Borrar Docente</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="preinscripciones.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xcedula" value="<?php echo $row["cedula"] ?>">
<?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="preinscripciones.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xcedula" value="<?php echo $row["cedula"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirmar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysql_connect("localhost", "ceula_sadce_admi", "j7BnJdCGzyQBGcud");
  mysql_select_db("ceulac_sadce");
  return $conn;
}

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 `tipodoc`, `cedula`, `nacionalidad`, `fechanac`, `apellidos`, `nombres`, `genero`, `edocivil`, `tlfcasa`, `tlfoficina`, `tlfcelular`, `email`, `id_programa`, `nom_programa`, `id_sede`, `nom_sede`, `fecha_preins`, `observaciones`, `status` FROM `preinscripciones`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`tipodoc` like '" .$filterstr ."') or (`cedula` like '" .$filterstr ."') or (`nacionalidad` like '" .$filterstr ."') or (`fechanac` like '" .$filterstr ."') or (`apellidos` like '" .$filterstr ."') or (`nombres` like '" .$filterstr ."') or (`genero` like '" .$filterstr ."') or (`edocivil` like '" .$filterstr ."') or (`tlfcasa` like '" .$filterstr ."') or (`tlfoficina` like '" .$filterstr ."') or (`tlfcelular` like '" .$filterstr ."') or (`email` like '" .$filterstr ."') or (`id_programa` like '" .$filterstr ."') or (`nom_programa` like '" .$filterstr ."') or (`id_sede` like '" .$filterstr ."') or (`nom_sede` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  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 `preinscripciones`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`tipodoc` like '" .$filterstr ."') or (`cedula` like '" .$filterstr ."') or (`nacionalidad` like '" .$filterstr ."') or (`fechanac` like '" .$filterstr ."') or (`apellidos` like '" .$filterstr ."') or (`nombres` like '" .$filterstr ."') or (`genero` like '" .$filterstr ."') or (`edocivil` like '" .$filterstr ."') or (`tlfcasa` like '" .$filterstr ."') or (`tlfoficina` like '" .$filterstr ."') or (`tlfcelular` like '" .$filterstr ."') or (`email` like '" .$filterstr ."') or (`id_programa` like '" .$filterstr ."') or (`nom_programa` like '" .$filterstr ."') or (`id_sede` like '" .$filterstr ."') or (`nom_sede` 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 `preinscripciones` (`tipodoc`, `cedula`, `nacionalidad`, `fechanac`, `apellidos`, `nombres`, `genero`, `edocivil`, `tlfcasa`, `tlfoficina`, `tlfcelular`, `email`, `id_programa`, `nom_programa`, `id_sede`, `nom_sede`, `fecha_preins`, `observaciones`, `status`) values (" .sqlvalue(@$_POST["tipodoc"], true).", " .sqlvalue(@$_POST["cedula"], true).", " .sqlvalue(@$_POST["nacionalidad"], true).", " .sqlvalue(@$_POST["fechanac"], true).", " .sqlvalue(@$_POST["apellidos"], true).", " .sqlvalue(@$_POST["nombres"], true).", " .sqlvalue(@$_POST["genero"], true).", " .sqlvalue(@$_POST["edocivil"], true).", " .sqlvalue(@$_POST["tlfcasa"], true).", " .sqlvalue(@$_POST["tlfoficina"], true).", " .sqlvalue(@$_POST["tlfcelular"], true).", " .sqlvalue(@$_POST["email"], true).", " .sqlvalue(@$_POST["id_programa"], true).", " .sqlvalue(@$_POST["nom_programa"], true).", " .sqlvalue(@$_POST["id_sede"], true).", " .sqlvalue(@$_POST["nom_sede"], true).", " .sqlvalue(@$_POST["fecha_preins"], true).", " .sqlvalue(@$_POST["observaciones"], true).", " .sqlvalue(@$_POST["status"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

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

  $sql = "update `preinscripciones` set `tipodoc`=" .sqlvalue(@$_POST["tipodoc"], true).", `cedula`=" .sqlvalue(@$_POST["cedula"], true).", `nacionalidad`=" .sqlvalue(@$_POST["nacionalidad"], true).", `fechanac`=" .sqlvalue(@$_POST["fechanac"], true).", `apellidos`=" .sqlvalue(@$_POST["apellidos"], true).", `nombres`=" .sqlvalue(@$_POST["nombres"], true).", `genero`=" .sqlvalue(@$_POST["genero"], true).", `edocivil`=" .sqlvalue(@$_POST["edocivil"], true).", `tlfcasa`=" .sqlvalue(@$_POST["tlfcasa"], true).", `tlfoficina`=" .sqlvalue(@$_POST["tlfoficina"], true).", `tlfcelular`=" .sqlvalue(@$_POST["tlfcelular"], true).", `email`=" .sqlvalue(@$_POST["email"], true).", `id_programa`=" .sqlvalue(@$_POST["id_programa"], true).", `nom_programa`=" .sqlvalue(@$_POST["nom_programa"], true).", `id_sede`=" .sqlvalue(@$_POST["id_sede"], true).", `nom_sede`=" .sqlvalue(@$_POST["nom_sede"], true).", `fecha_preins`=" .sqlvalue(@$_POST["fecha_preins"], true).", `observaciones`=" .sqlvalue(@$_POST["observaciones"], true).", `status`=" .sqlvalue(@$_POST["status"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `preinscripciones` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`cedula`";
  if (@$_POST["xcedula"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xcedula"], true);
  };
  $pk .= ")";
  return $pk;
}
 ?>
