// Fattori di sconto
var Sconto6mesi = 0.80;
var Sconto12mesi = 0.70;

// costo azionario
var Costo1mese = 59;
var Costo6mesi = Math.round(Costo1mese * 6 * Sconto6mesi);
var Costo12mesi = Math.round(Costo1mese * 12 * Sconto12mesi);

// costo trading future
var CostoTFuture1mese = 59;
var CostoTFuture6mesi = Math.round(CostoTFuture1mese * 6 * Sconto6mesi);
var CostoTFuture12mesi = Math.round(CostoTFuture1mese * 12 * Sconto12mesi);

// costo covered warrant
var CostoCWarrant1mese = 49;
var CostoCWarrant6mesi = Math.round(CostoCWarrant1mese * 6 * Sconto6mesi);
var CostoCWarrant12mesi = Math.round(CostoCWarrant1mese * 12 * Sconto12mesi);

// costro commodities
var CostoCommodities1mese = 31;
var CostoCommodities6mesi = Math.round(CostoCommodities1mese * 6 * Sconto6mesi);
var CostoCommodities12mesi = Math.round(CostoCommodities1mese * 12 * Sconto12mesi);

// costo personal advisor
var CostoPersonalizzati1mese = 49;
var CostoPersonalizzati6mesi = Math.round(CostoPersonalizzati1mese * 6 * Sconto6mesi);
var CostoPersonalizzati12mesi = Math.round(CostoPersonalizzati1mese * 12 * Sconto12mesi);

// costo mensile sms
var CostoSMS = Math.round( 2 );

//////////////////////////////////
var totalSum = 0;
var nOptions = 0;
var nMonths = 0;
var label = "";

function MkLabel( label, text )
{
  if( label != "" )
  {
    label += ", ";
  }
  label += text;
  return label;
}

