#!/usr/bin/perl

use strict;
use lib './';
use Poll;
$ENV{'QUERY_STRING'}=$ENV{'REQUEST_URI'};
$ENV{'QUERY_STRING'}=~s/^.*\?(.*)$/$1/;
use CGI qw/:standard/;
use constant POLLPATH=>'/home/oc/cgi-bin/poll/data'; 

my $deBug = 1;
my %cookie=(); 
my %form=();

my $poll=new WWW::Poll;
$poll->path(POLLPATH);

&main();

sub main {

        $ENV{'SCRIPT_NAME'}=$ENV{'DOCUMENT_URI'};
	my $query = new CGI;
	foreach ($query->param) {
		$form{$_}=$query->param($_);
	}
	foreach ($query->cookie) {
		$cookie{$_}=$query->cookie($_);
	}   
	my $action = $query->param(-name=>'action');
	if ( $action eq 'vote') {
		&vote_poll( $query->param(-name=>'vote') );
	} elsif ( $action eq 'view' ) {
		&get_results;
	} elsif ( $query->param(-name=>'poll_id') ) {
		&get_results( $query->param(-name=>'poll_id') );
	} else {
		&display_poll;
	}
}

sub vote_poll {
	my $vote = shift;
        my $pollid=$poll->get;
        if ($cookie{"poll$pollid"} eq '') {
		$poll->vote($vote); # votes on latest poll
	}
	&get_results;
}

sub display_poll {
	my $pollid = $poll->get;
        if ($cookie{"poll$pollid"} ne '') {
            &get_results($pollid); 
            return;
        }
	my $font = qq|<FONT SIZE=-1>|;
	my $html = "
			<FORM ACTION=\"/cgi-bin/poll/poll.cgi\">
			<INPUT TYPE=hidden NAME=action VALUE=vote>
			<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 width=100%>
			<TD BGCOLOR=#000000>
			
			<TABLE CELLPADDING=4 CELLSPACING=1 BORDER=0 width=100%>
			<TR><TD COLSPAN=2 VALIGN=top BGCOLOR=#bbccff><strong>".$font.$poll->question."&nbsp;</strong></TD></TR>
			<TR><TD COLSPAN=2 VALIGN=top BGCOLOR=#ffffff>";
	foreach ($poll->akeys) {
		$html .= "
			<table border=0 cellpadding=3 cellspacing=0><TR>
				<TD VALIGN=top BGCOLOR=#ffffff width=1%><INPUT TYPE=radio NAME=vote VALUE=".$_."></TD>
				<TD BGCOLOR=#ffffff>".$font.$poll->answers->{$_}."</TD>
			</TR></table>";
	}
	$html .= "
			<center><table>
			<TR>
				<TD VALIGN=top ALIGN=center BGCOLOR=#ffffff COLSPAN=2>
					".$font."
					<INPUT TYPE=submit  VALUE=\" vote \">
				</td>
			</tr>
			</table></td></tr>
			<TR><TD COLSPAN=2 VALIGN=top BGCOLOR=#ffffff>
			<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td>
			<center><A HREF=".$ENV{SCRIPT_NAME}."?action=view>Results</A>			</td>
			</tr></table>
			</TD>
			</TR>
			</TABLE>
			</TD></tr></TABLE></form>";

	&print_poll($html);
}

sub get_results {
	my $poll_id = shift;
	my $html = $poll->get_html($poll_id);
	&print_poll($html);
}

sub print_poll {
	my $html = shift;
        my $pollid = $poll->get;
        if ($form{'action'} eq 'vote') {
                my $cookie1=cookie(
		        -name=>"poll$pollid",
		        -value=>"yes",
		        -expires=>'+5y',
			-path=>'/',
		);
		my $uri=$ENV{HTTP_REFERER};
                $uri=~s/^(.*)\?.*$/$1/g;
		unless ($uri=~m/www\.opencores\.org/ && $uri!=~m/cgi-bin/) {
			$uri='/';
  		}
		$uri.="?action=view";
		print redirect(
			-cookie=>[$cookie1],
			-uri=>$uri,
		);
        } else {
            print header;
        }
	print $html;
	print <<END
		<p>
		<TABLE CELLPADDING=1 CELLSPACING=0 BORDER=0 width=100%>
	        <TD BGCOLOR=#000000>
		<TABLE CELLPADDING=3 CELLSPACING=0 BORDER=0 width=100% bgcolor=#ffffff>
		<tr><td><center><a href="http://www.opencores.org/poll_archive/">Past polls</a></td></tr></table>
		</td></tr></table>
END
;
}
