<?php require_once('Connections/conn.php'); ?>
<?php
session_start();
  // Captura la datos del usuario que se logueó (Cédula, Tipo de Usuario y Username).
  $cedula_usuario=$_SESSION['CEDULA_USUARIO'];
  $username=$_SESSION['MM_Username'];
  $tipo_usuario=$_SESSION['PRIVILEGIO'];
 
  mysql_select_db("pregrado_pregrado");
  mysql_query ("SET NAMES 'utf8'"); 
  $sql = "select apellidos,nombres from usuarios where cedula='$cedula_usuario'";
  $rs=mysql_query($sql,$conn);
  $row=mysql_fetch_array($rs);
  $intruso=$row['apellidos'].', '.$row['nombres'];
  
  if (!isset($_SESSION['MM_Username']) || $_SESSION['MM_Username']=="") {
      // Registra el acceso indebido.
		$consulta=addslashes($sql);
  		$fecha=date("Y-m-d G:i:A");
  		$accion="Intento de acceso sin iniciar sesión.";
  		$script="oferta_intensivos.php";
  		$ip=obtener_ip();
  		$sql1 = "insert into `intrusos` (`username`, `tipo_usuario`,`cedula`, `ip`, `fecha`, `accion`, `script`) values ('$username', '$tipo_usuario', '$cedula_usuario', '$ip', '$fecha', '$accion', '$script')";
  		mysql_query($sql1, $conn) or die(mysql_error());
	    // Destruye la sessión.
	    session_destroy();
	    header ("Location: modulos/inicio/index.php");
		return;
	  }  

  if ($tipo_usuario!="1" && $tipo_usuario!="9") {
        // Registra el acceso indebido.
		$consulta=addslashes($sql);
  		$fecha=date("Y-m-d G:i:A");
  		$accion="Intento de Acceso indebido";
  		$script="oferta_intensivos.php";
  		$ip=obtener_ip();
  		$sql1 = "insert into `intrusos` (`username`, `tipo_usuario`,`cedula`, `ip`, `fecha`, `accion`, `script`) values ('$username', '$tipo_usuario', '$cedula_usuario', '$ip', '$fecha', '$accion', '$script')";
  		mysql_query($sql1, $conn) or die(mysql_error());
        // Destruye la sesión.
		session_destroy();
        $inicial = "salida_intruso.php?intruso=$intruso";
        header(sprintf("Location: %s", $inicial));
		return;
  }  
  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_pregrado -- oferta_intensivos</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>
<script src="jquery.min.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>
<script>
$(document).ready(function(){
	$("#mencion").change(function(){
		$.post("carga_oferta_intensivo.php",{id:$(this).val(),c:document.form1.carrera.value,p:document.form1.pensum.value},function(data){$("#codigo").html(data);})
	});
})
</script>

<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Oferta Acad&eacute;mica Semestre Intensivo</h2></td></tr></table>
<?php
  if (!login()) exit;
?>
<div style="float: right"><a href="oferta_intensivos.php?a=logout">[ Salir ]</a></div>
<br>
<?php
  require_once('Connections/conn.php');
  mysql_select_db("pregrado_pregrado");
  mysql_query ("SET NAMES 'utf8'");
  
  $showrecs = 250;
  $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">!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>Unidades Curriculares Ofertadas <?php echo $startrec + 1 ?> - <?php echo $reccount ?> of <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="oferta_intensivos.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_materia" ?>"<?php if ($filterfield == "nombre_materia") { echo "selected"; } ?>><?php echo htmlspecialchars("nombre_materia") ?></option>
<option value="<?php echo "mencion" ?>"<?php if ($filterfield == "mencion") { echo "selected"; } ?>><?php echo htmlspecialchars("mencion") ?></option>
<option value="<?php echo "semestre" ?>"<?php if ($filterfield == "semestre") { echo "selected"; } ?>><?php echo htmlspecialchars("semestre") ?></option>
<option value="<?php echo "carrera" ?>"<?php if ($filterfield == "carrera") { echo "selected"; } ?>><?php echo htmlspecialchars("carrera") ?></option>
<option value="<?php echo "pensum" ?>"<?php if ($filterfield == "pensum") { echo "selected"; } ?>><?php echo htmlspecialchars("pensum") ?></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="oferta_intensivos.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="3"width="100%">
<tr>
<td class="hr" align="center">Ver</td>
<td class="hr" align="center">Borrar</td>
<td class="hr" align="center">Nro.</td>
<td class="hr"><a class="hr" href="oferta_intensivos.php?order=<?php echo "codigo" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Código") ?></a></td>
<td class="hr"><a class="hr" href="oferta_intensivos.php?order=<?php echo "nombre_materia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Unidad Curricular") ?></a></td>
<td class="hr" align="center"><a class="hr" href="oferta_intensivos.php?order=<?php echo "mencion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Componente o Mención") ?></a></td>
<td class="hr" align="center"><a class="hr" href="oferta_intensivos.php?order=<?php echo "semestre" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Semestre") ?></a></td>
<td class="hr"><a class="hr" href="oferta_intensivos.php?order=<?php echo "carrera" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Carrera") ?></a></td>
<td class="hr"><a class="hr" href="oferta_intensivos.php?order=<?php echo "pensum" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Pensum") ?></a></td>
</tr>
<?php
  $nro=1;
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
	  if($row["mencion"]=="COMUN") $nombre_mencion="Componente General";
  if($row["mencion"]=="ACE") $nombre_mencion="Aduanas y Comercio Exterior";
  if($row["mencion"]=="FIN") $nombre_mencion="Finanzas Públicas";
  if($row["mencion"]=="RT") $nombre_mencion="Rentas";
