<?php session_start();
  if (!isset($_SESSION['MM_Username'])) {
      header ("Location: modulos/inicio/index.php");
	  }
  
   if ( @$_POST["sql"]=="insert"){
      $filterfield="id_carrera";
	  $filter=@$_POST["id_carrera"];
	  }	
	  
  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 -- materias_intro</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Gesti&oacute;n de Unidades Curriculares (Introductorio) </h2></td></tr></table>

<?php
  $conn = connect();
  $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();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }
 

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
	case "retView":
	  $id_carrera = @$_GET["id_carrera"];
	  Limpiar($recid, $id_carrera);  
	  viewrec($recid);
      break;   
    case "edit":
	  editrec($recid);
      break;
	case "retorno":
	  $id_carrera = @$_GET["id_carrera"];
	  Limpiar($recid, $id_carrera);  
	  editrec($recid);
      break; 
    case "del":
      deleterec($recid);
      break;
	case "retDelete":
	  $id_carrera = @$_GET["id_carrera"];
	  Limpiar($recid, $id_carrera);  
	  deleterec($recid);
      break;     
    default:
      select();
      break;
  }

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

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

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


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

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Número de Unidades Curriculares <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form name="form1" action="materias_intro.php" method="post" onSubmit="return validar_datos()">
<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 Programa") ?></option>
<option value="<?php echo "id_materia" ?>"<?php if ($filterfield == "id_materia") { echo "selected"; } ?>><?php echo htmlspecialchars("Código Unidad Curricular") ?></option>
<option value="<?php echo "nombre_materia" ?>"<?php if ($filterfield == "nombre_materia") { echo "selected"; } ?>><?php echo htmlspecialchars("Denominación Unidad Curricular") ?></option>
<option value="<?php echo "semestre" ?>"<?php if ($filterfield == "semestre") { echo "selected"; } ?>><?php echo htmlspecialchars("Semestre") ?></option>
<option value="<?php echo "uc" ?>"<?php if ($filterfield == "uc") { echo "selected"; } ?>><?php echo htmlspecialchars("Créditos") ?></option>
<option value="<?php echo "orden" ?>"<?php if ($filterfield == "orden") { echo "selected"; } ?>><?php echo htmlspecialchars("Orden") ?></option>
<option value="<?php echo "hp" ?>"<?php if ($filterfield == "hp") { echo "selected"; } ?>><?php echo htmlspecialchars("HP") ?></option>
<option value="<?php echo "ht" ?>"<?php if ($filterfield == "ht") { echo "selected"; } ?>><?php echo htmlspecialchars("HT") ?></option>
<option value="<?php echo "ths" ?>"<?php if ($filterfield == "ths") { echo "selected"; } ?>><?php echo htmlspecialchars("THS") ?></option>
<option value="<?php echo "electiva" ?>"<?php if ($filterfield == "electiva") { echo "selected"; } ?>><?php echo htmlspecialchars("Electiva") ?></option>
<option value="<?php echo "status" ?>"<?php if ($filterfield == "status") { echo "selected"; } ?>><?php echo htmlspecialchars("Status") ?></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="materias_intro.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="materias_intro.php?order=<?php echo "id_carrera" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Programa") ?></a></td>
<td class="hr"><a class="hr" href="materias_intro.php?order=<?php echo "id_materia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Código") ?></a></td>
<td class="hr"><a class="hr" href="materias_intro.php?order=<?php echo "nombre_materia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Denominación de la Unidad Curricular") ?></a></td>
<td class="hr"><a class="hr" href="materias_intro.php?order=<?php echo "orden" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Orden") ?></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="materias_intro.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="materias_intro.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="materias_intro.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["id_carrera"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_materia"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nombre_materia"]) ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["orden"]) ?></td>

