var TONDE 		= 1;
var QUADRE		= 2;
var ESAGONALI	= 3;
var DENSITY 	= 7.85;
var RESULT 		= "weight";
var HEXCONST	= 0.866;
var expr			= /^\s*\d+(\.\d+)?\s*$/;

function peso(arg){
	var L;
	switch(arg){
		case TONDE:
		L = document.forms[0].elements["diametro"].value;
		if (! L.match(expr))
		{
			alert("Errore: diametro indicato non corretto");
			document.forms[0].elements[RESULT].value = 0;
			return;
		}
		//document.forms[0].elements[RESULT].value = fixedRound((Math.pow(L / 2), 2) * Math.PI * DENSITY /1000);
		L  = L / 2;
		L  = Math.pow(L , 2);
		L *= Math.PI
		break;
		
		case QUADRE:
		if (! document.forms[0].elements["lato1"].value.match(expr))
		{
			alert("Errore: primo lato indicato non corretto");
			document.forms[0].elements[RESULT].value = 0;
			return;
		}
		if (! document.forms[0].elements["lato3"].value.match(expr))
		{
			alert("Errore: secondo lato indicato non corretto");
			document.forms[0].elements[RESULT].value = 0;
			return;
		}
		L = document.forms[0].elements["lato1"].value * document.forms[0].elements["lato3"].value;
		break;
		
		case ESAGONALI:
		L = document.forms[0].elements["lato2"].value;
		if (! L.match(expr))
		{
			alert("Errore: lato esagono indicato non corretto");
			document.forms[0].elements[RESULT].value = 0;
			return;
		}
		//document.forms[0].elements[RESULT].value = fixedRound((Math.pow(L / 2), 2) * Math.PI * DENSITY /1000);
		L  = L * L * HEXCONST;
		break;
	}
	document.forms[0].elements[RESULT].value = fixedRound(partial(L));
}

function fixedRound(arg)
{
	var tmp;
	tmp = Math.round(arg * 100);
	return tmp / 100;
}

function partial(arg)
{
	return arg * DENSITY / 1000;
}

function copia()
{
	document.forms[0].elements['lato3'].value = document.forms[0].elements['lato1'].value;
}


