#!/usr/bin/perl

use strict;
use vars qw{%Form $Self $d};
require '/home/webmangr/cgi-box/polls/common.pm';

$Self = '/cgi-box/polls/vote';

&Decode('+');
&Connect();
&Main();

sub Main {

  my($r,$Skip,$Count);

  &Poll($Form{POLL});
  if($Form{POLL_ID} == 0) {
    &Error('Sorry, you have entered an invalid poll identifier.');
  };

  if($Form{POLL_ACTIVE} ne 'Y') {
    $Form{ERROR} = 'Sorry, this poll is now closed.';
    $Skip = 1;

  } elsif($Form{VOTE}) {
    if(scalar(split(',',$Form{OPTION})) != $Form{POLL_REQUIRED}) {
      if($Form{POLL_REQUIRED}==1) {
        $Form{ERROR} = "Please make a selection from the available options.";
      } else {
        $Form{ERROR} = "Please make $Form{POLL_REQUIRED} selections from the available options.";
      };

    } elsif($ENV{HTTP_COOKIE} =~ /pollsv_$Form{POLL_ID}_ok/) {
      $Form{ERROR} = 'Sorry, you may only complete this poll once.';

#   } elsif($d->select("SELECT COUNT(1) FROM PL_TRACKS WHERE POLL=? AND IP=? AND STAMP > (UNIX_TIMESTAMP()-86400*3)",\$Count,$Form{POLL_ID},$ENV{REMOTE_ADDR}) && $d->row() && $Count>0) {
#   $Form{ERROR} = 'Sorry, you may only complete this poll once.';

    } else {
      &SetCookie("pollsv_$Form{POLL_ID}_ok=1; expires=Fri, 31-Dec-10 23:59:59 GMT");
#     $d->delete("DELETE FROM PL_TRACKS WHERE POLL=? AND IP=?",$Form{POLL_ID},$ENV{REMOTE_ADDR});
#     $d->insert("INSERT INTO PL_TRACKS (POLL,IP,STAMP) VALUES(?,?,UNIX_TIMESTAMP())",$Form{POLL_ID},$ENV{REMOTE_ADDR});
      $d->update("UPDATE PL_POLLS SET VOTES=VOTES+1 WHERE ID=?",$Form{POLL_ID});
      $d->update("UPDATE PL_OPTIONS SET VOTES=VOTES+1 WHERE ID IN ($Form{OPTION})");
      if($Form{POLL_REQUIRED}==1) {
        $Form{ERROR} = 'Your selection has been recorded.';
      } else {
        $Form{ERROR} = 'Your selections have been recorded.';
      };
      $Skip = 1;
    };

  };

  if(!$Skip) {

    my(%Option,$Checked,$Check,$Count,$Width);
    my $Size = $Form{POLL_ROWS} || 99999;
    $Width = sprintf("width=\"%d%%\"",100/(int($Form{POLL_OPTIONS}/$Size)+1));
    $r .= "<table border=0 cellspacing=0 cellpadding=0><tr><td valign=top $Width>";
    $d->select('SELECT * FROM PL_OPTIONS WHERE POLL=? ORDER BY ID',$Form{POLL_ID},\%Option);
    while($d->row()) {
      if(++$Count > $Size) {
        $Count = 1;
        $r .= "</td><td valign=top $Width>";
      };
      $Check = ",$Form{OPTION},";
      if($Check =~ /,$Option{ID},/) { $Checked = ' checked'; } else { $Checked = ''; };
      if($Form{POLL_REQUIRED}==1) {
        $r .= "<input type=radio name=OPTION value=$Option{ID} $Checked> $Option{DETAIL} &nbsp; &nbsp; &nbsp;<BR>\n";
      } else {
        $r .= "<input type=checkbox name=OPTION VALUE=$Option{ID} $Checked> $Option{DETAIL} &nbsp; &nbsp; &nbsp;<BR>\n";
      };
    };
    $r .= '</td></tr></table>';
    $r .= &Expand('<p><input type="hidden" name="POLL" value="{POLL}"><input type="submit" value="Vote" name="VOTE" style="width=100"></p>');

  };

  $Form{CHOICES} = $r;

  &Show('template_'.$Form{POLL_TEMPLATE});

};