</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function showrow($row, $recid)
  {
  
  global $conn;
  $conn=connect();
  mysql_select_db($database_conn, $conn);
  mysql_query ("SET NAMES 'utf8'");
 
  $id_carrera=$row["id_carrera"];
  $query_rs_carreras = "SELECT carreras.id_carrera, carreras.nombre_carrera FROM carreras WHERE id_carrera='$id_carrera'";
  $rs_carreras = mysql_query($query_rs_carreras, $conn) or die(mysql_error());
  $row_rs_carreras = mysql_fetch_assoc($rs_carreras);
  $totalRows_rs_carreras = mysql_num_rows($rs_carreras);
  $carrera=$row["id_carrera"].':'.$row_rs_carreras["nombre_carrera"]

?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Código del Programa")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($carrera) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código Unidad Curricular")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_materia"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Denominación Unidad Curricular")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombre_materia"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Orden")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["orden"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Total Horas")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["ths"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["status"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  
  global $conn;
  $conn=connect();
  mysql_select_db($database_conn, $conn);
  mysql_query ("SET NAMES 'utf8'");
 
  $id_carrera=$row["id_carrera"];
  $query_rs_carreras = "SELECT carreras.id_carrera, carreras.nombre_carrera FROM carreras WHERE id_carrera='$id_carrera'";
  $rs_carreras = mysql_query($query_rs_carreras, $conn) or die(mysql_error());
  $row_rs_carreras = mysql_fetch_assoc($rs_carreras);
  $totalRows_rs_carreras = mysql_num_rows($rs_carreras);
  $carrera=$row["id_carrera"].':'.$row_rs_carreras["nombre_carrera"]

?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Programa")."&nbsp;" ?></td>
<td class="dr"><input name="id_carrera" disabled="disabled" type="text"  value="<?php echo str_replace('"', '&quot;', trim($carrera)) ?>" size="85" maxlength="85"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código Unidad Curricular")."&nbsp;" ?></td>
<td class="dr"><input name="id_materia" type="text" disabled="disabled" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>" size="15" maxlength="15"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Denominación Unidad Curricular")."&nbsp;" ?></td>
<td class="dr"><textarea cols="65" rows="3" name="nombre_materia" maxlength="255"><?php echo str_replace('"', '&quot;', trim($row["nombre_materia"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Orden")."&nbsp;" ?></td>
<td class="dr"><input name="orden" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["orden"])) ?>" size="1" maxlength="2"></td>
</tr>

<tr>
<td class="hr"><?php echo htmlspecialchars("Total Horas")."&nbsp;" ?></td>
<td class="dr"><input name="ths" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["ths"])) ?>" size="1" maxlength="2"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr">
  <select name="status">
       <option value="<?php echo str_replace('"', '&quot;', trim($row["status"])) ?>"><?php echo str_replace('"', '&quot;', trim($row["status"])) ?></option>
	   <option value="ACTIVA">ACTIVA</option>
       <option value="INACTIVA">INACTIVA</option>
  </select>
</td>
</tr>
</table>
<?php } ?>

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

mysql_select_db($database_conn, $conn);
 mysql_query ("SET NAMES 'utf8'");
$query_rs_carreras = "SELECT carreras.id_carrera, carreras.nombre_carrera FROM carreras ORDER BY carreras.nombre_carrera";
$rs_carreras = mysql_query($query_rs_carreras, $conn) or die(mysql_error());
$row_rs_carreras = mysql_fetch_assoc($rs_carreras);
$totalRows_rs_carreras = mysql_num_rows($rs_carreras);
 
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Código del Programa (*)")."&nbsp;" ?></td>

<td class="dr">
<select name="id_carrera" style="width:500px" >
<option value="0">Seleccione...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_rs_carreras['id_carrera']?>"><?php echo $row_rs_carreras['id_carrera'].":".$row_rs_carreras['nombre_carrera']?></option>
  <?php
} while ($row_rs_carreras = mysql_fetch_assoc($rs_carreras));
  $rows = mysql_num_rows($rs_carreras);
  if($rows > 0) {
      mysql_data_seek($rs_carreras, 0);
	  $row_rs_carreras = mysql_fetch_assoc($rs_carreras);
  }
?></select>
</td>

