<?php session_start();
 if (!isset($_SESSION['MM_Username'])) {
      header ("Location: modulos/inicio/index.php");
	  }
  $nombre=$_GET["nombre"];
  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>sadce -- correquisitos</title>
<meta name="generator" http-equiv="content-type" content="text/html">
<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>Correquisitos</h2></td></tr>
<tr><td><?php echo $nombre ?></td></tr>
</table>

<?php
  $conn = connect();
  
  $id_materia=$_GET['id_materia'];
  $id_carrera=$_GET['id_carrera']; 
  $sem=$_GET['sem']; 
  $matVolver=$_GET["matVolver"];
  $nombre=$_GET["nombre"];
  
  
  $showrecs = 10;
  $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($id_carrera, $id_materia);
      break;
    case "update":
      sql_update($id_carrera, $id_materia);
      break;
    case "delete":
      sql_delete($id_carrera, $id_materia);
      break;
  }

  switch ($a) {
    case "add":
      addrec($matVolver, $recid, $id_carrera, $id_materia, $sem, $nombre);
      break;
    case "view":
      viewrec($recid, $id_carrera, $id_materia, $sem,  $matVolver, $nombre);
      break;
    case "edit":
      editrec($recid, $id_carrera, $id_materia, $sem, $matVolver, $nombre);
      break;
    case "del":
      deleterec($recid, $id_carrera, $id_materia, $sem, $matVolver, $nombre);
      break;
    default:
      select($id_carrera, $id_materia, $sem, $matVolver, $nombre);
      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);
?>
</td></tr></table>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?php function select($id_carrera, $id_materia, $sem, $matVolver,$nombre)
  {
  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($id_carrera, $id_materia);
  $count = sql_getrecordcount($id_carrera, $id_materia);
  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);
?>

<?php $matVolver=$_GET["matVolver"];?>
<?php $nombre=$_GET["nombre"];?>




<table class="bd" border="0" cellspacing="1" cellpadding="4">

<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="correquisitos.php?nombre=<?php echo $nombre ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&matVolver=<?php echo $matVolver ?>" 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_carrera" ?>"<?php if ($filterfield == "id_carrera") { echo "selected"; } ?>><?php echo htmlspecialchars("Código de Carrera") ?></option>
<option value="<?php echo "id_materia" ?>"<?php if ($filterfield == "id_materia") { echo "selected"; } ?>><?php echo htmlspecialchars("Código de Materia") ?></option>
<option value="<?php echo "id_correquisito" ?>"<?php if ($filterfield == "id_correquisito") { echo "selected"; } ?>><?php echo htmlspecialchars("Correquisito") ?></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="correquisitos.php?a=reset&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&nombre=<?php echo $nombre ?>">Quitar Filtro</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount, $id_carrera, $id_materia, $sem, $matVolver, $nombre); ?>
<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="center"><a class="hr" href="correquisitos.php?order=<?php echo "id_carrera" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Código de Carrera") ?></a></td>
<td class="hr" align="center"><a class="hr" href="correquisitos.php?order=<?php echo "id_materia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Código de Materia") ?></a></td>
<td class="hr" align="center"><a class="hr" href="correquisitos.php?order=<?php echo "id_correquisito" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Correquisito") ?></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="correquisitos.php?a=view&recid=<?php echo $i ?>&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo htmlspecialchars($row["id_carrera"]) ?>&id_materia=<?php echo htmlspecialchars($row["id_materia"]) ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>"><img src="imagenes/search.gif" border="0"></a></td>
<td class="<?php echo $style ?>" align="center"><a href="correquisitos.php?a=edit&recid=<?php echo $i ?>&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo htmlspecialchars($row["id_carrera"]) ?>&id_materia=<?php echo htmlspecialchars($row["id_materia"]) ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>"><img src="imagenes/edit.gif" border="0"></a></td>
<td class="<?php echo $style ?> align="center""><a href="correquisitos.php?a=del&recid=<?php echo $i ?>&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo htmlspecialchars($row["id_carrera"]) ?>&id_materia=<?php echo htmlspecialchars($row["id_materia"]) ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>"><img src="imagenes/delete.png" border="0"></a></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["id_carrera"]) ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["id_materia"]) ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["id_correquisito"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount, $id_carrera, $id_materia, $sem, $matVolver, $nombre); ?>
<?php } ?>

