var configStarted = false;

function UpdateCluster()
{
	var CORE_PER_CPU = 4;
	var MAX_SWITCH_GE = 4;
	var MAX_SWITCH_10GE = 6;
	var MAX_SWITCH_FC = 6;
	var MAX_NODES = 42;
	
	var dimByNodes = document.getElementById("1-Nodi").checked;
	var nodeNum = parseInt(document.getElementById("1-Nodi-numero").value);
	var dimByProcs = document.getElementById("1-Core").checked;
	var procNum = parseInt(document.getElementById("1-Core-numero").value);
	
	var maxUnits = MAX_NODES;
	
	var withFrontend = document.getElementById("3-FrontEnd-Si").checked;
	var frontendUnits = parseInt(document.getElementById("3-Chassis").value);
	var ramFrontend = document.getElementById("3-RAM").value;
	var diskDimFrontend = document.getElementById("3-DimDisco").value;
	var diskNumFrontend = document.getElementById("3-NumDisco").value;
	
	var nodeType = document.getElementById("4-Tipo").value;
	var ramPerNode = document.getElementById("4-RAM").value;
	var diskDimPerNode = document.getElementById("4-DimDisco").value;
	var diskNumPerNode = document.getElementById("4-NumDisco").value;

	var switchGE = document.getElementById("5-NetworkingGiga").checked;
	var connNumGE = parseInt(document.getElementById("5-GigaConnessioni").value);

	var withDedicatedNetwork = document.getElementById("5-NetworkingDedicato").checked;
	var dedicatedNetworkType = document.getElementById("5-DedicatoTipo").value;

	var withStorage = document.getElementById("6-Storage").checked;
	
	var withConsolle = document.getElementById("7-Consolle").checked;
	var consolleType = document.getElementById("7-Tipo").value;
	var withSwitchKvm = document.getElementById("7-Switch").checked;
	
	var withUps = document.getElementById("8-Ups").checked;
	var upsType = document.getElementById("8-Tipo").value;
	
	var units = 0;

	// Aggiorna le opzioni per il taglio degli hard-disk in base al tipo (SARA o SAS/SCSI)
	if (document.getElementById("3-TipoDisco").value == "SATA")
	{
		dimDisco = document.getElementById("3-DimDisco");
		dimDisco.options[0].text = "250";
		dimDisco.options[1].text = "500";
		dimDisco.options[2].text = "750";
		dimDisco.options[3].text = "1000";
	}
	else
	{
		dimDisco = document.getElementById("3-DimDisco");
		dimDisco.options[0].text = "75";
		dimDisco.options[1].text = "147";
		dimDisco.options[2].text = "300";
		dimDisco.options[3].text = "450";
	}

	// Aggiorna le opzioni per il taglio degli hard-disk in base al tipo (SARA o SAS/SCSI)
	if (document.getElementById("4-TipoDisco").value == "SATA")
	{
		dimDisco = document.getElementById("4-DimDisco");
		dimDisco.options[0].text = "250";
		dimDisco.options[1].text = "500";
		dimDisco.options[2].text = "750";
		dimDisco.options[3].text = "1000";
	}
	else
	{
		dimDisco = document.getElementById("4-DimDisco");
		dimDisco.options[0].text = "75";
		dimDisco.options[1].text = "147";
		dimDisco.options[2].text = "300";
		dimDisco.options[3].text = "450";
	}
	
	// Indica a init.js di jquery che è iniziata una configurazione, e anche se l'utente torna
	// alla prima schermata bisogna chiedere conferma se abbandona la pagina
	configStarted = true;
	
	// Visualizza l'armadio rack
	if (document.getElementById("2-Rack12").checked == true)
	{
		document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/12U_left.jpg) bottom";
		document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/12U_right.jpg) bottom";
		if (navigator.appVersion.indexOf("MSIE 6.0") > 0)
			document.getElementById("e_spacer").style.height = "105px";
		else
			document.getElementById("e_spacer").style.height = "106px";
		maxUnits = 12;
	}
	else if (document.getElementById("2-Rack25").checked == true)
	{
		document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/25U_left.jpg) bottom";
		document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/25U_right.jpg) bottom";
		if (navigator.appVersion.indexOf("MSIE 6.0") > 0)
			document.getElementById("e_spacer").style.height = "105px";
		else
			document.getElementById("e_spacer").style.height = "106px";
		maxUnits = 25;
	}
	else if (document.getElementById("2-Rack42").checked == true)
	{
		document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left.jpg) bottom";
		document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/right.jpg) bottom";
		if (navigator.appVersion.indexOf("MSIE 6.0") > 0)
			document.getElementById("e_spacer").style.height = "84px";
		else
			document.getElementById("e_spacer").style.height = "85px";
		maxUnits = 42;
	}
	else
	{
		document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left_trasp.jpg) bottom";
		document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/right_trasp.jpg) bottom";
		document.getElementById("e_spacer").style.height = "85px";
		maxUnits = 42;
	}
	
	// Stabilisce il numero di nodi richiesti
	if (dimByProcs == true)
	{
		if (nodeType == "1" || nodeType == "4")
			nodeNum = procNum / (2 * CORE_PER_CPU);
		else
			nodeNum = procNum / (4 * CORE_PER_CPU);
	}
	else
	{
		if (nodeType == "1" || nodeType == "4")
			procNum = nodeNum * (2 * CORE_PER_CPU);
		else
			procNum = nodeNum * (4 * CORE_PER_CPU);
	}
	
	// Visualizza il frontend
	if (withFrontend)
	{
		if (frontendUnits == 1)
		{
			document.getElementById("Frontend1U").style.display = "block";
			document.getElementById("Frontend2U").style.display = "none";
		}
		else
		{
			document.getElementById("Frontend1U").style.display = "none";
			document.getElementById("Frontend2U").style.display = "block";
		}
		units += frontendUnits;
	}
	else
	{
		document.getElementById("Frontend1U").style.display = "none";
		document.getElementById("Frontend2U").style.display = "none";
	}
	
	// Visualizza i nodi di calcolo
	for (i = 1; i <= nodeNum; ++i)
	{
		if (units < MAX_NODES)
		{
			if (units < maxUnits)
			{
				document.getElementById("Nodo" + i).style.display = "block";
				if (nodeType == "3")
				{
					//document.getElementById("Nodo" + i).style.display = "none";
					//document.getElementById("Nodo" + i).className = "";
					document.getElementById("Nodo" + i).className = "twin1U";
				}
				else if (nodeType == "1" || nodeType == "2")
				{
					//document.getElementById("Nodo" + i).className = "nodo1U";
					document.getElementById("Nodo" + i).className = "nodo1U";
				}
				else if (nodeType == "4" || nodeType == "5")
					document.getElementById("Nodo" + i).className = "nodo2U";
			}
			else
				document.getElementById("Nodo" + i).style.display = "none";
		}
		
		if (nodeType == "1" || nodeType == "2" || nodeType == "3")
			units += 1;
		else
			units += 2;
	}
	for (i = nodeNum + 1; i <= MAX_NODES; ++i)
		document.getElementById("Nodo" + i).style.display = "none";
	
	// Visualizza lo switch GigaEthernet
	if (switchGE)
	{
		// Numero di nodi totali, contando anche il front-end
		if (withFrontend)
			hostNum = nodeNum + 1;
		else
			hostNum = nodeNum;
		
		// Numero di nodi totali nel caso di server twin
		if (nodeType == "3")
			hostNum *= 2;

		if (hostNum <= 48 / connNumGE)
			numSwitch = 1;
		else
		{
			numSwitch = (hostNum*connNumGE - 1) / 42 + 1;
			if (numSwitch > MAX_SWITCH_GE)
				numSwitch = MAX_SWITCH_GE;
		}

		for (i = 1; i <= MAX_SWITCH_GE; ++i)
		{
			if (i <= numSwitch)
				document.getElementById("SwitchGE" + i).style.display = "block";
			else
				document.getElementById("SwitchGE" + i).style.display = "none";
		}
		
		units += numSwitch;
	}
	else
	{
		for (i = 1; i <= MAX_SWITCH_GE; ++i)
			document.getElementById("SwitchGE" + i).style.display = "none";
	}
	
	// Visualizza lo switch per il networking dedicato
	if (withDedicatedNetwork)
	{
		// Numero di nodi totali nel caso di server twin
		hostNum = nodeNum;
		if (nodeType == "3")
			hostNum *= 2;
		
		if (dedicatedNetworkType == "10gbps")
		{
			if (hostNum <= 20)
				numSwitch = 1;
			else
			{
				numSwitch = (hostNum-1) / 16 + 1;
				if (numSwitch > MAX_SWITCH_10GE)
					numSwitch = MAX_SWITCH_10GE;
			}
			
			for (i = 1; i <= MAX_SWITCH_10GE; ++i)
			{
				if (i <= numSwitch)
					document.getElementById("Switch10GE" + i).style.display = "block";
				else
					document.getElementById("Switch10GE" + i).style.display = "none";
			}
			
			units += numSwitch;
		
			document.getElementById("SwitchIB1U").style.display = "none";
			document.getElementById("SwitchIB4U").style.display = "none";
			document.getElementById("SwitchIB7U").style.display = "none";
			for (i = 1; i <= MAX_SWITCH_FC; ++i)
				document.getElementById("SwitchFC" + i).style.display = "none";
		}
		else if (dedicatedNetworkType == "ib")
		{
			if (hostNum <= 24)
			{
				document.getElementById("SwitchIB1U").style.display = "block";
				document.getElementById("SwitchIB4U").style.display = "none";
				document.getElementById("SwitchIB7U").style.display = "none";
				units += 1;
			}
			else if (hostNum > 24 && hostNum <= 48)
			{
				document.getElementById("SwitchIB1U").style.display = "none";
				document.getElementById("SwitchIB4U").style.display = "block";
				document.getElementById("SwitchIB7U").style.display = "none";
				units += 4;
			}
			else
			{
				document.getElementById("SwitchIB1U").style.display = "none";
				document.getElementById("SwitchIB4U").style.display = "none";
				document.getElementById("SwitchIB7U").style.display = "block";
				units += 7;
			}
		
			for (i = 1; i <= MAX_SWITCH_10GE; ++i)
				document.getElementById("Switch10GE" + i).style.display = "none";
			for (i = 1; i <= MAX_SWITCH_FC; ++i)
				document.getElementById("SwitchFC" + i).style.display = "none";
		}
		else
		{
			if (hostNum <= 20)
				numSwitch = 1;
			else
			{
				numSwitch = (hostNum-1) / 16 + 1;
				if (numSwitch > MAX_SWITCH_FC)
					numSwitch = MAX_SWITCH_FC;
			}
			
			for (i = 1; i <= MAX_SWITCH_FC; ++i)
			{
				if (i <= numSwitch)
					document.getElementById("SwitchFC" + i).style.display = "block";
				else
					document.getElementById("SwitchFC" + i).style.display = "none";
			}
			
			units += numSwitch;
		
			for (i = 1; i <= MAX_SWITCH_10GE; ++i)
				document.getElementById("Switch10GE" + i).style.display = "none";
			document.getElementById("SwitchIB1U").style.display = "none";
			document.getElementById("SwitchIB4U").style.display = "none";
			document.getElementById("SwitchIB7U").style.display = "none";
		}
	}
	else
	{
		for (i = 1; i <= MAX_SWITCH_10GE; ++i)
			document.getElementById("Switch10GE" + i).style.display = "none";
		document.getElementById("SwitchIB1U").style.display = "none";
		document.getElementById("SwitchIB4U").style.display = "none";
		document.getElementById("SwitchIB7U").style.display = "none";
		for (i = 1; i <= MAX_SWITCH_FC; ++i)
			document.getElementById("SwitchFC" + i).style.display = "none";
	}
	
	// Visualizza lo storage
	if (withStorage)
	{
		document.getElementById("Storage").style.display = "block";
		units += 2;
	}
	else
		document.getElementById("Storage").style.display = "none";

	// Visualizza la consolle
	if (withConsolle)
	{
		if (consolleType == "ripiano")
		{
			if (maxUnits == 12)
			{
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/12U_right_cons.jpg) bottom";
				document.getElementById("Consolle10U").style.display = "none";
			}
			else if (maxUnits == 25)
			{
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/25U_right_cons.jpg) bottom";
				document.getElementById("Consolle10U").style.display = "none";
			}
			else
			{
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/right.jpg) bottom";
				document.getElementById("Consolle10U").style.display = "block";
				units += 10;
			}
			document.getElementById("Consolle1U").style.display = "none";
		}
		else if (consolleType == "esterna")
		{
			document.getElementById("Consolle1U").style.display = "none";
			document.getElementById("Consolle10U").style.display = "none";
			if (maxUnits == 12)
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/12U_right.jpg) bottom";
			else if (maxUnits == 25)
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/25U_right.jpg) bottom";
			document.getElementById('message').innerHTML = "Consolle esterna.";
			fadeIn('e_msg_div', 1000);
		}
		else
		{
			document.getElementById("Consolle1U").style.display = "block";
			document.getElementById("Consolle10U").style.display = "none";
			if (maxUnits == 12)
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/12U_right.jpg) bottom";
			else if (maxUnits == 25)
				document.getElementById("e_right2").style.background = "url(images/cluster_espresso/rack/25U_right.jpg) bottom";
			units += 1;
		}
	}
	else
	{
		document.getElementById("Consolle1U").style.display = "none";
		document.getElementById("Consolle10U").style.display = "none";
	}

	// Visualizza lo switch KVM
	if (withSwitchKvm)
	{
		document.getElementById("SwitchKVM").style.display = "block";
		units += 2;
	}
	else
		document.getElementById("SwitchKVM").style.display = "none";

	// Visualizza l'UPS
	if (withUps)
	{
		if (upsType == "rack")
		{
			document.getElementById("Ups").style.display = "block";
			if (document.getElementById("2-NoRack").checked == true)
				document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left_trasp.jpg) bottom";
			else
				document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left.jpg) bottom";
			units += 3;
		}
		else
		{
			document.getElementById("Ups").style.display = "none";
			if (document.getElementById("2-NoRack").checked == true)
				document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left_trasp_ups15U.jpg) bottom";
			else
				document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left_ups15U.jpg) bottom";
		}
	}
	else
	{
		document.getElementById("Ups").style.display = "none";
			if (document.getElementById("2-NoRack").checked == true)
				document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left_trasp.jpg) bottom";
			else
				document.getElementById("e_right1").style.background = "url(images/cluster_espresso/rack/left.jpg) bottom";
	}
	
	// Nasconde i nodi di calcolo che eccedono la capacità del rack
	if (units > maxUnits)
	{
		if (nodeType == "1" || nodeType == "2" || nodeType == "3")
			nodesToHide = units - maxUnits;
		else
			nodesToHide = parseInt((units - maxUnits) / 2 + 1);

		for (i = parseInt(nodeNum - nodesToHide + 1); i <= nodeNum; ++i)
		{
			if (i <= MAX_NODES)
				document.getElementById("Nodo" + i).style.display = "none";
		}
		document.getElementById("message").innerHTML = "Nodi da " + parseInt(nodeNum - nodesToHide + 1) + " a " + nodeNum + " in uno o più altri rack";
		fadeIn('e_msg_div', 1000);
	}
	
	// Aggiorna il box con il riepilogo del cluster
	totRacks = parseInt(units / maxUnits + 1);
	totMem = parseInt(ramPerNode * nodeNum);
	if (withFrontend)
		totMem += parseInt(ramFrontend);
	totDisk = parseFloat(diskDimPerNode * diskNumPerNode * nodeNum / 1000.0).toFixed(2);
	if (withFrontend)
		totDisk += parseFloat(diskDimFrontend * diskNumFrontend / 1000.0).toFixed(2);
	document.getElementById("summary_racks").innerHTML = '' + totRacks;
	document.getElementById("summary_nodes").innerHTML = '' + nodeNum;
	document.getElementById("summary_procs").innerHTML = '' + procNum;
	document.getElementById("summary_mem").innerHTML = '' + totMem;
	document.getElementById("summary_disk").innerHTML = '' + totDisk;
}
function ShowInfo()
{
	document.getElementById("e_info").style.display = "block";
}