?>
<tr>

<td class="<?php echo $style ?>" align="center"><a href="oferta_intensivos.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="oferta_intensivos.php?a=del&recid=<?php echo $i ?>"><img src="imagenes/delete.png" border="0" ></a></td>

<td class="<?php echo $style ?>" align="right"><?php echo $nro ?>&nbsp;</td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["codigo"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nombre_materia"]) ?></td>
<td class="<?php echo $style ?>" ><?php echo $nombre_mencion; ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["semestre"]) ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["carrera"]) ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["pensum"]) ?></td>
</tr>
<?php
$nro++;
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function login()
{
  global $_POST;
  global $_SESSION;
  global $conn;

  global $_GET;
  if (isset($_GET["a"]) && ($_GET["a"] == 'logout')) $_SESSION["logged_in"] = false;
  if (!isset($_SESSION["logged_in"])) $_SESSION["logged_in"] = false;
  if (!$_SESSION["logged_in"]) {
    $login = "";
    $password = "";
    if (isset($_POST["login"])) $login = @$_POST["login"];
    if (isset($_POST["password"])) $password = @$_POST["password"];

    if (($login != "") && ($password != "")) {
     
      mysql_select_db("pregrado_pregrado");
      mysql_query ("SET NAMES 'utf8'");
      $sql = "select `psw_oferta` from `usuarios` where `username` = '" .$login ."'";
      $res = mysql_query($sql, $conn) or die(mysql_error());
      $row = mysql_fetch_assoc($res) or $row = array(0 => "");;
      if (isset($row)) reset($row);

      if (isset($password) && ($password == trim(current($row)))) {
        $_SESSION["logged_in"] = true;
    }
    else {
?>
<p><b><font color="-1">Lo sentimos, la combinación de usuario/contraseña es incorrecta</font></b></p>
<?php } } }if (isset($_SESSION["logged_in"]) && (!$_SESSION["logged_in"])) { ?>
<form action="oferta_intensivos.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td>Usuario</td>
<td><input type="text" name="login" value="<?php echo $login ?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" value="<?php echo $password ?>"></td>
</tr>
<tr>
<td><input type="submit" name="action" value="Acceder"></td>
</tr>
</table>
</form>
<?php
  }
  if (!isset($_SESSION["logged_in"])) $_SESSION["logged_in"] = false;
  return $_SESSION["logged_in"];
} ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Código")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["codigo"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Unidad Curricular")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombre_materia"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Componente o Mención")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["mencion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Semestre")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["semestre"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Carrera")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["carrera"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Pensum")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["pensum"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
  
  $sql_oferta = "SELECT * FROM materias WHERE pensum='6' ORDER BY semestre,nombre_materia";
  $rst_oferta = mysql_query($sql_oferta, $conn) or die(mysql_error());
  $row_oferta = mysql_fetch_assoc($rst_oferta);
  $total_rows_oferta = mysql_num_rows($rst_oferta);
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="70%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Carrera")."&nbsp;" ?></td>
<td class="dr"><select id="carrera" name="carrera">
                 <option value="LCF">LICENCIATURA EN CIENCIAS FISCALES</option>
               </select></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Pensum")."&nbsp;" ?></td>
<td class="dr"><select id="pensum" name="pensum">
                 <option value="6">&nbsp;6&nbsp;</option>
               </select></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Componente o Mención")."&nbsp;" ?></td>
<td class="dr"><select id="mencion" name="mencion" >
                 <option value="0">SELECCIONE...</option>
                 <option value="COMUN">COMPONENTE GENERAL</option>
                 <option value="ACE">ADUANAS Y COMERCIO EXTERIOR</option>
                 <option value="FIN">FINANZAS P&Uacute;BLICAS</option>
                 <option value="RT">RENTAS</option>
               </select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Unidad Curricular a Ofertar")."&nbsp;" ?></td>
<td class="dr"><select id="codigo" name="codigo"  onChange="validar_oferta_intensivo(this.value)">
				<option value="0">SELECCIONE...</option>
			   </select>
</td>
</tr>

<iframe name="comando" id="comando" frameborder="0" height="0" width="0"></iframe>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="oferta_intensivos.php?a=add">Agregar Oferta</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="oferta_intensivos.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="oferta_intensivos.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="oferta_intensivos.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="oferta_intensivos.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="oferta_intensivos.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="oferta_intensivos.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="oferta_intensivos.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="oferta_intensivos.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form name="form1" id="form1" enctype="multipart/form-data" action="oferta_intensivos.php" method="post" onSubmit="return validar_datos()">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "codigo" => "",
  "nombre_materia" => "",
  "mencion" => "",
  "semestre" => "",
  "carrera" => "",
  "pensum" => "");
