#!/bin/perl -w

use strict;

use CGI;
use CGI qw(:standard *table *TR *th *td *ul *ol);
use CGI qw/unescape/;
use Gramene::Literature::GetRefD; 
use Gramene::Page; 
use Gramene::Utils 'pager';  


my $entries_per_page = 20; 

my $page_title = "Literature search";
my $q = CGI->new();
my $page = Gramene::Page->new(Apache->request);


my $page_no = $q->param('page_no') || 1;
 
my $table_only = $q->param("table");

unless( ( $table_only ) && ( $table_only eq "1" ) ) {
print $q->header;


print $page->start_html( -title=>$page_title);
print $page->start_body();


########################


print <<SMALLBAR1;
<table>
  <tr>
    <td>&nbsp;&nbsp;<img src="/images/icons/grain_icon.jpg" alt="grain_icon" height=16 width=16 align="top">
    </td>
    <td>
      &nbsp;<a href="/literature/" class="gopage"><b>Literature Home</b></a>&nbsp;
      |&nbsp;<a href="/newsletters/rice_genetics/" class="gopage"><b>Rice Genetics Newsletters</b></a>&nbsp;
   |&nbsp;<a href="/tutorials/literature.html" class="gopage"><b>Tutorial</b></a>&nbsp;   
   |&nbsp;<a href="/fom/cache/1.html" class="gopage"><b>FAQ</b></a>&nbsp;
 </td>
  </tr>
</table>
<p></p>
SMALLBAR1


print $q->table( {-border=>'0',
		  -cellspacing=>'0',
		  -cellpadding=>'3',
		  -width=>'98%',
                  -align=>'CENTER'},
		 $q->start_form(-method=>'GET'),

                 $q->Tr( {-align=>'CENTER', -nowrap=>'nowrap'},
	                 $q->th( {-align=>'CENTER',
				  -class=>'searchtitle'},
			         "Literature search" )
		
		       ),
		 $q->Tr( {-align=>'CENTER',
			  -valign=>'CENTER',
			  -nowrap=>'nowrap',
			  -class=>'searchbody'},
			 $q->td( {-align=>'CENTER'},
				 "<span class='alert'>Search for publications:</span>",
			         "&nbsp;&nbsp;&nbsp;",
				 $q->textfield( -name=>'word',
						-size=>40,
						-maxlength=>100 ),
				 "&nbsp;&nbsp;&nbsp;",
                                 $q->submit( -name=>'do_it',
					     -value=>'Search' ),
			         $q->p( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ e.g. ",
					$q->a( {-href=>"pub_search?word=wessler+sr"}, "Wessler SR" ),
					",&nbsp;",
                                        $q->a( {-href=>"pub_search?word=rice+genome"}, "rice genome" ),
					",&nbsp;",
					$q->a( {-href=>"pub_search?word=Rice+Genetics+Newsletter"}, "Rice Genetics Newsletter" ),
					"]" ) ) 
  
			),
		 
		
		
		 $q->end_form() );

print "<p></p>";
print $q->hr();

}



###########################

unless( $q->param() ) {

print <<FRONTPAGE;
<table border="0" cellspacing="0" cellpadding="0" width="98%" align="CENTER"><tr><td>
<ul>
  <li>Enter one or more search terms.</li>
  <li>Enter author names as "wessler sr", "wessler s", or "wessler". Initials are optional.</li>
  <li>Enter journal titles.</li>
</ul>
</td></tr></table>
FRONTPAGE

}  

############# URL recording starts ###################

if( $q->param() ) {

    my @names = $q->param();
    my $param_string = '';
    foreach my $name ( @names ) {
        if( $param_string ) {
	    $param_string = $param_string."&";
	} 
	my $myvalue = $q->param( $name );
	$myvalue =~ s/ /+/g;
	$param_string = $param_string.$name."=".$myvalue;
    }
    my $url = "/db/literature/pub_search?".$param_string;
    $url = join( "\n", $url =~ /(.{1,80})/g );
#    print STDERR ( "\n[URL] $url\n\n" );

}

############# URL recording ends ###################


if( $q->param("word") ) {

    my $test_word = $q->param("word");
    $test_word = unescape($test_word);
 

    $test_word  =~ tr/A-Z/a-z/;    #in lower case
    $test_word =~ tr/a-z//cd;     #delete non-letters

    if( $test_word ) {

        my $db = Gramene::Literature::GetRefD->whatever();
	$db->connect_to_ora( );
	

	my $original = $q->param("word");
        $original = unescape($original);
	my ( $author_original_a, $author_original_b );

	##########################
	# prepare author search  #
	##########################
	my $author = $original;
	$author =~ tr/a-z/A-Z/;
	my @fields = split( / /, $author );
	my ( $author_a, $author_b );
	if( $fields[1] ) {
	    
	    if( ( length( $fields[1] ) ) == 2 ) {
	        $author_a = "% ".$fields[0]."-".substr( $fields[1], 0, 1 )."-".substr( $fields[1], 1, 1 )."%";
		$author_original_a = $fields[0]."-".substr( $fields[1], 0, 1 )."-".substr( $fields[1], 1, 1 );
	    } else {
	        $author_a = "% ".$fields[0]."-".substr( $fields[1], 0, 1 )."%";
		$author_original_a = $fields[0]."-".substr( $fields[1], 0, 1 );
	    }

	    if( ( length( $fields[0] ) ) == 2 ) {
	        $author_b = "% ".$fields[1]."-".substr( $fields[0], 0, 1 )."-".substr( $fields[0], 1, 1 )."%";
		$author_original_b = $fields[1]."-".substr( $fields[0], 0, 1 )."-".substr( $fields[0], 1, 1 );
	    } else {
	        $author_b = "% ".$fields[1]."-".substr( $fields[0], 0, 1 )."%";
		$author_original_b = $fields[1]."-".substr( $fields[0], 0, 1 );
	    }

	} else {
	    $author_a = "% ".$fields[0]."-"."%";
	    $author_b = "% ".$fields[0]."-"."%";
	    $author_original_a = $fields[0];
	    $author_original_b = "#";
	}
	    

	###########################
	# prepare keyword search  #
	###########################
	#my $keyword = join( "%", @fields );
	#$keyword = "%".$keyword."%";
	my $keyword = $author;
	#$keyword = "%"." ".$keyword." "."%";

	##########################
	# prepare source search  #
	##########################
	my $source = $author;

	#######################
	# get search results  #
	#######################
	my @ref_ids = $db->get_all_refs( $author_a, $author_b, $keyword, $source );
     
	if( !@ref_ids ) {
	  
            print $q->p();
	    display_number( $q, 0 );
	   
        } else {
            
	  if(($table_only ) && ( $table_only eq "1" )){
            display_references( $q, $db, $original, $author_original_a, $author_original_b, 1, 20, @ref_ids );
          }else{
            display_references_new( $q, $db, $original, $author_original_a, $author_original_b, $page_no, $entries_per_page, @ref_ids );
          } 
        } #End else


    } else {

        print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "Sorry, the input is not valid. Please try again." ) ) );

    }

}


