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

<SCRIPT LANGUAGE="JavaScript">

function tjs_haut() {
	var indice=window.document.formulaire.lib_id.selectedIndex
	if (indice<0) {
		alert("Aucune ligne n'est selectionnee");
	}
	if (indice>0) {	
		tjs_swap(indice,indice-1);
	}
}

function tjs_bas() {
	var indice=window.document.formulaire.lib_id.selectedIndex
	if (indice<0) {
		alert("Aucune ligne n'est selectionnee");
	}
	if (indice<window.document.formulaire.lib_id.options.length-1) {	
		tjs_swap(indice,indice+1);
	}
}

function tjs_swap(i,j) {
	var valeur=window.document.formulaire.lib_id.options[i].value;
	var texte=window.document.formulaire.lib_id.options[i].text;
	window.document.formulaire.lib_id.options[i].value=window.document.formulaire.lib_id.options[j].value;
	window.document.formulaire.lib_id.options[i].text=window.document.formulaire.lib_id.options[j].text;
	window.document.formulaire.lib_id.options[j].value=valeur;
	window.document.formulaire.lib_id.options[j].text =texte;
	window.document.formulaire.lib_id.selectedIndex=j
	tjs_ordre(window.document.formulaire);
}

function tjs_ordre(f) {
	var l=f.lib_id;
	var ordre="";
	for(var i=0;i<window.document.formulaire.lib_id.options.length;i++) {
		if (i>0) {ordre+="-";}
		ordre+=window.document.formulaire.lib_id.options[i].value;		
	}
	f.ordre.value=ordre;
}

</SCRIPT>




<?php

if (isset($_GET["species"])) $species=$_GET["species"];

	if( (isset($_GET["species"])) and (strlen($species)>0) ){
		$query1="select count(*) as \"COUNT\" from \"LIBRARY\" where \"SPECIES\"='$species'";
		$stmta=pg_query($c,$query1) or die("<strong>Error</strong> ".pg_last_error($c));
		if($rowa=pg_fetch_array($stmta))
			$count=$rowa["COUNT"];
		else $count="";		
		$query="SELECT \"LIBRARY_ID\",\"TITLE\",\"DEV_STAGE\" FROM \"LIBRARY\" WHERE \"SPECIES\"='$species' ORDER BY \"ORDRE\"";
		$stmt = pg_query($c,$query) or die("<strong>Error</strong> ".pg_last_error($c));
	}


?>




<h2><strong><em>Library classification.</em></strong> </h2>
<center>
<form name="formulaire" action="library_hierarchie2.php" method="get" target="_blank">
	<table width="99%" border="0" cellspacing="0" background="pictures/barre.jpg">
	  <tr bgcolor="#990000">
	  	<td>&nbsp;</td>
		<td>&nbsp;</td>
	  	<td align="right" background="pictures/barre-red.jpg">
		    <em><strong>Library classification</strong></em>
		</td>
	  	
	  </tr><tr>
	      <td align="right"><br><input type="checkbox" name="test" value="y" checked></td>
		  <td align="left"><br><strong>Perform read test without recording data.</strong></td>
		  <td>&nbsp;</td>
	  </tr><tr>
		<td colspan="4"><hr size="1" color="#990000"></td>
	  </tr><tr>
		<td align="right"><br><strong>Species :&nbsp;</strong> </td>
		<td align="left"><br><select name="species" size="1" onChange="self.location.href = 'library_hierarchie.php?species=' + this.options[ this.selectedIndex ].value">
            <option ></option>
                     <?php
                      
                      $requete="select \"NAME\" from \"SPECIES\" ";
                      $stmt0 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
                      while($row0=pg_fetch_array($stmt0))
                      {
                        $species2=$row0["NAME"];
                        //echo "<option value=\"$species2\" >$species2</option>";
                        
				
                        if ($species==$species2)
						echo "<option value=\"$species\" selected>$species</option>";
					  else
						echo "<option value=\"$species2\">$species2</option>";
					  
			
                                    
                        }
				    ?>
                    </select><br><br>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		
		
	</tr><tr>
		<td>&nbsp;</td>
        <td background="pictures/barre-red.jpg" width='600'>
        <table>
        	<tr>
        		<td colspan="3"><center><br><strong>Libraries : </strong></center><br></td>
            </tr><tr>
            	<td align="left">&nbsp;LIB_ID</td>
        		<td align="center">TITLE</td>
        		<td align="center">DEV_STAGE&nbsp;</td>
        	</tr><tr>
				<td colspan="3">
					<?php
					
					if (!isset($count))
						print "<select name=\"lib_id\" size=\"10\" style='width:600px'>";
					else
            			print "<select name=\"lib_id\" size=\"".$count."\" style='width:600px'>";
					 $id="";
					 while($row=pg_fetch_array($stmt))
				 	{
						print "<option value=\"".$row["LIBRARY_ID"]."\">";
						print $row["LIBRARY_ID"]." &nbsp;&nbsp;||&nbsp;&nbsp;". $row["TITLE"]."&nbsp;&nbsp;||&nbsp;&nbsp;".$row["DEV_STAGE"]."</option>\n";
					
						$id .= $row["LIBRARY_ID"].":";
				 	}
				?>
				</select>
				</td>
			</tr>
		</table>
		<td align="left">
					
			<INPUT type=button value='Up' style='width:100px' onClick='tjs_haut()'>
			<br>
			<INPUT type=button value='Down' style='width:100px' onClick='tjs_bas()'>
		</td>
		
		</tr><tr>
			<td align="right">
				List order: 
			<td align="left"> <INPUT type=text size='50' name=ordre>
			</td>
			<td>&nbsp;</td>
	  </tr><tr>
	  	<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>
			<br><br>
			<input name="submit" type="image" src="pictures/button-submit.jpg" onClick="submit">&nbsp;&nbsp;&nbsp;
			<br>
		</td>
	  </tr>
	</table>

</form>
</center>


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