function apri_listini(list) {
	if (LANG == 0) { l_page = "listini"; } else { l_page = "lists"; }
	window.open(l_page+'.htm?'+list,'listini','resizable=yes,menubar=no,toolbar=yes,locationbar=no,scrollbars=yes,statusbar=no,width=780,height=550');
}

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" : "");
}

var numprod=0;
var sel_country = "";
var sel_condition = "";

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 è stata inserita nel carrello la seguente banconota:\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 banknote 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' stata aggiunta al carrello la seguente banconota:\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 banknote 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() {
	var codice = document.carrello.articolo.value;
	var prezzo = document.carrello.prezzo.value;
	var quant = parseInt(document.carrello.quantita.value);

	items = codice;
	var charect = "";
	for (var x = 1 ; x <= items.length ; x++) 
	{
	if (items.substring(x-1,x) == "'") 
	{ charect+="´"; }
	else 
	{ charect+=items.substring(x-1,x); }
	}
	codice = charect;

	check_flag = 0;

	if (codice=="" || codice==null || codice=="null") {
		if (LANG==0) {
			alert("Inserire la descrizione della banconota desiderata\nnel seguente formato:\n\n[paese] [pick] (firma) [data] [condizioni] [valore facciale].");
		} else {
			alert("Please insert the description of the desired banknotes\nby the following format:\n\n[country] [pick] (signature) [date] [conditions] [face value].");
		}
		check_flag = 1;
	}
	
	if ((prezzo<=0 || prezzo=="" || prezzo==null || prezzo=="null") && check_flag==0) {
		if (LANG==0) {
			alert("Valorizzare il prezzo della banconota 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 banknotes 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 banconote desiderate.");
		} else {
			alert("Please insert the quantity of desired banknotes.");
		}
		check_flag = 1;
	}

	if (check_flag==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);

		if (VALORE_COOKIE==null || VALORE_COOKIE=="" || VALORE_COOKIE=="null" || VALORE_COOKIE=="0") {
			primavolta(codice, prezzo, quant);
		} else {
			aggiungi2(codice, prezzo, quant);
		}
		document.carrello.articolo.value = document.carrello.articolo.defaultValue;
		document.carrello.prezzo.value = document.carrello.prezzo.defaultValue;
		document.carrello.prezzo_euro.value = document.carrello.prezzo_euro.defaultValue;
		document.carrello.prezzo_usd.value = document.carrello.prezzo_usd.defaultValue;
		document.carrello.quantita.value = document.carrello.quantita.defaultValue;
	}

}

function componi() {

	var stato = sel_country
	var pick = document.carrello.pick.value
	var firma = document.carrello.sign.value
	var data = document.carrello.date.value
	var cond = sel_condition
	var valore = document.carrello.facevalue.value

	check_flag = 0;

	if (stato=="" || stato==null || stato=="null") {
		if (LANG==0) {
			alert("Selezionare lo stato della banconota desiderata\ndall\'apposito elenco.");
		} else {
			alert("Please select the country of the desired banknote\nfrom the suitable list.");
		}
		check_flag = 1;
	}
	
	if ((pick=="" || pick==null || pick=="null") && check_flag==0) {
		if (LANG==0) {
			alert("Inserire il codice «PICK» della banconota desiderata.\n\nSe la banconota è di recente emissione e, pertanto,\nnon ha ancora un codice ufficiale, inserire «NEW».\n\nSe la banconota non ha un codice ufficiale, inserire\nla sigla «N.D.».");
		} else {
			alert("Please insert the «PICK» code of the desired banknote.\n\nIf the banknote has been recently issued, so it has\nnot an official code, insert «NEW».\n\nIf the banknote has never had an official code, insert «N.D.».");
		}
		check_flag = 1;
	}

	if ((data=="" || data==null || data=="null") && check_flag==0) {
		if (LANG==0) {
			alert("Inserire la data di emissione della banconota. La data\npuò essere intera (es. 12/10/1984 oppure 12.10.1984) o può\ncomprendere solo l'anno di emissione (es. 1984).\n\nSe non si conosce l\'anno di emissione inserire «N.D.».");
		} else {
			alert("Please insert the date of issue of the banknotes. The date\ncould be complete (e.g. 12/10/1984 or 12.10.1984) or can\nbe only the year of issue (e.g. 1984).\n\nIf you don\'t know the date of issue please insert «N.D.».");
		}
		check_flag = 1;
	}

	if ((cond=="" || cond==null || cond=="null") && check_flag==0) {
		if (LANG==0) {
			alert("Selezionare il codice relativo alle condizioni formali\ndella banconota desiderata.\n\nIl codice è pubblicato nel listino.");
		} else {
			alert("Please select the code of the formal conditions of the\ndesiredbanknote.\n\nThe code is published into the price-list.");
		}
		check_flag = 1;
	}

	if ((valore=="" || valore==null || valore=="null") && check_flag==0) {
		if (LANG==0) {
			alert("Inserire il valore facciale della banconota\n(es. 100 dollars, 1000 lire, ecc.).");
		} else {
			alert("Please insert the face value of the banknote\n(e.g. 100 dollars, 1000 lire, etc.).");
		}
		check_flag = 1;
	}

	if (check_flag==0) {
		if ((firma == null) || (firma == "null") || (firma == "")) {
			document.carrello.articolo.value = stato + " " + pick + " " + data + " " + cond + " " + valore;
		} else {
			document.carrello.articolo.value = stato + " " + pick + " (" + firma + ") " + data + " " + cond + " " + valore;
		}
	}
}

function SwitchEuro() {
	items = document.carrello.prezzo_euro.value;

	var charect = "";
	for (var n = 1 ; n <= items.length ; n++) 
	{
	if (items.substring(n-1,n) == "€") 
	{ charect+=""; }
	else 
	{ charect+=items.substring(n-1,n); }
	}
	items = charect;

	var charect = "";
	for (var n = 1 ; n <= items.length ; n++) 
	{
	if (items.substring(n-1,n) == ",") 
	{ charect+="."; }
	else 
	{ charect+=items.substring(n-1,n); }
	}
	
	document.carrello.prezzo_euro.value = parseMoney(Math.round(charect*100),CENTS,true);
	document.carrello.prezzo.value = Math.round(document.carrello.prezzo_euro.value*100);
	document.carrello.prezzo_usd.value = parseMoney(Math.round(document.carrello.prezzo.value*cambio)/100,CENTS,true);
} 
