$(document).ready(function(){
	// formulario login
	$('#fencuesta').ajaxForm({target:'#fencuesta_result', dataType:'json', data:{js:'1'}, beforeSubmit:fencuesta_beforesubmit,success:fencuesta_response,clearForm: false,replaceTarget:false}); }); 	
function fencuesta_beforesubmit(formData, jqForm, options) { 
		if ($('input:radio[name=pollAnswerID]:checked').length) {
			
		}else{			
			$('#fencuesta_result').html('Elije una opcion');
			return false; 			
		}		
		$('#pollSubmit').hide();
		return true;
	}
function fencuesta_response(responseText, statusText, xhr, $form)  { 
	if( responseText.Respuestas.length > 0 ){
		Votaciones(responseText.Respuestas,responseText.Datos.TotalVotos);
		//$('#fencuesta_result').html(responseText.Datos.TotalVotos+' votos totales');
	}else{ 
		$('#pollSubmit').show();		
		$('#fencuesta_result').html(responseText); 
	}
}
function Votaciones(Datos,TotalVotos){
	$.each(Datos, function(i,item){
		var pollAnswerID = item.IdRespuesta;
		var pollAnswerPoints = item.Puntos;
		var pollAnswerColor = item.Color;
		var pollPercentage = Math.round(100 * pollAnswerPoints / TotalVotos);							
		$(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor);
		$(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"});
		$("#pollAnswer" + pollAnswerID).html(" (" + pollPercentage + " %)");
		$("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons
	});
	$("#pollSubmit").attr("disabled", "disabled"); //disable the submit button
}
