//function for voting in poll

function vote(pollId,pollAnswer){  
  
  if ( pollAnswer == 0) doVote(pollId,0);
  var inputy = document.getElementsByName("answer");    
  for ( i = 0 ; i < inputy.length ; i++ )
  {
    if (inputy[i].checked) doVote(pollId,inputy[i].value);
  }

}

function doVote(pollId,pollAnswer){
  var req = mint.Request();
  names = new Array();
  values = new Array();
  
  var sondaBox = getElementsByClass("div","smallBox")[0];  
  var sonda = document.getElementById("poll");
  var loading = document.getElementById("loading");
  sondaBox.style.height = sondaBox.clientHeight+"px"; 
  sonda.style.display = "none";  
  loading.style.display = "block";
    
  //request  
  
  req.getJSON = true;
  req.AddParam("pollId", pollId);
  req.AddParam("answerId", pollAnswer);  
  req.OnSuccess = function() 
  {    
      for(var i = 0; i < this.responseJSON.length; ++i) 
      {
          names[i] = this.responseJSON[i].name; 
          values[i] = this.responseJSON[i].value;            
      }     
      generateChart(names, values);
  }
  req.Send("/webservices/poll.php");
     
  if ( pollAnswer != 0 )
  {    
    rookie = readCookie("polls");        
    if ( rookie ){
      con = rookie;      
      eraseCookie("polls");      
      new_con = con+"|"+pollId;      
      createCookie("polls",new_con,180);
    }
    else{
      createCookie("polls",pollId,180);
    }        
  }
  
}

function generateChart(names, values){
    
    var colors = new Array(10);
    
    colors[0] = '#006C8A';
    colors[1] = '#016C62';    
    colors[2] = '#A40000';
    colors[3] = '#FF5029';
    colors[4] = '#732800';
    colors[5] = '#42212A'; 
    colors[6] = '#00325B';
    colors[7] = '#004A40';
    colors[8] = '#7A2894';
    colors[9] = '#9D8384';
    colors[10] = '#006DAD';
    colors[11] = '#00AFEF';
    colors[12] = '#00AD9C';
    colors[13] = '#497331';
    colors[14] = '#F71932';
    colors[15] = '#FF9429';
    colors[16] = '#F76251';
    colors[17] = '#F60090';
  
    var results = document.getElementById("results");
    var loading = document.getElementById("loading");
        
    //oblicz procenty
    percents = new Array();
    var sum = 0;
    for ( var it=0; it < values.length ; it++)
    {
      sum += parseInt(values[it]);
    }
    
    for ( var it=0; it < values.length ; it++)
    {
      percents[it] = parseInt((values[it]/sum)*100);      
    }
    
    //generuj wyniki
    for (var i=0; i < names.length ; i++)
    {
      document.getElementById('chart'+i).style.backgroundPosition = (2*percents[i])+"px";     
      document.getElementById('chart_percent'+i).innerHTML = "<b>"+percents[i]+"%</b>"; // ("+values[i]+")";
      
      if ( i > 18 ) k = i % 19; 
      else k = i;      
      document.getElementById('chart'+i).style.backgroundColor = colors[k];
    }
    
    //usun animowanego gifa i wrzuc wyniki
    loading.style.display = "none";
    results.style.display = "block";
}