##################################


if( $q->param("all_ids") ) {

    my $string = $q->param("all_ids");

    my @ref_ids = split( /,/, $string );

    my $db = Gramene::Literature::GetRefD->whatever();
    $db->connect_to_ora( );
	
    if( !@ref_ids ) {
	    
        display_number( $q, 0 );
	
    } else {
            
        display_references( $q, $db, "#", "#", "#", 1, 20, @ref_ids );

    } #End else


}
  

############################

if( $q->param("source_id") ) {

    my $db = Gramene::Literature::GetRefD->whatever();

    $db->connect_to_ora( );

    my $source_id = $q->param("source_id");
    my $year_id = $q->param("year");
   
    my @ref_ids = $db->source_id_to_ref( $source_id, $year_id );


    if( !@ref_ids ) {
        display_number( $q, 0 );
    } else {
        
        if( $#ref_ids == 0 ) {
	    display_number( $q, 1 );
	} else {
	    display_number( $q, ($#ref_ids+1) );
	}
	display_references( $q, $db, @ref_ids );

    }

}


if( $q->param("author") ) {

   
    my $person_id = $q->param("author");

    if($person_id =~/\D/){

        print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "Sorry, the id is not valid. Please try again." ) ) );

    }else{
   

    my $db = Gramene::Literature::GetRefD->whatever();

    $db->connect_to_ora( );
    my $person_name = $db->get_person_name( $person_id );
    my $author_original_a = $person_name;
    my $author_original_b = "#";
    my $original = "#";

    my @ref_ids = $db->person_to_ref( $person_id );


    if( !@ref_ids ) {
        display_number( $q, 0 );
    } else {
        if(($table_only ) && ( $table_only eq "1" )){
            display_references( $q, $db, $original, $author_original_a, $author_original_b, 1, 20, @ref_ids );
	  }else{
        display_references_new( $q, $db, $original, $author_original_a, $author_original_b, $page_no, $entries_per_page, @ref_ids );
     }
    } #End else
    
  }
}


########################

if( $q->param("refs") ) {

    my $string = $q->param("refs");

    my @ref_ids = split( /,/, $string );


    my $db = Gramene::Literature::GetRefD->whatever();

    $db->connect_to_ora( );
    my $current_page;
    my $item_number = 20;
    my $original = $q->param( 'matching_word' );
    my $author_original_a = $q->param( 'matching_author_a' );
    my $author_original_b = $q->param( 'matching_author_b' );

   

        my @param_names = $q->param();
	#print( "******PARAM: @param_names\n" );
	my $found = 0;
	my $i = 0;
	my $got_you;
	while( ( $i <= $#param_names ) && ( !$found ) ) {
	    if( $param_names[$i] =~ /nextp/ ) {
	        $found = 1;
		$current_page = $q->param( 'next_current' );
		#print( "*****NEXT: $current_page\n" );
	    } elsif( $param_names[$i] =~ /prevp/ ) {
	        $found = 1;
		$current_page = $q->param( 'prev_current' );
		#print( "*****NEXT: $current_page\n" );
	    } elsif( $param_names[$i] =~ /pageno/ ) {
	        $found = 1;
		$got_you = $param_names[$i];
		#print( "***got: $got_you\n" );
		my $dot = index( $got_you, "." );
		#print( "***dot: $dot\n" );
		my $length = $dot - 6;
		$current_page = substr( $got_you, 6, $length );
	    }
	    $i++;
	}


    if( !@ref_ids ) {
        display_number( $q, 0 );
    } else {
        
        display_references( $q, $db, $original, $author_original_a, $author_original_b, $current_page, $item_number, @ref_ids );

    }


}



##########################



if( $q->param("ref_id") ) {

    my $db = Gramene::Literature::GetRefD->whatever();
    $db->connect_to_ora( );

    my $ref_id = $q->param("ref_id");
  
    my $valid_id = $db->validate_id( $ref_id );
  
    if( $valid_id ) {
        if($q->param("show_protein")){
           display_associated_objects($db, $q, $ref_id,'Protein');
        }elsif($q->param("show_gene")){
           display_associated_objects($db, $q, $ref_id,'Gene');
        }elsif($q->param("show_qtl")){
           display_associated_objects($db, $q, $ref_id,'QTL');
        }elsif($q->param("show_marker")){
           display_associated_objects($db, $q, $ref_id,'Markers');
        }elsif($q->param("show_cmap")){
           display_associated_objects($db, $q, $ref_id,'CMap');
        }elsif($q->param("show_diversity")){
	   display_associated_objects($db, $q, $ref_id,'Genetic Diversity Data');
	}else{
          display_detail( $db, $q, $ref_id );
       } 
     } else {

        print $q->hr();

        print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "Sorry, the reference id you entered is not valid. Please try again." ) ) );
    }

}