function CalculateSum()
{
  totalSum = 0;
  nOptions = 5;
  nMonths = 0;
  label = "";

  //////////////////////////////////////
  // Azioni Europa
  if( document.forms['form1'].elements['radioAzioniEuropa'][0].checked )
  {
    totalSum += Costo1mese;
    label = MkLabel( label, "Europa [1m]" );
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioAzioniEuropa'][1].checked )
  {
    totalSum += Costo6mesi;
    label = MkLabel( label, "Europa [6m]" );
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioAzioniEuropa'][2].checked )
  {
    totalSum += Costo12mesi;
    label = MkLabel( label, "Europa [12m]" );
    if( nMonths < 12 )
    {
      nMonths = 12;
    }
  }
  else
  {
      nOptions--;
  }

  //////////////////////////////////////
  // Azioni USA
  if( document.forms['form1'].elements['radioAzioniUSA'][0].checked )
  {
    totalSum += Costo1mese;
    label = MkLabel( label, "USA [1m]" );
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioAzioniUSA'][1].checked )
  {
    totalSum += Costo6mesi;
    label = MkLabel( label, "USA [6m]" );
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioAzioniUSA'][2].checked )
  {
    totalSum += Costo12mesi;
    label = MkLabel( label, "USA [12m]" );
    if( nMonths < 12 )
    {
      nMonths = 12;
    }
  }
  else
  {
      nOptions--;
  }

  //////////////////////////////////////
  // Trading Future
  if( document.forms['form1'].elements['radioTradingFuture'][0].checked )
  {
    totalSum += CostoTFuture1mese;
    label = MkLabel( label, "Features [1m]" );
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioTradingFuture'][1].checked )
  {
    totalSum += CostoTFuture6mesi;
    label = MkLabel( label, "Features [6m]" );
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioTradingFuture'][2].checked )
  {
    totalSum += CostoTFuture12mesi;
    label = MkLabel( label, "Features [12m]" );
    if( nMonths < 12 )
    {
      nMonths = 12;
    }
  }
  else
  {
      nOptions--;
  }

  //////////////////////////////////////
  // Futures Commodities
  if( document.forms['form1'].elements['radioFuturesComodities'][0].checked )
  {
    totalSum += CostoCommodities1mese;
    label = MkLabel( label, "Commodities [1m]" );
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioFuturesComodities'][1].checked )
  {
    totalSum += CostoCommodities6mesi;
    label = MkLabel( label, "Commodities [6m]" );
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioFuturesComodities'][2].checked )
  {
    totalSum += CostoCommodities12mesi;
    label = MkLabel( label, "Commodities [12m]" );
    if( nMonths < 12 )
    {
      nMonths = 12;
    } 
  }
  else
  {
      nOptions--;
  }
  
  //////////////////////////////////////
  // Azioni Report Week
  if( document.forms['form1'].elements['radioAzioniReportWeek'][0].checked )
  {
    totalSum += CostoCommodities1mese;
    label = MkLabel( label, "Report [1m]" );
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioAzioniReportWeek'][1].checked )
  {
    totalSum += CostoCommodities6mesi;
    label = MkLabel( label, "Report [6m]" );
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioAzioniReportWeek'][2].checked )
  {
    totalSum += CostoCommodities12mesi;
    label = MkLabel( label, "Report [12m]" );
    if( nMonths < 12 )
    {
      nMonths = 12;
    }
  }
  else
  {
      nOptions--;
  }
  
  //////////////////////////////////////
  // Portafogli Personalizzati
  /*if( document.forms['form1'].elements['radioPortafogliPersonalizzati'][0].checked )
  {
    totalSum += CostoPersonalizzati1mese;
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioPortafogliPersonalizzati'][1].checked )
  {
    totalSum += CostoPersonalizzati6mesi;
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioPortafogliPersonalizzati'][2].checked )
  {
    totalSum += CostoPersonalizzati12mesi;
    if( nMonths < 12 )
    {
      nMonths = 12;
    }
  }
  else
  {
      nOptions--;
  }*/

  //////////////////////////////////////
  // Covered Warrant, alias BRUNO MOLTRASIO
  /*if( document.forms['form1'].elements['radioCoveredWarrant'][0].checked )
  {
    totalSum += CostoCWarrant1mese;
    label = MkLabel( label, "DDT [1m]" );
    if( nMonths < 1 )
    {
      nMonths = 1;
    }
  }
  else if( document.forms['form1'].elements['radioCoveredWarrant'][1].checked )
  {
    totalSum += CostoCWarrant6mesi;
    label = MkLabel( label, "DDT [6m]" );
    if( nMonths < 6 )
    {
      nMonths = 6;
    }
  }
  else if( document.forms['form1'].elements['radioCoveredWarrant'][2].checked )
  {
    totalSum += CostoCWarrant12mesi;
    label = MkLabel( label, "DDT [12m]" );
    if( nMonths < 12 )
    {
      nMonths = 12;
    }
  }
  else
  {
      nOptions--;
  }*/


  //////////////////////////////////////
  // Facciamo il totale e calcoliamo lo sconto
  var totalSMS = document.forms['form1'].elements['chkSMS'].checked ? (CostoSMS * nMonths) : 0;
  var totalNoDiscount = totalSum + totalSMS;

  //////////////////////////////////////
  // Sconto sull'acquisto di pił opzioni
  switch( nOptions )
  {
    case 2:
      totalSum -= (totalSum / 100 * 20);    // sconto 20%
      break;
    case 3:
      totalSum -= (totalSum / 100 * 30);    // sconto 30%
      break;
    case 4:
      totalSum -= (totalSum / 100 * 40);    // sconto 40%
      break;
    case 5:
    case 6:
    case 7:
      totalSum -= (totalSum / 100 * 43);    // sconto 43%
      break;
  }

  if( totalSMS > 0 )
  {
    totalSum += totalSMS;
    label = MkLabel( label, "SMS" );
  }

  label = "Abbonamento Cobraf.com: " + label;
  document.forms['buy'].elements['item_name'].value = label;
  document.forms['buy'].elements['amount'].value = Math.round(totalSum);

  var discount = totalNoDiscount-totalSum;
  document.forms['buy'].elements['discount'].value = Math.round(discount);
}
