<?
#On recupere le sondage de la periode courante
$catId = getQActualCategory();
# on recupere la question du sondage courant
$motion = getQMotion($catId);

//debug("cookie=".$_COOKIE[$COOKIE_SONDAGE]." CatId=".$catId);
//debug("isset cookie=".isset($_COOKIE[$COOKIE_SONDAGE]));
if($motion)
{
	
	if(($vote == 0) && ($_COOKIE[$COOKIE_SONDAGE] != $catId) )
	{

		# on recupere les réponses
		$reponses = getQMotionTypesAnswers($motion['MotionId']);
		echo "<form method='post' name='main' onSubmit='return check(this)'  action='".$SERVER_URL."sondage_valid.php3'>";
		echo $sess->hfield();
		echo "<b>".stripslashes($motion['Title'])."</b>";
		
		echo "<table width='100%' cellpadding=0 cellspacing=0 style='line-height:7px;'>\n";
		while(list($repId, $reponse) = each($reponses))
		{
			echo "<tr><td width=\"5%\" valign=top><input type=radio value=\"".$reponse['Id']."\" name=rep></td>\n";
			echo "<td class=smallDark>".stripslashes($reponse['Answer']);
			echo "</td></tr>";
		}		
		echo "<tr><td /><td align=right>".
			"<input type=hidden name=submit value='reponse'>".
			"<input type='image' src='".$IMAGES_DIR."boutons/btn_ok.png' name='Ok' value='Voter'> &nbsp;".
			"</td></form></tr>";				
		echo "</table>";

	}
	else
	{	
			
		# on recupere la question du sondage courant
		$motion = getQMotion($catId);
		# on recupere les réponses
		$reponses = getQMotionTypesAnswers($motion['MotionId']);	
		
		echo "<b>".stripslashes($motion['Title'])."</b>";
		echo "<br>";
		echo "<br>Vos réponses :";	
		$tab_rep=Array();
		$total=0;
		$i=0;
		while(list($repId, $reponse) = each($reponses))
		{
			$total += $reponse['Extra'];
			$tab_rep[$i]["Answer"] = $reponse['Answer'];
			$tab_rep[$i]["Extra"] = $reponse['Extra'];
			$i++;
		}
	
		if($total>0)
		{
			echo "<table border=0 valign=center cellpadding=0 cellspacing=1 width=100%>\n";
			for($j=0;$j<$i;$j++)
			{
			
				$pourc=number_format($tab_rep[$j]['Extra']*100/$total,2);
				if((int)($j%2)==0)
					echo "<tr bgcolor='#EEEEEE' valign=center><td class=smallDark valign=center><img src='".$IMAGES_DIR."carreorange.gif' height=5 width=".$pourc.">&nbsp;".number_format($tab_rep[$j]['Extra']*100/$total,1)." %&nbsp;".stripslashes($tab_rep[$j]['Answer']);
				else
					echo "<tr bgcolor='#FFFFFF' valign=center><td class=smallDark valign=center><img src='".$IMAGES_DIR."carreorange.gif' height=5 width=".$pourc.">&nbsp;".number_format($tab_rep[$j]['Extra']*100/$total,1)." %&nbsp;".stripslashes($tab_rep[$j]['Answer']);
				echo "</td></tr>";
			}		
			echo "</table>";
			echo "<div align=right>".$total." réponses</div>";
		}
	}
}
else
{
	echo "Pas de sondage en ce moment";
}

?>
<script>
	function check(doc_this)
	{
		<?php 
		if($reponses)
		{
		?>
			Selectionne = false;
			for (i = 0 ; i < <?php echo count(array_keys($reponses)) ?> ; i++)
			{
				if (document.main.rep[i].checked)
				{
					Selectionne = true;
					break;
				}
			}
		
			if (Selectionne == false)
			{
				alert("Vous sélectionner une réponse !");
				return false;
			}
		<?
		}
		?>
	}
</script>
