function checkq(id) {
	if (document.getElementById("q" + id).value == "") {
		alert("Per aggiungere un prodotto al carrello è necessario specificare la quantità desiderata");
		document.getElementById("q" + id).focus()
		return false;
	}
	else
	{
		var str = document.getElementById("q" + id).value
		if (str.match(/\D/)) {
			alert("Indicare la quantità desiderata con caratteri numerici");
			document.getElementById("q" + id).focus()
			return false;
		}
		else {
			if (parseInt(document.getElementById("q" + id).value) < parseInt(document.getElementById("qminima" + id).value)) {
				alert("La quantità minima acquistabile per questo prodotto è di " + document.getElementById("qminima" + id).value + " unità");
				document.getElementById("q" + id).value = document.getElementById("qminima" + id).value
				document.getElementById("q" + id).focus()
				return false;
			}
			else if (parseInt(document.getElementById("q" + id).value) > parseInt(document.getElementById("qmassima" + id).value))
			{
				alert("La quantità massima acquistabile per questo prodotto è di " + document.getElementById("qmassima" + id).value + " unità");
				document.getElementById("q" + id).value = document.getElementById("qmassima" + id).value
				document.getElementById("q" + id).focus()
				return false;
			}
			{
				//si prende l'action del form
				var action = $("prd" + id).action
				var url = window.location.href;
				//se è un prodotto normale e non si sta nella pagina del carrello si usa l'ajax per il carrello virtuale
				if (action.contains('addto.asp') && url.indexOf("carrello.asp") == -1) {
					addToCart(id);
					return false;
				}
				else {
					var idn = document.getElementById("idn");
					if (idn != null && (idn.value == 10281 || idn.value == 10401 || idn.value == 10448 || idn.value == 10447 || idn.value == 10445) && url.indexOf("carrello.asp") == -1)
					//Controllo se sono bomboniere perché in tal caso devo passare per la personalizzazione pur mettendo il prodotto nel carrello
					{
						addToCart(id);
						//Imposto i campi per la personalizzazione a quelli per la personalizzazione
						//var idfield = document.getElementById("id");
						//var personalizationID = document.getElementById("personalizationID");
						//alert(idfield.value);
						//alert(personalizationID.value);
						//idfield.value = personalizationID.value;
						//alert(idfield.value);
						//alert(personalizationID.value);
						//var idcfield = document.getElementById("idc");
						//idcfield = "gcnjicdo";
						//alert(id);
					}
					return true;
				}
			}
		}
	}
}

