/* 
 * Funzione che permette di sostituire parte di una stringa con una nuova stringa.
 */
function ReplaceSubString(Stringa, StringaDaCercare, StringaDaSostituire) {
	temp = "" + Stringa;
	while (temp.indexOf(StringaDaCercare)>-1) {
		pos= temp.indexOf(StringaDaCercare);
		temp = "" + (temp.substring(0, pos) + StringaDaSostituire + temp.substring((pos + StringaDaCercare.length), temp.length));
	}
	return temp;
}


function verificaNumero(fForm, nomeCampo){
	var sNum = fForm.elements[nomeCampo].value;
	if(sNum != ""){
		sNum = ReplaceSubString(sNum, ".", "");
		var pattern =/\b,\b/ig;						// Individua il carattere ","
		var sVal = sNum.replace(pattern,".");		// Lo sostituisce con un "."
		var iNum = Number(sVal);
	
		if(isNaN(iNum) == true){
			alert('E\' necessario specificare un valore numerico');
			return null;
			fForm.elements[nomeCampo].focus();
		}
	}
	return iNum;
}

function ricalcola(){
	var fForm = document.forms['calcolo'];

	var spes_for = 0;
	var bar = 0;
	var dis_ruo_for = 0;
	var bar1 = 0;
	var spes1 = 0;
	var bar2 = 0;
	var bar_carico = 0;
	var spes2 = 0;
	var portata = 0;
	var peso1 = 0;
	var peso2 = 0;
	
	var valore1 = verificaNumero(fForm, "spes_for");
	if(valore1 != null){
		spes_for = Number(valore1);
	}
	
	
	valore1 = verificaNumero(fForm, "bar");
	
	if(valore1 != null){
		bar = Number(valore1);
	}

	valore1 = verificaNumero(fForm, "dis_ruo_for");
	if(valore1 != null){
		dis_ruo_for = Number(valore1);
	}

	valore1 = verificaNumero(fForm, "bar1");
	if(valore1 != null){
		bar1 = Number(valore1);
	}

	valore1 = verificaNumero(fForm, "spes1");
	if(valore1 != null){
		spes1 = Number(valore1);
	}
	
	valore1 = verificaNumero(fForm, "bar2");
	if(valore1 != null){
		bar2 = Number(valore1);
	}

	valore1 = verificaNumero(fForm, "spes2");
	if(valore1 != null){
		spes2 = Number(valore1);
	}
	
	valore1 = verificaNumero(fForm, "bar_carico");
	if(valore1 != null){
		bar_carico = Number(valore1);
	}
	
	valore1 = verificaNumero(fForm, "portata");
	if(valore1 != null){
		portata = Number(valore1);
	}
	
	valore1 = verificaNumero(fForm, "peso1");
	if(valore1 != null){
		peso1 = Number(valore1);
	}

	valore1 = verificaNumero(fForm, "peso2");
	if(valore1 != null){
		peso2 = Number(valore1);
	}
	
	var dis_ruo_carr = dis_ruo_for + bar;
	fForm.dis_ruo_carr.value = dis_ruo_carr;
		
	var dis_ruo_att1 = dis_ruo_for - spes_for + bar1;
	fForm.dis_ruo_att1.value = dis_ruo_att1;
		
	var dis_ruo_att2 = dis_ruo_for + spes1 + bar2;
	fForm.dis_ruo_att2.value = dis_ruo_att2;
		
	var dis_ruo_carico = dis_ruo_for - spes_for + spes1 + bar_carico + spes2;
	fForm.dis_ruo_carico.value = dis_ruo_carico;
	
	
	fForm.bar_carico2.value = bar_carico;
	
	var port_res = 0;
	
	if(dis_ruo_carico != 0){
		port_res = (portata * dis_ruo_carr) - (peso1 * dis_ruo_att1) - (peso2 * dis_ruo_att2);
		port_res = port_res / dis_ruo_carico;
	}
	
	fForm.port_res.value = parseInt(port_res);
}	

function caricaPagina(){
	document.forms['calcolo'].portata.focus();
}

function stampapagina() {
	var IDicostampa = eval("iconastampa");
	IDicostampa.style.display = "none";	
	window.print();
	IDicostampa.style.display = "";

}	

