$(function(){
	
	//Inicio scripts ENQUETE ------------------------------------------------------------------	   
	
	//acao botao votar
	$("#bt_votar").click(function(){
		
		var enquete = $("#enqueteId").val();
		var opcao = pegaOpcaoSelecionada();
		var comentario = $("#comentario").val();
		
		if(opcao == ""){
			alert("Por favor, selecione uma opção.");
			return false;
		}
		
		// Show the loading div  
		$("#contentLoading").css("display","block");

		// Hide current enquete
		$("#boxEnquete").hide();
		
		// Load the data  
		$.post("../_include/enquete/action.php",
			{
				enquete		: enquete,
				opcao		: opcao,
				comentario 	: comentario
			},
			function(resposta) {
				// When the Ajax call finishes:
				if(resposta == "ok" || resposta == "ja_votou"){
					if(resposta == "ok"){
						alert("Obrigado pela sua participação");
					}else if(resposta == "ja_votou"){
						alert("Você já participou na votação");
					}
					mostraResultado(enquete);
				}else{
					alert("Caro participante.\nOcorreu um erro ao enviar os dados.\nPor favor, tente novamente em alguns instantes.");
					// Show current enquete
					$("#boxEnquete").show();	
				}
			}
		);
		return false;
	});
	
	function mostraResultado(enquete){
		// Load the data  
		$.post("../_include/enquete/resultado.php", { enquete : enquete },
			function(resposta) {
				if(resposta!="err"){
					// When the Ajax call finishes:
					$("#boxEnquete").html(resposta);
				}
				// Hide the loading div  
				$("#contentLoading").css("display","none");
				// Show current enquete
				$("#boxEnquete").show();
			}
		);
	}
	
	function pegaOpcaoSelecionada(){
		var opc = "";
		$("#enquete").find("input[type=radio]").each(function(){
			if( $(this).is(':checked') ){
				opc = $(this).val();
			}
		});
		return opc;
	}
	
	//Fim scripts ENQUETE ------------------------------------------------------------------	
	
		
});

function removeHTMLTags(source){
	/* 
		This line is optional, it replaces escaped brackets with real ones, 
		i.e. < is replaced with < and > is replaced with >
	*/	
	source = source.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = source.replace(/<\/?[^>]+(>|$)/g, "");

	return strTagStrippedText;
}