function HideInfo()
{
	document.getElementById("e_info").style.display = "none";
}

function SendForm()
{
	window.onbeforeunload = null;
	
	if (document.ClusterEspr.tipo.value == "preventivo")
	{
		var nome = document.ClusterEspr.nome.value;
		var cognome = document.ClusterEspr.cognome.value;
		var azienda = document.ClusterEspr.azienda.value;
		var email = document.ClusterEspr.email.value;
		var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		var privacy = document.ClusterEspr.privacy.checked;

		// CONTROLLO NOME E COGNOME
		if (nome == "") {
			alert("Il campo 'Nome' è obbligatorio.");
			return false;
		}
		else if (cognome == "") {
			alert("Il campo 'Cognome' è obbligatorio.");
			return false;
		}
		else if (azienda == "") {
			alert("Il campo 'Azienda/Istituto' è obbligatorio.");
			return false;
		}
		// CONTROLLO EMAIL
		else if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
			alert("Indirizzo email non valido.");
			return false;
		}
		// CONTROLLO PRIVACY
		else if (privacy == false) {
			alert("Per procedere devi dare il consenso per il trattamento dei dati personali.");
			return false;
		}
		//INVIA IL MODULO
		else {
			document.ClusterEspr.action = "cluster_espresso_query.php?op=preventivo";
			document.ClusterEspr.submit();
		}
	}
	else if (document.ClusterEspr.tipo.value == "pdf")
	{
		var email = document.ClusterEspr.email2.value;
		var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		var privacy = document.ClusterEspr.privacy2.checked;

		// CONTROLLO EMAIL
		if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
			alert("Indirizzo email non valido.");
			return false;
		}
		// CONTROLLO PRIVACY
		else if (privacy == false) {
			alert("Per procedere devi dare il consenso per il trattamento dei dati personali.");
			return false;
		}
		//INVIA IL MODULO
		else {
			document.ClusterEspr.action = "cluster_espresso_query.php?op=pdf";
			document.ClusterEspr.submit();
		}
	}
	else
	{
		alert("Per procedere devi selezionare una delle due opzioni e compilare i campi richiesti.");
		return false;
	}
}

function setOpacity2(id, opacity) {
	var target = document.getElementById(id);
	if(document.all) // se sto usando internet explorer
		target.style.filter = "alpha(opacity=" + opacity + ")";
	else
		target.style.opacity = opacity / 100;
}

function fadeIn(divID, time) {
	target = document.getElementById(divID);
	var speed = Math.round(time / 100); //arrotonda il valore della divisione
	var frame = 0;
	for(fade = 0; fade < 100; fade++) {
		setTimeout("setOpacity2('"+divID+"', "+fade+")", (speed * frame));
		frame++;
	}
	setTimeout("fadeOut('"+divID+"',1000)", 4000);
}

function fadeOut(divID, time) {
	target = document.getElementById(divID);
	var speed = Math.round(time / 100); //arrotonda il valore della divisione
	var frame = 0;
	for(fade = 100; fade > 0; fade--) {
		setTimeout("setOpacity2('"+divID+"', "+fade+")", (speed * frame));
		frame++;
	}
 }
