<?php
include("connection.php");
include("frames_header.php");
?>

<script language="JavaScript">
function getValue(){
    for(a=0; a< document.formu.length; a++){
        if(document.formu[a].checked==true){
			obj=document.formu[a];
            return obj.value;
        }
    }
}            
</script>

<form action="anatomy-fate-update.php" method="post" name="formu" target="_blank">

<?php
	$region = $_GET["region"];
	$dev = $_GET["dev"];
	$fates = $_GET["fate"];
	
	if(strlen($fates)==0)
	{
	print "<center><h2><strong>No match</strong></h2><br>please select at least one fate</center>";
		include("frames_footer.php");
	    return;

	}

	if( (strlen($dev)<=0)or(ereg("___",$dev)) ){
	    print "<center><h2><strong>No match</strong></h2><br>please select one developmental stage</center>";
		include("frames_footer.php");
	    return;
	}

	$requete= "SELECT \"NAME\", \"SPECIES\" FROM \"DEV_STAGE\" WHERE \"DEV_STAGE_ID\"= $dev";
    $stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
    if( $row=pg_fetch_array($stmt)){
	    print '<input name="dev" type="hidden" value="'.$dev.'">';  //memorise le dev_stage_id
		print '<input type="hidden" name="fatelist" value="'.implode(":",$fates).'">';//memorise les fates
		
		print "<center><h2><strong><em>".$row["SPECIES"]."  (".$row["NAME"].")<br>Anatomical tree</em></strong> </h2></center><br><br>";
	    
		//creation des colonnes pour les fates
		print '<center><table width="99%" border="0" cellspacing="0" cellpadding="0" background="pictures/barre.jpg"><tr bgcolor="#990000"><td align="center"><strong>Anatomical tree</strong></td>';
		for($a=0; $a<count($fates); ++$a){
		    print '<td align="center"><strong>'.$a.'</strong></td>';
		}
		print "</tr>";

/*SIMULATION BOF*/
$nivo=0;
function select_recursif_descendant_2($conn,$start,$res,$nivo)
{
global $dev;
//global $level;
global $region;
global $fkw;
global $fates;

//global $nivo;
/*ON ECRIT SA REQUETE*/

//print "la";
if ($start!='')
{
$requete="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"DEV_STAGE_ID\"= ".$dev." and  \"CHILD_OF\"= $start ";
//print $start;
$level++;
}
else {
$requete="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"DEV_STAGE_ID\"= ".$dev." and  \"CHILD_OF\" is null";
//$level=0;
}

$stmt=pg_query($conn,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

//if (pg_num_rows($stmt2)==0) {$level--;}

if (pg_num_rows($stmt)!=0) $nivo++;//print "Nz";
if (pg_num_rows($stmt)==0) $nivo--;//print "Ze";

while($row=pg_fetch_array($stmt))
	{
			print "<tr>";
			for($a=0;$a<count($fates)+1;++$a){
				print '<td valign="top"><hr color="#871F1F" size="1"></td>';
			}
			print "</tr>";
		
		
	        print "<tr><td width=\"500\">";
			for($a=0;$a<$nivo-1;++$a){
				if( $nivo>1 )
					echo '&nbsp;&nbsp;<font color="#999999">|</font>&nbsp;&nbsp;';
			}
			
			if( $row["TYPE"]=='cell' )
				print "<img src=\"pictures/cell.gif\">";
			else if( $row["TYPE"]=='organism' )
				print "<img src=\"pictures/tetard.gif\">";
			else
				print "<img src=\"pictures/region.gif\">";
			
			
			if($row["ANAT_ID"]==$region){
				echo '<A NAME="tag"> <font color="#FF9900"><strong>'.$row["NAME"]."</strong></font> </A>";//.OCIResult($stmt, "ANAT_ID");
				////print "<input name=\"region\" type=\"radio\" value=\"".OCIResult($stmt, "ANAT_ID")."\" ";
			    if($nivo==1)
				    print "checked ";
				print ">";
			}else{
				echo $row["NAME"];
				
			}
			
			    //recup des fates deja definis
				$requete2= "SELECT \"KEYWORDS\" FROM \"FATE\" WHERE \"ANAT_ID\"= ".$row["ANAT_ID"];
				$stmt2 = pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));
				$fkw = (array) null;
				$a=0;
				while($row2=pg_fetch_array($stmt2)){
				    $fkw[$a++]=$row2["KEYWORDS"];
					
				}
			
			
			print "</td>";
			//generation des checkbox pour les differents fates
            for($a=0;$a<count($fates);++$a){
			    print "<td align=\"center\" valign=\"top\"><input name=\"f[]\" type=\"checkbox\" value=\"".$fates[$a]."--".$row["ANAT_ID"]."\"";
				if( in_array($fates[$a], $fkw) )
				    print " checked";
				print "><br><font color=\"#CC3300\" size=\"-2\">".$fates[$a]."&nbsp;</font></td>";
			}
		    print "</tr>";

	
	$res=select_recursif_descendant_2($conn,$row["ANAT_ID"],$res,$nivo);

	
	}

return $res;
}

select_recursif_descendant_2($c,null,$res,$nivo);




		print "</table>";
		print '<br><table width="99%" border="0" cellspacing="0" cellpadding="0"><tr><td align="right"><input name="submit" type="image" src="pictures/button-submit.jpg" onClick="submit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table></center>';
	
	}else{ print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>"; }
?>

</form>

<?php
    include("frames_footer.php");
?>