function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + value +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function vedicarrello() {
	if (LANG == 0) { c_page = "carrello"; } else { c_page = "cart"; }
	window.open(c_page+".htm","_top");
}

function primavolta(articolo, prezzo, quant) {
	var VALORE_COOKIE = '#@'+articolo+'@'+prezzo+'@'+quant+'##';
	var NOME_COOKIE = "Order";
	pathname = location.pathname;
	myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
	var largeExpDate = new Date ();
	largeExpDate.setTime(largeExpDate.getTime()+(365 * 24 * 3600 * 1000));
	SetCookie(NOME_COOKIE,VALORE_COOKIE,largeExpDate,myDomain);
	var now = new Date();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	var year = now.getYear();
	year = ((year < 2000) ? 1900 : 0) + year;
	month = ((month < 10) ? "0" : "") + month;
	date = ((date < 10) ? "0" : "") + date;
	
	if ( codice_personale != "" && codice_personale != null && codice_personale != "null" ) {
		var ordercode = codice_personale + month + date;
	} else {
		var quotenumber = 22 ;
		var randomnumber = Math.random() ;
		var rand1 = Math.round( (quotenumber-1) * randomnumber) + 1 ;
		var randomnumber = Math.random() ;
		var rand2 = Math.round( (quotenumber-1) * randomnumber) + 1 ;
		var randomnumber = Math.random() ;
		var rand3 = Math.round( (quotenumber-1) * randomnumber) + 1 ;
		var randomnumber = Math.random() ;
		var rand4 = Math.round( (quotenumber-1) * randomnumber) + 1 ;
		quotes = new Array
		quotes[1] = "A"
		quotes[2] = "B"
		quotes[3] = "C"
		quotes[4] = "D"
		quotes[5] = "E"
		quotes[6] = "F"
		quotes[7] = "G"
		quotes[8] = "H"
		quotes[9] = "J"
		quotes[10] = "K"
		quotes[11] = "M"
		quotes[12] = "N"
		quotes[13] = "P"
		quotes[14] = "R"
		quotes[15] = "S"
		quotes[16] = "T"
		quotes[17] = "U"
		quotes[18] = "V"
		quotes[19] = "W"
		quotes[20] = "X"
		quotes[21] = "Y"
		quotes[22] = "Z"
		var ordercode = quotes[rand1] + quotes[rand2] + quotes[rand3] + quotes [rand4] + month + date;
	}

	var VALORE_COOKIE = ordercode;
	var NOME_COOKIE = "ckordercode";

	SetCookie(NOME_COOKIE,VALORE_COOKIE,largeExpDate,myDomain);

	var prezzo_unitario_euro = parseMoney(prezzo,CENTS,true);
	var prezzo_unitario_usd = parseMoney(Math.round(prezzo*cambio)/100,CENTS,true);
	var prezzo_totale_euro = parseMoney((prezzo*quant),CENTS,true);
	var prezzo_totale_usd = parseMoney(Math.round(prezzo*quant*cambio)/100,CENTS,true);
	if (LANG == 0) {
		alert ("Le è stato assegnato un nuovo carrello con codice «"+ordercode+"»\ned è stato inserito nel carrello il seguente catalogo:\n\nDescrizione: " + articolo + "\nPrezzo: Euro " + prezzo_unitario_euro + "  ($ " + prezzo_unitario_usd + ")\nQuantità: " + quant + "\nTotale articolo: Euro " + prezzo_totale_euro + "  ($ " + prezzo_totale_usd + ")");
	} else {
		alert ("You\'ve got a new shopping cart with code «"+ordercode+"»\nand the following catalog has been inserted into the cart:\n\nDescription: " + articolo + "\nPrice: Euro " + prezzo_unitario_euro + "  ($ " + prezzo_unitario_usd + ")\nQuantity: " + quant + "\nTotal amount: Euro " + prezzo_totale_euro + "  ($ " + prezzo_totale_usd + ")");
	}
}

