// CALCOLA RISPARMIO: calcola il risparmio prodotti dai PC Aciva rispetto a quelli tradizionali
function calcolaRisparmio()
{	
	var valore = "";
	
	// azzera messaggi di errore
	document.getElementById("errori_foglioCalcolo").innerHTML = "";
	
	// leggi dati essenziali
	var numeroOreLavorative = Number(document.getElementById("numeroOreLavorative").value.replace(/,/, "."));
	var numeroGiorniLavorativi = Number(document.getElementById("numeroGiorniLavorativi").value.replace(/,/, "."));
	var numeroPcInstallati = Number(document.getElementById("numeroPcInstallati").value.replace(/,/, "."));
	var costoKw = Number(document.getElementById("costoKw").value.replace(/,/, "."));
	
	// aggiorna costo kilowatt
	document.getElementById("costoKw").value = (String(Math.round(costoKw * 100) / 100)).replace(".", ",");
	if ((document.getElementById("costoKw").value.indexOf(",")  + 1) == (document.getElementById("costoKw").value.length - 1))
	{ document.getElementById("costoKw").value = document.getElementById("costoKw").value + "0"; }
	
	if ((numeroOreLavorative > 0 && numeroOreLavorative < 25) && (numeroGiorniLavorativi > 0 && numeroGiorniLavorativi < 365) && (numeroPcInstallati > 0))
	{
		// scrivi consumo annuo Activa acceso
		var consumoMedioActiva = Number(document.getElementById("consumoMedioActiva").value.replace(/,/, "."));
		var consumoAnnuoActiva = (numeroOreLavorative * numeroGiorniLavorativi * consumoMedioActiva) / 1000;
		document.getElementById("consumoAnnuoActiva").value = (String(Math.round(consumoAnnuoActiva * 100) / 100)).replace(".", ",");
		if ((document.getElementById("consumoAnnuoActiva").value.indexOf(",")  + 1) == (document.getElementById("consumoAnnuoActiva").value.length - 1))
		{ document.getElementById("consumoAnnuoActiva").value = document.getElementById("consumoAnnuoActiva").value + "0"; }
		
		// scrivi consumo annuo Activa stand by 
		var consumoMedioStandbyActiva = Number(document.getElementById("consumoMedioStandbyActiva").value.replace(/,/, "."));
		var consumoAnnuoStandbyActiva = ((((365 - numeroGiorniLavorativi) * 24) + ((24 - numeroOreLavorative) * numeroGiorniLavorativi)) * consumoMedioStandbyActiva) / 1000;
		document.getElementById("consumoAnnuoStandbyActiva").value = String(Math.round(consumoAnnuoStandbyActiva * 100) / 100).replace(".", ",");
		if ((document.getElementById("consumoAnnuoStandbyActiva").value.indexOf(",")  + 1) == (document.getElementById("consumoAnnuoStandbyActiva").value.length - 1))
		{ document.getElementById("consumoAnnuoStandbyActiva").value = document.getElementById("consumoAnnuoStandbyActiva").value + "0"; }		
		
		// scrivi totale consumo Activa
		var totaleConsumoActiva = consumoAnnuoStandbyActiva + consumoAnnuoActiva;
		document.getElementById("totaleConsumoActiva").value = String(Math.round(totaleConsumoActiva * 100) / 100).replace(".", ",");
		if ((document.getElementById("totaleConsumoActiva").value.indexOf(",")  + 1) == (document.getElementById("totaleConsumoActiva").value.length - 1))
		{ document.getElementById("totaleConsumoActiva").value = document.getElementById("totaleConsumoActiva").value + "0"; }
		
		// scrivi consumo annuo Pc acceso
		var consumoMedioPc = Number(document.getElementById("consumoMedioPc").value.replace(/,/, "."));
		var consumoAnnuoPc = (numeroOreLavorative * numeroGiorniLavorativi * consumoMedioPc) / 1000;
		document.getElementById("consumoAnnuoPc").value = String(Math.round(consumoAnnuoPc * 100) / 100).replace(".", ",");
		if ((document.getElementById("consumoAnnuoPc").value.indexOf(",")  + 1) == (document.getElementById("consumoAnnuoPc").value.length - 1))
		{ document.getElementById("consumoAnnuoPc").value = document.getElementById("consumoAnnuoPc").value + "0"; }
		
		// scrivi consumo annuo Pc stand by 
		var consumoMedioStandbyPc = Number(document.getElementById("consumoMedioStandbyPc").value.replace(/,/, "."));
		var consumoAnnuoStandbyPc = ((((365 - numeroGiorniLavorativi) * 24) + ((24 - numeroOreLavorative) * numeroGiorniLavorativi)) * consumoMedioStandbyPc) / 1000;
		document.getElementById("consumoAnnuoStandbyPc").value = String(Math.round(consumoAnnuoStandbyPc * 100) / 100).replace(".", ",");
		if ((document.getElementById("consumoAnnuoStandbyPc").value.indexOf(",")  + 1) == (document.getElementById("consumoAnnuoStandbyPc").value.length - 1))
		{ document.getElementById("consumoAnnuoStandbyPc").value = document.getElementById("consumoAnnuoStandbyPc").value + "0"; }
		
		// scrivi totale consumo Pc
		var totaleConsumoPc = consumoAnnuoStandbyPc + consumoAnnuoPc;
		document.getElementById("totaleConsumoPc").value = String(Math.round(totaleConsumoPc * 100) / 100).replace(".", ",");
		if ((document.getElementById("totaleConsumoPc").value.indexOf(",")  + 1) == (document.getElementById("totaleConsumoPc").value.length - 1))
		{ document.getElementById("totaleConsumoPc").value = document.getElementById("totaleConsumoPc").value + "0"; }
	
		// scrivi totali	
		var costoTotaleConsumiActiva = (totaleConsumoActiva * costoKw) * numeroPcInstallati;
		document.getElementById("costoTotaleConsumiActiva").value = String(Math.round(costoTotaleConsumiActiva * 100) / 100).replace(".", ",");
		if ((document.getElementById("costoTotaleConsumiActiva").value.indexOf(",")  + 1) == (document.getElementById("costoTotaleConsumiActiva").value.length - 1))
		{ document.getElementById("costoTotaleConsumiActiva").value = document.getElementById("costoTotaleConsumiActiva").value + "0"; }
		
		var costoTotaleConsumiPc = (totaleConsumoPc * costoKw) * numeroPcInstallati;
		document.getElementById("costoTotaleConsumiPc").value = String(Math.round(costoTotaleConsumiPc * 100) / 100).replace(".", ",");
		if ((document.getElementById("costoTotaleConsumiPc").value.indexOf(",")  + 1) == (document.getElementById("costoTotaleConsumiPc").value.length - 1))
		{ document.getElementById("costoTotaleConsumiPc").value = document.getElementById("costoTotaleConsumiPc").value + "0"; }
		
		// scrivi risparmio e percentuali
		var risparmio = costoTotaleConsumiPc - costoTotaleConsumiActiva;
		document.getElementById("risparmio").value = String(Math.round(risparmio * 100) / 100).replace(".", ",");
		if ((document.getElementById("risparmio").value.indexOf(",")  + 1) == (document.getElementById("risparmio").value.length - 1))
		{ document.getElementById("risparmio").value = document.getElementById("risparmio").value + "0"; }
		
		var percentuale = (risparmio * 100) / costoTotaleConsumiPc;
		document.getElementById("percentuale").value = String(Math.round(percentuale * 100) / 100).replace(".", ",");
		if ((document.getElementById("percentuale").value.indexOf(",")  + 1) == (document.getElementById("percentuale").value.length - 1))
		{ document.getElementById("percentuale").value = document.getElementById("percentuale").value + "0"; }
	}

	// controllo correttezza dati inseriti
	if (!(numeroOreLavorative > 0 && numeroOreLavorative < 25))
	{
		document.getElementById("errori_foglioCalcolo").innerHTML = document.getElementById("errori_foglioCalcolo").innerHTML + "<p><strong>Attenzione</strong>! Il numero di ore lavorative deve essere compreso tra 0 e 24.</p>"
	}
	if (!(numeroGiorniLavorativi > 0 && numeroGiorniLavorativi < 365))
	{
		document.getElementById("errori_foglioCalcolo").innerHTML = document.getElementById("errori_foglioCalcolo").innerHTML + "<p><strong>Attenzione</strong>! Il numero di giorni lavorativi deve essere compreso tra 0 e 365.</p>"
	}	
	if (!(numeroPcInstallati > 0))
	{
		document.getElementById("errori_foglioCalcolo").innerHTML = document.getElementById("errori_foglioCalcolo").innerHTML + "<p><strong>Attenzione</strong>! Il numero di PC installati deve essere maggiore di 0.</p>"
	}
	
}