</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Código Unidad Curricular (*)")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_materia" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>" onBlur="verificar(document.addrec.id_carrera.value,this.value)"><iframe name="comando" id="comando" with="0" height="0" frameborder="0"></iframe></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Denominación de la Unidad Curricular (*)")."&nbsp;" ?></td>
<td class="dr"><textarea cols="60" rows="3" name="nombre_materia" maxlength="255"><?php echo str_replace('"', '&quot;', trim($row["nombre_materia"])) ?></textarea></td>
</tr>

<tr>
   <td class="hr"><?php echo htmlspecialchars("Orden")."&nbsp;" ?></td>
   <td class="dr"><input name="orden" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["orden"])) ?>" size="1" maxlength="2"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Total Horas")."&nbsp;" ?></td>
<td class="dr"><input name="ths" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["ths"])) ?>" size="1" maxlength="2"></td>
</tr>

<tr>
<td class="hr"><?php echo htmlspecialchars("Status (*)")."&nbsp;" ?></td>
<td class="dr">
     <select name="status">
       <option value="ACTIVA">ACTIVA</option>
     </select>
</td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="materias_intro.php?a=add">Agregar Unidad Curricular</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="materias_intro.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="materias_intro.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="materias_intro.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="materias_intro.php?page=<?php echo $page + 1 ?>">Siguiente&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

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

<?php function addrec()
{
?>
<table class="bd" width="100%"><tr><td class="hr">Los campos marcados con (*) son obligatorios </td></tr></table>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="materias_intro.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" name="addrec" action="materias_intro.php" method="post" onSubmit="return validar_datos()">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id_carrera" => "",
  "id_materia" => "",
  "nombre_materia" => "",
  "semestre" => "",
  "uc" => "",
  "orden" => "",
  "hp" => "",
  "ht" => "",
  "ths" => "",
  "electiva" => "",
  "status" => "");
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);
  $id_materia=$row['id_materia'];
  $id_carrera=$row['id_carrera'];
  $nombre="Unidad Curricular: ".$row['nombre_materia'];
  $sem=$row['semestre'];
  showrecnav("view", $recid, $count, $id_carrera, $id_materia, $sem, $nombre);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="materias_intro.php?a=add">Agregar Unidad Curricular</a></td>
<td><a href="materias_intro.php?a=edit&recid=<?php echo $recid ?>">Editar Unidad Curricular</a></td>
<td><a href="materias_intro.php?a=del&recid=<?php echo $recid ?>">Borrar Unidad Curricular</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);
  $id_materia=$row['id_materia'];
  $id_carrera=$row['id_carrera'];
  $nombre="Unidad Curricular: ".$row['nombre_materia'];
  $sem=$row['semestre'];
  showrecnav("view", $recid, $count, $id_carrera, $id_materia, $sem, $nombre);
?>
<br>
<form name="editrec" enctype="multipart/form-data" action="materias_intro.php" method="post">
<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"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Guardar Cambios"></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);
  $id_materia=$row['id_materia'];
  $id_carrera=$row['id_carrera'];
  $nombre="Unidad Curricular: ".$row['nombre_materia'];
  $sem=$row['semestre'];
  showrecnav("view", $recid, $count, $id_carrera, $id_materia, $sem, $nombre);
