function sendPoll(){
	var chkAnswer = '';
	var txtAnswer = '';
	var id = $("#Poll_pollID").val();
	
	for(i=1;i<=10;i++){
		if($("#pollAnswer" + i).length==0){
			chkAnswer += '0|';
		}else{
			if($("#pollAnswer" + i + ":checked").length == 0){
				chkAnswer += '0|';
			}else{
				chkAnswer += '1|';
			}
		}
		if($("#pollText" + i).length==0){
			txtAnswer += ' |';
		}else{
			if($("#pollAnswer" + i + ":checked").length == 0){
				txtAnswer += ' |';
			}else{
				txtAnswer += $("#pollText" + i).val().replace(/\|/g,'-') + '|';
			}
		}
	}
	if(getCookie("SIUUrologyPoll"+id) == ""){
		$.ajax({
			type: "POST",
			url: "poll.aspx",
			data: "id="+id+"&chk="+chkAnswer+"&txt="+txtAnswer+"&memid="+$("#drawID").val()+"&lastName="+$("#drawSurname").val(),
			success: function(msg){
				if (msg==''){
					var date = new Date();
					date.setTime(date.getTime()+(360*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
					document.cookie = "SIUUrologyPoll"+id+"=1"+expires+"; path=/";
					showPollResults();
				}
			}
		});
	}
}

function showPollResults(){
	$("#btnPollVote").hide();
	$("#btnPollView").hide();
	
	var aAnswer = new Array;
	$.ajax({
		type: "POST",
		url: "pollAnswer.aspx",
		data: "id="+$("#Poll_pollID").val(),
		success: function(msg){
			aAnswer = msg.split("|");
	
			for(i=1;i<=10;i++){
				$("#pollAnswer" + i).hide();
				$("#pollText" + i).hide();
				$("#pollAnswer" + i).after("<div id='pollBar"+i+"' class='pollBar'>50%</div>");
				$("#pollBar" + i).animate({width: (aAnswer[i-1]==0)?15:aAnswer[i-1]*2 }, 1000);
				$("#pollBar" + i).html(Math.round(aAnswer[i-1])+'%');

			}

			if($("#drawID").length != 0){
				$("#drawID").parent().hide();
				$("#drawSurname").parent().hide();
			}
		}
	});
}

$("#btnPollVote").click(function(){
	var bSend = true;
	if($("input[id*='pollAnswer']:checked").length == 0){
		alert('Please, check at least one answer.');
		bSend = false;
	}else{
		$("input[id*='pollAnswer']").each(function(index, value) {
			if($("#pollText" + (index + 1)).length != 0 && $("#pollAnswer" + (index + 1) + ":checked").length != 0 && $("#pollText" + (index + 1)).val() == ''){
				alert('Please, enter your answer.');
				bSend = false;
			}
		});
		if(($("#drawID").length != 0 && $("#drawID").val() == '') || ($("#drawSurname").length != 0 && $("#drawSurname").val() == '')){
			alert('Please, enter your meeting ID # and last name.');
			bSend = false;
		}
	}
	
	if(bSend) sendPoll();
	return false;
});
$("#btnPollView").click(function(){
	showPollResults()
	return false;
});
$(document).ready(function(){
	$("#dialogPoll").dialog({
		modal: true,
		autoOpen: false,
		resizable: true
	});
	
	if(getCookie("SIUUrologyPoll"+$("#Poll_pollID").val()) != "") showPollResults();
});

function getCookie(c_name){
	/*if (document.cookie.length>0)  {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}*/
	return "";
}
