// JavaScript Document
// Is numeric test
function is_numeric(val){
	return (val == parseFloat(val));
}

// Validate form
function validate_form(){
	var has_points;
	var error_flag = 0;
	var error_msg = 'Si \u00E8 verificato un errore:\n';

	// Check affidamento
	var affidamento = document.getElementById('affidamento').value;
	has_points = affidamento.indexOf('.') >= 0;
	affidamento = affidamento.replace(",", ".");
	if (has_points || !is_numeric(affidamento) || affidamento < 0){
		error_flag = 1;
		error_msg = error_msg + '- Importo affidamento accordato deve essere un numero e non pu\u00F2 essere negativo. Per favore utilizza la virgola per i decimali.\n';
	}

	// Check tasso_debitore
	var tasso_debitore = document.getElementById('tasso_debitore').value;
	has_points = tasso_debitore.indexOf('.') >= 0;
	tasso_debitore = tasso_debitore.replace(",", ".");
	if (has_points || !is_numeric(tasso_debitore) || tasso_debitore < 0 || tasso_debitore > 15.75){
		error_flag = 1;
		error_msg = error_msg + '- Tasso debitore deve essere un numero, non pu\u00F2 essere negativo o superiore a 15.75%. Per favore utilizza la virgola per i decimali.\n';
	}

	// Check aliquota_cmds
	var aliquota_cmds = document.getElementById('aliquota_cmds').value;
	has_points = aliquota_cmds.indexOf('.') >= 0;
	aliquota_cmds = aliquota_cmds.replace(",", ".");
	if (has_points || !is_numeric(aliquota_cmds) || aliquota_cmds < 0 || aliquota_cmds > 2){
		error_flag = 1;
		error_msg = error_msg + '- Aliquota CMDS deve essere un numero, non pu\u00F2 essere negativo o superiore a 2%. Per favore utilizza la virgola per i decimali.\n';
	}

	// Check importo_altre_spese
	var importo_altre_spese = document.getElementById('importo_altre_spese').value;
	has_points = importo_altre_spese.indexOf('.') >= 0;
	importo_altre_spese = importo_altre_spese.replace(",", ".");
	if (has_points || !is_numeric(importo_altre_spese) || importo_altre_spese < 0){
		error_flag = 1;
		error_msg = error_msg + '- Importo altre spese deve essere un numero e non pu\u00F2 essere negativo. Per favore utilizza la virgola per i decimali.\n';
	}

	// Return
	if (error_flag){
		alert(error_msg);		
		return false;
	}
	
	return true;
}


function calculate(){
	// Validate form
	if (!validate_form()) return;

	// Get values
	var affidamento = parseFloat(document.getElementById('affidamento').value.replace(",", "."));
	var tasso_debitore = parseFloat(document.getElementById('tasso_debitore').value.replace(",", ".")) / 100;
	var aliquota_cmds = parseFloat(document.getElementById('aliquota_cmds').value.replace(",", ".")) / 100;
	var importo_altre_spese = parseFloat(document.getElementById('importo_altre_spese').value.replace(",", "."));

	// Calculate
	var importo_interessi = (affidamento == 0)? 0 : (Math.pow(1+(Math.pow(1+tasso_debitore/(12/3), 12/3)-1), 3/12)-1) * affidamento;
	var importo_commissione = (affidamento * aliquota_cmds + importo_altre_spese)/4;
	var isc = (affidamento == 0)? 0 : (Math.pow((affidamento + importo_interessi + importo_commissione) / affidamento, 12/3)-1) * 100;

	// Set new values
	document.getElementById('importo_interessi').value = importo_interessi.toFixed(2);
	document.getElementById('importo_commissione').value = importo_commissione.toFixed(2);
	document.getElementById('isc').value = isc.toFixed(2) + ' %';
}
