#!/usr/bin/perl

# example polling program
# use the below in place of use WWW::Poll to test local 
#use lib qw( ../ );
#use Poll;

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

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

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

&main();

sub main {
        $poll=new WWW::Poll;
        $poll->path($POLLPATH);


        $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');
		&list_polls;
}


sub list_polls {
	my %polls = $poll->list;
	my $html = "<UL>";
        my $pollid=$poll->get();
	$html.="<table cellpadding=8 border=0>";
	my $mod=3;
	my $counter=$mod;
        my $rem;
 
        my @id=keys %polls;
        foreach (@id) {
            $_='0'.$_ if (length($_)<2);
        }
        foreach (@id) {
            $_='0'.$_ if (length($_)<3);
        }

	foreach (sort @id) {
           $_=~s/^0*//g;
	   if ($_<$pollid) {
		import WWW::Poll;
		my $poll=new WWW::Poll;
		$poll->path($POLLPATH);
		my $z = $counter / $mod;
		$rem = $z - int $z;
     		$html.="<tr>" if ($rem eq '0');
		$html.="<td valign=bottom>".$poll->get_html($_)."</td>";
		$counter++;
		my $z = $counter / $mod;
		$rem = $z - int $z;
     		$html.="</tr>" if ($rem eq '0');
           }
	}
	if ($counter==$mod) {
		$html.="<td>No polls in archive jet.</td>";
	}
        $html.="</tr>" if ($rem ne '0');
	$html .= "</table></UL>";
	&print_poll($html);
}


sub print_poll {
	my $html = shift;
        my $pollid = $poll->get;
        print header;
	print $html;
}
