/**
***********************************
* Funzione per disegnare il grafico
***********************************
*/
var count =0;
function disegna()
{
  var table = document.getElementById("tabgraf");
  var tbody = document.getElementById("tbody");
  var tr = document.getElementById("graf");
  //tr.removeChild(tr.firstChild);
  ottimizza();
  
  if(count > 0)
  {
   tbody.removeChild(tr);
   var t = document.createElement("tr");
   t.setAttribute("id","graf");
   tr =t;
   td = document.createElement("td");
   tr.appendChild(td);
  }
  for (var i=0; i<data.length; i++)
  {

    td =createTd(data[i][1],data[i][3],data[i][0],data[i][2]);
    tr.appendChild(td);
  }
  if(count >0) 
  { 
   tbody.insertBefore(tr,tbody.firstChild);
  }
  count=1;
}

/*********************************************************
* Crea una colonna del grafico
* Parametri: percentuale, Stringa di testo, colore barra
**********************************************************
*/
function createTd(data1,data3,data2,color)
{
  //TD
  td = document.createElement("td");
  testoTd = document.createTextNode(data1+"%");
  td.style.textAlign="left";
  td.style.verticalAlign="bottom";
  td.style.borderBottom="1px solid red";
  
  //td.setAttribute("style",styleTd);
  td.appendChild(testoTd);
  
  //DIV
  div = document.createElement("div");
  div.appendChild(document.createTextNode(" "));
  //div.style.background=color;
  div.style.width="25px";
  data3 = (data3*3).toFixed(1);
  // div.style.height=data3 +"px";
  
  var img = document.createElement("img");
  img.setAttribute("src",color);
  img.setAttribute("alt","");
  img.style.height = data3 + "px";
  img.style.width = "25px";
  img.style.border = "0";
  img.style.margin="0px";
  
  div.appendChild(img);
  //div.setAttribute(\"style",styleDiv);
  td.appendChild(div);
  
  //TD
        testo2 = document.createTextNode(data2);
        td.appendChild(testo2);
  
  return td;  
}

function ottimizza()
{
max = data[0][1];

 for(var i=0; i<data.length; i++)
   {
    if(i<data.length-1)
    {
     max = Math.max(max,data[i+1][1]);
    }
   }
   
//Incremento la max percentuale di un valore costante
max+=10;

// ricalcolo i valori in proporzione
for(var j=0; j<data.length; j++)
  {
    data[j][3] =  ((data[j][1]*100)/max);
    
  }
  return max;
}