if( $q->param("acedb") ) {

    my $db = Gramene::Literature::GetRefD->whatever();
    $db->connect_to_ora( );

    my $acedb = $q->param("acedb");
    $acedb =~ tr/a-z/A-Z/;

    my $ref_id = $db->acedb_to_ref_id( $acedb );
    if( $ref_id ) {

        display_detail( $db, $q, $ref_id ); 
     } else {

        print $q->hr();

        print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "Sorry, the Acedb id is not valid. Please try again." ) ) );
    }

}


unless( ( $table_only ) && ( $table_only eq "1" ) ) {

    print $page->end_body;

}


sub display_detail {

    my ( $db, $q, $ref_id ) = @_;

        my ( $title, $source_id, $year, $volume, $start_page, $end_page ) = $db->ref_id_to_ref( $ref_id );
	my $abstract = $db->ref_id_to_abstract( $ref_id ); 
	my $source_name = $db->source_id_to_source_name( $source_id );
	my $source_url = $db->source_id_to_source_url( $source_id );

	my ($id_ref, $name_ref) = $db->ref_id_to_author( $ref_id );
	my @person_ids = @$id_ref;
	my @person_names = @$name_ref;

	my $string;
	for( my $i=0; $i<=$#person_ids; $i++ ) {
	    my $temp = $q->a( {-href=>"pub_search?author=$person_ids[$i]"}, $person_names[$i] );
	    $string = $string.$temp;
	    if( $i != $#person_ids ) {
	        $string = $string.", ";
	    }
	}
	
	unless( $string ) {
	    $string = "Not available";
	}


	my ( @urls, @labels, $comment, $db_name_ref, $acc_ref, @db_names, @accs );
	my ( $url_ref, $label_ref ) = $db->ref_id_to_url( $ref_id );
	if( $url_ref ) {
            @urls = @$url_ref;
	    @labels = @$label_ref;
	}

	( $db_name_ref, $acc_ref ) = $db->ref_id_to_xref( $ref_id );
	my $xref;
	@db_names = @$db_name_ref;
	@accs = @$acc_ref;
	if( ( @db_names ) && ( @accs ) ) {
	    
    
	    for( my $i=0; $i<=$#db_names; $i++ ) {
	    
	        if( $db_names[$i] eq "PubMed" ) {
		    $xref = $xref.$q->p( $q->a( {-target=>'cross_reference',
						 -href=>"http://www.ncbi.nlm.nih.gov:80/entrez/query.fcgi?cmd=Retrieve&db=PubMed&list_uids=$accs[$i]&dopt=Abstract"}, 
						"<img src=\"/images/icons/pubmed.jpg\" alt=\"Click here to read\" border=\"0\">" ) );
		} 
	    }

      
	} else {
	    $xref = "Not available";
	}



    #############################
        

    
    print $q->br();
    print start_table( {-width=>'98%', -cellpadding=>'5', -border=>'0', -align=>'CENTER'} ); 
    
    print $q->Tr( $q->th( {-class=>'resultstitle',
			   -colspan=>'3', 
			   -align=>'CENTER'}, "Search result" ) );
    print $q->Tr( {-valign=>'TOP'},
		  $q->th( {-align=>'LEFT',
			   -width=>'15%',
			   -class=>'resultstitle'}, "Reference ID" ),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'},$ref_id ) );

    print $q->Tr( {-valign=>'TOP'},
		  $q->th( {-align=>'LEFT',
			   -width=>'15%',
			   -class=>'resultstitle'}, "Title" ),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'},$title ) );
    print start_TR;
		   
			     
        print $q->th( {-align=>'LEFT',
		       -class=>'resultstitle'}, "Source" );

    my $source_string = "";
    if( $source_name ) {
        $source_string = ", ";
    }

    $source_string = $source_string.$year;
    
    if( $volume ) {
        $source_string = $source_string.", ".$volume;
    }

    if( $start_page ) {
        $source_string = $source_string.", "."pp.".$start_page;
	if( $end_page ) {
	  $source_string = $source_string."-".$end_page;
	}else {
	  $source_string = $source_string."-".$start_page;
	}
    }

    

        if( $source_url ) {
	     print $q->td( {-align=>'LEFT',
			    -colspan=>'2',
			    -class=>'resultsbody'},
			   $q->a( {-target=>'journal_home', -href=>"$source_url"}, $source_name ), 
			   #", ", $year, ", ", $volume, ", ", $start_page, "-", $end_page
			   $source_string );
        } else {
	  if( $source_name ) {
             print $q->td( {-align=>'LEFT',
			    -colspan=>'2',
			    -class=>'resultsbody'},
			    $source_name, 
			    #", ", $year, ", ", $volume, ", ", $start_page, "-", $end_page
			   $source_string );
	  }else {
	    print $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'},
			   $source_string );
	  }
        }
    print end_TR;
    print $q->Tr( {-valign=>'TOP'},
			     
		  $q->th( {-align=>'LEFT',
			   -class=>'resultstitle'}, "Authors" ),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'}, $string ) );
    print $q->Tr( {-valign=>'TOP'},
			      
		  $q->th( {-align=>'LEFT',
			   -class=>'resultstitle'}, "Abstract" ),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'}, $abstract ) );
   
    print $q->Tr( {-valign=>'TOP'},
			     
		  $q->th( {-align=>'LEFT',
			   -class=>'resultstitle'}, "Cross-reference" ),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'}, $xref ) );

    if( @urls ) {
        
        my $ref_url_string;
	
	for( my $i=0; $i<=$#urls; $i++ ) {

	    my $temp;
	    if( $labels[$i] ) {

                $temp = $q->p( $q->a( {-target=>'ref_url', -href=>"$urls[$i]" }, $urls[$i] ), "($labels[$i])" );
	    } else {
	        $temp = $q->p( $q->a( {-target=>'ref_url', -href=>"$urls[$i]" }, $urls[$i] ) );
	    }
	    
	    $ref_url_string = $ref_url_string.$temp;
	} #End for
        
        print $q->Tr( {-valign=>'TOP'},
			     
		  $q->th( {-align=>'LEFT',
			   -class=>'resultstitle'}, "URLs" ),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'}, $ref_url_string ) );
    }

    ############################

    my ( $protein_acc_ref, $protein_name_ref ) = $db->ref_id_to_protein( $ref_id );
    my ( $gene_acc_ref, $gene_name_ref ) = $db->ref_id_to_gene( $ref_id );
    my ( $qtl_acc_ref, $qtl_symbol_ref ) = $db->ref_id_to_qtl( $ref_id ); 
    my ( $marker_id_ref, $marker_type_ref, $marker_name_ref ) = $db->ref_id_to_marker( $ref_id );
    my @cmap_map_set_ref = $db->get_xref_keys($ref_id,'gramene.cmap.map_set','acc'); 
    my @diversity_exp_ref = $db->get_xref_keys($ref_id,'gramene.diversity.experiment','id'); 

    my $objects_links='';
 
    if($protein_acc_ref){
         my $n = scalar(@$protein_acc_ref);
         $objects_links=$objects_links.$q->a( {-target=>'association', -href=>"/db/literature/pub_search?ref_id=$ref_id&show_protein=1"}, "Proteins ($n)")."&nbsp;&nbsp;&nbsp;&nbsp;" if $n>0;
	  
    }
    
    if($gene_acc_ref){
         my $n = scalar(@$gene_acc_ref);
         $objects_links=$objects_links.$q->a( {-target=>'association', -href=>"/db/literature/pub_search?ref_id=$ref_id&show_gene=1"}, "Genes ($n)")."&nbsp;&nbsp;&nbsp;&nbsp;" if $n>0;
    }

    if($qtl_acc_ref){
        my $n = scalar(@$qtl_acc_ref);
         $objects_links=$objects_links.$q->a( {-target=>'association', -href=>"/db/literature/pub_search?ref_id=$ref_id&show_qtl=1"}, "QTLs ($n)")."&nbsp;&nbsp;&nbsp;&nbsp;" if $n>0;
    }

    if($marker_id_ref){
         my $n = scalar(@$marker_id_ref);
         $objects_links=$objects_links.$q->a( {-target=>'association', -href=>"/db/literature/pub_search?ref_id=$ref_id&show_marker=1"}, "Markers ($n)")."&nbsp;&nbsp;&nbsp;&nbsp;" if $n>0;
    }


    if(@cmap_map_set_ref){
         my $n = scalar(@cmap_map_set_ref);
         $objects_links=$objects_links.$q->a( {-target=>'association', -href=>"/db/literature/pub_search?ref_id=$ref_id&show_cmap=1"}, "CMap Map Set ($n)")."&nbsp;&nbsp;&nbsp;&nbsp;" if $n>0;
    }


    if(@diversity_exp_ref){
         my $n = scalar(@diversity_exp_ref);
         $objects_links=$objects_links.$q->a( {-target=>'association', -href=>"/db/literature/pub_search?ref_id=$ref_id&show_diversity=1"}, "Diversity Experiment ($n)")."&nbsp;&nbsp;&nbsp;&nbsp;" if $n>0;
    }

    if($objects_links){

         print $q->Tr( {-valign=>'TOP'},
			     
		  $q->th( {-align=>'LEFT',
			   -class=>'resultstitle'}, $q->a( {-target=>'association'}, "Associated Data")),
		  $q->td( {-align=>'LEFT',
			   -colspan=>'2',
			   -class=>'resultsbody'}, "$objects_links" ) );
   
    } 
    print end_table; 
 

print "<p>&nbsp;&nbsp;<span class='alert'>Please note:</span><br>&nbsp;&nbsp;To request reprints, please contact the authors or the source/journal website. Due to copyright issues Gramene does not distribute reprints.</p> ";


  }


##################################



sub display_associated_objects{


    my ( $db, $q, $ref_id,$object_type ) = @_;
    my ($object_attribute, $object_data);

    if($object_type eq 'Protein' ){
	my ( $protein_acc_ref, $protein_name_ref ) = $db->ref_id_to_protein( $ref_id );
	$object_attribute = ['Accession', 'Name'];

	foreach my $acc (@$protein_acc_ref){
	    push @{$object_data->{$object_attribute->[0]}},qq[<a href="/db/protein/protein_search?acc=$acc">$acc</a>];
	}
	$object_data->{$object_attribute->[1]} = $protein_name_ref;

    }elsif($object_type eq 'Gene'){
	my ( $gene_acc_ref, $gene_name_ref ) = $db->ref_id_to_gene( $ref_id );

	$object_attribute = ['Accession', 'Name'];

	foreach my $acc (@$gene_acc_ref){
	    push @{$object_data->{$object_attribute->[0]}},qq[<a href="/db/genes/search_gene?acc=$acc">$acc</a>];
	}
	$object_data->{$object_attribute->[1]} = $gene_name_ref;

    }elsif($object_type eq 'QTL'){
	my ( $qtl_acc_ref, $qtl_symbol_refs ) = $db->ref_id_to_qtl( $ref_id ); 


	$object_attribute = [
        'Accession', 
        'Trait Symbol', 
        'Trait Name', 
        'Published Symbol'
    ];

    my $qtl_xref   = '<a href="/db/qtl/qtl_display?qtl_accession_id=%s">%s</a>';
    my $trait_xref = '<a href="/db/qtl/qtl_display?query=%s'
        . '&search_field=trait_symbol&species=&submit=Submit">%s</a>';

    for my $i ( 0 .. $#{ $qtl_acc_ref } ) {
        my $qtl_accession_id = $qtl_acc_ref->[ $i ];
        my $symbols          = $qtl_symbol_refs->[ $i ];

        push @{ $object_data->{ $object_attribute->[0] } },
            sprintf( $qtl_xref, $qtl_accession_id, $qtl_accession_id );
        push @{ $object_data->{ $object_attribute->[1] } },
            sprintf( $trait_xref, 
                $symbols->{'trait_symbol'}, $symbols->{'trait_symbol'} 
            );
        push @{ $object_data->{ $object_attribute->[2] } },
            $symbols->{'trait_name'};
        push @{ $object_data->{ $object_attribute->[3] } },
            $symbols->{'published_symbol'};
    }

    }elsif($object_type eq 'Markers' ){
	my ( $marker_id_ref, $marker_type_ref, $marker_name_ref ) = $db->ref_id_to_marker( $ref_id );

	$object_attribute = ['Name', 'Type'];
	
	for(my $i=0; $i<= $#$marker_id_ref; $i++){
	    my $name = $marker_name_ref->[$i] || 'NA';
	    my $id = $marker_id_ref->[$i];
	    push @{$object_data->{$object_attribute->[0]}},qq[<a href="/db/markers/marker_view?action=marker_search&marker_id=$id">$name</a>];
	}
	$object_data->{$object_attribute->[1]} = $marker_type_ref;

    }elsif($object_type eq 'CMap' ){
	my @cmap_map_set_ref = $db->get_xref_keys($ref_id,'gramene.cmap.map_set','acc'); 
	$object_attribute = ['Map Set Accession'];

	foreach my $acc (@cmap_map_set_ref){
	    push @{$object_data->{$object_attribute->[0]}},qq[<a href="/db/cmap/map_set_info?map_set_aid=$acc">$acc</a>];
	}
	

    }elsif($object_type eq 'Genetic Diversity Data' ){

	my ( $div_exp_id_ref, $div_exp_name_ref ) = $db->ref_id_to_diversity( $ref_id );

	$object_attribute = ['Experiment Title'];

	for(my $i=0; $i<scalar(@$div_exp_id_ref);$i++){
	    push @{$object_data->{$object_attribute->[0]}},qq[<a href="/db/diversity/diversity_view?action=view&object=div_experiment&id=$div_exp_id_ref->[$i]">$div_exp_name_ref->[$i]</a>];
	}
    }

    my ( $title, $source_id, $year, $volume, $start_page, $end_page ) = $db->ref_id_to_ref( $ref_id );

    my $obj_fields  = scalar(@$object_attribute) || 1; 

    print start_table(
        {
            -width  => '98%',
            -class  => 'data-table',
            -border => '0',
            -align  => 'CENTER'
        }
    );

    print $q->Tr(
        $q->th(
            {
                -align  => 'LEFT',
                -valign => 'TOP'
            },
            "Reference ID"
        ),
        $q->td(
            {
                -align   => 'LEFT',
                -colspan => $obj_fields,
                -class   => 'resultsbody'
            },
            $q->a(
                { -href => "/db/literature/pub_search?ref_id=$ref_id" },
                "$ref_id"
            )
        )
    );
    print $q->Tr(
        $q->th(
            {
                -align  => 'LEFT',
                -valign => 'TOP'
            },
            "Title"
        ),
        $q->td(
            {
                -align   => 'LEFT',
                -colspan => $obj_fields,
                -class   => 'resultsbody'
            },
            "$title"
        )
    );
    
    if ( $object_data ) {
        my $row = scalar( @{ $object_data->{ $object_attribute->[0] } } ) + 1;

        print start_TR();
        print $q->th(
            {
                -align   => 'LEFT',
                -valign  => 'TOP',
                -rowspan => $row
            },
            "Associated $object_type"
        );

        foreach my $obj_attr ( @$object_attribute ) {
            print $q->th( { -align => 'CENTER' }, $obj_attr );

        }
        print end_TR();

        for ( my $i = 0; $i < $row; $i++ ) {
            print start_TR();
            foreach my $obj_attr ( @$object_attribute ) {
                print $q->td(
                    {
                        -class => 'resultsbody',
                        -align => 'CENTER'
                    },
                    $object_data->{$obj_attr}->[$i]
                );

            }
            print end_TR();
        }
    }
    else {
        print $q->Tr(
            $q->th(
                {
                    -class  => 'resultstitle',
                    -align  => 'LEFT',
                    -valign => 'TOP'
                },
                "Associated $object_type"
            ),

            $q->td(
                {
                    -align   => 'LEFT',
                    -colspan => '2',
                    -class   => 'resultsbody'
                },
                "Not Available"
            )
        );

    }

    print end_table;
}
##################################   

sub display_number {

    my ( $q, $number ) = @_;
    if( $number == 0 ) {
        
        print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "Sorry, no matching records have been found." ) ) );
    } elsif ( $number == 1 ) {
	print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "1 matching record has been found." ) ) );
    } else {
	print $q->table( {-border=>'0',
			  -cellpadding=>'2',
			  -width=>'98%',
			  -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 $number, " matching records have been found." ) ) );
    }

}

