function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
var newwindow;
var newwindow2;
function sceltaColore(num) {
	w = 650;
	h = 500;
	l = (screen.width - w) / 2;
	t = (screen.height - h) / 2;
	newwindow = window.open('colori.php?n='+num,'memo','width='+w+',height='+h+',top='+t+',left='+l+',status=no,toolbar=no,menubar=no,location=no,directories=no,resizable=yes,scrollbars=yes');
}
function sceltaColore2(num) {
	w = 650;
	h = 500;
	l = (screen.width - w) / 2;
	t = (screen.height - h) / 2;
	newwindow2 = window.open('colori2.php?n='+num,'memo2','width='+w+',height='+h+',top='+t+',left='+l+',status=no,toolbar=no,menubar=no,location=no,directories=no,resizable=yes,scrollbars=yes');
}
function attiva(testo,num) {
	modifica = document.getElementById("colore"+num);
	modifica.value = testo;
	if (document.getElementById("ral"+num).value != '-na-') document.getElementById("ral"+num).value = "";
	document.getElementById("prezzoC"+num).value = "0.00";
	parziale1calcola(num);
	newwindow.close();
}
function attiva2(testo,num) {
	modifica = document.getElementById("ral"+num);
	modifica.value = testo;
	if (testo != '') {
		document.getElementById("colore"+num).value = "";
		parziale2calcola(num);
	} else {
		document.getElementById("prezzoC"+num).value = "0.00";
		parziale1calcola(num);
	}
	newwindow2.close();
}
function openpdf(file) {
//	outpdf.location.href = 'outpdf.php?url='+file;
	outpdf.location.href = 'pdf/'+file;
	window.scroll(0,700);
}
/*function adattaFrame() {
	parent.ridimensiona();
	parent.parent.ridimensiona();
}
function ridimensiona() {
	resizeFrame(document.getElementById("outpdf"));
}
function resizeFrame(f) {
	f.style.height = (f.contentWindow.document.body.scrollHeight + 100) + "px";
}*/
function parziale1(num,test) {
	if (isNaN(test.value)) {
		alert("Il numero inserito non è valido");
		test.value = 0;
	}
	parziale1calcola(num);
}
function parziale1calcola(num) {
	if (sumisura[num] == undefined) { //standard
		if (!isNaN(document.getElementById("prezzoO"+num).value)) {
			document.getElementById("prezzoP"+num).value = (Number(document.getElementById("ripiani18_"+num).value)*Number(document.getElementById("prezzo_ripiani18_"+num).value) + Number(document.getElementById("ripiani25_"+num).value)*Number(document.getElementById("prezzo_ripiani25_"+num).value) + Number(document.getElementById("barre_"+num).value)*Number(document.getElementById("prezzo_barre_"+num).value)).toFixed(2);
			document.getElementById("prezzoT"+num).value = (Number(document.getElementById("prezzoO"+num).value) + Number(document.getElementById("prezzoP"+num).value) + Number(document.getElementById("prezzoC"+num).value)).toFixed(2);
		}
	} else { //su misura
		if (prezzotot != undefined && prezzotot > 0) {
			document.getElementById("prezzoP"+num).value = (Number(document.getElementById("ripiani18_"+num).value)*Number(document.getElementById("prezzo_ripiani18_"+num).value) + Number(document.getElementById("ripiani25_"+num).value)*Number(document.getElementById("prezzo_ripiani25_"+num).value) + Number(document.getElementById("barre_"+num).value)*Number(document.getElementById("prezzo_barre_"+num).value)).toFixed(2);
			document.getElementById("prezzoT"+num).value = (prezzotot + Number(document.getElementById("prezzoP"+num).value) + Number(document.getElementById("prezzoC"+num).value)).toFixed(2);
		}
	}
}
function parziale2(num,test) {
	if (isNaN(test.value)) {
		alert("Il numero inserito non è valido");
		test.value = 0;
	}
	parziale2calcola(num);
}
function parziale2calcola(num) {
	if (sumisura[num] == undefined) { //standard
		document.getElementById("ralmq"+num).value = (Number(document.getElementById("H"+num).value)/1000*Number(document.getElementById("L"+num).value)/1000).toFixed(2);
		if (!isNaN(document.getElementById("prezzoO"+num).value) && document.getElementById("ral"+num).value != '') {
			document.getElementById("prezzoC"+num).value = (Number(document.getElementById("ralmq"+num).value)*35).toFixed(2);
			if (Number(document.getElementById("prezzoC"+num).value) < 50) {
				alert("Il prezzo minimo fatturabile per il RAL è di 50 euro");
				document.getElementById("prezzoC"+num).value = "50.00";
			}
			document.getElementById("prezzoT"+num).value = (Number(document.getElementById("prezzoO"+num).value) + Number(document.getElementById("prezzoP"+num).value) + Number(document.getElementById("prezzoC"+num).value)).toFixed(2);
		}
	} else { //su misura
		h = document.getElementById("H"+num).value;
		p = document.getElementById("P"+num).value;
		if (!isNaN(h) && !isNaN(p) && h != '' && p != '') {
			if (h < 1550) {
				if (p < 500) { prezzo = sumisura[num][0][0]; pacc1 = accessori[num][0][0][0]; pacc2 = accessori[num][0][0][1]; pacc3 = accessori[num][0][0][2]; }
				else { prezzo = sumisura[num][0][1]; pacc1 = accessori[num][0][1][0]; pacc2 = accessori[num][0][1][1]; pacc3 = accessori[num][0][1][2]; }
			} else if (h < 2350) {
				if (p < 500) { prezzo = sumisura[num][1][0]; pacc1 = accessori[num][1][0][0]; pacc2 = accessori[num][1][0][1]; pacc3 = accessori[num][1][0][2]; }
				else { prezzo = sumisura[num][1][1]; pacc1 = accessori[num][1][1][0]; pacc2 = accessori[num][1][1][1]; pacc3 = accessori[num][1][1][2]; }
			} else {
				if (p < 500) { prezzo = sumisura[num][2][0]; pacc1 = accessori[num][2][0][0]; pacc2 = accessori[num][2][0][1]; pacc3 = accessori[num][2][0][2]; }
				else { prezzo = sumisura[num][2][1]; pacc1 = accessori[num][2][1][0]; pacc2 = accessori[num][2][1][1]; pacc3 = accessori[num][2][1][2]; }
			}
			prezzotot = prezzo + 50;
			document.getElementById("prezzo_ripiani18_"+num).value = pacc1;
			document.getElementById("prezzo_ripiani25_"+num).value = pacc2;
			document.getElementById("prezzo_barre_"+num).value = pacc3;
			$('#prezzodett_ripiani18_'+num).html('(€ '+pacc1+')');
			$('#prezzodett_ripiani25_'+num).html('(€ '+pacc2+')');
			$('#prezzodett_barre_'+num).html('(€ '+pacc3+')');
			document.getElementById("ralmq"+num).value = (Number(h)/1000*Number(document.getElementById("L"+num).value)/1000).toFixed(2);
			if (document.getElementById("ral"+num).value != '') {
				document.getElementById("prezzoC"+num).value = (Number(document.getElementById("ralmq"+num).value)*35).toFixed(2);
				if (Number(document.getElementById("prezzoC"+num).value) < 50) {
					alert("Il prezzo minimo fatturabile per il RAL è di 50 euro");
					document.getElementById("prezzoC"+num).value = "50.00";
				}
			}
			document.getElementById("prezzoT"+num).value = (prezzotot + Number(document.getElementById("prezzoP"+num).value) + Number(document.getElementById("prezzoC"+num).value)).toFixed(2);
			parziale1calcola(num);
		} else {
			prezzotot = 0;
			document.getElementById("prezzoT"+num).value = 0;
			document.getElementById("prezzo_ripiani18_"+num).value = 0;
			document.getElementById("prezzo_ripiani25_"+num).value = 0;
			document.getElementById("prezzo_barre_"+num).value = 0;
			$('#prezzodett_ripiani18_'+num).html('');
			$('#prezzodett_ripiani25_'+num).html('');
			$('#prezzodett_barre_'+num).html('');
			parziale1calcola(num);
		}
	}
}
function debugform(modulo,num) {
	verifica = true;
	if (document.getElementById("quantita"+num).value == '' || isNaN(document.getElementById("quantita"+num).value)) {
		alert("La quantità indicata non è valida");
		verifica = false;
	} else if (document.getElementById("ripiani18_"+num).value != '' && isNaN(document.getElementById("ripiani18_"+num).value)) {
		alert("Il numero di ripiani da 18 non è valido");
		verifica = false;
	} else if (document.getElementById("ripiani25_"+num).value != '' && isNaN(document.getElementById("ripiani25_"+num).value)) {
		alert("Il numero di ripiani da 25 non è valido");
		verifica = false;
	} else if (document.getElementById("barre_"+num).value != '' && isNaN(document.getElementById("barre_"+num).value)) {
		alert("Il numero di barre aggiuntive non è valido");
		verifica = false;
	} else if (document.getElementById("colore"+num).value == '' && (document.getElementById("ralmq"+num).value != '' && (document.getElementById("ral"+num).value == ''))) {
		alert("Scegliere il colore");
		verifica = false;
	} else {
		verifica = (controlla_campo('H',num,'Altezza') && controlla_campo('L',num,'Larghezza') && controlla_campo('P',num,'Profondità'));
	}
	if (verifica) {
		modulo.QUANTITY.value = document.getElementById("quantita"+num).value;
		if (document.getElementById("prezzoT"+num) != null) {
			modulo.PRICE.value = document.getElementById("prezzoT"+num).value;
		} else {
			modulo.PRICE.value = 0;
		}
		var separatore = " / ";
		var acapo = "<br/>";
		
		var riga1 = new Array();
		riga1[0] = print_campo("H=",output_campo(document.getElementById("H"+num).value));
		riga1[1] = print_campo("L=",output_campo(document.getElementById("L"+num).value));
		riga1[2] = print_campo("P=",output_campo(document.getElementById("P"+num).value));
		sriga1 = riga1[0];
		sriga1 += separa(sriga1,riga1[1],separatore)+riga1[1];
		sriga1 += separa(sriga1,riga1[2],separatore)+riga1[2];
		
		var riga2 = new Array();
		riga2[0] = print_campo("Colore = ",output_campo(document.getElementById("colore"+num).value));
		riga2[1] = print_campo("RAL = ",output_campo(document.getElementById("ral"+num).value));
		sriga2 = riga2[0];
		sriga2 += separa(sriga2,riga2[1],separatore)+riga2[1];
		sriga2 = separa(sriga1,sriga2,acapo) + sriga2;

		var riga3 = new Array();
		riga3[0] = print_campo("Ripiani da 18: n. ",output_campo(document.getElementById("ripiani18_"+num).value));
		riga3[1] = print_campo("Ripiani da 25: n. ",output_campo(document.getElementById("ripiani25_"+num).value));
		sriga3 = riga3[0];
		sriga3 += separa(sriga3,riga3[1],separatore)+riga3[1];
		sriga3 = separa(sriga1 + sriga2,sriga3,acapo) + sriga3;

		var riga4 = new Array();
		riga4[0] = print_campo("Barre: n. ",output_campo(document.getElementById("barre_"+num).value));
		sriga4 = riga4[0];
		sriga4 = separa(sriga1 + sriga2 + sriga3,sriga4,acapo) + sriga4;

		modulo.NAME.value = sriga1 + sriga2 + sriga3 + sriga4;
/*		modulo.ADDITIONALINFO.value = "H="+modulo.H.value+" - L="+modulo.P.value+" - P="+modulo.P.value;
		modulo.ADDITIONALINFO2.value = "Colore="+modulo.colore.value;
		modulo.ADDITIONALINFO3.value = "Ripiani da 18="+modulo.ripiani18.value+" - da 25="+modulo.ripiani25.value;
		modulo.ADDITIONALINFO4.value = "Barre="+modulo.barre.value;*/
		AddToCart(modulo);
	}
	return false;
}
function separa(testo1,testo2,separatore) {
	if (testo1 != '' && testo2 != '')
		return separatore;
	else
		return '';
}
function output_campo(campo) {
	if (campo == '') {
		return "0";
	} else {
		return campo;
	}
}
function print_campo(testo,valore) {
	if (valore == '0' || valore == '-na-') {
		return '';
	} else {
		return testo+valore;
	}		
}
function controlla_campo(campo,num,output) {
	valutare0 = document.getElementById(campo+num);
	valutare1 = document.getElementById(campo+'F_'+num);
	valutare2 = document.getElementById(campo+'1_'+num);
	valutare3 = document.getElementById(campo+'2_'+num);
	//alert('debug: '+valutare0.value+' - '+valutare1.value + ' - '+valutare2.value + ' - '+valutare3.value);
	if (valutare0.value == '' && valutare0.value != '-na-') {
		alert("Compilare il campo "+output);
		return false;
	} else {
		if (valutare1.value != 'no') {
			return true;
		} else if (valutare2.value != 'no' && valutare3.value != 'no') {
			if (parseInt(valutare0.value) < parseInt(valutare2.value) || parseInt(valutare0.value) > parseInt(valutare3.value)) {
				alert("Il campo " + output + " deve essere compreso fra " + valutare2.value + " e " + valutare3.value);
				return false;
			} else {
				return true;
			}
		} else if (valutare2.value != 'no' && valutare3.value == 'no') {
			if (parseInt(valutare0.value) < parseInt(valutare2.value)) {
				alert("Il campo " + output + " deve essere almeno " + valutare2.value);
				return false;
			} else {
				return true;
			}
		} else if (valutare2.value == 'no' && valutare3.value != 'no') {
			if (parseInt(valutare0.value) > parseInt(valutare3.value)) {
				alert("Il campo " + output + " deve essere al massimo " + valutare3.value);
				return false;
			} else {
				return true;
			}
		} else if (valutare0.value == '-na-') {
			return true;
		} else {
			alert("Si è verificato un errore, si prega di ricontrollare i valori inseriti");
			return false;
		}
	}
}
function runEffect(num) {
	var selectedEffect = 'blind';
	var options = {};
	$("#effect"+num).toggle(selectedEffect,options,500);
};

function effetto(num) {
	runEffect(num);
	eval("document.freccia"+num).src="immagini/freccia"+valuta_stato(num)+".gif";
	return false;
}
function valuta_stato(num) {
	if (stato_img[num]) {
		stato_img[num] = false;
		return stato_off;
	} else {
		stato_img[num] = true;
		return stato_on;
	}	
}
function prepara_array(num) {
	for (i=1;i<=num;i++) {
		stato_img[i] = true; //tutto aperto
	}
}
var stato_off = "giu";
var stato_on = "su";
var stato_img = Array;
var sumisura = new Array();
var accessori = new Array();

