<?php
include ("db/dbMy.class.php");

$db= new dbMy();
$sql = "select * from poll where activa = 1";
$listaPolls = $db->get($sql);
$size_polls = sizeof($listaPolls);

if($size_polls == 1) $style = "";
else $style = "style=\"display: none;\"";
?>
<script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/jquery.validate.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){

	var i = 0;
	
	$(".linkpoll").click(function(){
		resetPolls();
		i = this.href.split('#')[1];
		$("#poll" + i).show();
		return false;
	});

	$(".resp").click(function(){
		$("#justif" + i).show();
	});

	$("#justif" + i).click(function(){
		$(this).children().val("");
		//$(this).css({color:"black", fontStyle:"normal"});
	});

	function resetPolls(){
		$(".polls").hide();
	}

	$(".formguardar").validate({
		rules: {resposta: {required: true}},
		messages: {resposta: "Por favor escolha uma opção."},
		submitHandler: function(form){
			var options = {target: '#resultadoc', clearForm: true};
			$(form).ajaxSubmit(options);
			return false;
		}
	});

});
</script>

<div>
	<?for($i=0;$i<$size_polls;$i++){?>
		<p>
			<a class="linkpoll" href="#<?=$i?>" ><? echo $listaPolls[$i]['pergunta'];?></a> 
			<div <?=$style ?>  id="poll<?=$i?>" class="polls">
				<form enctype="multipart/form-data" name="formguardar" class="formguardar" action="poll_guardar.php" method="POST">
					<?for($j=1;$j<=10;$j++){
						if($listaPolls[$i]["r$j"] != ""){?>
						<p>
							<input class="resp" id="resp<?=$j?>" type="radio" name="resposta" value="c<?=$j?>">
							<?=$listaPolls[$i]["r$j"]; ?>
						</p>
					<? }}?>
					<p style="display: none;" id="justif<?=$i?>"><textarea name="justificacao" rows="5" cols="50">Justifique aqui a sua resposta</textarea></p>
					<input type="hidden" name="idPoll" value="<?=$listaPolls[$i]["id"] ?>">
					<p><input type="submit" value="Votar" name="cmd"></p>
				</form>	
			</div>
		</p>
	<?}?>
	<div id="resultadoc"></div>
</div>