use strict;


sub VectorizeParams  # ($input, $name, $name, .... )
{
    my $input = shift;
    foreach my $k (@_)
    {
        defined( $input->{$k} ) and
            $input->{$k} = [ split( "\0", $input->{$k} ) ];
    }
}

# Optlink( $opts, $action, $key, $val, $text, [$$current] )
# Optlink( $opts, $action, $key, [$val ...], [$text ...], [$$current] )
# Optlink( $opts, $action, $text )
sub OptLink  
{
    my ($opts, $action, $key, $vals, $texts, $current ) = @_;
    ref( $action ) or $action = [ $action, '' ];
    my $tx = "";

    if( $#_ == 2 )  # Optlink( $opts, $action, $text )
    {
        $texts = $key;
        $vals = 0;
        $key = '';
    }

    ref( $vals ) or do { $vals = [ $vals ]; $texts = [$texts]; };
    for( my $i = 0; $i <= $#{$vals}; ++ $i )
    {
        $tx and $tx .= "|";
        if( $opts->{$key} eq $vals->[$i] ) { # allready active, disable link
            $tx .= html( $texts->[$i] );
            $current and $$current = $texts->[$i];
        }  else {
            $tx .= "<A HREF=\"$action->[0]?";
            foreach my $k ( keys( %{$opts} ) )
            {
                if( ref( $opts->{$k} ) )
                {
                    foreach my $v ( @{$opts->{$k}} )
                    {
                        $tx .= "$k=$v&";
                    }
                }
                else
                {
                    $tx .= $k eq $key ? "$k=$vals->[$i]&" : "$k=$opts->{$k}&"; 
                }
            }
            $tx =~ s/.$//;  # delete ending char
            $tx .= html( "$action->[1]\">$texts->[$i]</A>" );
        }
    }
    return $tx;
}


# OptCheck( $opts, $keys, $texts, $ptexts = [...], [$$current] )
# ptexts = [separator, separator_for_current, end_separator_for_current,
#           current_if_all_checked,current_if_none_checked]
#  sub OptCheck
#  {
#      my ($opts, $keys, $texts, $ptexts, $current ) = @_;
#      my $tx = '';
#      my $ctx = '';
#      my $sctx = '';
#      my $all = 1;
#      my $any = 0;

#      $current and !$$current and $$current = '';

#      for( my $i = 0; $i <= $#{$keys}; ++ $i )
#      {
#          $i and $tx .= $ptexts->[0];     # separator
#          $tx .= "<input type='checkbox' name='$keys->[$i]' " .
#              ( $opts->{$keys->[$i]} ? ' checked>' : '>' ) .
#                  $texts->[$i];
#          ( $opts->{$keys->[$i]} and $any = 1 ) or $all = 0;
#          $opts->{$keys->[$i]} and $current and do
#          {
#              $ctx .= $sctx . $texts->[$i];
#              $sctx = $ptexts->[1];       # separator_for_current
#          };
#      }
#      $ctx .= $ptexts->[2];               # end_separator_for_current
#      $all and $ctx  = $ptexts->[3];      # current_if_all_checked
#      !$any and $ctx  = $ptexts->[4];      # current_if_none_checked

#      $current and $$current .= $ctx;

#      return $tx;
#  }



sub OptForm
{
    my $opts = shift;
    my $tx = '';
    foreach my $k (keys(%{$opts}))
    {
        if( ref( $opts->{$k} ) )
        {
            foreach my $v ( @{$opts->{$k}} )
            {
                $tx .= "<input type=hidden name=\'$k\' value=\'$v\'>\n";
            }
        }
        else
        {
            $tx .= "<input type=hidden name=\'$k\' value=\'$opts->{$k}\'>\n";
        }
    }
    return $tx;
}

#Paper( $id, $title )
#paper( { 'id' => $id, 'title' => $title } )
sub PaperFullLink
{
    my ($id, $title) = ref( $_[0] ) ?
        ( $_[0]->{'id'}, $_[0]->{'title'} ) : @_;

    return "<a href=\"view_paper.cgi?paper_id=$id\" target='paper'>".
        "<font size=\"5\">$id</font> - " .
            "$title</A>";
}

sub PaperFull
{
    my ($id, $title) = ref( $_[0] ) ?
        ( $_[0]->{'id'}, $_[0]->{'title'} ) : @_;

    return "<font size=\"5\">$id</font> - $title</A>";
}

sub PaperHead
{
    my ($P, $R, $S) = @_;

    print html( "<H2>Article $P->{'id'}: $P->{'title'}</h2>\n" );

	print "<table><tr valign=top><th align='right'>Author(s): ";

	foreach ( split( /;/, $P->{'authors'} ) )
	{
	    m/^(.*)\{(.*)\}$/;
        my $a = $2;

        $S and ( $S->{'affiliation'} eq $a or $S->{'buddy_affiliation'} eq $a )
            and $a = "<span style='color:red'>$a</span>";


	    print html( "<td>$1 <td>$a\n<tr valign=top>\n<td>" );
	}

    $R and do {
        print "<tr><th align='right' >Reviewers:<td>";
        
        foreach my $r ( @{$R} )
        {
            print_one_rev_person( $r );
            my $a = $r->{'affiliation'};

            $S and ( $S->{'affiliation'} eq $a or
                     $S->{'buddy_affiliation'} eq $a )
                and $a = "<span style='color:red'>$a</span>";
            print "<td>$a\n<tr><td><td>";
        }
    };

    $S and do {

        print "<tr><th align='right'>AC: <td>";
        print_one_ac_person( $S );
        print "<td>$S->{'affiliation'}\n";
        print "<tr><th align='right'>Buddy: <td>";
        print_one_buddy_person( $S );
        print "<td>$S->{'buddy_affiliation'}\n<td>";
    };

    print "</table>\n";
}


sub PageHead
{
    my ($title, $helpurl) = @_;

    print "<table width=100%
        style='background-color:#DDDDFF'><tr><td><H2>$title</h2><td
        align='right'>". 
        ( $helpurl ? "<a href='$helpurl'>Help</a>" : '' ) . "</table>\n";
}


sub html_normal_text
{
    my ($text) =  @_;

    $text =~ s/\r//g;
    $text =~ s/~/&nbsp;/g;
    $text =~ s/</&lt;/g;
    $text =~ s/>/&gt;/g;

    $text =~ s/\n[ ]*\n/\n<p>/gi;

    return $text;
}

sub is_editor_of
{
    my ($sock, $ac_id, $paper_id) = @_;
    my $select = "SELECT paper_id\n" .
                 "FROM synthese\n" .
                 "WHERE paper_id='$paper_id' AND editor_id='$ac_id'";
    if( DEBUG() ) { dprintsql( "SQL_EDITOR_OF", $select ); }

    my $sth = $sock->prepare( $select ) or die( "mySQL ERROR: ".$sock->errstr );
    $sth->execute() or die( "mySQL ERROR: ".$sock->errstr );
    my $is = $sth->rows();
    $sth->finish();
    return $is;
}

sub is_buddy_of
{
    my ($sock, $ac_id, $paper_id) = @_;
    my $select = "SELECT paper_id\n" .
                 "FROM synthese\n" .
                 "WHERE paper_id='$paper_id' AND buddy_id='$ac_id'";
    if( DEBUG() ) { dprintsql( "SQL_EDITOR_OF", $select ); }

    my $sth = $sock->prepare( $select ) or die( "mySQL ERROR: ".$sock->errstr );
    $sth->execute() or die( "mySQL ERROR: ".$sock->errstr );
    my $is = $sth->rows();
    $sth->finish();
    return $is;
}



1;
