$(document).ready(function(){
	$("#blackBody").hide();
	$("#boxsezione").hide();
});
function apriForm(idProd,lang){
	ApriPagina('form-ordine.php?lang='+lang,883,idProd);
	$("#blackBody").show().animate({opacity:0.0},0); 
	$("#blackBody").animate({opacity:0.6},800); 
	$("#boxsezione").show().animate({opacity:0.0},0); 
	$("#boxsezione").animate({opacity:1.0},800);
	if (idProd!=0) {
		setTimeout('SetQuantity('+idProd+')',1);
	}
	//clearInterval(int1);
	//alert(document.getElementsByName('quantita1').value);
	//document.getElementsByName('quantita1').value="1";
}
function chiudiForm(){
	//rimuoviamo quelli con class formError	
	$(".formError").remove();
	$("#boxsezione").animate({opacity:0.0},800);
	$("#blackBody").animate({opacity:0.0},800); 
	setTimeout('hideForm()',800);
}
function hideForm() {
	$("#boxsezione").hide(); 
	$("#blackBody").hide(); 
}
function SetQuantity(idProd) {
	var MyQuantityField = document.getElementById('quantita'+idProd);
	var MyPriceField = document.getElementById('prezzo'+idProd);
	var MySubtotaleString = document.getElementById('subtotale'+idProd);
	var MySubtotaleField = document.getElementById('subtotaleField'+idProd);
	MyQuantityField.value = "1";
	Subtotale = new String (MyQuantityField.value * MyPriceField.value);
	Subtotale = Subtotale.replace(/\./g,",");
	var Subtotale_array=Subtotale.split(",");
	if (Subtotale_array[1].length==1) {
		Subtotale=Subtotale+"0";				  
	}
	MySubtotaleString.innerHTML="<b>Eur "+Subtotale+"</b>";
	MySubtotaleField.value=Subtotale;
}
function RefreshPrice(idProd) {
	var MyQuantityField = document.getElementById('quantita'+idProd); //quanti
	var MyPriceField = document.getElementById('prezzo'+idProd); //prezzo
	var MySubtotaleString = document.getElementById('subtotale'+idProd); //subtotale
	var MySubtotaleField = document.getElementById('subtotaleField'+idProd); //subtotale
	var numericExpression = /^[0-9]+$/;

	//devo verificare che sia un numero
	if(MyQuantityField.value == ""){
		var qta = 0;
		document.getElementById('quantita'+idProd).value = 0;
	}else if(isNaN(MyQuantityField.value)){
		alert("Attenzione, la quantita ordinata deve essere numerica");
		azzera(idProd);
		return 1;
	}else if(MyQuantityField.value < 0){
		alert("Attenzione, la quantita ordinata deve essere maggiore o uguale a 0");
		azzera(idProd);
		return 1;
	}else if(!(MyQuantityField.value).match(numericExpression)) {
	alert("Attenzione, la quantita ordinata deve essere intera");
		azzera(idProd);
		return 1;
	}else{
		var qta = MyQuantityField.value;
	}
	
	Subtotale = new String (qta * MyPriceField.value); //calcolo
//devo fare l'arrotondamento a 2 cifre decimali
	Subtotale = new String (Math.round(Subtotale * 100) / 100);
	Subtotale_print = Subtotale.replace(/\./g,","); //metto la virgola per stampare

	if(Subtotale > 0){
		var temp = Subtotale_print.split(",");
		if(temp.length == 2){
			if( temp[1].length < 2){
				var dopo = temp[1] + "0";
			}else{
				var dopo = temp[1];
			}
			Subtotale_print = "<b>Eur "+temp[0]+","+dopo+"</b>";
		}else{
			Subtotale_print = "<b>Eur "+temp[0]+",00</b>";
		}
	}else if(Subtotale == 0){
		Subtotale_print = "<b>-</b>";
	}
	MySubtotaleString.innerHTML=Subtotale_print;
	MySubtotaleField.value=Subtotale;
	
	aggiornaTotale();
}
function azzera(idProd){
	document.getElementById('quantita'+idProd).value = 0;
	document.getElementById('subtotale'+idProd).innerHTML = "<b>-</b>";
	document.getElementById('subtotaleField'+idProd).value = 0;
	aggiornaTotale();
}

function aggiornaTotale(){
	var t = 0;
	for(i=1;i<=3;i++){
		var temp = parseFloat(document.getElementById('subtotaleField'+i).value);
		if(temp >= 0){
			t += temp;
		}
	}
	t = new String (Math.round(t * 100) / 100);
	if(t > 0){
		t = new String (t); //calcolo
		t = t.replace(/\./g,","); //metto la virgola per stampare
		var temp = t.split(",");
		if(temp.length == 2){
			if( temp[1].length < 2){
				var dopo = temp[1] + "0";
			}else{
				var dopo = temp[1];
			}
			t = temp[0]+","+dopo;
		}else{
			t = temp[0]+",00";
		}
	}else if(t == 0){
		t = "0,00";
	}
	
	document.getElementById('prezzo_finale').innerHTML="<b>Eur "+t+"</b>";
}
