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

<html>
<body bgcolor="#999966" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<title>ANISEED CURATOR - Add Embryo Manipulation</title><br>
 
<?php
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<center><strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
	    print '</center></body></html>';
		return;
	}
?>


<form action="addembryomanip2.php" method="GET" name="formulaire">

<?
$id=$_GET["id"];
$species=$_GET["species"];
$dev=$_GET["dev"];


print "<input type=\"hidden\" name=\"id\" value=\"$id\">";

print "<br><center><h1>Add an embryo manipulation to $id</h1></center><br><br>";


 print '<center><table width="99%" cellpadding="0" cellspacing="0" border="1" bordercolor="#333333">  <tr bgcolor="#990000" bordercolor="#990000" align="center" valign="middle">  <td><strong>Removed anatomy part*</strong></td>  <td><strong>From stage*</strong></td></tr>';
print "<tr>";

print "<td >";
if (strlen($dev)!=0)
{
//Arbre anatomique

$req="SELECT \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='$dev'";
$st=pg_query($c,$req);
if (pg_num_rows($st)>0) 
{
$row=pg_fetch_array($st);
$devid=$row["DEV_STAGE_ID"];
}
else $devid=$dev;

$nivo=0;
/*
$requete="SELECT \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='$dev' and \"SPECIES\"='$species'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$devid=$row["DEV_STAGE_ID"];
*/


print "<input type=\"hidden\" name=\"devid\" value=\"$devid\">";

function select_recursif_descendant_2($conn,$start,$res,$nivo)
{
global $devid;
//global $level;
global $anatid;
//global $nivo;
/*ON ECRIT SA REQUETE*/

//print "la";
if ($start!='')
{
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"DEV_STAGE_ID\"= ".$devid." and  \"CHILD_OF\"= $start ";

//print $start;
$level++;
}
else {
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"DEV_STAGE_ID\"= ".$devid." and  \"CHILD_OF\" is null";
//$level=0;

}

$stmt2=pg_query($conn,$requete2);

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

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

while($row2=pg_fetch_array($stmt2))
	{
for($a=0;$a<$nivo-1;++$a){
				if( $nivo>1 )
					echo '&nbsp;&nbsp;<font color="#999999">|</font>&nbsp;&nbsp;';
			}
			
			if( $row2["TYPE"]=='cell' )
				print "<img src=\"pictures/cell.gif\">";
			else if( $row2["TYPE"]=='organism' )
				print "<img src=\"pictures/tetard.gif\">";
			else
				print "<img src=\"pictures/region.gif\">";
			
			
			if($row2["ANAT_ID"]==$anatid){
				echo '<A NAME="tag"> <font color="#FF9900"><strong>'.$row2["NAME"]."</strong></font> </A>";//.OCIResult($stmt, "ANAT_ID");
				print "<input name=\"region\" type=\"radio\" value=\"".$row2["ANAT_ID"]."\" ";
			    //if($nivo==1)
				    print "checked ";
				print ">";
			}else{
				echo $row2["NAME"];
				if((strlen($anatid)>0)){
					print "<input name=\"region\" type=\"radio\" value=\"".$row2["ANAT_ID"]."\">";//.OCIResult($stmt, "ANAT_ID");
				}else{
					print "<input name=\"region\" type=\"radio\" value=\"".$row2["ANAT_ID"]."\"";//.OCIResult($stmt, "ANAT_ID");
			        if($nivo==1)
					    print "checked ";
					print ">";
				}
			}
				print '<br>';
	
	$res=select_recursif_descendant_2($conn,$row2["ANAT_ID"],$res,$nivo);

	
	}

return $res;
}

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

}
else print "&nbsp;";
print "</td>";

//Les stages
//print "<td align=\"center\"><select name=\"from\" onChange=\"javascript: self.location.href='addembryomanip.php?id='+$id+'&species='+$species+'&structure='+this.options[this.selected].value";


print "<td align=\"center\" valign=\"top\"><select name=\"from\" onChange=\"javascript:self.location.href='addembryomanip.php?id=$id&species=$species&dev='+this.options[this.selectedIndex].text\" >";



//print "<td align=\"center\"><select name=\"from\" onChange=\"javascript:alert('salut')\">";


print "<option></option>";
$requete="SELECT \"NAME\",\"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"SPECIES\"='$species' ORDER BY \"DEV_PERCENT\"" ;
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
if ($row["NAME"]==$dev) print "<option selected>".$row["NAME"]."</option>";
else print "<option>".$row["NAME"]."</option>";
}
print "</select></td></table>";
?>
<br>
<br>
<br>
<center><input name="submit" type="image" src="pictures/button-submit.jpg" onClick="submit"></center>                                          

</form>
</body>
</html>
