<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

function vote($poll) {
  global $userID;
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
	
	if($poll) $lastpoll = safe_query("SELECT * FROM ".PREFIX."poll WHERE aktiv='1' AND laufzeit>".time()." AND intern<=".isclanmember($userID)." and pollID='".$poll."' LIMIT 0,1");
	else {
		$num = mysql_num_rows(safe_query("SELECT * FROM ".PREFIX."poll WHERE aktiv='1' AND laufzeit>".time()." AND intern<=".isclanmember($userID).""));
		if($num) {
			$start = rand(0,($num-1));
			$lastpoll = safe_query("SELECT * FROM ".PREFIX."poll WHERE aktiv='1' AND laufzeit>".time()." AND intern<=".isclanmember($userID)." ORDER BY pollID DESC LIMIT ".$start.",".($start+1)."");
		}	else {
			echo 'no active poll<br><br>&#8226; <a href="index.php?site=polls">show polls</a>';
			return true;
		}
	}

	//else $lastpoll = safe_query("SELECT * FROM ".PREFIX."poll WHERE aktiv='1' AND laufzeit>".time()." AND intern<=".isclanmember($userID)." ORDER BY pollID DESC LIMIT 0,1");
	
	$anz = mysql_num_rows($lastpoll);
	$ds = mysql_fetch_array($lastpoll);
	if($anz) {
      $anz=mysql_num_rows(safe_query("SELECT pollID FROM `".PREFIX."poll` WHERE pollID='".$ds[pollID]."' AND hosts LIKE '%".$_SERVER[REMOTE_ADDR]."%' AND intern<=".isclanmember($userID).""));
      if($_COOKIE['poll'] == $ds[pollID] OR $anz) {

		    echo'<table cellpadding="2" cellspacing="0">
			     <tr><td><b>'.$ds[titel].'</b></td></tr>';
								 
			for ($n=1; $n<=10; $n++) {
	            if($ds[o.$n]) $options[]=$ds[o.$n];
			}
				
			$votes = safe_query("SELECT * FROM ".PREFIX."poll_votes WHERE pollID='$ds[pollID]' ");
			$dv = mysql_fetch_array($votes);
			$gesamtstimmen = $dv[o1] + $dv[o2] + $dv[o3] + $dv[o4] + $dv[o5] + $dv[o6] + $dv[o7] + $dv[o8] + $dv[o9] + $dv[o10];
								
			echo'<tr><td>'.$gesamtstimmen.' votes</td></tr>';
								
			$n=1;
			foreach ($options as $option) {
			    $stimmen = $dv[o.$n];
				if ($gesamtstimmen) {
				    $perc = $stimmen / $gesamtstimmen * 10000;
					settype($perc, "integer");
					$perc = $perc/100;
				}
				else $perc = 0;
				$picwidth = $perc;
				settype($picwidth, "integer");
										
				echo'<tr><td>'.$option.': '.$perc.'%</td></tr>';
									
				if ($picwidth) echo'<tr><td>
								    <table width="104" cellspacing="1" cellpadding="1" bgcolor="'.BORDER.'">
									  <tr bgcolor="'.BG_2.'">
									    <td background="images/icons/poll_bg.gif"><img src="images/icons/poll.gif" width="'.$picwidth.'" height="5"></td>
									  </tr>
									</table>
								  </td></tr>';
				$n++;
			}
			
			$anzcomments = getanzcomments($ds[pollID], 'po');
			$comments='<a href="index.php?site=polls&pollID='.$ds[pollID].'">['.$anzcomments.'] comments</a>';
				
			echo'<tr><td><br>'.$comments.'<br><br>&#8226; <a href="index.php?site=polls">show polls</a></td></tr></table>';
			unset($options);
		}
		else {
			echo'<table cellspacing="2" cellpadding="0">
				   <tr><form method="post" action="polls.php?action=vote">
				   <td><b>'.$ds[titel].'</b><br><br></td><tr><td>';
				
			for ($n=1; $n<=10; $n++) {
			    if($ds[o.$n]) $options[]=$ds[o.$n];
			}
			$n=1;
			foreach ($options as $option) {
		        echo'<input class="input" type="radio" name="vote" value="'.$n.'"> '.$option.'<br>';
				$n++;    
			}
			echo'</td></tr><tr><td><br>
			    <input type="hidden" name="pollID" value="'.$ds[pollID].'">
			    <input type="submit" value="vote"></td></form></tr><tr><td><br>&#8226; <a href="index.php?site=polls">show polls</a></td></tr></table>';
	    }
	}	else echo 'no active poll<br><br>&#8226; <a href="index.php?site=polls">show polls</a>';
}

vote($pollID);
?>