showroweditor($row, false);
?>
<p><input type="submit" name="action" value="Ofertar"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>

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

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="oferta_intensivos.php" method="post"  onSubmit="return validar_datos()">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xcodigo" value="<?php echo $row["codigo"] ?>">
<input type="hidden" name="xmencion" value="<?php echo $row["mencion"] ?>">
<input type="hidden" name="xsemestre" value="<?php echo $row["semestre"] ?>">
<input type="hidden" name="xcarrera" value="<?php echo $row["carrera"] ?>">
<input type="hidden" name="xpensum" value="<?php echo $row["pensum"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Ofertar"></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="oferta_intensivos.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xcodigo" value="<?php echo $row["codigo"] ?>">
<input type="hidden" name="xmencion" value="<?php echo $row["mencion"] ?>">
<input type="hidden" name="xsemestre" value="<?php echo $row["semestre"] ?>">
<input type="hidden" name="xcarrera" value="<?php echo $row["carrera"] ?>">
<input type="hidden" name="xpensum" value="<?php echo $row["pensum"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirmar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;
  
  mysql_select_db($database_conn, $conn);
  mysql_query ("SET NAMES 'utf8'");

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT * FROM `oferta_intensivos`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`codigo` like '" .$filterstr ."') or (`nombre_materia` like '" .$filterstr ."') or (`mencion` like '" .$filterstr ."') or (`semestre` like '" .$filterstr ."') or (`carrera` like '" .$filterstr ."') or (`pensum` like '" .$filterstr ."')";
  }
  $sql .= " order by semestre,mencion";
  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;
  
  mysql_select_db($database_conn, $conn);
  mysql_query ("SET NAMES 'utf8'");

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `oferta_intensivos`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`codigo` like '" .$filterstr ."') or (`nombre_materia` like '" .$filterstr ."') or (`mencion` like '" .$filterstr ."') or (`semestre` like '" .$filterstr ."') or (`carrera` like '" .$filterstr ."') or (`pensum` 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;
  
  $pensum=@$_POST["pensum"];
  $carrera=@$_POST["carrera"];
  $id_materia=@$_POST["codigo"];
  
  
  $sql_oferta = "SELECT * FROM materias WHERE pensum='$pensum' and id_carrera='$carrera' and id_materia='$id_materia'";
  $rst_oferta = mysql_query($sql_oferta, $conn) or die(mysql_error());
  $row_oferta = mysql_fetch_assoc($rst_oferta);
  $total_rows_oferta = mysql_num_rows($rst_oferta);
  
  $semestre=$row_oferta["semestre"];
  $nombre_materia=$row_oferta["nombre_materia"];

  $sql = "insert into `oferta_intensivos` (`codigo`, `nombre_materia`, `mencion`, `semestre`, `carrera`, `pensum`) values (" .sqlvalue(@$_POST["codigo"], true).", '$nombre_materia', " .sqlvalue(@$_POST["mencion"], true).", '$semestre', " .sqlvalue(@$_POST["carrera"], true).", " .sqlvalue(@$_POST["pensum"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

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

  $sql = "update `oferta_intensivos` set `codigo`=" .sqlvalue(@$_POST["codigo"], true).", `nombre_materia`=" .sqlvalue(@$_POST["nombre_materia"], true).", `mencion`=" .sqlvalue(@$_POST["mencion"], true).", `semestre`=" .sqlvalue(@$_POST["semestre"], false).", `carrera`=" .sqlvalue(@$_POST["carrera"], true).", `pensum`=" .sqlvalue(@$_POST["pensum"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `oferta_intensivos` 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 .= "(`mencion`";
  if (@$_POST["xmencion"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xmencion"], true);
  };
  $pk .= ") and ";
  $pk .= "(`semestre`";
  if (@$_POST["xsemestre"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xsemestre"], false);
  };
  $pk .= ") and ";
  $pk .= "(`carrera`";
  if (@$_POST["xcarrera"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xcarrera"], true);
  };
  $pk .= ") and ";
  $pk .= "(`pensum`";
  if (@$_POST["xpensum"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xpensum"], true);
  };
  $pk .= ")";
  return $pk;
}
 ?>


 <script type="text/javascript">
	function validar_oferta_intensivo(id_materia)
	{
	  url="validar_oferta_intensivo.php?id_materia="+id_materia;
	  comando.location=url;
	}

</script>  

<script type="text/javascript">
	function validar_datos()
	{
	   		
		if(document.form1.mencion.value==0){
			alert("El campo <<Componente o Mención>> es obligatorio");
			document.form1.mencion.focus();
			return false;
			}	
		if(document.form1.codigo.value==0){
			alert("El campo <<Unidad Curricular a Ofertar>> es obligatorio");
			document.form1.codigo.focus();
			return false;
			}
			
			
	    return true;
	}
	
</script>	