var utilizzo;
var interessi;
var oneri;
var cms;
var spese;
var speseAn;
var periodo;
var validazione=true;
function INTERESSI(utilizzo, interessi, periodo) {
if (periodo=="999") 
	periodoUt=3;
else 
	periodoUt=periodo;
 return ((interessi * utilizzo * periodoUt / 12) / 100);
}

function ONERI_CMS(utilizzo, cms, spese, speseAn, periodo) {
if (periodo=="999") {
		return (((cms * utilizzo) / 100 ) + ((spese + speseAn)* (1 / 4)));
	}else {
		periodoUt=periodo
		if (periodo > 12){			
			return (((cms * utilizzo) / 100 ) + speseAn *(periodoUt / 12)+ spese);
		}else {
			return (((cms * utilizzo) / 100 ) + ((spese + speseAn)* (periodoUt / 12)));
		}
	}
	
}

function ONERI(utilizzo, oneri, spese, speseAn, periodo) {
if (periodo=="999") {
		return (((((oneri * utilizzo) / 100 )  + speseAn + spese) * (1 / 4)));
	}else {
		periodoUt=periodo;
		if (periodo > 12){			
			return ((((oneri * utilizzo) / 100 )+ speseAn) * (periodoUt / 12)  + spese );
		}else {
			return ((((oneri * utilizzo) / 100 )  + spese + speseAn) * (periodoUt / 12));
		}
	}
 
}

function ONERI_TOT(utilizzo, oneri, cms, spese, speseAn, periodo) {
if (periodo=="999") {
		return (((cms * utilizzo) / 100 ) + ((((oneri * utilizzo) / 100 )  +(spese + speseAn)) * (1 / 4)));
	}else {
		periodoUt=periodo;	
		if (periodo > 12){			
			return ((cms * utilizzo) / 100 ) + (((oneri * utilizzo) / 100 )  + speseAn * (periodoUt / 12)) + spese;
		}else {
			return (((cms * utilizzo) / 100 ) + ((((oneri * utilizzo) / 100 )  +(spese + speseAn)) * (periodoUt / 12)));
		}	 
	}
	
 
}
function ISC(utilizzo, interessi, oneri, periodo) {
if (periodo=="999") 
	periodoUt=3
else 
	periodoUt=periodo
  return (Math.pow((1 + (interessi + oneri) / utilizzo),(12/periodoUt)) - 1);
}
function FMT(numero, decimali) {
  var formato = Math.pow(10, decimali);
  return Math.round(numero * formato) / formato;
}
function FMT_PERC(numero, decimali) {
  var formato = Math.pow(10, decimali);
  return Math.round(numero * formato * 100) / formato;
}
function parsePeriod(valore) {
  patt=/^[0-9]+\,[0-9]+$/;
  if (patt.test(valore)) {
    return valore.replace(",", ".");
  } else {
    return valore;
  }
}
function resetOut() {
	validazione=true;
    document.getElementById('isc_interessi_result').value = "";
    document.getElementById('isc_oneri_result').value = "";
    document.getElementById('isc_result').value = "";
}
function valida() {
  var num;
  utilizzo = document.getElementById('isc_utilizzo').value;
  utilizzo = parsePeriod(utilizzo);
  num = new Number(utilizzo);
  if (isNaN(num)|| num<=0) {
    alert("Il campo 'Accordato' deve essere un importo maggiore di zero.");
    validazione=false;
    return;
  }
  utilizzo = num;
  interessi = document.getElementById('isc_interessi').value;
  interessi = parsePeriod(interessi);
  num = new Number(interessi);
  if (isNaN(num) || (interessi == "")) {
    alert("Il campo 'Tasso debitore' deve essere valorizzato con un numero.");
    validazione=false;
    return;
  }
  interessi = num;
  oneri = document.getElementById('isc_oneri').value;
  oneri = parsePeriod(oneri);
  num = new Number(oneri);
   if (isNaN(num)) {
    alert("Il campo 'Aliquota commissione' deve essere valorizzato con un importo.");
    validazione=false;
    return;
  }
  oneri = num;
  cms = document.getElementById('isc_cms').value;
  cms = parsePeriod(cms);
  num = new Number(cms);
  if (isNaN(num)) {
    alert("Il campo 'Commissione massimo scoperto' deve essere valorizzato con un importo.");
    validazione=false;
    return;
  }
  cms=num;
  spese = document.getElementById('isc_spese').value;
  spese = parsePeriod(spese);
  num = new Number(spese);
  spese = num;
  if (isNaN(num)) {
    alert("Il campo 'Importo altre spese (una tantum)' deve essere valorizzato con un importo.");
    validazione=false;
    return;
  }
  spese=num;
  speseAn = document.getElementById('isc_spese_ann').value;
  speseAn = parsePeriod(speseAn);
  num = new Number(speseAn);
  if (isNaN(num)) {
    alert("Il campo 'importo altre spese (annuali)' deve essere valorizzato con un importo.");
    validazione=false;
    return;
  }  
  speseAn=num;
  periodo = document.getElementById('isc_periodo').value;
  num = new Number(periodo);
  if (isNaN(num)||(num == 0)) {
    alert("Devi selezionare la durata del finanziamento dalla tendina.");
    validazione=false;
    return;
  }

}
function calcolaISC_CMS() {
  var nInteressi = INTERESSI(utilizzo, interessi, periodo);
  var nOneri = ONERI_CMS(utilizzo, cms, spese, speseAn,periodo);
  document.getElementById('isc_interessi_result').value = FMT(nInteressi, 2);
  document.getElementById('isc_oneri_result').value = FMT(nOneri, 2);
  document.getElementById('isc_result').value = FMT_PERC(ISC(utilizzo, nInteressi, nOneri, periodo), 2) + " %";
}

function calcolaISC() {
  var nInteressi = INTERESSI(utilizzo, interessi, periodo);
  var nOneri = ONERI(utilizzo, oneri, spese, speseAn, periodo);
  document.getElementById('isc_interessi_result').value = FMT(nInteressi, 2);
  document.getElementById('isc_oneri_result').value = FMT(nOneri, 2);
  document.getElementById('isc_result').value = FMT_PERC(ISC(utilizzo, nInteressi, nOneri, periodo), 2) + " %";
}
function calcolaISC_TOT() {
  var nInteressi = INTERESSI(utilizzo, interessi, periodo);
  nOneri=ONERI_TOT(utilizzo, oneri, cms, spese, speseAn, periodo);
  document.getElementById('isc_interessi_result').value = FMT(nInteressi, 2);
  document.getElementById('isc_oneri_result').value = FMT(nOneri, 2);
  document.getElementById('isc_result').value = FMT_PERC(ISC(utilizzo, nInteressi, nOneri, periodo), 2) + " %";
}

function calcola_TAEG() {
	var cms = document.getElementById('isc_cms').value;
	cms = parsePeriod(cms);
	var oneri = document.getElementById('isc_oneri').value;
	oneri = parsePeriod(oneri);

		valida();
		if (validazione == true){
			if (!(cms == "") && (oneri == "")){
				calcolaISC_CMS();
			} else {
				if (((cms == "") && !(oneri == ""))||((cms == "") && (oneri == ""))){
			 		calcolaISC();
				}else {
					if (!(cms == "") && !(oneri == "")){
						calcolaISC_TOT();
					}
				}
			}
		}else {
			resetOut();
			return;
		}
	
}

