<?php

	include ("frames_header.php"); 
	include ("connection.php");
	
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
		include ("frames_footer.php"); 
		return;
	}

	$id = $_POST["id"];
	$annotation = $_POST["annotation"];	
		
	pg_query($c,"begin") or die ("<strong>Error: </strong>".pg_last_error($c));

	if($annotation=="none"){
		$khoueiry="DELETE FROM \"SO_DESCRIPTION\" WHERE \"PROMOTER_ID\"=$id";	 
	} else {
	
		//Check if it exists already!
		
		$kh2="SELECT * FROM \"SO_DESCRIPTION\" WHERE \"PROMOTER_ID\"=$id";	 
		$stest=pg_query($c,$kh2) or die ("<strong>Error: </strong>".pg_last_error($c));
		$rtest=pg_fetch_array($stest);
		if ($rtest){		
			$khoueiry="UPDATE \"SO_DESCRIPTION\" SET \"SO_ID\"='$annotation' WHERE \"PROMOTER_ID\"=$id";	 
		} else {
			$khoueiry="INSERT INTO \"SO_DESCRIPTION\" (\"SO_ID\", \"PROMOTER_ID\") VALUES ('$annotation', $id)";
		}
			
	}
  	
	pg_query($c,$khoueiry) 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));	  	  	
	
	pg_query($c,"end") or die ("<strong>Error: </strong>".pg_last_error($c));	  
	
	echo '<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>';

	include ("frames_footer.php"); 

?>