$(document.pollForm).ready(function() 
{  //7 

	$("#pollAjaxLoader").hide(); //hide the ajax loader
	$("#pollMessage").hide(); //hide the ajax loader
	$("#pollSubmit").click(
	function()
	{//6
		var pollAnswerVal = $('input:radio[name=pollAnswerID]:checked').val();//Getting the value of a selected radio element.
		if ($('input:radio[name=pollAnswerID]:checked').length) {//5
			$("#pollAjaxLoader").show(); //show the ajax loader
			$.ajax({  //4
				type: "POST",  
				url: "functions.php",  
				data: { pollAnswerID: pollAnswerVal, action: "vote" },
				success: function(theResponse) { //3
					//the functions.php returns a response like "1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" which the first number is the answerID, second is the points it has and third is the color for that answer's graph. The last number is the sum of all points for easilt calculating percentages.
					if (theResponse == "voted") 
					{//2
						$("#pollAjaxLoader").hide(); //hide the ajax loader
						$("#pollMessage").html("Maaf, Anda sudah melakukan voting sebelumnya. ").fadeTo("slow", 1,function()
						{
						setTimeout(function()
				 		{
							$("#pollMessage").fadeOut("slow");
							}, 3000);																		 
						});
					} //2
					else 
					{//2
						var numberOfAnswers 		= (theResponse).split("-").length-2;//calculate the number of answers
						var splittedResponse 		= (theResponse).split("-");
						var pollAnswerTotalPoints 	= splittedResponse[numberOfAnswers+1];
	
						for (i=0;i<=numberOfAnswers;i++)
						{//1
							var splittedAnswer 		= (splittedResponse[i]).split("|");
							var pollAnswerID 		= (splittedAnswer[0]);
							var pollAnswerPoints 	= (splittedAnswer[1]);
							var pollAnswerColor 	= (splittedAnswer[2]);
							var pollPercentage		= (100 * pollAnswerPoints / pollAnswerTotalPoints);
							$(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor);
							$(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"});
							$("#pollAnswer" + pollAnswerID).html("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " votes");
							$("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons
						}//1
						$("#pollMessage").html("Terima kasih sudah melakukan voting. ").fadeTo("slow", 1,function()
						{
						setTimeout(function()
				 		{
							$("#pollMessage").fadeOut("slow");
							}, 3000);																		 
						});
						$("#pollAjaxLoader").hide(); //hide the ajax loader again
						$("#pollSubmit").hide();
						$("#pollHasil").hide();
						$("#back").show();
					}//2
				}  //3
		});  //4
		return false; 
		
		} //5
		else {
			$("#pollMessage").html("Anda belum memilih jawaban.   ").fadeTo("slow", 1, function()
			{
				setTimeout(function()
				 {
					$("#pollMessage").fadeOut("slow");
				}, 3000);																		 
			});
			return false;
		}
	
	});//6

});//7




	
