<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
</head>
<body>
<?php
date_default_timezone_set('UTC');

if(isset($_GET['dia']) && $_GET['dia']!="" ) 
{ 
// si existe lo deserializamos para poder tratarlo 
$codprog = unserialize($_GET['dia']); 
$horaext = unserialize($_GET['extra']); 

$idj=$_GET['idj'];
//echo $iddepa."<br>";
$ids=$_GET['ids'];
//echo $idserv."<br>";
$idga=$_GET['idga'];
//echo $idga."<br>";
$ida=$_GET['ida'];
//echo $ida."<br>";
$idh=$_GET['idh'];
//echo $idh."<br>";
}
//$anno=$_GET['anno'];
//$mm=$_GET['mes'];
//$idhora=$_GET['idhora'];
//$idarea=$_GET['idarea'];
$idsa=2;
$anno=2010;
$mm=10;
$per=1;
//$per=$_GET['nivel'];
session_start();
$usuario =$_SESSION['usuario'];
$idusu='1';
include("bd/sql_variables.php");
$db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysql_select_db("$sql_db");

for ($j=1;$j<=count($codprog);$j++){
$codp = $codprog[$j-1];
$extra = $horaext[$j-1];
if($codprog[$j-1]!=0){
$sqlconsult="select ug_id from tb_usutrab where ut_codprog='$codp' and tp_id='$per' and uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and j_id='$idj' and ut_idserva='$idsa'";
$resultconsult = mysql_query($sqlconsult);
if(mysql_num_rows($resultconsult)!=0){
while($row = mysql_fetch_array($resultconsult)){
$coduni=$row['ug_id'];
}
//consullta si esta programada en el dia 
$sqlupdatedia="update tb_horaprog".$anno." set ut_codprog=upper('$codp'), hp_tipohora='$extra', ug_id='$coduni', pu_id='$idusu' where hp_dia='$j' and h_id='$idh' and a_id='$ida' and ga_id='$idga' and tp_id='$per' and uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and j_id='$idj'";
$sqlconsultadia = "select * from tb_horaprog".$anno." where hp_dia='$j' and h_id='$idh' and a_id='$ida' and ga_id='$idga' and tp_id='$per' and uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and j_id='$idj'";
$resultconsultadia = mysql_query($sqlconsultadia);
if(mysql_num_rows($resultconsultadia)==0){
$sqlinserturno="insert into tb_horaprog".$anno." values('$j',upper('$codp'),'$idh','$ida','$idga','$per','$coduni','$anno','$mm','$ids','$idj','$extra','$idsa','$idusu')";
echo $sqlinserturno."<br>";
$resultinsertturno = mysql_query($sqlinserturno);
}else{
$sqlupdatedia="update tb_horaprog".$anno." set ut_codprog=upper('$codp'), hp_tipohora='$extra', ug_id='$coduni', pu_id='$idusu' where hp_dia='$j' and h_id='$idh' and a_id='$ida' and ga_id='$idga' and tp_id='$per' and uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and j_id='$idj'";
//echo $sqlupdatedia."<br>";
$resulturnoupdate = mysql_query($sqlupdatedia);
}}
}else{
$sqldeletedia="delete from tb_horaprog".$anno." where hp_dia='$j' and h_id='$idh' and a_id='$ida' and ga_id='$idga' and tp_id='$per' and uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and j_id='$idj'";
//echo $sqldeletedia."<br>";
$resulturnodelete = mysql_query($sqldeletedia);
}}
$iddepa= $idj;
$idserv= $ids;
$idserva= $idsa;
$idga=$idga;
include("area.php");
?>
</body>
</html>
