<?php	
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
include("connection.php");  

//Title before frames_header
print '<title>ANISEED v3.0 - Anatomy Tree</title>';


?>
<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_notregion.length; ++a){
	   		if( window.opener.selected_part_notregion[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_notregion(obj.value,id);
			window.opener.update_notregion();
		}else{
			
			for (a=0; a<window.opener.selected_part_notregion.length; ++a)
				if(window.opener.selected_part_notregion[a][1]==id){
					window.opener.retrenchtoarray_notregion(a);
					break;
				}
			window.opener.update_notregion();
			
		}
	}
	
	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_notregion(obj.value,id);
			window.opener.update_notregion();
			obj.checked=true;
		}
	}
	
	</script>



</head>



<!--<body bgcolor="#f7fbfe" text="#000000" link="#000000" vlink="#000000" alink="#000000" onUnload="window.opener.formulaire.region.focus();">-->
<body bgcolor="#f7fbfe" onUnload="window.opener.formulaire.region.focus();">
<font color="black">
<form name="formu">
<table width="100%" border="0" cellspacing="0">
  <tr bgcolor="#FFFFFF">
    <td> <strong><font color="#D64242">Anatomical dictionary</font></strong></td>
  	</tr>
  <tr>
    <td>
		<p><font color="#456898" size="2"><b>Select anatomical structures.</b></font></p>
    </td>
  </tr>
</table>

<font size="-2" color="#000000">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";
	
function select_recursif_descendant($conn,$start,$res,$a)
{
global $species;
global $tab;
/*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 order by \"NAME\"";
else
$requete="SELECT distinct * FROM \"ANAT_ENTITY\" WHERE  \"SPECIES\"='".$species."' AND \"DEV_STAGE_ID\"=".$tab[$a]." and  \"CHILD_OF\" is null order by \"NAME\"";


$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,$a);
	}
return $res;
}

	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";




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


		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>
</font>
</body>
</html>

