//www.farmamica.com

	var toDtls = false;  // modificare a true se si desidera ridirezionare sulla pagina di dettaglio prodotto
	var ckDays = 7; // giorni di validità del cookie del carrello
	
	var valuta="EUR ";
var valuta2="USD ";
var exch= .687653863;
var SecVal= 1;
var sepmigl=".";
var sepdec=",";
var PutDec= 1;
var PutDec2= 1;
var scimp = new Array( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var scperc = new Array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);

	
	var nc = "eDenter_basket";
			
	function MakeCookieExpirationH(hours) {
		dt = new Date(); 
		dt.setTime(dt.valueOf() + hours * 3600000); 
		sdt = dt.toGMTString(); 
		return "; expires=" + sdt + ";"; 
	}
	
	function MettiPunti(prezzo) {
		var newprice='';
		var oldprice=prezzo+'s';
		var lung=oldprice.length-1;
		oldprice=oldprice.substring(0,lung);
		while (lung>3) {
			newprice=sepmigl+oldprice.substring(lung-3,lung)+newprice;
			oldprice=oldprice.substring(0,lung-3);
			lung=oldprice.length;  
		}
		newprice=oldprice+newprice;
		return (newprice);
	}
	
	function alterError(value) {
		if (PutDec !=0) {
			if (value<=0.99) {
				newPounds = '0';
			} else {
				newPounds = parseInt(value);
			}
			newPence = Math.round((value - newPounds)* 100);
			if (newPence == 100) {newPence =0; newPounds ++;}
			if (eval(newPence) <= 9) newPence='0'+newPence;
			newString = MettiPunti(newPounds);
			if (eval(newPence) != 0) {
				newString= newString + sepdec + newPence;
			}
			else {
				newString= newString + sepdec + '00';
			}
		}
		else {
			newPounds = Math.round(value);
			newString = MettiPunti(newPounds);
		}
		return (newString);
	}

