<?php	include("connection.php");  ?>
<html>
<head>
	<title>Anatomy Tree</title>
	<style>
	body{
	   font: 10pt Verdana,sans-serif;
	   color: white;
	}
	.branch{
	   cursor: pointer;
	   cursor: hand;
	   display: block;
	}
	.leaf{
	   display: none;
	   margin-left: 16px;
	}
	a{
	   text-decoration: none;
	}
	a:hover{
	   text-decoration: underline;
	}
	</style>


	<script language="JavaScript">
	var openImg = new Image();
	openImg.src = "pictures/region.gif";
	var closedImg = new Image();
	closedImg.src = "pictures/region.gif";

	
	
	function showBranch(branch){
	   var objBranch = window.document.getElementById(branch).style;
	   if (objBranch.display=="block")
		  objBranch.display="none";
	   else
		  objBranch.display="block";
	   //swapFolder('I' + branch);
	}
	/*
	function swapFolder(img){
	   //sert initialement a changer une icone en une autre
	   objImg = document.getElementById(img);
	   if (objImg.src.indexOf('pictures/region.gif')>-1) //closed
		  objImg.src = openImg.src;
	   else
		  objImg.src = closedImg.src;
	
	}
	*/
	function tree(){
	   this.branches = new Array();
	   this.add = addBranch;
	   this.write = writeTree;
	}

	function addBranch(branch){
	   this.branches[this.branches.length] = branch;
	}
	
	function writeTree(){
	   var treeString = '';
	   var numBranches = this.branches.length;
	   for (var i=0;i <numBranches;i++)
		  treeString += this.branches[i].write();
	   window.document.write(treeString);
	}

	function branch(id, type, text, parent, nbr){
	   this.nbr = nbr;
	   this.id = id;
	   this.type = type;
	   this.text = text;
	   this.parent = parent;
	   this.write = writeBranch;
	   this.add = addLeaf;
	   this.leaves = new Array();
	}

	function addLeaf(leaf){
	   this.leaves[this.leaves.length] = leaf;
	}
	
	function writeBranch(){
	   var branchString = '<span class="branch" ' + ' onClick="showBranch(\'' + this.id + '\')"';
	   
	   dejavu=new Boolean(false);
	   for(a=0; a<window.opener.selected_part.length; ++a){
	   		if( window.opener.selected_part[a][1] == this.id ){
				dejavu=true;
				break;
			}
	   }
	   
	   if(this.nbr==0){
	       available="";
	   }else{
	       available=" ("+this.nbr+")";
	   }
	   
	   if(dejavu==true){
		   if(this.type=='organism')
				branchString += '><img src="pictures/tetard.gif" id="I' + this.id + '">' + this.text + ' <input checked name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);"> <img src="pictures/down.gif" border="0" onClick="self.checkChild('+this.id+');">' +available;
		   else if(this.type=='organ')
				branchString += '><img src="pictures/structure.gif" id="I' + this.id + '">' + this.text + ' <img src="pictures/up.gif" border="0" onClick="self.checkParent('+this.id+');">  <input checked name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);"> <img src="pictures/down.gif" border="0" onClick="self.checkChild('+this.id+');">' +available;
		   else if(this.type=='structure')
				branchString += '><img src="pictures/region.gif" id="I' + this.id + '">' + this.text + ' <img src="pictures/up.gif" border="0" onClick="self.checkParent('+this.id+');">  <input checked name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);"> <img src="pictures/down.gif" border="0" onClick="self.checkChild('+this.id+');">' +available;
		   else if(this.type=='stage')
		   		branchString += '><img src="pictures/stage.gif" id="I' + this.id + '">' + this.text;
		   else
		   		branchString += '><img src="pictures/cell.gif" id="I' + this.id + '">' + this.text + ' <img src="pictures/up.gif" border="0" onClick="self.checkParent('+this.id+');">  <input checked name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);">' +available;		
	   }else{
		   if(this.type=='organism')
				branchString += '><img src="pictures/tetard.gif" ide="I' + this.id + '">' + this.text + ' <input name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);"> <img src="pictures/down.gif" border="0" onClick="self.checkChild('+this.id+');">' +available;
		   else if(this.type=='organ')
				branchString += '><img src="pictures/structure.gif" id="I' + this.id + '">' + this.text + ' <img src="pictures/up.gif" border="0" onClick="self.checkParent('+this.id+');">  <input name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);"> <img src="pictures/down.gif" border="0" onClick="self.checkChild('+this.id+');">' +available;
		   else if( (this.type=='structure')||(this.type=='region') )
				branchString += '><img src="pictures/region.gif" id="I' + this.id + '">' + this.text + ' <img src="pictures/up.gif" border="0" onClick="self.checkParent('+this.id+');">  <input name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);"> <img src="pictures/down.gif" border="0" onClick="self.checkChild('+this.id+');">' +available;		
		   else if(this.type=='stage')
		   		branchString += '><img src="pictures/stage.gif" id="I' + this.id + '">' + this.text;
		   else
		   		branchString += '><img src="pictures/cell.gif" id="I' + this.id + '">' + this.text + ' <img src="pictures/up.gif" border="0" onClick="self.checkParent('+this.id+');">  <input name="c'+this.id+'" type="checkbox" value="'+this.text+'" onClick="self.selection(this);">' +available;
	   } 
	   
	   branchString += '</span>';
	   branchString += '<span class="leaf" id="';
	   branchString += this.id + '">';
	   var numLeaves = this.leaves.length;
	   for (var j=0;j<numLeaves;j++)
		  branchString += this.leaves[j].write();
	   branchString += '</span>';
	   return branchString;
	}
	/*
	function leaf(id, text, link){
	   this.id = id;
	   this.text = text;
	   this.link = link;
	   this.write = writeLeaf;
	}
	
	function writeLeaf(){
	   var leafString = '<a href="' + this.link + '">';
	   leafString += '<img src="pictures/cell.gif" border="0">';
	   leafString += this.text;
	   
	   
	   
	   dejavu=new Boolean(false);
	   for(a=0; a<opener.selected_part.length; ++a){
	   		if( opener.selected_part[a][1] == this.id ){
				dejavu=true;
				break;
			}
	   }
	   if(dejavu==true){
	   		leafString += '</a> <img src="pictures/up.gif" border="0"> <input checked name="'+this.text+'" type="checkbox" value="'+this.id+'" onClick="self.selection(this);" > <br>';
	   }else{
	   		leafString += '</a> <img src="pictures/up.gif" border="0"> <input name="'+this.text+'" type="checkbox" value="'+this.id+'" onClick="self.selection(this);" > <br>';
	   }
	   
	   
	   return leafString;
	}
	*/
	
	//ajoute le nom dans la 1 fenetre
	function selection(obj){
		//enleve le c dans le nom de checkbox pour recuperer l'id
		id=obj.name.substr(1,obj.name.length-1);
		if(obj.checked){
			window.opener.addtoarray(obj.value,id);
			window.opener.update();
		}else{
			
			for (a=0; a<window.opener.selected_part.length; ++a)
				if(window.opener.selected_part[a][1]==id){
					window.opener.retrenchtoarray(a);
					break;
				}
			window.opener.update();
			
		}
	}
	
	function checkChild(id){
	
		id2='z'+id;  //pour retrouver le nom de l'objet declare par la fonction PHP.
		
		obj=eval(id2);
		child=0;
		chemin=new Array();
		
		do{
			while( 1==1 ){
				
				bool=false;
				if(chemin.length>=0){
					for(a=0; a<chemin.length; ++a){
						if(obj==chemin[a][0]){
							bool=true;
							break;
						}
					}
				}
				if(bool == false){
					chemin[a]=[obj,0];
				}else{
					chemin[a]=[obj,++chemin[a][1]];
				}
				bool=false;
				
				//checkobj=eval('c'+obj.id);
				checkobj=eval('window.document.formu.c'+obj.id);				
				
				checkIt(checkobj);
				child=chemin[chemin.length-1][1];
				
				if(obj.leaves.length-1<child)
					break;
				
				obj=obj.leaves[child];
			}
			
			if(chemin.length-1==0)
				break;
				
			chemin=chemin.slice(0,chemin.length-1);
			obj=chemin[chemin.length-1][0];
		    
		}while( 1==1  );
		
	}
	
	function checkParent(id){
		
		obj=eval('z'+id);
		
		while(obj.parent!=null){
			checkobj=eval('window.document.formu.c'+obj.id);
			checkIt(checkobj);
			
			obj=eval('z'+obj.parent);
		}
		
	}
	
	function checkIt(obj){
		if(obj.checked==false){
			id=obj.name.substr(1,obj.name.length-1);
			window.opener.addtoarray(obj.value,id);
			window.opener.update();
			obj.checked=true;
		}
	}
	
	</script>



