<?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 -- sedes</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>Gestion de Cohortes</tr></table>
<?php
  $conn = connect();
  $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;


  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: sedes</td></tr>
<tr><td>Registros <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="sedes_new.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 "codigo" ?>"<?php if ($filterfield == "codigo") { echo "selected"; } ?>><?php echo htmlspecialchars("codigo") ?></option>
<option value="<?php echo "nombre_sede" ?>"<?php if ($filterfield == "nombre_sede") { echo "selected"; } ?>><?php echo htmlspecialchars("nombre_sede") ?></option>
<option value="<?php echo "ubicacion_sede" ?>"<?php if ($filterfield == "ubicacion_sede") { echo "selected"; } ?>><?php echo htmlspecialchars("ubicacion_sede") ?></option>
<option value="<?php echo "codigo_opsu" ?>"<?php if ($filterfield == "codigo_opsu") { echo "selected"; } ?>><?php echo htmlspecialchars("codigo_opsu") ?></option>
<option value="<?php echo "ced_firma" ?>"<?php if ($filterfield == "ced_firma") { echo "selected"; } ?>><?php echo htmlspecialchars("ced_firma") ?></option>
<option value="<?php echo "titulo_firma" ?>"<?php if ($filterfield == "titulo_firma") { echo "selected"; } ?>><?php echo htmlspecialchars("titulo_firma") ?></option>
<option value="<?php echo "nombres_firma" ?>"<?php if ($filterfield == "nombres_firma") { echo "selected"; } ?>><?php echo htmlspecialchars("nombres_firma") ?></option>
<option value="<?php echo "apellidos_firma" ?>"<?php if ($filterfield == "apellidos_firma") { echo "selected"; } ?>><?php echo htmlspecialchars("apellidos_firma") ?></option>
<option value="<?php echo "status" ?>"<?php if ($filterfield == "status") { echo "selected"; } ?>><?php echo htmlspecialchars("status") ?></option>
<option value="<?php echo "siglas" ?>"<?php if ($filterfield == "siglas") { echo "selected"; } ?>><?php echo htmlspecialchars("siglas") ?></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="sedes_new.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="sedes_new.php?order=<?php echo "codigo" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("codigo") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "nombre_sede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nombre_sede") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "ubicacion_sede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("ubicacion_sede") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "codigo_opsu" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("codigo_opsu") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "ced_firma" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("ced_firma") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "titulo_firma" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("titulo_firma") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "nombres_firma" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nombres_firma") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "apellidos_firma" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("apellidos_firma") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "status" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("status") ?></a></td>
<td class="hr"><a class="hr" href="sedes_new.php?order=<?php echo "siglas" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("siglas") ?></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="sedes_new.php?a=view&recid=<?php echo $i ?>">Ver</a></td>
<td class="<?php echo $style ?>"><a href="sedes_new.php?a=edit&recid=<?php echo $i ?>">Editar</a></td>
<td class="<?php echo $style ?>"><a href="sedes_new.php?a=del&recid=<?php echo $i ?>">Borrar</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["codigo"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nombre_sede"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["ubicacion_sede"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["codigo_opsu"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["ced_firma"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["titulo_firma"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nombres_firma"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["apellidos_firma"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["status"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["siglas"]) ?></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("codigo")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["codigo"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nombre_sede")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombre_sede"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ubicacion_sede")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["ubicacion_sede"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("codigo_opsu")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["codigo_opsu"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ced_firma")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["ced_firma"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("titulo_firma")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["titulo_firma"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nombres_firma")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombres_firma"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("apellidos_firma")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["apellidos_firma"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("status")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["status"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("siglas")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["siglas"]) ?></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("codigo")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="codigo" maxlength="2" value="<?php echo str_replace('"', '&quot;', trim($row["codigo"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nombre_sede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nombre_sede" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["nombre_sede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ubicacion_sede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="ubicacion_sede" maxlength="40" value="<?php echo str_replace('"', '&quot;', trim($row["ubicacion_sede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("codigo_opsu")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="codigo_opsu" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["codigo_opsu"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ced_firma")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="ced_firma" maxlength="12" value="<?php echo str_replace('"', '&quot;', trim($row["ced_firma"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("titulo_firma")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="titulo_firma" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["titulo_firma"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nombres_firma")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nombres_firma" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["nombres_firma"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("apellidos_firma")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="apellidos_firma" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["apellidos_firma"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("status")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="status" maxlength="1" value="<?php echo str_replace('"', '&quot;', trim($row["status"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("siglas")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="siglas" maxlength="3" value="<?php echo str_replace('"', '&quot;', trim($row["siglas"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="sedes_new.php?a=add">Agregar </a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="sedes_new.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="sedes_new.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="sedes_new.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="sedes_new.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="sedes_new.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="sedes_new.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="sedes_new.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="sedes_new.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="sedes_new.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "codigo" => "",
  "nombre_sede" => "",
  "ubicacion_sede" => "",
  "codigo_opsu" => "",
  "ced_firma" => "",
  "titulo_firma" => "",
  "nombres_firma" => "",
  "apellidos_firma" => "",
  "status" => "",
  "siglas" => "");
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="sedes_new.php?a=add">Agregar </a></td>
<td><a href="sedes_new.php?a=edit&recid=<?php echo $recid ?>">Editar </a></td>
<td><a href="sedes_new.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="sedes_new.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xcodigo" value="<?php echo $row["codigo"] ?>">
<input type="hidden" name="xnombre_sede" value="<?php echo $row["nombre_sede"] ?>">
<input type="hidden" name="xubicacion_sede" value="<?php echo $row["ubicacion_sede"] ?>">
<?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="sedes_new.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xcodigo" value="<?php echo $row["codigo"] ?>">
<input type="hidden" name="xnombre_sede" value="<?php echo $row["nombre_sede"] ?>">
<input type="hidden" name="xubicacion_sede" value="<?php echo $row["ubicacion_sede"] ?>">
<?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", "siga_admin", "j7BnJdCGzyQBGcud");
  mysql_select_db("siga_postgrado");
  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 `codigo`, `nombre_sede`, `ubicacion_sede`, `codigo_opsu`, `ced_firma`, `titulo_firma`, `nombres_firma`, `apellidos_firma`, `status`, `siglas` FROM `sedes`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`codigo` like '" .$filterstr ."') or (`nombre_sede` like '" .$filterstr ."') or (`ubicacion_sede` like '" .$filterstr ."') or (`codigo_opsu` like '" .$filterstr ."') or (`ced_firma` like '" .$filterstr ."') or (`titulo_firma` like '" .$filterstr ."') or (`nombres_firma` like '" .$filterstr ."') or (`apellidos_firma` like '" .$filterstr ."') or (`status` like '" .$filterstr ."') or (`siglas` 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 `sedes`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`codigo` like '" .$filterstr ."') or (`nombre_sede` like '" .$filterstr ."') or (`ubicacion_sede` like '" .$filterstr ."') or (`codigo_opsu` like '" .$filterstr ."') or (`ced_firma` like '" .$filterstr ."') or (`titulo_firma` like '" .$filterstr ."') or (`nombres_firma` like '" .$filterstr ."') or (`apellidos_firma` like '" .$filterstr ."') or (`status` like '" .$filterstr ."') or (`siglas` 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 `sedes` (`codigo`, `nombre_sede`, `ubicacion_sede`, `codigo_opsu`, `ced_firma`, `titulo_firma`, `nombres_firma`, `apellidos_firma`, `status`, `siglas`) values (" .sqlvalue(@$_POST["codigo"], true).", " .sqlvalue(@$_POST["nombre_sede"], true).", " .sqlvalue(@$_POST["ubicacion_sede"], true).", " .sqlvalue(@$_POST["codigo_opsu"], true).", " .sqlvalue(@$_POST["ced_firma"], true).", " .sqlvalue(@$_POST["titulo_firma"], true).", " .sqlvalue(@$_POST["nombres_firma"], true).", " .sqlvalue(@$_POST["apellidos_firma"], true).", " .sqlvalue(@$_POST["status"], true).", " .sqlvalue(@$_POST["siglas"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

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

  $sql = "update `sedes` set `codigo`=" .sqlvalue(@$_POST["codigo"], true).", `nombre_sede`=" .sqlvalue(@$_POST["nombre_sede"], true).", `ubicacion_sede`=" .sqlvalue(@$_POST["ubicacion_sede"], true).", `codigo_opsu`=" .sqlvalue(@$_POST["codigo_opsu"], true).", `ced_firma`=" .sqlvalue(@$_POST["ced_firma"], true).", `titulo_firma`=" .sqlvalue(@$_POST["titulo_firma"], true).", `nombres_firma`=" .sqlvalue(@$_POST["nombres_firma"], true).", `apellidos_firma`=" .sqlvalue(@$_POST["apellidos_firma"], true).", `status`=" .sqlvalue(@$_POST["status"], true).", `siglas`=" .sqlvalue(@$_POST["siglas"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `sedes` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`codigo`";
  if (@$_POST["xcodigo"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xcodigo"], true);
  };
  $pk .= ") and ";
  $pk .= "(`nombre_sede`";
  if (@$_POST["xnombre_sede"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xnombre_sede"], true);
  };
  $pk .= ") and ";
  $pk .= "(`ubicacion_sede`";
  if (@$_POST["xubicacion_sede"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xubicacion_sede"], true);
  };
  $pk .= ")";
  return $pk;
}
 ?>