function MostraCarrello(l) {
                var sfondocarrello = '#F7F7F7';
		var dImg = new Array("","");
		dImg[0] = '<img src="edis_disp_no.gif" alt="Articolo non disponibile o quantità richiesta superiore alla giacenza del nostro magazzino">';
		dImg[1] = '<img src="edis_disp_yes.gif" alt="Articolo disponibile">';
		nc = "eDenter_basket" + l;
                index = document.cookie.indexOf(nc);
		countbegin = (document.cookie.indexOf("=", index) + 1);
        	countend = document.cookie.indexOf(";", index);
        	if (countend == -1) {
            		countend = document.cookie.length;
        	}
		if (index == -1) {
			countbegin = 0;
			countend = 0;
		}
		fulllist = document.cookie.substring(countbegin, countend);
		index = document.cookie.indexOf("eDenter_disp");
		countbegin = (document.cookie.indexOf("=", index) + 1);
        	countend = document.cookie.indexOf(";", index);
        	if (countend == -1) {
            		countend = document.cookie.length;
        	}
		if (index == -1) {
			countbegin = 0;
			countend = 0;
		}
		available = document.cookie.substring(countbegin, countend);
		totprice = 0;
		totiva = 0;
		impsconto = 0;
		impscontoiva = 0;
		document.writeln('<form name="updateform">');
		document.writeln('<div align="left">');
		document.writeln('<table width="100%" border="0" cellpadding="2">');
		document.writeln('<tr bgcolor="#BDEFC6"><td><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Articolo"+'</font></b></td><td><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Varianti"+'</font></b></td><td align="center"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Quantità"+'</font></b></td><td align="right"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Prezzo unitario"+'</font></b></td><td align="right"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Prezzo totale IVA inc."+'</font></b></td><td align="center" colspan="2"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">' + "Disponibilit&agrave;" + '</font></b></td></tr>');
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
				thisitem = 1;
				thisavail = available.substring(itemlist, itemlist +1);
				if (thisavail == "") {
					availimg=0;
				} else {
					availimg=parseInt(available.substring(itemlist, itemlist +1));
				}
			} 
			else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				thequantity = unescape(fulllist.substring(itemstart, itemend));
				if (thequantity * 1 >= scq2 && scq2 > -1) {
					theprice = theprice * imp2;
					ivaitem = ivaitem * imp2;
				} else if (thequantity * 1 >= scq1 && scq1 > -1) {
					theprice = theprice * imp1;
					ivaitem = ivaitem * imp1;
				}
				itemtotal = 0;
				itemtotal = (eval(theprice*thequantity));
				itemtotaliva = 0;
				itemtotaliva = (eval(ivaitem*thequantity));
				temptotal = itemtotal * 100;
				totprice = totprice + itemtotal;
				totiva = totiva + itemtotaliva;
				itemlist=itemlist+1;
				document.write('<tr valign="top" bgcolor="'+sfondocarrello+'"><td><table border="0" cellpadding="2" cellspacing="1"><tr><td align="center" valign="top"><a href="dettview1.php?cod=' + thecode + '" title="Vai alla scheda dettagliata del prodotto"><img style="border: 0px;" width="50" src="show_pic.php?id=' + artid + '"></a><br><font color="#000000" face="Arial,Helvetica" size="1"><a href="prodid.php?cod=' + thecode + '&lang=1" target="_self"><b>' + thecode + '</b></a></font></td><td valign="middle"><font color="#000000" face="Arial,Helvetica" size="-1"><a href="' + artlink + '" title="Vai alla scheda del prodotto"><b>' + thecode + ' ' +theitem+'</b></a></font>&nbsp;<font color="#000000" face="Verdana,Arial,Helvetica" size="1">(' + pesoitem + ' kg)</font></td></tr></table></td>');
				option1234 = '';
				if (option1 != '') {
					option1234 = option1;
				}
				if (option2 != '') {
					if (option1234 != '') {
						option1234 = option1234 + '<br>';
					}
					option1234=option1234 + option2;
				}
				if (option3 != '') {
					if (option1234 != '') {
						option1234 = option1234 + '<br>';
					}
					option1234=option1234 + option3;
				}
				if (option4 != '') {
					if (option1234 != '') {
						option1234 = option1234 + '<br>';
					}
					option1234=option1234 + option4;
				}
				document.write('<td><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+option1234 +'</font></td>');
				
				/*****/
				if (availimg == 0) {				
					window.alert(" << " +theitem+ " >>: " + "articolo non disponibile nella quantità richiesta!\n" + "L'articolo con il codice << " +thecode+ " >> ha superato la quantità in giacenza!\n" + "Sei stato reindirizzato sull'articolo -> " +theitem+ " onde poter inserire una nuova quantità.");
					lnkTo = artlink;
					if (toDtls) {
						lnkTo = "dettview1.php?id=" + artid;
					}
					EliminaArt(itemlist, lnkTo);
					return;
				}
				/*****/
				
				document.write('<td valign="top" align="center"><input type="text" name="quant'+itemlist+'" value="'+thequantity+'" size="3" maxlength="3"><br><a href="javascript:ModificaArt('+itemlist+',document.updateform.quant'+itemlist+'.value)" onMouseOver="window.status=\'\'; return true;"><font color="#000000" face="Verdana,Arial,Helvetica" size="1">'+"<b>Aggiorna</b>"+'</font><br><img src="edis_reload.gif" border="0" alt="Aggiorna"></a></td><td valign="top" align="right"><br><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+alterError(eval(theprice))+'</font></td><td valign="top" align="right"><br><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+alterError(itemtotal)+'</font></td><td align="center" valign="top"><br>' + dImg[availimg] + '</td><td align="center" valign="top"><a href="javascript:EliminaArt('+itemlist+')" onclick="return confirm(\'Confermi la cancellazione di << '+theitem+' >> dal tuo carrello?\')" onMouseOver="window.status=\'\'; return true"><img src="edis_delete.gif" border="0" alt="Elimina"><br><font color="#000000" face="Verdana,Arial,Helvetica" size="1">'+"<b>Elimina</b>"+'</font><br><img src="edis_delete.gif" border="0" alt="Elimina"></a></td></tr>');

                if (sfondocarrello == '#F7F7F7') {
                	sfondocarrello = '#FFFFFF';
                }
				else {
                	sfondocarrello = '#F7F7F7';
                }
			}
			else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) theitem = unescape(fulllist.substring(itemstart, i));
				if (thisitem==2) thecode = unescape(fulllist.substring(itemstart, i));
				if (thisitem==3) artid = unescape(fulllist.substring(itemstart, i));
				if (thisitem==4) option1 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==5) option2 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==6) option3 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==7) option4 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==8) pesoitem = unescape(fulllist.substring(itemstart, i));
				if (thisitem==9) theprice = unescape(fulllist.substring(itemstart, i));
				if (thisitem==10) ivaitem = unescape(fulllist.substring(itemstart, i));
				if (thisitem==11) codeitem= unescape(fulllist.substring(itemstart, i));
				if (thisitem==12) scq1 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==13) imp1 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==14) scq2 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==15) imp2 = unescape(fulllist.substring(itemstart, i));
				if (thisitem==16) artlink = unescape(fulllist.substring(itemstart, i));
				if (thisitem==17) alq = unescape(fulllist.substring(itemstart, i));
				if (thisitem==18) acqid = unescape(fulllist.substring(itemstart +1, i -1));
				thisitem++;
				itemstart=i+1;
			}
		}

		if (itemlist==0) {
			document.writeln('<tr bgcolor="#F7F7F7"><td colspan="7"><font color="#EB0404" face="Verdana,Arial,Helvetica" size="2">'+"( Il tuo carrello attualmente &egrave; vuoto! )"+'</font></td></tr>');
		}

		document.writeln('<tr bgcolor="#BDEFC6"><td colspan="4"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Totale ordine"+'</font></b></td><td align="right" nowrap><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+valuta+alterError(totprice)+'</font></b></td><td colspan="2">&nbsp;</td></tr>');
		
		document.writeln('<tr bgcolor="#F7F7F7"><td colspan="4"><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Total order ( other value )"+'</font></td><td align="right" nowrap><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+valuta2+ConvertiValuta2(totprice)+'</font></td><td colspan="2">&nbsp;</td></tr>');


		scp = 1;
		if (scl > 0) {
			scp = 1 - scl;
			scp = Math.round(scp * 100) / 100;
		} else if (totprice * 1 > scimp[l] && scperc[l] * 1 < 1) {
			scp = scperc[l];
		}
		if (scp < 1) {
			newtotprice = totprice * scp;
			impsconto = totprice - newtotprice;
			totprice = newtotprice;
			newtotiva = totiva * scp;
			impscontoiva = totiva - newtotiva;
			totiva = newtotiva;
			
			document.writeln('<tr bgcolor="#F7F7F7"><td colspan="4"><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Sconto Cliente"+'</font></td><td align="right" nowrap><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+ '-' +alterError(impsconto)+'</font></td><td colspan="2">&nbsp;</td></tr>');

			document.writeln('<tr bgcolor="#BDEFC6"><td colspan="4"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"Totale scontato"+'</font></b></td><td align="right" nowrap><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+valuta+alterError(totprice)+'</font></b></td><td colspan="2">&nbsp;</td></tr>');

		}

		if (1 == 0) {
			document.writeln('<tr bgcolor="#BDEFC6"><td colspan="4"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"IVA"+'</font></b></td><td align="right" nowrap><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+valuta+alterError(totiva)+'</font></b></td><td colspan="2">&nbsp;</td></tr>');
			
			document.writeln('<tr bgcolor="#BDEFC6"><td colspan="4"><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+"IVA"+'</font></b></td><td align="right" nowrap><b><font color="#000000" face="Verdana,Arial,Helvetica" size="2">'+valuta2+ConvertiValuta2(totiva)+'</font></b></td><td colspan="2">&nbsp;</td></tr>');

		}
		document.writeln('</table><cr>');
		document.writeln('</div>');
		document.writeln('</form>');
	}
		
	function ModificaArt(itemno, newquant, lnk) {
		newItemList = "";
		itemlist = 0;
		if (isNaN(newquant)) {
			newquant=0;
		}
		newquant=Math.floor(newquant);
		if (newquant <= 0) {
			rc = alert("Inserire una quantità corretta!\n\nIn questo campo è necessario inserire\nun numero intero positivo\ncompreso tra 1 e 99 (es. 2).");
			document.updateform.elements['quant'+itemno].focus();
			document.updateform.elements['quant'+itemno].select();
		} 
		else {
			for (var i = 0; i <= fulllist.length; i++) {
				if (fulllist.substring(i,i+1) == '[') {
					thisitem = 1;
					itemstart = i+1;
					fullstart = i+1;
				} 
				else if (fulllist.substring(i,i+1) == ']') {
					itemend = i;
					itemlist=itemlist+1;
					if (itemlist != itemno) {
						newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
					}
					else {
						newItemList = newItemList + "[" + escape(theitem) + "|";
						newItemList = newItemList + escape(thecode) + "|";
						newItemList = newItemList + escape(artid) + "|";
						newItemList = newItemList + escape(option1) + "|";
						newItemList = newItemList + escape(option2) + "|";
						newItemList = newItemList + escape(option3) + "|";
						newItemList = newItemList + escape(option4) + "|";
						newItemList = newItemList + escape(pesoitem) + "|";
						newItemList = newItemList + escape(theprice) + "|";
						newItemList = newItemList + escape(ivaitem) + "|";
						newItemList = newItemList + escape(codeitem) + "|";
						newItemList = newItemList + escape(scq1) + "|";
						newItemList = newItemList + escape(imp1) + "|";
						newItemList = newItemList + escape(scq2) + "|";
						newItemList = newItemList + escape(imp2) + "|";
						newItemList = newItemList + escape(artlink) + "|"
						newItemList = newItemList + escape(alq) + "|";
						newItemList = newItemList + "!" + escape(acqid) + "!|";
						newItemList = newItemList + escape(newquant) + "]";
					}
				}
				else if (fulllist.substring(i,i+1) == '|') {
					if (thisitem==1) theitem = unescape(fulllist.substring(itemstart, i));
					if (thisitem==2) thecode = unescape(fulllist.substring(itemstart, i));
					if (thisitem==3) artid = unescape(fulllist.substring(itemstart, i));
					if (thisitem==4) option1 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==5) option2 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==6) option3 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==7) option4 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==8) pesoitem = unescape(fulllist.substring(itemstart, i));
					if (thisitem==9) theprice = unescape(fulllist.substring(itemstart, i));
					if (thisitem==10) ivaitem = unescape(fulllist.substring(itemstart, i));
					if (thisitem==11) codeitem= unescape(fulllist.substring(itemstart, i));
					if (thisitem==12) scq1 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==13) imp1 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==14) scq2 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==15) imp2 = unescape(fulllist.substring(itemstart, i));
					if (thisitem==16) artlink = unescape(fulllist.substring(itemstart, i));
					if (thisitem==17) alq = unescape(fulllist.substring(itemstart, i));
					if (thisitem==18) acqid = unescape(fulllist.substring(itemstart +1, i -1));
					thisitem++;
					itemstart=i+1;
				}
			}
			index = document.cookie.indexOf(nc);
			//document.cookie=nc + "="+newItemList + MakeCookieExpirationH(72);
			document.cookie=nc + "="+newItemList;
			if (!lnk) lnk = "cart.php";
			self.location = lnk;
		}
	}

	function EliminaArt(itemno, lnk) {
		newItemList = null;
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				theitem = fulllist.substring(itemstart, itemend);
				itemlist=itemlist+1;
				if (itemlist != itemno) {
					newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
				}
			}
		}
		index = document.cookie.indexOf(nc);
		//document.cookie=nc + "="+newItemList + MakeCookieExpirationH(72);
		document.cookie=nc + "="+newItemList ;
		if (!lnk) lnk = "cart.php";
		self.location = lnk;
	}
	
	function ConvertiValuta2(value) {
		if (PutDec !=0) {
			value = Math.round(value * 100) / 100;
		}
		else {
			value = Math.round(value);
		}
		value2 = value / exch;
		if (PutDec2 !=0) {
			if (value2<=0.99) {
				newPounds = '0';
			} else {
				newPounds = parseInt(value2);
			}
			newPence = Math.round((value2 - newPounds)* 100);
			if (newPence == 100) {newPence =0; newPounds ++;}
			if (eval(newPence) <= 9) newPence='0'+newPence;
			newString = MettiPunti(newPounds);
			if (eval(newPence) != 0) {
				newString= newString + sepdec + newPence;
			}
			else {
				newString= newString + sepdec + '00';
			}
		}
		else {
			newPounds = Math.round(value2);
			newString = MettiPunti(newPounds);
		}
		return (newString);
	}

	function SvuotaCarrello() {
		if (totprice <= 0) {
		rc = alert("Attenzione!\nNel tuo carrello non è presente nessun articolo!");
		return false;
		}
		if (confirm("Vuoi veramente rimuovere tutti gli articoli dal carrello?")) {
			index = document.cookie.indexOf(nc);
			document.cookie= nc + "=.";
			self.location = "cart.php";
		}
	}
	
	function saveCart(l) {
		nc = "eDenter_basket" + l;
		index = document.cookie.indexOf(nc);
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		if (countend == -1) {
			countend = document.cookie.length;
		}
		if (index == -1) {
			countbegin = 0;
			countend = 0;
		}
		else {
			fulllist = document.cookie.substring(countbegin, countend);
			document.cookie = nc + "=" + fulllist + MakeCookieExpirationH(ckDays * 24);
			alert("Il tuo carrello sarà salvato per " + ckDays + " giorni!");
			//self.location = "cart.php";
			self.location.reload();
		}
	}