</head>


<body bgcolor="#505F70" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" onUnload="window.opener.formulaire.region.focus();">

<form name="formu">
<table width="100%" border="0" cellspacing="0">
  <tr bgcolor="#990000">
    <td> <strong><font color="#FFFFFF">Anatomical dictionary</font></strong></td>
  	</tr>
  <tr bgcolor="#FFFFCC">
    <td>
		<p><font color="#000000">Select anatomical structures.</font></p>
    </td>
  </tr>
</table>

<font size="-2">Click on terms to expand tree.</font>
<br><br>


<?php

	$tab = split(':', $_GET["iddev"]);
	$tabname = split(':', $_GET["namedev"]);
        $species = $_GET["species"];
	
	

	echo "<script language=\"JavaScript\">\n";
	echo "var myTree = new tree();\n";
	
	for($a=0; $a<count($tab)-1; ++$a){	
		
		//le premier noeud est le nom du stade
		echo "var "."stage$a"."= new branch('".$tabname[$a]."', 'stage', '".$tabname[$a]."',null);\n";
		/*
		$query= "SELECT NAME, ANAT_ID, TYPE, CHILD_OF, DISPO FROM CIONA_DESIGN.ANAT_ENTITY  WHERE SPECIES= '$species' AND DEV_STAGE_ID= ".$tab[$a]."  CONNECT BY CHILD_OF = PRIOR ANAT_ID START WITH CHILD_OF IS NULL";
		$stmt = OCIParse($connection, $query);
		OCIExecute($stmt);
		while(OCIFetch($stmt)){
			
			echo "var z".OCIResult($stmt, "ANAT_ID")." = new branch('".OCIResult($stmt, "ANAT_ID")."','".OCIResult($stmt, "TYPE")."','".OCIResult($stmt, "NAME")."','".OCIResult($stmt, "CHILD_OF")."','".OCIResult($stmt, "DISPO")."');\n";
			if(OCIResult($stmt, "CHILD_OF")==null)
				echo "stage$a".".add(z".OCIResult($stmt, "ANAT_ID").");\n";
			else
				echo "z".OCIResult($stmt, "CHILD_OF").".add(z".OCIResult($stmt, "ANAT_ID").");\n";	
		*/


/* SIMULATION D'UNE REQUETE SUR HIERARCHIE PAR UNE PROCEDURE DE REQUETES RECURSIVES */

/* VARIANTE DANS LA REQUETE POUR LA CONNEXION ET LE RESULTAT DU SELECT */


function select_recursif_descendant($conn,$start,$res)
{
global $species;
global $tab;
global $a;
/*ON ECRIT SA REQUETE*/

if ($start!='') 
$requete="SELECT distinct * FROM \"ANAT_ENTITY\" WHERE  \"SPECIES\"='".$species."' AND \"DEV_STAGE_ID\"=".$tab[$a]." and  \"CHILD_OF\"=$start";
else
$requete="SELECT distinct * FROM \"ANAT_ENTITY\" WHERE  \"SPECIES\"='".$species."' AND \"DEV_STAGE_ID\"=".$tab[$a]." and  \"CHILD_OF\" is null";


$stmt=pg_query($conn,$requete);
while ($row=pg_fetch_array($stmt))
	{
	echo "var z".$row["ANAT_ID"]." = new branch('".$row["ANAT_ID"]."','".$row["TYPE"]."','".$row["NAME"]."','".$row[ "CHILD_OF"]."','".$row["DISPO"]."');\n";
			if($row["CHILD_OF"]==null)
				echo "stage$a".".add(z".$row["ANAT_ID"].");\n";
			else
				echo "z".$row["CHILD_OF"].".add(z".$row["ANAT_ID"].");\n";		


	$res=select_recursif_descendant($conn,"'".$row["ANAT_ID"]."'",$res);
	}
return $res;
}

$res=select_recursif_descendant($c,null,$res);



/* FIN DE LA SIMULATION */







		/*}*/
		echo "myTree.add("."stage$a".");\n";
	}

echo "myTree.write();\n";
echo "</script>\n";


?>


<br><br><br>
<center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>
<br>
- use the <img src="pictures/up.gif"> button to select all anatomy terms including the selected one.<br><br>
- use the <img src="pictures/down.gif"> button to select all anatomy terms included in the selected one.<br><br>
- <input name="" type="checkbox" value="" checked> Exact term.<br><br>
- <strong>(5)</strong> number of in situs available for this precise term. 

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