#!/bin/perl -w

use strict;

use CGI;
use CGI qw(:standard *table *TR *th *td *ul *ol);
use Gramene::GetRefD; 
use GramenePage; 


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

my $table_only = $q->param("table");

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


print $q->start_html( -title=>$page_title,
		      -style=>{'src'=>$page->stylesheet}
		    );
print $page->start_body();


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


print <<SMALLBAR;
<table align="CENTER">
  <tr>
    <td>&nbsp;<img src="/images/icons/grain_icon.jpg" alt="grain_icon" height=16 width=16 align="top">
    </td>
    <td>
      &nbsp;<b>[</b>&nbsp;<b>Literature Database</b>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/newsletters/rice_genetics/" class="gopage"><b>Rice Genetics Newsletters</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
    </td>
  </tr>
</table>
SMALLBAR

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

                 $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=smith+je"}, "Smith JE" ),
					",&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 $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 "smith je", "smith j", or "smith". 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;
    }
    print STDERR ( "\n[URL] /perl/pub_search?$param_string\n\n" );

}

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


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

    my $test_word = $q->param("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::GetRefD->whatever();
	$db->connect_to_ora( );
	

	my $original = $q->param("word");
	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 {
            
            display_references( $q, $db, $original, $author_original_a, $author_original_b, 1, 20, @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::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::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 $db = Gramene::GetRefD->whatever();

    $db->connect_to_ora( );

    
    my $person_id = $q->param("author");
    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 {

        display_references( $q, $db, $original, $author_original_a, $author_original_b, 1, 20, @ref_ids );

    } #End else
    

}


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

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

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

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


    my $db = Gramene::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::GetRefD->whatever();
    $db->connect_to_ora( );

    my $ref_id = $q->param("ref_id");

    my $valid_id = $db->validate_id( $ref_id );
    if( $valid_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 reference id you entered is not valid. Please try again." ) ) );
    }

}


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

    my $db = Gramene::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'}, "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_id_ref, $protein_acc_ref, $protein_name_ref ) = $db->ref_id_to_protein( $ref_id );
    my @protein_ids = @$protein_id_ref;
    my @protein_accs = @$protein_acc_ref;
    my @protein_names = @$protein_name_ref;
    if( @protein_names ) {

        print start_TR();

        my $row = scalar( @protein_ids ) + 1;

        print $q->th( {-class=>'resultstitle',
		       -align=>'LEFT',
		       -valign=>'TOP',
		       -rowspan=>$row}, "Related Proteins" );
        

        print $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "Accession Number" );
	print $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "Name" );
	

	print end_TR();

	for( my $i=0; $i<=$#protein_ids; $i++ ) {

	    my $temp = $q->a( {-target=>'protein', -href=>"/perl/protein_search?protein_id=$protein_ids[$i]" }, $protein_accs[$i] );
	    print $q->Tr( $q->td( {-class=>'resultsbody',
			   	   -align=>'CENTER'}, $temp ),
			  $q->td( {-class=>'resultsbody',
			   	   -align=>'CENTER'}, $protein_names[$i] ) );
	}
    }

    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." ) ) );
    }

}


#################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();
	#################### 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=>"/perl/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=>"/perl/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();
	#################### 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

}