sub display_references_new{


    my ( $q, $db, $original, $author_original_a, $author_original_b, $current_page, $item_number, @ref_ids_all ) = @_; 
    if(@ref_ids_all && scalar(@ref_ids_all)>0){
        my( $pager, $data ) = pager(  
	      current_page       => $current_page,  
	      url                => $q->self_url(),  
	      entries_per_page   => $item_number ,  
	      data               => \@ref_ids_all,  
        );  
  
      
  print start_table( {-border=>'0', -cellpadding=>'2', -cellspacing=>'0', -width=>'100%', -align=>'CENTER', -nowrap=>'nowrap'} );    
  print TR({-align=>"center", -class=>"datatitle"},th("$pager")); 
    
        my $i = ($current_page -1)* $item_number ;
        foreach my $ref_id (@$data){  
	  #for( my $i=($start_item-1); $i<=($end - 1); $i++ ) {
		    
	    my ( $title, $source_id, $year, $volume, $start_page, $end_page ) = $db->ref_id_to_ref( $ref_id );
	    my $index = $i+1;
	    my $title_string = "(".$q->a( {-class=>'gopage',
	    -href=>"/db/literature/pub_search?ref_id=$ref_id"}, "More info"
	    ).")"." [Reference ID: $ref_id]";

	    my ($id_ref, $name_ref) = $db->ref_id_to_author( $ref_id );
	    my @person_ids = @$id_ref;
	    my @person_names = @$name_ref;

	    my $string;
	    for( my $i=0; $i<=$#person_ids; $i++ ) {
	        my $temp = $q->a( {-href=>"/db/literature/pub_search?author=$person_ids[$i]"}, $person_names[$i] );
		$string = $string.$temp;
		if( $i != $#person_ids ) {
		    $string = $string.", ";
		}
	    }
	    unless( $author_original_a eq "#" ) {
	        $string =~ s/\b($author_original_a)/<span class='matching'>$1<\/span>/ig;
	    }
	    unless( $author_original_b eq "#" ) {
	        $string =~ s/\b($author_original_b)/<span class='matching'>$1<\/span>/ig;
	    }

	    print start_TR( {-align=>'center'} );
	   

print <<ITEM;
	  <td align="LEFT">   
	    <dl>
	      <dt>
		<table cellspacing="0" cellpadding="0">
		  <tr>
		    <td align="CENTER" valign="CENTER">&nbsp;
		      </td><td align="CENTER" valign="CENTER"><span class="reftitle"><b>$index.<b></span>&nbsp;&nbsp;
	            </td>
		    <td align="LEFT" valign="CENTER">
		      $string
		    </td>
		  </tr>
		</table>
	      </dt>
ITEM
	    
	    my ( $source_name, $source_url );
	    if( $source_id ) {
	        $source_name = $db->source_id_to_source_name( $source_id );
		$source_url = $db->source_id_to_source_url( $source_id );
	    }

	    my $source_string;
	    if( $source_name ) {
	        if( $source_url ) {
		    $source_string = $q->a( {-target=>'journal_home', -href=>"$source_url"}, $source_name ).", ".$year;
		} else {
		    $source_string = $source_name.", ".$year;
		}
	    } else {
	        $source_string = $year;
	    }
	
	    if( $volume ) {
		$source_string = $source_string.", "."vol.".$volume;
	    }

	    if( $start_page ) {
		$source_string = $source_string.", "."pp".$start_page;
		if( $end_page ) {
		    $source_string = $source_string."-".$end_page;
		} else {
		    $source_string = $source_string."-".$start_page;
		}
	    }
	    
	    unless( $original eq "#" ) {
	        $title =~ s/\b($original)/<span class='matching'>$1<\/span>/ig;
	        $source_string =~ s/($original)/<span class='matching'>$1<\/span>/ig;
	    }

	    print( "<dd>" );
	    
	    print( "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td><span class=\"reftitle\">$title</span>&nbsp;&nbsp;$title_string</td></tr>" );
	    
	    
	    print( "<tr><td>$source_string</td></tr>" );
	    print( "</table>" );
	   
	    print( "</dl>" );
	    print( "</td></tr>" );
            $i++;
	}#End for

        print TR({-align=>"center", -class=>"datatitle"},th("$pager")); 
        print end_table;
   

   } 

}