<?php function showrow($row, $recid, $nombre)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Carrera")."&nbsp;" ?></td>
<td class="dr"><input disabled="disabled" name="id_carrera" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["id_carrera"])) ?>" size="8" maxlength="5"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código de Materia")."&nbsp;" ?></td>
<td class="dr"><input disabled="disabled" type="text" name="id_materia" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Correquisito")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_correquisito" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_correquisito"])) ?>"></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("Carrera")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_carrera" maxlength="5" value="<?php echo str_replace('"', '&quot;', trim($row["id_carrera"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código de Materia")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_materia" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código del Correquisito")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_correquisito" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_correquisito"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showrowadd($row, $iseditmode, $sem)
  {
  global $conn;
  $conn=connect();

  mysql_select_db($database_conn, $conn);

  $cc=$row[id_carrera];
  $query_rstCorre = "SELECT materias.id_materia, materias.nombre_materia FROM materias WHERE materias.id_carrera='$cc' AND materias.semestre=$sem";
  $rstCorre = mysql_query($query_rstCorre, $conn) or die(mysql_error());
  $row_rstCorre = mysql_fetch_assoc($rstCorre);
  $totalRows_rstCorre = mysql_num_rows($rstCorre);
  
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Carrera")."&nbsp;" ?></td>
<td class="dr"><input disabled="disabled" name="id_carrera" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["id_carrera"])) ?>" size="8" maxlength="5"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código de Materia")."&nbsp;" ?></td>
<td class="dr"><input disabled="disabled" type="text" name="id_materia" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Correquisito")."&nbsp;" ?></td>
<td class="dr">
<select name="id_correquisito" onChange="verificarCo(document.addco.id_carrera.value,document.addco.id_materia.value,this.value)">
<option value="0">seleccione prerrequisito...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_rstCorre['id_materia']?>"><?php echo $row_rstCorre['nombre_materia']?></option>
  <?php
} while ($row_rstCorre = mysql_fetch_assoc($rstCorre));
  $rows = mysql_num_rows($rstCorre);
  if($rows > 0) {
      mysql_data_seek($rstCorre, 0);
	  $row_rstCorre = mysql_fetch_assoc($rstCorre);
  }
?>
</select>
</td><iframe name="corequisito" id="corequisito" with="0" height="0" frameborder="0"></iframe>
</tr>
</table>

<?php 
mysql_free_result($rstCorre);
} ?>

<?php function showpagenav($page, $pagecount, $id_carrera, $id_materia, $sem, $matVolver, $nombre)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="materias.php?a=retorno&recid=<?php echo $matVolver ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>">Regresar</a>&nbsp;</td>
<td><a href="correquisitos.php?a=add&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>">Agregar CorrequisitoOOOO</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="correquisitos.php?page=<?php echo $page - 1 ?>&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&nombre=<?php echo $nombre ?>">&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="correquisitos.php?page=<?php echo $j ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="correquisitos.php?page=<?php echo $startpage ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="correquisitos.php?page=<?php echo $page + 1 ?>&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&nombre=<?php echo $nombre ?>">Siguiente&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

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

<?php function addrec($matVolver, $recid, $id_carrera, $id_materia, $sem, $nombre)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="correquisitos.php?matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form name="addco" id="addco" enctype="multipart/form-data" action="correquisitos.php?recid=<?php echo $recid ?>&id_carrera=<?php echo $id_carrera ?>&sem=<?php echo $sem ?>&id_materia=<?php echo $id_materia ?>&nombre=<?php echo $nombre ?>&matVolver=<?php echo $matVolver ?>" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id_carrera" => "$id_carrera",
  "id_materia" => "$id_materia",
  "id_correquisito" => "");
showrowadd($row, false, $sem);
?>
<input type="hidden" name="yid_carrera" value="<?php echo $id_carrera ?>">
<input type="hidden" name="yid_materia" value="<?php echo $id_materia ?>">