function addToCart(id)
{
	var firstProduct
	var date = new Date()
	var myHTMLRequest = new Request.HTML({ url: 'addto.asp', method: 'get',
		onRequest: function() {
			firstProduct = false
			//si controlla se è il primo prodotto inserito
			if ($("cartBox").getStyle("display") == "none") {
				firstProduct = true
				//$("cartList").setStyle("opacity", "0")
				$("cartBox").setStyle("display", "block")
				$("cartBox").get('tween', { property: 'opacity', duration: 'long' }).start(0, 1);
			}
			//nel caso fosse visibile il box dei messaggi
			if ($("cart-msg-err").getStyle("display") == "block") {
				$("cart-msg-err").setStyle("display", "none");
			}
			//nel caso già esista prima viene cancellata
			if ($('dt' + $('id' + id).value)) $('dt' + $('id' + id).value).dispose();
			if ($('dd' + $('id' + id).value)) $('dd' + $('id' + id).value).dispose();
			//si aggiunge la gif loading
			var dt = new Element('dt', { 'id': 'dt' + $('id' + id).value });
			var img = new Element('img', { 'src': '/files/condivisi/icone/loading.gif' });
			dt.adopt(img)
			$("cartList").adopt(dt)
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var obj = JSON.decode(responseHTML);
			if (obj.code == "1") {
				//si cancella il dt con l'immagine di loading
				if ($('dt' + $('id' + id).value)) $('dt' + $('id' + id).value).dispose();
				if (obj.prdQuantity > 0) {
					//si aggiunge una lista al carrello
					var dt = new Element('dt', { 'id': 'dt' + $('id' + id).value });
					var a = new Element('a', { 'href': "dettaglio_prodotto.asp?id=" + $("id" + id).value }).set("html", obj.prdQuantity + " " + obj.prdTitle + " &#8364;" + obj.prdPrice);
					dt.adopt(a)
					var dd = new Element('dd', { 'id': 'dd' + $('id' + id).value });
					//				var ul = new Element('ul');
					//				var li1 = new Element('li').setProperty("html", "<strong>Qt&agrave;.</strong>: " + obj.prdQuantity);
					//				var li2 = new Element('li').setProperty("html", "<strong>Prezzo</strong>: " + obj.prdPrice + "&#8364;");
					//				ul.adopt(li1)
					//				ul.adopt(li2)
					//				dd.adopt(ul)
					$("cartList").adopt(dt)
					$("cartList").adopt(dd)
					//si aggiorna il totale
					$("cartTotal").setProperty("html", "<strong>Totale:</strong> " + obj.cartTot + "&#8364;");
					//effetto background sul dt appena inserito
					if (!firstProduct) $('dt' + $('id' + id).value).get('tween', { property: 'background-color', duration: 'long' }).start('#1290E4', '#fff');
					alert("Il prodotto è stato aggiunto nel carrello.")
				}
				//se ci sono dei messaggi di disponibilità mancante				
				if (obj.msg != "") {
					if ($("cart-msg-err").getStyle("display") == "none") $("cart-msg-err").setStyle("display", "block")
					var p = new Element('p').set("html", obj.msg);
					$("cart-msg-err").empty();
					$("cart-msg-err").adopt(p)
				}
			}
			else {
				//si cancella il dt con l'immagine di loading
				if ($('dt' + $('id' + id).value)) $('dt' + $('id' + id).value).dispose();
				// si inserisce il messaggio
				if ($("cart-msg-err").getStyle("display") == "none") $("cart-msg-err").setStyle("display", "block")
				var p = new Element('p').setProperty("html", obj.msg);
				$("cart-msg-err").empty();
				$("cart-msg-err").adopt(p)
			}
		},
		onException: function(headerName, value) {
			//si cancella il dt con l'immagine di loading
			if ($('dt' + $('id' + id).value)) $('dt' + $('id' + id).value).dispose();
			// si inserisce il messaggio
			if ($("cart-msg-err").getStyle("display") == "none") $("cart-msg-err").setStyle("display", "block")
			var p = new Element('p').setProperty("html", "Il prodotto non è stato inserito. Riprovare.");
			$("cart-msg-err").empty();
			$("cart-msg-err").adopt(p)
		}
	}
	).send("ajax=1&id=" + $("id" + id).value + "&q=" + $("q" + id).value + "&mm=" + $("mm" + id).value + "&t=" + date.getTime());
}

function showimg(arg_img_tipo, arg_img_name, idprodotto)
{
	if (arg_img_name == '') {
		arg_img_name = 'files/foto/' + arg_img_tipo + '.jpg'
	}
	else {
		var now = new Date();
		arg_img_name = 'files/foto/' + arg_img_name + '?t=' + now.getTime()
	}
	document.getElementById(arg_img_tipo + idprodotto).src = arg_img_name;
}

function showimgdett(arg_img_tipo, arg_img_name, idprodotto, width, height) {
	if (arg_img_name == '') {
		arg_img_name = 'files/foto/' + arg_img_tipo + '.jpg'
	}
	else {
		var now = new Date();
		arg_img_name = 'files/foto/' + arg_img_name + '?t=' + now.getTime()
	}
	document.getElementById(arg_img_tipo + idprodotto).src = arg_img_name;
	document.getElementById(arg_img_tipo + idprodotto).width = width;
	document.getElementById(arg_img_tipo + idprodotto).height = height;
}

function showbanner(arg_img_tipo, arg_img_name, idprodotto)
{
	if (arg_img_name == '') {
		arg_img_name = 'files/banners/' + arg_img_tipo + '.gif'
	}
	else {
		var now = new Date();
		arg_img_name = 'files/banners/' + arg_img_name + '?t=' + now.getTime()
	}
	document.getElementById(arg_img_tipo + idprodotto).src = arg_img_name;
}

function openwin(url, name, attr) {
	window.open(url, name, attr)
}

function URLEncode(arg) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" + 				// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()"; 				// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var plaintext = arg
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+"; 			// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert("Unicode Character '" +
							ch +
							"' cannot be encoded using standard URL encoding.\n" +
							"(URL encoding only supports 8-bit characters.)\n" +
							"A space (+) will be substituted.");
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};


function goToUrl(arg, id, idc) {
	if (arg != "") {
		location.href = "spot_count.asp?s=" + URLEncode(arg)
	}
	else {
		location.href = "dettaglio_prodotto.asp?id=" + id + "&idc=" + idc
	}
}