?>
<br>
<form action="materias_intro.php" name="delrec" 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"] ?>">
<?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");
  mysql_query ("SET NAMES 'utf8'");
  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 `id_carrera`, `id_materia`, `nombre_materia`, `semestre`, `uc`, `orden`, `hp`, `ht`, `ths`, `electiva`, `status` FROM `materias_intro`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id_carrera` like '" .$filterstr ."') or (`id_materia` like '" .$filterstr ."') or (`nombre_materia` like '" .$filterstr ."') or (`semestre` like '" .$filterstr ."') or (`uc` like '" .$filterstr ."') or (`orden` like '" .$filterstr ."') or (`hp` like '" .$filterstr ."') or (`ht` like '" .$filterstr ."') or (`ths` like '" .$filterstr ."') or (`electiva` like '" .$filterstr ."') or (`status` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by semestre,orden";
  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 `materias_intro`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id_carrera` like '" .$filterstr ."') or (`id_materia` like '" .$filterstr ."') or (`nombre_materia` like '" .$filterstr ."') or (`semestre` like '" .$filterstr ."') or (`uc` like '" .$filterstr ."') or (`orden` like '" .$filterstr ."') or (`hp` like '" .$filterstr ."') or (`ht` like '" .$filterstr ."') or (`ths` like '" .$filterstr ."') or (`electiva` like '" .$filterstr ."') or (`status` 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;
  $semestre=0;
  $ht=0;
  $hp=0;
  $uc=0;
  
  $sql = "insert into `materias_intro` (`id_carrera`, `id_materia`, `nombre_materia`, `semestre`, `uc`, `orden`, `hp`, `ht`, `ths`, `electiva`, `status`) values (" .sqlvalue(@$_POST["id_carrera"], true).", " .sqlvalue(@$_POST["id_materia"], true).", " .sqlvalue(@$_POST["nombre_materia"], true).", '$semestre', '$uc', " .sqlvalue(@$_POST["orden"], false).", '$hp', '$ht', " .sqlvalue(@$_POST["ths"], false).", " .sqlvalue(@$_POST["electiva"], true).", " .sqlvalue(@$_POST["status"], true).")";
  mysql_query($sql, $conn)  or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;
  $hts=@$_POST["hp"]+@$_POST["ht"];
  $sql = "update `materias_intro` set `nombre_materia`=" .sqlvalue(@$_POST["nombre_materia"], true).", `orden`=" .sqlvalue(@$_POST["orden"], false).", `ths`=" .sqlvalue(@$_POST["ths"], true).", `status`=" .sqlvalue(@$_POST["status"], true) ." where " .primarykeycondition();

  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;
  
  $id_materia=@$_POST["xid_materia"];
  
  mysql_select_db($database_conn, $conn);
  $sql = "SELECT * FROM historico_notas WHERE id_materia='$id_materia'";
  $rs=mysql_query($sql, $conn);
  $reg=mysql_num_rows($rs);
  if ($reg != 0) {
	    ?>
	     <script type="text/javascript">
	        msj="Unidad Curricular " +'<<<?php echo $id_materia; ?>>>'+" no se puede borrar. Tiene calificaciones registradas."
	 	    alert(msj);
	     </script>
	 <?php
	 }
	 else {
          $sql = "delete from `materias_intro` where " .primarykeycondition();
          mysql_query($sql, $conn) or die(mysql_error());
     }
}

function primarykeycondition()
{
  global $_POST;
  $pk = "";
 /* $pk .= "(`id_carrera`";
  if (@$_POST["xid_carrera"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_carrera"], true);
  };
  $pk .= ") and ";
  $pk .= "(`id_materia`";
  if (@$_POST["xid_materia"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_materia"], true);
  };
  $pk .= ")";*/
 
  $pk .= "(`id_materia`";
  if (@$_POST["xid_materia"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_materia"], true);
  };
  $pk .= ")";
  return $pk;
}

function limpiar($recid, $id_carrera)
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;
  
    $filter = $id_carrera;
    $filterfield = "id_carrera";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
}
 ?>
 
 <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_carrera.value==""){
			alert("Campo <<Programa>> es obligatorio");
			document.addrec.id_carrera.focus();
			return false;
			}
	     if(document.addrec.id_materia.value==""){
			alert("Campo <<Código Unidad Curricular>> es obligatorio");
			document.addrec.id_materia.focus();
			return false;
			}	
		if(document.addrec.nombre_materia.value==""){
			alert("Campo <<Denominación Unidad Curricular>> es obligatorio");
			document.addrec.nombre_materia.focus();
			return false;
			}						
			return true;
	}
	
function total_horas()
	{
	 	$nth=document.editrec.hp+document.editrec.ht;
		return $nth;
			}
	     
function verificar(id_carrera,id_materia)
{
 	if (id_materia!="0")
	 {
	 url="validar_materia_intro.php?id_carrera="+id_carrera+"&id_materia="+id_materia;
	 comando.location=url;
	 }
    
}
</script>
