var container;
function votar(nmDataList,codEnquete)
{
	container = window.document.getElementById(nmDataList);
	
	var dlVotacao = document.getElementById( nmDataList );
	var rButtons = dlVotacao.getElementsByTagName( "input" );
	var codOpcao = 0;									
		for(x = 0; x < rButtons.length; x++)
		{
			if( rButtons[x].checked )
			{
				codOpcao = parseInt( rButtons[x].value );
			}
		}
		if(codOpcao > 0)
		{
			if((LerCookie(codEnquete)) == codEnquete)
			{
				return false;
			}
			else
			{
				Poll.Votar(codOpcao,retorno);
			}
		}
		else {
			alert("Selecione uma opção.");
			return false;
		}
}


function VerificarVotacao(nmDataList,codEnquete)
{
	container = window.document.getElementById(nmDataList);
	if((LerCookie(codEnquete)) == codEnquete)
	{
		Poll.MostrarResultado(codEnquete,MostrarResultado_CallBack);
	}
}

function retorno(response)
{
	if(response.error != null)
	{
		alert(response.error.description);
		return false;
	}
	else
	{
	
		var ds = response.value;
		if(ds!=null && typeof(ds) == "object" && ds.Tables!=null)
		{
			var s = new Array();
			var valorCookie;
		    
			s[s.length] = "<TABLE>";
			s[s.length] = "<TR><TD>&nbsp;</td></tr>";
			
			for(var i=0;i<ds.Tables[0].Rows.length;i++)
			{
				s[s.length] = "<TR><TD class='texto_corpo'>"+ds.Tables[0].Rows[i].porcentagem.replace(".","")+"% "+ ds.Tables[0].Rows[i].description +"</TD></TR>";
				s[s.length] = "<TR><TD><TABLE width='"+ds.Tables[0].Rows[i].porcentagem+"%'><TR bgColor='#faac36' height='5'><TD></TD></TR></TABLE></TD></TR>";
			}
			s[s.length] = "<TR class='texto_negrito'><TD>"+ds.Tables[0].Rows[0].totalVotos+" pessoas votaram nesta enquete</TD></TR>";
			s[s.length] = "</table>";
		
		   valorCookie = ds.Tables[0].Rows[0].pollId;
		   EscreveCookie(valorCookie,valorCookie,24);
		   
		    var dvRes = container.getElementsByTagName("div");
			dvRes[0].innerHTML = s.join("");
			dvRes[1].style.display = "none";
		}
  	}
}
//Esta função só e chamada quando o usuário já votou na enquete(validação é feita por cookie)
function MostrarResultado_CallBack(response)
{
		if(response.error != null)
		{
			alert(response.error.description);
			return false;
		}
		else
		{
				var ds = response.value;
				if(ds!=null && typeof(ds) == "object" && ds.Tables!=null)
				{
					var s = new Array();
					var valorCookie;
					
					s[s.length] = "<TABLE>";
					s[s.length] = "<TR><TD>&nbsp;</td></tr>";
					
					for(var i=0;i<ds.Tables[0].Rows.length;i++)
					{
						s[s.length] = "<TR><TD class='texto_corpo'>"+ds.Tables[0].Rows[i].porcentagem.replace(".","")+"% "+ ds.Tables[0].Rows[i].description +"</TD></TR>";
						s[s.length] = "<TR><TD><TABLE width='"+ds.Tables[0].Rows[i].porcentagem+"%'><TR bgColor='#faac36' height=5'><TD></TD></TR></TABLE></TD></TR>";
					}
					s[s.length] = "<TR><TD class='texto_negrito'>"+ds.Tables[0].Rows[0].totalVotos+" pessoas votaram nesta enquete</TD></TR>";
					s[s.length] = "<TR><TD>&nbsp;</TD></TR>";
					s[s.length] = "<TR><TD class='texto_negrito'>Seu voto já foi computado!</TD></TR>";
					s[s.length] = "</table>";
									
					var dvRes = container.getElementsByTagName("div");
					dvRes[0].innerHTML = s.join("");
					dvRes[1].style.display = "none";
				}
		}
}

function EscreveCookie(name, value, time_exp){
 if(time_exp != "" ){
  var exp = new Date();
  var expira = exp.getTime() + (time_exp * 3600000);
  exp.setTime(expira);
  document.cookie = "" + name + "=" + value + "; expires=" + exp.toGMTString();
 }
}

function LerCookie(name){
 var cookieValue = "";
 var search = name + "=";
 if(document.cookie.length > 0){
  offset = document.cookie.indexOf(search);
  if (offset != -1){
   offset += search.length;
   end = document.cookie.indexOf(";", offset);
   if (end == -1) end = document.cookie.length;
   cookieValue = unescape(document.cookie.substring(offset, end))
  }
 }
 return cookieValue;
}