<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php } ?>


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

<?php function editrec($recid, $id_carrera, $id_materia, $sem, $matVolver, $nombre)
{
  $res = sql_select($id_carrera, $id_materia);
  $count = sql_getrecordcount($id_carrera, $id_materia);
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count, $id_carrera, $id_materia, $sem, $matVolver, $nombre);
?>
<br>
<form enctype="multipart/form-data" action="correquisitos.php" method="Guardar">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid_carrera" value="<?php echo $row["id_carrera"] ?>">
<input type="hidden" name="xid_materia" value="<?php echo $row["id_materia"] ?>">
<input type="hidden" name="xid_correquisito" value="<?php echo $row["id_correquisito"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Guardar Cambios"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function deleterec($recid, $id_carrera, $id_materia, $sem, $matVolver, $nombre)
{
  $res = sql_select($id_carrera, $id_materia);
  $count = sql_getrecordcount($id_carrera, $id_materia);
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count, $id_carrera, $id_materia, $sem, $matVolver, $nombre);
?>
<br>
<form action="correquisitos.php?recid=<?php echo $recid ?>&matVolver=<?php echo $matVolver ?>&id_carrera=<?php echo $id_carrera ?>&id_materia=<?php echo $id_materia ?>&sem=<?php echo $sem ?>&nombre=<?php echo $nombre ?>" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid_carrera" value="<?php echo $row["id_carrera"] ?>">
<input type="hidden" name="xid_materia" value="<?php echo $row["id_materia"] ?>">
<input type="hidden" name="xid_correquisito" value="<?php echo $row["id_correquisito"] ?>">
<?php showrow($row, $recid, $nombre) ?>
<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($id_carrera, $id_materia)
{
  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_carrera`, `id_materia`, `id_correquisito` FROM `correquisitos` WHERE correquisitos.id_carrera='$id_carrera' AND correquisitos.id_materia='$id_materia'";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " and " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " and (`id_carrera` like '" .$filterstr ."') or (`id_materia` like '" .$filterstr ."') or (`id_correquisito` 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($id_carrera, $id_materia)
{
  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 `correquisitos` WHERE correquisitos.id_carrera='$id_carrera' AND correquisitos.id_materia='$id_materia'";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " and " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " and (`id_carrera` like '" .$filterstr ."') or (`id_materia` like '" .$filterstr ."') or (`id_correquisito` like '" .$filterstr ."')";
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert($id_carrera, $id_materia)
{
  global $conn;
  global $_POST;
  
  $sql = "insert into `correquisitos` (`id_carrera`, `id_materia`, `id_correquisito`) values (" .sqlvalue(@$_POST["yid_carrera"], true).", " .sqlvalue(@$_POST["yid_materia"], true).", " .sqlvalue(@$_POST["id_correquisito"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update($id_carrera, $id_materia)
{
  global $conn;
  global $_POST;

  $sql = "update `correquisitos` set `id_correquisito`=" .sqlvalue(@$_POST["id_correquisito"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete($id_carrera, $id_materia)
{
  global $conn;

  $sql = "delete from `correquisitos` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id_materia`";
  if (@$_POST["xid_materia"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_materia"], true);
  };
  $pk .= ") and ";
  $pk .= "(`id_correquisito`";
  if (@$_POST["xid_correquisito"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_correquisito"], true);
  };
  $pk .= ")";
  return $pk;
}
 ?>
 
   <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));
	}
	
	function validar_datos()
	{
	 	if(document.addrec.id_prerrequisito.value=="0"){
			alert("Prerrequisito es obligatorio");
			document.addrec.id_carrera.focus();
			return false;
			}			
			return true;
	}
	
function total_horas()
	{
	 	$nth=document.editrec.hp+document.editrec.ht;
		return $nth;
			}
	     
function verificarCo(id_carrera,id_materia,id_correquisito)
{
   	if (id_correquisito!="0")
	 {
	 url="validar_correquisito.php?id_carrera="+id_carrera+"&id_materia="+id_materia+"&id_correquisito="+id_correquisito;
	 corequisito.location=url;
	 }
    
}

</script>
