// JavaScript Document
function hidda()
{
	var valori = new Array('s','g','pa','pb','pc','pd','pe','pf');
	for(var i=0;i<valori.length;i++)
	{
		hide(valori[i]);
	}
	
	
}
function hide(tag)
{
	var i=0;
	if(document.getElementById(tag+0).style.display != 'none')
	{
		document.getElementById(tag).style.display ='block';
		document.getElementById(tag+'c').style.display ='none';
		for(i=0;i<8;i++)
		{
		document.getElementById(tag+i).style.display = 'none';
		}
	}
	else
	{
		document.getElementById(tag).style.display ='none';
		document.getElementById(tag+'c').style.display ='block';
		for(i=0;i<8;i++)
		{
		document.getElementById(tag+i).style.display = 'block';
		}
	}
	
}


function disabilita(i)
{
	for(var j=0;j<document.dest.nation.length;j++)
	{
		if(j!=i)
		{
		document.dest.nation[j].disabled = true;
		}
	}
}
function abilita()
{
	for(var j=0;j<document.dest.nation.length;j++)
	{		
		document.dest.nation[j].disabled = false;
	}
}
function modVal(i,num,nome)
{

	var numero = parseInt(document.dati.elements[i].value);//era elements[i]
	numero = numero + num;
	
	for(var j=0;document.dati.elements[j].name != "-" + nome;j++);
	
	if(numero>0){
		document.dati.elements[i].value = numero;//era elements[i]
		document.dati.elements[j].disabled = false;
	}
	else
	{
		document.dati.elements[i].value = 0;		
		document.dati.elements[j].disabled = true;
	}
}				

function modVol(i,k,num)
{
	var nome = k;
	var tipoShip;
	var EU = 3;
	var USA = 4;
	var OT = 5;
	var shipping = 0;
	var j=0;
	var valore = parseInt(document.dati.volume.value); //volume teorico della busta
	
	
	/* questa parte cerca l'indice dell'oggetto*/
	for(j=0;document.dati.elements[j].name != k;j++);
	
	k = j;	
	/*fine parte di ricerca*/
	
	for(j=0;j<document.dest.nation.length;j++)
	{
		if(document.dest.nation[j].checked == true)
		{
			tipoShip = document.dest.nation[j].value;
			j = document.dest.nation.length+1;
		}
	}
	if(j == document.dest.nation.length)
	{
		resettaImporto();
		alert("Select shipping zone");
		return;
	}
	modVal(k,num,nome);
	valore = valore + i;
	document.dati.volume.value = valore;
	
	if(tipoShip == "USA")
	{
		shipping = USA;
	}
	else if(tipoShip == "EU")
	{
		shipping = EU;
	}
	else
	{
		shipping = OT;
	}
	if(valore<100)
	{
		document.dati.shipping.value = shipping;//categoria peso fino a 100 g
	}
	else// valore è necessariamente maggiore di 100
	{
		for(j=0;valore>250;j++)//j conta quante buste da 250g ci vogliono
		{
			valore = valore - 250;
			
		}
		if(valore <= 100 && valore!=0)
			{
				document.dati.shipping.value = j*(shipping+1)+shipping;
				calcolaTotale();
				return;
			}
		else
		{
			j++;
			document.dati.shipping.value = j*(shipping+1);
			calcolaTotale();
				return;
		}
		
	}
	calcolaTotale();
}

function sommaCosto(importo)
{
	
	document.dati.amount.value = parseInt(document.dati.amount.value) + importo;
	calcolaTotale();
}
function resettaImporto()
{
	document.dest.reset();
	for(var i=0;i<document.dati.elements.length;i++)
	{
		if(document.dati.elements[i].value == "-")
		{
			document.dati.elements[i].disabled = true;
		}
	}
	document.dati.amount.value = 0;
	document.dati.totale.value = 0;
	document.dati.shipping.value = 0;
	document.dati.volume.value = 0;
	calcolaTotale();
}
function calcolaTotale()
{
	document.dati.totale.value = parseInt(document.dati.amount.value) + parseInt(document.dati.shipping.value);
}
	
function creaMsg()
{
	counter=0;
	var msg = "";
	for(var i=0;i<19;i++)//controlla ogni campo
	{
		if(document.dati.elements[i*3].value>0)
		{
		msg = msg +"|" + document.dati.elements[i*3].value + " " +document.dati.elements[i*3].name +"| \n";
		counter++;
		}
	}
	if(counter>8)
	{
		alert("You have selected more than 8 fields");		
	}
	return msg;
}
function modificaDati()
{
	if(checkDisclaimer())
	{
	document.paga.shipping.value = document.dati.shipping.value;
	document.paga.amount.value = document.dati.amount.value;
	document.paga.item_name.value = creaMsg();
		if(counter>8)
		{
			return false;
		}
	return true;
	}
	else
	{
		alert("Please accept the disclaimer before continue!")
		return false;
	}
}
function checkDisclaimer()
{
	return document.dati.disclaimer;
}
function cambiaStatoDisc()
{
	document.dati.disclaimer = !document.dati.disclaimer;
}
function vediCarrello()
{
	alert("Your cart: \n "+creaMsg());
}