<?php
	//connection
	include ("frames_header.php");
	include("connection.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></center>";
	    include ("frames_footer.php"); 
		return;
	}
?>

<?php

//not being used for the moment...
//include("utils.php");

$id=$_POST["regid"];
$number=$_POST["number"];

pg_query($c,"begin") or die("<strong>ERROR </strong>".pg_last_error($c));
	
for($i=0;$i<$number;$i++){
if($_POST["being".$i]==1){
	$motif_id=$_POST["motif"];	
	$begin=$_POST["begin".$i]-1;   
	$stop=$_POST["stop".$i]-1; 
	
	//0 does not exist!
	//$add=1;
	//if(($begin*$stop)<=0){ $add = 0; }
	//$size = abs($stop-$begin)+$add;
	#$size = $stop - $begin + 1;

	$comments = "";
	if (isset($_POST["comments".$i])){
		$comments=$_POST["comments".$i];   
	}

	//Get the sequence from the Regulatory region...
	$requete="SELECT A.\"BEGIN\", A.\"END\", B.\"SEQUENCE\" from \"PROMOTER\" A, \"SEQUENCE\" B where \"MOL_ID\"=\"PROMOTER_ID\" AND \"PROMOTER_ID\"=$id";
	$stmt=pg_query($c,$requete);
	if ($row=pg_fetch_array($stmt)) {
  
 
		$reg_begin = $row["BEGIN"];
		$lob = $row["SEQUENCE"];
		pg_query ($c, "begin");		
		$handle_lo = pg_lo_open($c,$lob,"r");
		$region=pg_lo_read($handle_lo);
		pg_query($c,"end");
		//$sequence = substr($region,$begin-$row["BEGIN"],$size);
		$sequence = substr($region,$begin,$stop - $begin + 1);
		
		//if(($row["BEGIN"]<0) && ($begin>=0)){
		//	$begin = $begin + 1;
		//	$stop = $begin + $size;
		//}
	}

	//Insertion	
	if(($reg_begin<0) && ($reg_begin+$begin>=0)){ $begin++;	}
	$begin = $reg_begin + $begin;
	
	if(($reg_begin<0) && ($reg_begin+$stop>=0)){ $stop++;	}
	$stop = $reg_begin + $stop;

	//check for confirmity...
	if($begin<$row["BEGIN"] || $stop>$row["END"]){
		print "<br/><center><strong>Given motif coordinates exceed sequence coordinates!</strong></center><br/>";
		include ("frames_footer.php"); 
		return;	    
	}
	
	
	$khoueiry="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\", \"CURATED\" ) values ( nextval('\"S_BIO_ID\"'),'n')";
	pg_query($c,$khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c));

	$requete="insert into \"MOTIF_COORD\" (\"MOTIF_COORD_ID\", \"MOTIF_ID\",\"SEQUENCE\",\"PROMOTER_ID\",\"BEGIN\",\"STOP\",\"COMMENTS\") values (currval('\"S_BIO_ID\"'),$motif_id,'$sequence',$id,'$begin','$stop','$comments')";
	pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

	$test="SELECT \"IS_ADMIN\" from \"AUTHOR\" where \"LOGIN\"='".$_SESSION["login"]."'";
	$stest=pg_query($c,$test);
	$rtest=pg_fetch_array($stest);
	if ($rtest["IS_ADMIN"]=='y'){
	      $khoueiry = "update \"ENTRY_ORIGIN\" set \"CURATOR_ID\"=".$_SESSION['curationauthor'].", \"CURATION_DATE\"=now() where \"BIO_ID\"='$id'";	  
	} else {
	      $khoueiry = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=now() where \"BIO_ID\"='$id'";		  
	}
	pg_query($c,$khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c));
} //end if
}

pg_query($c,"commit") or die("<strong>ERROR </strong>".pg_last_error($c));
	
echo '<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>';

include ("frames_footer.php"); 
		
?>
