<?
$id = $_SESSION['id'];
if(isset($_POST['update'])){
	mysql_query("update insti_act set insti_codigo=$id, insti_act_descr='$i034a' where insti_act_codigo=".$_POST['update'], $link);
	$act_id= $_POST['update'];
} else {
	mysql_query("insert into insti_act(insti_codigo, insti_act_descr) values ($id, '$i034a')", $link);
	$act_id= mysql_insert_id();
}

/*$q = mysql_query("select tematica_codigo from tematica", $link);
$n = mysql_num_rows($q);
mysql_free_result($q);

mysql_query("delete from tem_act where insti_act_codigo=".$act_id, $link);/////////////
for ($i=1; $i<=$n; $i++){
	$cad = "i035".$i;
	if(!empty($_POST[$cad])){
		mysql_query("insert into tem_act(insti_act_codigo, tematica_codigo) values ($act_id ,$_POST[$cad])", $link);
	}
}*/

$q = mysql_query("select max(tematica_codigo) from tematica", $link);
$n = mysql_fetch_row($q);
mysql_free_result($q);
mysql_query("delete from tem_act where insti_act_codigo=".$act_id, $link);/////////////
for ($i=1; $i<=$n[0]; $i++){
	$cad = "i035".$i;
	if(!empty($_POST[$cad])){
		mysql_query("insert into orien_tem(insti_codigo, tematica_codigo, rank) values ($id , $i, $_POST[$cad])", $link);
		mysql_query("insert into tem_act(insti_act_codigo, tematica_codigo, rank) values ($act_id , $i, $_POST[$cad])", $link);
	}
}

$q = mysql_query("select pobl_meta_codigo from pobl_meta", $link);
$n = mysql_num_rows($q);
mysql_free_result($q);

mysql_query("delete from benef_act where insti_act_codigo=".$act_id, $link);/////////////
for ($i=1; $i<=$n; $i++){
	$cad = "i036".$i;
	if(!empty($_POST[$cad])){
		mysql_query("insert into benef_act(insti_act_codigo, pobl_meta_codigo) values ($act_id ,$_POST[$cad])", $link);
	}
}

$q = mysql_query("select vocacion_codigo from vocacion", $link);
$n = mysql_num_rows($q);
mysql_free_result($q);

mysql_query("delete from alcance_act where insti_act_codigo=".$act_id, $link);/////////////
for ($i=1; $i<=$n; $i++){
	$cad = "i037".$i;
	if(!empty($_POST[$cad])){
		mysql_query("insert into alcance_act(insti_act_codigo, vocacion_codigo) values ($act_id ,$_POST[$cad])", $link);
	}
}
if(isset($aceptaro)){
		header("location:index4-1.htm");
	} else if(isset($aceptar)){
		header("location:proyectos.htm");
	}
?>