function aggiungi2(articolo, prezzo, quant) {
	var sost=0;
	var sostflag=0;
	var subpuntatore=0;
	var NOME_COOKIE="Order";

	function getCookieVal (offsetb) {
		var endstrb = document.cookie.indexOf (";", offsetb);
		if (endstrb == -1)
		endstrb = document.cookie.length;
		return unescape(document.cookie.substring(offsetb, endstrb));
	}
	function GetCookie (searchcookie) {
		var argb = searchcookie + "=";
		var alenb = argb.length;
		var clenb = document.cookie.length;
		var ib = 0;
		while (ib < clenb) {
			var jb = ib + alenb;
			if (document.cookie.substring(ib, jb) == argb)
			return getCookieVal (jb);
			ib = document.cookie.indexOf(" ", ib) + 1;
			if (ib == 0) break;
		}
		return null;
	}

	VALORE_COOKIE = GetCookie(NOME_COOKIE);

	var string = "Order=" + VALORE_COOKIE;

	var puntatore=string.lastIndexOf("Order=#");
	var lung=string.indexOf("##");

	if (puntatore == -1) {

		primavolta(articolo, prezzo, quant);

		} else {
			var newstring="#";
			while (puntatore<=lung) {
				sost=0;
				if (string.charAt(puntatore) == "@") {
					puntatore++;
					subpuntatore=puntatore;
					while (string.charAt(puntatore) != "@") puntatore++;
					if (string.substring(subpuntatore, puntatore) == articolo) sost=1;
					newstring+="@"+string.substring(subpuntatore, puntatore);
					puntatore++;
					subpuntatore=puntatore;
					while (string.charAt(puntatore) != "@") puntatore++;
					newstring+="@"+string.substring(subpuntatore, puntatore);
					puntatore++;
					subpuntatore=puntatore;
					while ((string.charAt(puntatore) != "@") && (string.charAt(puntatore) != "#")) puntatore++;
					if (sost == 1) {
						var somma=parseInt(quant);
						somma+=parseInt(string.substring(subpuntatore, puntatore));
						if (somma > 999) somma=999;
						newstring+="@"+somma;
						sost=0;
						sostflag=1;
						}
					else newstring+="@"+string.substring(subpuntatore, puntatore);
					puntatore--;
					}
				puntatore++;
			}

		if (sostflag == 1) {
			newstring+="##";
			} else {
			newstring=string.substring(6,lung)+"@"+articolo+"@"+prezzo+"@"+quant+"##";
			}

		var VALORE_COOKIE = newstring;
		var NOME_COOKIE = "Order";
		pathname = location.pathname;
		myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		var largeExpDate = new Date ();
		largeExpDate.setTime(largeExpDate.getTime()+(365 * 24 * 3600 * 1000));
		SetCookie(NOME_COOKIE,VALORE_COOKIE,largeExpDate,myDomain);
		var prezzo_unitario_euro = parseMoney(prezzo,CENTS,true);
		var prezzo_unitario_usd = parseMoney(Math.round(prezzo*cambio)/100,CENTS,true);
		var prezzo_totale_euro = parseMoney((prezzo*quant),CENTS,true);
		var prezzo_totale_usd = parseMoney(Math.round(prezzo*quant*cambio)/100,CENTS,true);
		if (LANG == 0) {
			alert ("E' stato aggiunto al carrello il seguente catalogo:\n\nDescrizione: " + articolo + "\nPrezzo: Euro " + prezzo_unitario_euro + "  ($ " + prezzo_unitario_usd + ")\nQuantità: " + quant + "\nTotale articolo: Euro " + prezzo_totale_euro + "  ($ " + prezzo_totale_usd + ")");
		} else {
			alert ("The following catalog has been inserted into the cart:\n\nDescription: " + articolo + "\nPrice: Euro " + prezzo_unitario_euro + "  ($ " + prezzo_unitario_usd + ")\nQuantity: " + quant + "\nTotal amount: Euro " + prezzo_totale_euro + "  ($ " + prezzo_totale_usd + ")");
		}
		}
}

function aggiungi(n) {
	var articolo = CATS[n][0] + " - " + CATS[n][1]; 

	items = articolo;
	var charect = "";
	for (var x = 1 ; x <= items.length ; x++) 
	{
	if (items.substring(x-1,x) == "'") 
	{ charect+="´"; }
	else 
	{ charect+=items.substring(x-1,x); }
	}
	articolo = charect;

	var prezzo = CATS[n][7]; 
	var quant = eval("document.FORM.quant"+n+".value");
	var NOME_COOKIE="Order";
	function getCookieVal (offsetb) {
		var endstrb = document.cookie.indexOf (";", offsetb);
		if (endstrb == -1)
		endstrb = document.cookie.length;
		return unescape(document.cookie.substring(offsetb, endstrb));
		}
	function GetCookie (searchcookie) {
		var argb = searchcookie + "=";
		var alenb = argb.length;
		var clenb = document.cookie.length;
		var ib = 0;
		while (ib < clenb) {
			var jb = ib + alenb;
			if (document.cookie.substring(ib, jb) == argb)
			return getCookieVal (jb);
			ib = document.cookie.indexOf(" ", ib) + 1;
			if (ib == 0) break;
			}
		return null;
		}
	VALORE_COOKIE = GetCookie(NOME_COOKIE);

	check_flag = 0;

	if (articolo=="" || articolo==null || articolo=="null") {
		if (LANG==0) {
			alert("Selezionare il catalogo desiderato\ndall'apposito elenco.");
		} else {
			alert("Please select the desired catalog\nfrom the suitable list.");
		}
		check_flag = 1;
	}
	
	if ((prezzo==0 || prezzo=="" || prezzo==null || prezzo=="null") && check_flag==0) {
		if (LANG==0) {
			alert("Valorizzare il prezzo del catalogo espresso in Euro.\nIndicare sempre i decimali dell'importo anche se non presenti\n(es. 23 Euro = 23.00).");
		} else {
			alert("Please insert the price of the catalog expressed in Euro\nYou must always declare the decimals even if they are not present\n(e.g. 23 Euro becomes 23.00).");
		}
		check_flag = 1;
	}
	
	if ((isNaN(quant) || quant<1 || quant>999) && check_flag==0) {
		if (LANG==0) {
			alert("Specificare la quantità di cataloghi desiderati.");
		} else {
			alert("Please insert the quantity of desired catalogs.");
		}
		check_flag = 1;
	}

	if (check_flag==0) {
		if (VALORE_COOKIE==null || VALORE_COOKIE=="" || VALORE_COOKIE=="null" || VALORE_COOKIE=="0") {
			primavolta(articolo, prezzo, quant);
		} else {
			aggiungi2(articolo, prezzo, quant);
		}
	}

}
