
// INCREMENTA O DECREMENTA LA CASELLA DEL NUMERO ARTICOLI
function contatore(vAct)
{
	if (vAct == 0) 
	{
		if (parseFloat(document.form.numero.value) > 1) 
			document.form.numero.value = parseFloat(document.form.numero.value)-1;
	}
	if (vAct == 1) document.form.numero.value = parseFloat(document.form.numero.value)+1;
	calcola_potenza();
}

// CALCOLA LA POTENZA EFFETTIVA DI UN COMPONENTE
function calcola_potenza()
{
	var Q_eff = parseFloat(document.form.pot_art.value);
	var N = parseFloat(document.form.numero.value);
	var T = document.form.tipo_art[0].checked;
	var Q;
	Q = Q_eff * N;
	if (T) Q = Q * 1.3;
	document.form.pot_eff.value = Math.round(Q);
}

// ABILITA O DISABILITA LE CASELLE DEI PARAMETRI DELL'ACQUA NEL CALCOLO
function abilita(C)
{
	document.form.pa.disabled = !C.checked;
	document.form.ta.disabled = !C.checked;
}

// APRE LA PAGINA CON LA PROCEDURA DI CALCOLO
function apri_calcolo(tipo)
{
	// Link alla pagina di calcolo
	if (tipo == 1)
	{
		if (document.form.pos[0].checked) tipo = document.form.pos[0].value;
		if (document.form.pos[1].checked) tipo = document.form.pos[1].value;
		lnk = "calcolo2.php?codid1="+document.form.codid1.value+"&acqua="+document.form.acqua.checked+"&pa="+document.form.pa.value+"&ta="+document.form.ta.value+"&gpr="+document.form.gpr.value+"&pos="+tipo;
	}
	else
	{
		lnk = "calcolo2.php?codid1="+document.form.codid1.value+"&acqua=0&pa=0&ta=0&gpr=0&pos=0";
	}
	window.open(lnk,'','top=20, left=20, width=700, height=530, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
}

// APRE LA PAGINA CON IL RISULTATO DEL CALCOLO
function apri_output()
{
	// Link alla pagina di calcolo
	lnk = "output.php?codid1="+document.form.codid1.value;
	window.open(lnk,'','top=20, left=20, width=700, height=530, toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
}

// APRE LA PAGINA DI STAMPA CON IL RISULTATO DEL CALCOLO
function apri_stampa()
{
	// Link alla pagina di calcolo
	lnk = "stampa.php?codid1="+document.form.codid1.value;
	//alert(lnk);
	window.open(lnk,'','top=45, left=45, width=700, height=530, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
}

// APRE LA PAGINA DI INVIO EMAIL PER SAGNALARE IL RISULTATO DEL CALCOLO
function apri_email()
{
	// Link alla pagina di calcolo
	lnk = "email.asp?codid1="+document.form.codid1.value;
	//alert(lnk);
	window.open(lnk,'','top=45, left=45, width=700, height=530, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
}

// APRE LA PAGINA DI DOWNLOAD SCHEDA TECNICA (specificare il prodotto = nprodotto)
function apri_download(nprodotto)
{
	// Link alla pagina di calcolo
	lnk = "download.php?nprodotto="+nprodotto;
	//alert(lnk);
	window.open(lnk,'','top=25, left=25, width=350, height=200, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
}

// APRE UN LINK
function apri_link(vLink)
{
	alert(vLink);
	window.open(vLink,'','top=5, left=5, width=400, height=300, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
}

// CALCOLA LA POTENZA DA DIDSSIPARE IN BASE ALLE TEMPERATURE INSERITE
function calcola_potenza_temp()
{
	var L = parseFloat(document.form.la.value) / 1000;
	var A = parseFloat(document.form.al.value) / 1000;
	var P = parseFloat(document.form.pr.value) / 1000;
	var M = converti_numero(document.form.k_materiale.value);
	var TIMIS = parseFloat(document.form.timis.value);
	var TEMIS = parseFloat(document.form.temis.value);
	var TIMAX = parseFloat(document.form.timax.value);
	var TEMAX = parseFloat(document.form.temax.value);
	var Pos = 0;
	var S = 0;
	var Q = 0;
	for(i=0; i<document.form.posizione.length; i++)
		if (document.form.posizione[i].checked) Pos = i+1;
	switch (Pos)
	{
		case 1:	S = 1.8 * A * (L + P) + 1.4 * L * P; break;
		case 2:	S = 1.4 * L * (A + P) + 1.8 * P * A; break;
		case 3:	S = 1.4 * P * (A + L) + 1.8 * L * A; break;
		case 4:	S = 1.4 * A * (L + P) + 1.4 * L * P; break;
		case 5:	S = 1.8 * L * A + 1.4 * L * P + P * A; break;
		case 6:	S = 1.4 * L * (A + P) + P * A; break;
		case 7:	S = 1.4 * L * A + 0.7 * L * P + P * A; break;
	}
	//Q = S * M * ((TIMIS-TEMIS)+(TEMAX-TIMAX)) * 1.3;
	Q = S * M * ((TIMIS-TEMIS)+(TEMAX-TIMAX));
	if ((TIMIS-TEMIS) > 10)
		Q = Q * 1.3;
	else
		Q = Q * (1.15 + (0.15 / 10) * (TIMIS-TEMIS));
		
	document.form.potenza.value = 0;
	if (!(isNaN(Q))) document.form.potenza.value = Math.round(Q);
	calcola_tinc();
	calcola_potenza_quadro();
}

// CALCOLA LA POTENZA DA DIDSSIPARE IN BASE AI COMPONENTI INSERITI
function calcola_potenza_comp()
{
	var P = converti_numero(document.form.tot_pot_comp.value);
	var Q = converti_numero(document.form.p_quadro.value);
	document.form.potenza.value = 0;
	document.form.potenza.value = Math.round(Q + P);
	calcola_tinc();
	//calcola_potenza_quadro();
}

// CALCOLA LA TEMPERATURA INTERNA AL QUADRO SE NON CI FOSSE NESSUN CONDIZIONATORE
function calcola_tinc()
{
	var L = parseFloat(document.form.la.value) / 1000;
	var A = parseFloat(document.form.al.value) / 1000;
	var P = parseFloat(document.form.pr.value) / 1000;
	var M = converti_numero(document.form.k_materiale.value);
	var TEMAX = parseFloat(document.form.temax.value);
	var SCHEDA = parseFloat(document.form.scheda.value);
	var Pos = 0;
	var S = 0;
	var R = 0;
	for(i=0; i<document.form.posizione.length; i++)
		if (document.form.posizione[i].checked) Pos = i+1;
	switch (Pos)
	{
		case 1:	S = 1.8 * A * (L + P) + 1.4 * L * P; break;
		case 2:	S = 1.4 * L * (A + P) + 1.8 * P * A; break;
		case 3:	S = 1.4 * P * (A + L) + 1.8 * L * A; break;
		case 4:	S = 1.4 * A * (L + P) + 1.4 * L * P; break;
		case 5:	S = 1.8 * L * A + 1.4 * L * P + P * A; break;
		case 6:	S = 1.4 * L * (A + P) + P * A; break;
		case 7:	S = 1.4 * L * A + 0.7 * L * P + P * A; break;
	}
	/*
	if (SCHEDA == 0) {
		var Q = parseFloat(document.form.tot_pot_comp.value);
		R = Math.round(TEMAX + Q / (M * S));
	}
	if (SCHEDA == 1) {
		var TIMIS = parseFloat(document.form.timis.value);
		var TEMIS = parseFloat(document.form.temis.value);
		R = Math.round(TEMAX + (TIMIS - TEMIS));
	}
	*/
	var POT = converti_numero(document.form.potenza.value);
	var QUA = converti_numero(document.form.p_quadro.value);
	R = Math.round(TEMAX + ((POT - QUA) / (M * S * 1.15)));

	document.form.tinc.value = 0;
	document.form.g_tinc.value = 0;
	if ((!(isNaN(R))) && (isFinite(R))) 
	{
		document.form.tinc.value = R;
		document.form.g_tinc.value = R;
	}
	//calcola_potenza_quadro();
}

// CALCOLA LA SUPERFICIE DEL QUANDRO
function calcola_area()
{
	var L = parseFloat(document.form.la.value) / 1000;
	var A = parseFloat(document.form.al.value) / 1000;
	var P = parseFloat(document.form.pr.value) / 1000;
	var S = 0;
	for(i=0; i<document.form.posizione.length; i++)
		if (document.form.posizione[i].checked) Pos = i+1;
	switch (Pos)
	{
		case 1:	S = 1.8 * A * (L + P) + 1.4 * L * P; break;
		case 2:	S = 1.4 * L * (A + P) + 1.8 * P * A; break;
		case 3:	S = 1.4 * P * (A + L) + 1.8 * L * A; break;
		case 4:	S = 1.4 * A * (L + P) + 1.4 * L * P; break;
		case 5:	S = 1.8 * L * A + 1.4 * L * P + P * A; break;
		case 6:	S = 1.4 * L * (A + P) + P * A; break;
		case 7:	S = 1.4 * L * A + 0.7 * L * P + P * A; break;
	}
	document.form.area.value = 0;
	if (!(isNaN(S))) 
	{
		S = S.toString();
		i = S.lastIndexOf(".");
		S = S.substring(0, i + 3)
		document.form.area.value = S;
		document.form.g_area.value = S;
	}
	//calcola_tinc();
}

// RECUPERA IL COEFFICIENTE TERMICO DEL MATERIALE
function get_k_materiale()
{
	for(i=0; i< document.form.materiale.length; i++)
		if (document.form.materiale.options[i].selected == true) str = document.form.materiale.options[i].text;
	u = str.lastIndexOf("[");
	tmp = parseFloat(str.substring(u + 1, str.length - 1));
	document.form.k_materiale.value = 0;
	if (!(isNaN(tmp))) document.form.k_materiale.value = tmp;
	calcola_tinc();
}

// CALCOLA LA POTENZA DI UN COMPONENTA DATA LA DIMENSIONE (TRAMITE L'EQUAZIONE DELLA CURVA DI RESA ESTRATTA DA EXCEL)
function potenza_componente_stan()
{
	//var T = parseFloat(document.form.tipo_stan.value);
	//var x = parseFloat(document.form.taglia.value);
	var T = converti_numero(document.form.tipo_stan.value);
	var x = converti_numero(document.form.taglia.value);
	var y = 0;
	switch (T)
	{
		// INTERRUTTORI DI POTENZA (8)
		case 8: y = 0.25019 * x	- 0.76200;  break;
		// CONVERTITORI (5)
		case 5: y = 25.50551 * x + 23.62362; break;
		// CONTATTORI DI POTENZA (2)
		case 2: y = 1.10903 * x - 0.43614; break;
		// TRASFORMATORI (14)
		case 14: y = 0.05846 * x + 2.38462; break;
		// ALIMENTATORI (1)
		case 1: y = 0.08862 * x + 0.84615; break;
		// FUSIBILI (6)
		case 6: y = 0.07515 * x + 0.04909; break;
		// CONTATTORI AUSILIARI DC (4)
		case 4: y = 7; break;
		// CONTATTORI AUSILIARI AC (3)
		case 3: y = 3; break;
		// INTERRUTTORI DI COMANDO (7)
		case 7: y = 5; break;
		// INTERRUTTORI DI PROTEZIONE (9)
		case 9:
			if (x <= 24) y = 0; 
			if ((x > 24) && (x < 80)) y = 4;
			if (x >= 80) y =	11;
			break;
		// INVERTER (10)
		case 10: y = 50 * x; break;
		// PLC (11)
		case 11: y = 2.5; break;
		// RESISTENZE (12)
		case 12: y = x; break;
		// SPIE LUMINOSE (13)
		case 13: y = x; break;
	}
	document.form.pot_art.value = Math.round(y); 
	calcola_potenza();
}

// RECUPERA L'UNITA' DI MISURA DEL TIPO COMPONENTE
function potenza_componente_pers()
{
	var tmp = "";
	document.form.tipo_art[1].checked = true;
	document.form.tipo_stan.value = 0;
	for(i=0; i< document.form.tipo_pers.length; i++)
		if (document.form.tipo_pers.options[i].selected == true) str = document.form.tipo_pers.options[i].text;
	u = str.lastIndexOf("[");
	if (u > 0) tmp = str.substring(u + 1, str.length - 1);
	document.form.pot_art.value = tmp;
	calcola_potenza();
}

// RECUPERA L'UNITA' DI MISURA DEL TIPO COMPONENTE
function get_um_gruppo()
{
	document.form.tipo_art[0].checked = true;
	document.form.tipo_pers.value = 0;
	document.form.um.value = document.form.tipo_stan.value;
	for(i=0; i< document.form.um.length; i++)
		if (document.form.um.options[i].selected == true) document.form.umd.value = document.form.um.options[i].text;
	document.form.taglia.value = 0;
	document.form.taglia.focus();
	document.form.taglia.select();
	potenza_componente_stan();
}

// COSTRUISCE UNA DESCRIZIONE DELL'ARTICOLO
function get_descr_art()
{
	// Componente standard
	if (document.form.tipo_art[0].checked)
	{
		for(i=0; i< document.form.tipo_stan.length; i++)
			if (document.form.tipo_stan.options[i].selected == true) tmp = document.form.tipo_stan.options[i].text;
		tmp = tmp+' '+document.form.taglia.value;
		for(i=0; i< document.form.um.length; i++)
			if (document.form.um.options[i].selected == true) tmp = tmp+' '+document.form.um.options[i].text;
	}
	// Componente personalizzato
	if (document.form.tipo_art[1].checked)
	{
		for(i=0; i< document.form.tipo_pers.length; i++)
			if (document.form.tipo_pers.options[i].selected == true) tmp = document.form.tipo_pers.options[i].text;
		//u = tmp.lastIndexOf("[");
		//if (u > 0) tmp = str.substring(u + 1, str.length - 1);
		//document.form.pot_art.value = tmp;
	}
	document.form.descr_art.value = tmp;
} 

// CONTROLLA IL VALORE INSERITO DELLA TEMAX
function controlla_temax()
{
	var lang = parseFloat(window.document.form.lang.value);
	var t = document.form.temax.value;
	if (t > 70) 
	{
		if (lang == 0) alert("Attenzione, la temperatura esterna massima consentita è 70 °C!");
		if (lang == 1) alert("Attention, max external temperature allowed is 70 °C!");
		document.form.temax.value = 70;
	}
	else if (t < -10) 
	{
		if (lang == 0) alert("Attenzione, la temperatura esterna minima consentita è -10 °C!");
		if (lang == 1) alert("Attention, min external temperature allowed is -10 °C!");
		document.form.temax.value = -10;
	}
	else if (t > 50) 
	{
		if (lang == 0) alert("Attenzione, per alcuni prodotti non è garantito il funzionamento con temperatura esterna maggiore di 50 °C!");
		if (lang == 1) alert("Attention, some products couldn't work with external temperature that exceeds 50 °C!");
	}
}

// CONTROLLA IL VALORE INSERITO DELLA TIMAX
function controlla_timax()
{
	var lang = parseFloat(window.document.form.lang.value);
	var t = document.form.timax.value;
	if (t > 70) 
	{
		if (lang == 0) alert("Attenzione, la temperatura interna massima consentita è 70 °C!");
		if (lang == 1) alert("Attention, max internal temperature allowed is 70 °C!");
		document.form.timax.value = 70;
	}
	else if (t < 20) 
	{
		if (lang == 0) alert("Attenzione, la temperatura interna minima consentita è 20 °C!");
		if (lang == 1) alert("Attention, min internal temperature allowed is 20 °C!");
		document.form.timax.value = 20;
	}
}

// CONTROLLA I VALORI INSERITI NEL CALCOLO CON LE TEMPERATURE
function controlla_temperature()
{
	var lang = parseFloat(window.document.form.lang.value);
	var t1 = parseFloat(document.form.temis.value);
	var t2 = parseFloat(document.form.timis.value);
	if (t1 > t2) 
	{
		if (lang == 0) alert("Attenzione, la temperatura interna deve essere maggiore di quella esterna!");
		if (lang == 1) alert("Attention, internal temperature must be largest than external!");
		document.form.timis.value = document.form.temis.value;
	}
}

// CALCOLA LA POTENZA TERMICA DISSIPATA DAL QUADRO
function calcola_potenza_quadro()
{
	var K = converti_numero(document.form.k_materiale.value);
	var S = parseFloat(document.form.area.value);
	var TIMAX = parseFloat(document.form.timax.value);
	var TEMAX = parseFloat(document.form.temax.value);
	var SCHEDA = parseFloat(document.form.scheda.value);
	var p = 0;
	//alert(S);
	document.form.p_quadro.value = p;
	p = (TEMAX - TIMAX) * K * S;
	p = p * 1.15;
	if (!(isNaN(p))) document.form.p_quadro.value = Math.round(p);
	//if (SCHEDA == 0) document.form.potenza.value = Math.round(parseFloat(document.form.tot_pot_comp.value) + p);
}

// CONVERTE UN VALORE STRINGA IN UN NUMERO
function converti_numero(str)
{
	var lang = parseFloat(window.document.form.lang.value);
	// Sostituisce la virgola con il punto come separatore decimale
	str = str.replace(",",".");
	// Trasforma la stringa ottenuta in numero
	str = parseFloat(str);
	if (isNaN(str)) 
	{
		if (lang == 0) alert("Attenzione! Inserire un valore numerico!");
		if (lang == 1) alert("Attention! Insert numeric value!");
		return 0;
	}
	// Restituisce il valore numerico
	return str;
}

function numero_positivo(numero)
{
	n = converti_numero(numero);
	if (n < 0) n = n * (-1);
	return n;
}