
//the array of colors contains unique Hex coded colours (without #) for the bars
var colors=new Array("AFD8F8", "F6BD0F", "8BBA00", "F6BD0F", "008E8E", "D64646");

var chart1;
var strXML;

/**
* updateChart method is called, when user changes any of the checkboxes.
* Here, we generate the XML data again and build the chart.
* @param domId domId of the Chart
*/
function updateChart(domId, sigarettes, cash)
{

  strXML = generateXML(sigarettes, cash);
  if (strXML != "")
  {
//    if (chart1 == null)
    //{
      chart1 = new FusionCharts("../inc/js/FusionCharts/FCF_Column3D.swf", "chart1Id", "400", "300");
      chart1.setDataXML(strXML);
      chart1.render("chart1div");
    /*}
    else
    {
      //Update it's XML - set animate Flag from AnimateChart checkbox in form
      //using updateChartXML method defined in FusionCharts JavaScript class
      updateChartXML(domId, strXML);
    }*/
  }

}

/**
* generateXML method returns the XML data for the chart based on the
* checkboxes which the user has checked.
* @return XML Data for the entire chart.
*/
function generateXML(sigarettes, cash)
{

  var cashLoss = getCashLoss(sigarettes, cash);
  if (cashLoss == -1)
  {
    return "";
  }

  amount_smokers = Array(20, 50, 100, 150, 200, 300);
  var strXML = "<graph caption='Totale jaarlijkse kosten per roker' xaxisname='Aantal rokers' yaxisname='Kosten' numberPrefix='Euro ' decimalPrecision='0' animation='1' showValues='0' formatNumber='1' formatNumberScale='0'>";
  var cashLossPerUser = 0;
  for (i = 0; i < 6; i++)
  {
    cashLossPerUser = Math.round(amount_smokers[i] * cashLoss * 100) / 100;
    strXML += "<set name='"+amount_smokers[i]+"' value='"+cashLossPerUser+"' color='"+colors[i]+"' />";
  }
  strXML += "</graph>";
  
  //Return data
  return strXML;
}

function getCashLoss(sigarettes, cash)
{

  sigarettes = parseInt(sigarettes.value);
  if (isNaN(sigarettes))
  {
    alert('Vul het gemiddeld aantal sigaretten per dag in');
    return -1;
  }
  cash = parseInt(cash.value);
  if (isNaN(cash))
  {
    alert('Vul het gemiddelde uurloon in');
    return -1;
  }
  return (sigarettes * 5.5 * (cash / 60) * 365) + 3291;

}