#################with next page
sub display_references {

    my ( $q, $db, $original, $author_original_a, $author_original_b, $current_page, $item_number, @ref_ids ) = @_;
    
    if( @ref_ids ) {

        
        my $start_item = ( $current_page - 1 ) * 20 + 1;

        my $tmp = $start_item + $item_number - 1;
	my $total = scalar( @ref_ids );
	my $end;
	if( $tmp < $total ) {
	    $end = $tmp;
	} else {
	    $end = $total;
	}
	#my $next = ( int ( $current_page / 10 ) + 1 ) * 10 + 1;
	
        my $total_page = int ( ( $total - 1 ) / $item_number ) + 1;
	my $start_page = int ( ( $current_page - 1 ) / 10 ) * 10 + 1;
	my $next = $start_page + 10;
	my $prev = $start_page - 1;

	my $end_page;
	if( $total_page <= ( $start_page + 9 ) ) {
	    $end_page = $total_page;
	} else {
	    $end_page = $start_page + 9;
	}

	my $ref_string = join( ",", @ref_ids ); 

	print $q->p();

	
	print start_table( {-border=>'0', -width=>'98%', -align=>'center', -cellpadding=>'0', -cellspacing=>'0'} );
	print ( "<tr><td>" );
	print $q->start_table( {-border=>'0', -cellpadding=>'2', -cellspacing=>'0', -width=>'100%', -align=>'CENTER', -nowrap=>'nowrap'} );
	print $q->start_form(-method=>'GET');
	#################### Get rid of the sticky behavior.
	my @pnames = $q->param();
	foreach my $p ( @pnames ) {
	    $q->param( -name=>$p, -value=>'' );
	}

	####################
	print $q->hidden( -name=>'next_current', -value=>$next, -override=>'1' );
	print $q->hidden( -name=>'prev_current', -value=>$prev, -override=>'1' );
	print $q->hidden( -name=>'matching_word', -value=>$original, -override=>'1' );
	print $q->hidden( -name=>'matching_author_a', -value=>$author_original_a, -override=>'1' );
	print $q->hidden( -name=>'matching_author_b', -value=>$author_original_b, -override=>'1' );
	print $q->hidden( -name=>'refs', -value=>$ref_string, -override=>'1' );

	print $q->start_TR( {-nowrap=>'nowrap'} );
	print $q->th( {-class=>'resultstitle', 
		       -align=>'CENTER'}, 
		      "Items $start_item-$end of $total" );
	print $q->th( {-class=>'resultstitle',
		       -align=>'CENTER'}, 
		      "Page $current_page of $total_page" );

	print ( "<th class=\"resultstitle\" align=\"CENTER\" nowrap=\"nowrap\">Select page:&nbsp;&nbsp;" );
	print ( "<table cellspacing=\"0\" cellpadding=\"0\"><tr>" );
	
	if( $start_page > 1 ) {
	    print( "<td>&nbsp;&nbsp;&nbsp;" );
	    print $q->image_button( -name=>'prevp',
                                    -src=>'/images/pub_images/prev.gif',
				    -alt=>'previous_pages',
				    -height=>'12',
				    -width=>'10',
				    -border=>'0');
	    print( "</td>" );
	}


	for ( my $i=$start_page; $i<=$end_page; $i++ ) {
	    
	    my $first = int ( $i / 100 );
	    my $second = int ( ( $i - $first * 100 ) / 10 );
	    my $third = $i % 10;
	   
	    my $alt = "page_".$i;
	    my $name = "pageno".$i;
	    my ( $src1, $src2, $src3 );

	    if( $i == $current_page ) {
	        $src1 = "/images/pub_images/"."black_".$first.".gif";
		$src2 = "/images/pub_images/"."black_".$second.".gif";
		$src3 = "/images/pub_images/"."black_".$third.".gif";
	    } else {
	        $src1 = "/images/pub_images/".$first.".gif";
		$src2 = "/images/pub_images/".$second.".gif";
		$src3 = "/images/pub_images/".$third.".gif";
	    }
		
	    print( "<td>&nbsp;&nbsp;&nbsp;" );
	    unless( $first == 0 ) {
	        print $q->image_button( -name=>$name,
					-src=>$src1,
					-alt=>$alt,
					-height=>'12',
					-width=>'7',
					-border=>'0');
	    }
	    unless( ( $first == 0 ) && ( $second == 0 ) ) {
	        print $q->image_button( -name=>$name,
					-src=>$src2,
					-alt=>$alt,
					-height=>'12',
					-width=>'7',
					-border=>'0');
	    }
	    print $q->image_button( -name=>$name,
				    -src=>$src3,
					-alt=>$alt,
					-height=>'12',
					-width=>'7',
					-border=>'0');
	    print( "</td>" );
	}
	
	if( $total_page > ( $start_page + 9 ) ) {
	    print( "<td>&nbsp;&nbsp;&nbsp;" );
	    print $q->image_button( -name=>'nextp',
                                    -src=>'/images/pub_images/next.gif',
				    -alt=>'next_pages',
				    -height=>'12',
				    -width=>'10',
				    -border=>'0');
	    print( "</td>" );
	}


	print ( "</tr></table>" );
	print ( "</th>" );
	print end_TR();
	print $q->end_form();
	print end_table();

	print ( "</td></tr>" );
	

	

	for( my $i=($start_item-1); $i<=($end - 1); $i++ ) {
		    
	    my ( $title, $source_id, $year, $volume, $start_page, $end_page ) = $db->ref_id_to_ref( $ref_ids[$i] );
	    my $index = $i+1;
	    my $title_string = "(".$q->a( {-class=>'gopage', -href=>"/db/literature/pub_search?ref_id=$ref_ids[$i]"}, "More info" ).")";

	    my ($id_ref, $name_ref) = $db->ref_id_to_author( $ref_ids[$i] );
	    my @person_ids = @$id_ref;
	    my @person_names = @$name_ref;

	    my $string;
	    for( my $i=0; $i<=$#person_ids; $i++ ) {
	        my $temp = $q->a( {-href=>"/db/literature/pub_search?author=$person_ids[$i]"}, $person_names[$i] );
		$string = $string.$temp;
		if( $i != $#person_ids ) {
		    $string = $string.", ";
		}
	    }
	    unless( $author_original_a eq "#" ) {
	        $string =~ s/\b($author_original_a)/<span class='matching'>$1<\/span>/ig;
	    }
	    unless( $author_original_b eq "#" ) {
	        $string =~ s/\b($author_original_b)/<span class='matching'>$1<\/span>/ig;
	    }

	    #if( $table_only eq "1" ) {
	    #    print start_TR( {-align=>'center', -class=>'resultsbody'} );
	    #} else {
	        print start_TR( {-align=>'center'} );
	    #}
	    #<img src="/images/icons/grain_icon.jpg" alt="grain_icon" height=16 width=16 align="center">

print <<ITEM;
	  <td align="LEFT">   
	    <dl>
	      <dt>
		<table cellspacing="0" cellpadding="0">
		  <tr>
		    <td align="CENTER" valign="CENTER">&nbsp;
		      </td><td align="CENTER" valign="CENTER"><span class="reftitle"><b>$index.<b></span>&nbsp;&nbsp;
	            </td>
		    <td align="LEFT" valign="CENTER">
		      $string
		    </td>
		  </tr>
		</table>
	      </dt>
ITEM
	    
	    my ( $source_name, $source_url );
	    if( $source_id ) {
	        $source_name = $db->source_id_to_source_name( $source_id );
		$source_url = $db->source_id_to_source_url( $source_id );
	    }

	    my $source_string;
	    if( $source_name ) {
	        if( $source_url ) {
		    $source_string = $q->a( {-target=>'journal_home', -href=>"$source_url"}, $source_name ).", ".$year;
		} else {
		    $source_string = $source_name.", ".$year;
		}
	    } else {
	        $source_string = $year;
	    }
	
	    if( $volume ) {
		$source_string = $source_string.", "."vol.".$volume;
	    }

	    if( $start_page ) {
		$source_string = $source_string.", "."pp".$start_page;
		if( $end_page ) {
		    $source_string = $source_string."-".$end_page;
		} else {
		    $source_string = $source_string."-".$start_page;
		}
	    }
	    
	    unless( $original eq "#" ) {
	        $title =~ s/\b($original)/<span class='matching'>$1<\/span>/ig;
	        $source_string =~ s/($original)/<span class='matching'>$1<\/span>/ig;
	    }

	    print( "<dd>" );
	    
	    print( "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td><span class=\"reftitle\">$title</span>&nbsp;&nbsp;$title_string</td></tr>" );
	    
	    
	    print( "<tr><td>$source_string</td></tr>" );
	    print( "</table>" );
	   
	    print( "</dl>" );
	    print( "</td></tr>" );

	}#End for
	

	print ( "<tr><td>" );
	print $q->start_table( {-border=>'0', -cellpadding=>'2', -cellspacing=>'0', -width=>'100%', -align=>'CENTER', -nowrap=>'nowrap'} );
	print $q->start_form(-method=>'GET');
	#################### Get rid of the sticky behavior.
	my @ppnames = $q->param();
	foreach my $p ( @ppnames ) {
	    $q->param( -name=>$p, -value=>'' );
	}

	####################
	print $q->hidden( -name=>'next_current', -value=>$next, -override=>'1' );
	print $q->hidden( -name=>'prev_current', -value=>$prev, -override=>'1' );
	print $q->hidden( -name=>'matching_word', -value=>$original, -override=>'1' );
	print $q->hidden( -name=>'matching_author_a', -value=>$author_original_a, -override=>'1' );
	print $q->hidden( -name=>'matching_author_b', -value=>$author_original_b, -override=>'1' );
	print $q->hidden( -name=>'refs', -value=>$ref_string, -override=>'1' );

	print $q->start_TR( {-nowrap=>'nowrap'} );
	print $q->th( {-class=>'resultstitle', 
		       -align=>'CENTER'}, 
		      "Items $start_item-$end of $total" );
	print $q->th( {-class=>'resultstitle',
		       -align=>'CENTER'}, 
		      "Page $current_page of $total_page" );

	print ( "<th class=\"resultstitle\" align=\"CENTER\" nowrap=\"nowrap\">Select page:&nbsp;&nbsp;" );
	print ( "<table cellspacing=\"0\" cellpadding=\"0\"><tr>" );
	
	if( $start_page > 1 ) {
	    print( "<td>&nbsp;&nbsp;&nbsp;" );
	    print $q->image_button( -name=>'prevp',
                                    -src=>'/images/pub_images/prev.gif',
				    -alt=>'previous_pages',
				    -height=>'12',
				    -width=>'10',
				    -border=>'0');
	    print( "</td>" );
	}


	for ( my $i=$start_page; $i<=$end_page; $i++ ) {
	    
	    my $first = int ( $i / 100 );
	    my $second = int ( ( $i - $first * 100 ) / 10 );
	    my $third = $i % 10;
	   
	    my $alt = "page_".$i;
	    my $name = "pageno".$i;
	    my ( $src1, $src2, $src3 );

	    if( $i == $current_page ) {
	        $src1 = "/images/pub_images/"."black_".$first.".gif";
		$src2 = "/images/pub_images/"."black_".$second.".gif";
		$src3 = "/images/pub_images/"."black_".$third.".gif";
	    } else {
	        $src1 = "/images/pub_images/".$first.".gif";
		$src2 = "/images/pub_images/".$second.".gif";
		$src3 = "/images/pub_images/".$third.".gif";
	    }

		
	    print( "<td>&nbsp;&nbsp;&nbsp;" );
	    unless( $first == 0 ) {
	        print $q->image_button( -name=>$name,
					-src=>$src1,
					-alt=>$alt,
					-height=>'12',
					-width=>'7',
					-border=>'0');
	    }
	    unless( ( $first == 0 ) && ( $second == 0 ) ) {
	        print $q->image_button( -name=>$name,
					-src=>$src2,
					-alt=>$alt,
					-height=>'12',
					-width=>'7',
					-border=>'0');
	    }
	    print $q->image_button( -name=>$name,
				    -src=>$src3,
					-alt=>$alt,
					-height=>'12',
					-width=>'7',
					-border=>'0');
	    print( "</td>" );
	}
	
	if( $total_page > ( $start_page + 9 ) ) {
	    print( "<td>&nbsp;&nbsp;&nbsp;" );
	    print $q->image_button( -name=>'nextp',
                                    -src=>'/images/pub_images/next.gif',
				    -alt=>'next_pages',
				    -height=>'12',
				    -width=>'10',
				    -border=>'0');
	    print( "</td>" );
	}


	print ( "</tr></table>" );
	print ( "</th>" );
	print end_TR();

	print $q->end_form();
	print end_table();
	print ( "</td></tr>" );

	print end_table;

	##################

    }#End if

}

