/****************************************************************************
Copyright (c) 2009 The Wojo Group

thewojogroup.com
simplecartjs.com
http://github.com/thewojogroup/simplecart-js/tree/master

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}

function VerificaNumerico(obj)
{
var valore = obj.value;
if(isNaN(valore))
{
alert("Attenzione, il numero di Telefono inserito non è corretto");
obj.select();
obj.focus();
}
}
function VerificaIva(obj)
{
var valore = obj.value;
if(isNaN(valore))
{
alert("Attenzione, la Partita IVA inserita non è corretta");
obj.select();
obj.focus();
}
}

function CaricaPagina(pagina) {
  var url = ""; // = document.URL.substr(0, document.URL.lastIndexOf("/") + 1);

  if (typeof XMLHttpRequest != "undefined") {
    x = new XMLHttpRequest();
  } else {
    try { x = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
      try { x = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) { x = null; }
    }
  }

  if (x) {
    x.onreadystatechange = function() { if (x.readyState == 4 && x.status == 200) { document.forms["form1"].submit(); } }
    x.open("GET", url + pagina, true);
    x.send(null);
  }
}

function copyAddr_modulo() {document.modulo.indirizzo_spedizione.value = document.modulo.indirizzo.value;}
function copyCap_modulo() {document.modulo.cap_spedizione.value = document.modulo.cap.value;}
function copyCity_modulo() {document.modulo.citta_spedizione.value = document.modulo.citta.value;}
function copyProv_modulo() {document.modulo.provincia_spedizione.value = document.modulo.provincia.value;}
function copyAddr_ppPro() {document.pp_data.indirizzo_spedizione.value = document.pp_data.indirizzo.value;}
function copyCap_ppPro() {document.pp_data.cap_spedizione.value = document.pp_data.cap.value;}
function copyCity_ppPro() {document.pp_data.citta_spedizione.value = document.pp_data.citta.value;}
function copyProv_ppPro() {document.pp_data.provincia_spedizione.value = document.pp_data.provincia.value;}

function submitModulo() {
	document.modulo.divcontent.value = document.getElementById('riepilogo').innerHTML;
	document.modulo.ordine.value = readCookie("simpleCart_ref"); 
	document.modulo.pagamento.value = document.getElementById('selezione').innerHTML;
}

function checkMail_Cheque() {
	return ControllaMail();
}
function checkMail_ppPro() {
	return ControllaMail("pp_pro");
}

function ControllaMail(form)
   {
	switch (form) {
		case "pp_pro":
			destinazione = 1;
			EmailAddr = document.pp_data.mail.value;
			break;
		default:
			destinazione = 0;
			EmailAddr = document.modulo.mail.value;
			break;
	}
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(EmailAddr))
      return true;
   else
      {
      alert("Attenzione, l'indirizzo E-Mail inserito non è corretto");
		if (destinazione==0)
			document.modulo.mail.focus();
		if (destinazione==1)
			document.pp_data.mail.focus();
      return false;
	  }
}
function control(val){

	switch( val ){
		case "pp_pro": // pp pro
			//fatturazione
			if (trim(document.pp_data.nome.value)=="") {
			  alert("Attenzione, il campo Nome è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.cognome.value)=="") {
			  alert("Attenzione, il campo Cognome è obbligatorio ");
			  return false;
			}
			if (trim(document.pp_data.telefono.value)=="") {
			  alert("Attenzione, il campo Telefono è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.mail.value)=="") {
			  alert("Attenzione, il campo E-mail è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.indirizzo.value)=="") {
			  alert("5ttenzione, il campo Indirizzo è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.cap.value)=="") {
			  alert("Attenzione, il campo CAP è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.citta.value)=="") {
			  alert("Attenzione, il campo Città è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.provincia.value)=="") {
			  alert("Attenzione, il campo Provincia è obbligatorio");
			  return false;
			}
			//spedizione
			if (trim(document.pp_data.indirizzo_spedizione.value)=="") {
			  alert("Attenzione, il campo Indirizzo (Spedizione) è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.cap_spedizione.value)=="") {
			  alert("Attenzione, il campo CAP (Spedizione) è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.citta_spedizione.value)=="") {
			  alert("Attenzione, il campo Città (Spedizione) è obbligatorio");
			  return false;
			}
			if (trim(document.pp_data.provincia_spedizione.value)=="") {
			  alert("Attenzione, il campo Provincia (Spedizione) è obbligatorio");
			  return false;
			}
			return true;
			break;
			
			default: // contrassegno
			//fatturazione
			if (trim(document.modulo.nome.value)=="") {
			  alert("Attenzione, il campo Nome è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.cognome.value)=="") {
			  alert("Attenzione, il campo Cognome è obbligatorio ");
			  return false;
			}
			if (trim(document.modulo.telefono.value)=="") {
			  alert("Attenzione, il campo Telefono è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.mail.value)=="") {
			  alert("Attenzione, il campo E-mail è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.indirizzo.value)=="") {
			  alert("Attenzione, il campo Indirizzo è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.cap.value)=="") {
			  alert("Attenzione, il campo CAP è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.citta.value)=="") {
			  alert("Attenzione, il campo Città è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.provincia.value)=="") {
			  alert("Attenzione, il campo Provincia è obbligatorio");
			  return false;
			}
			//spedizione
			if (trim(document.modulo.indirizzo_spedizione.value)=="") {
			  alert("Attenzione, il campo Indirizzo (Spedizione) è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.cap_spedizione.value)=="") {
			  alert("Attenzione, il campo CAP (Spedizione) è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.citta_spedizione.value)=="") {
			  alert("Attenzione, il campo Città (Spedizione) è obbligatorio");
			  return false;
			}
			if (trim(document.modulo.provincia_spedizione.value)=="") {
			  alert("Attenzione, il campo Provincia (Spedizione) è obbligatorio");
			  return false;
			}
			return true;
			break;

	}
}

var NextId=1,Custom="Custom",GoogleCheckout="GoogleCheckout",PayPal="PayPal",Email="Email",AustralianDollar=AUD="AUD",CanadianDollar=CAD="CAD",CzechKoruna=CZK="CZK",DanishKrone=DKK="DKK",Euro=EUR="EUR",HongKongDollar=HKD="HKD",HungarianForint=HUF="HUF",IsraeliNewSheqel=ILS="ILS",JapaneseYen=JPY="JPY",MexicanPeso=MXN="MXN",NorwegianKrone=NOK="NOK",NewZealandDollar=NZD="NZD",PolishZloty=PLN="PLN",PoundSterling=GBP="GBP",SingaporeDollar=SGD="SGD",SwedishKrona=SEK="SEK",SwissFranc=CHF="CHF",USDollar=USD="USD";
function Cart(){

	var me = this;
	/* member variables */
	me.Version = '2.0.1';
	me.Shelf = new Shelf();
	me.items = {};
	me.isLoaded = false;
	me.pageIsReady = false;
	me.quantity = 0;
	me.total = 0;
	me.taxRate = 0;
	me.taxCost = 0;
	me.shippingFlatRate = 0;
	me.shippingTotalRate = 0;
	me.shippingQuantityRate = 0;
	me.shippingRate = 0;
	me.shippingCost = 0;
	me.currency = USD;
	me.checkoutTo = PayPal;
	me.email = "";
	me.merchantId	 = "";
	me.cartHeaders = ['Nome','Prezzo','Quantità','Totale'];
	/* 
		cart headers: 
		you can set these to which ever order you would like, and the cart will display the appropriate headers
		and item info.  any field you have for the items in the cart can be used, and 'Total' will automatically 
		be price*quantity.  
		
		there are keywords that can be used:
			
			1) "_input" - the field will be a text input with the value set to the given field. when the user
				changes the value, it will update the cart.  this can be useful for quantity. (ie "Quantity_input")
			
			2) "increment" - a link with "+" that will increase the item quantity by 1
			
			3) "decrement" - a link with "-" that will decrease the item quantity by 1
			
			4) "remove" - a link that will remove the item from the cart 
			
			5) "_image" or "Image" - the field will be an img tag with the src set to the value. You can simply use "Image" if
				you set a field in the items called "Image".  If you have a field named something else, like "Thumb", you can add
				the "_image" to create the image tag (ie "Thumb_image").
				
			6) "_noHeader" - this will skip the header for that field (ie "increment_noHeader")
		
	
	*/
	
	


	/******************************************************
			add/remove items to cart  
 	 ******************************************************/

	me.add = function () {
		var me=this;
		/* load cart values if not already loaded */
		if( !me.pageIsReady 	) { 
			me.initializeView(); 
			me.update();	
		}
		if( !me.isLoaded 		) { 
			me.load(); 
			me.update();	
		}
		
		var newItem = new CartItem();
		
		/* check to ensure arguments have been passed in */
		if( !arguments || arguments.length === 0 ){
			error( 'No values passed for item.');
			return;
		}
		var argumentArray = arguments;
		if( arguments[0] && typeof( arguments[0] ) != 'string' && typeof( arguments[0] ) != 'number'  ){ 
			argumentArray = arguments[0]; 
		} 
	
		newItem.parseValuesFromArray( argumentArray );
		newItem.checkQuantityAndPrice();
		
		/* if the item already exists, update the quantity */
		if( me.hasItem(newItem) ) {
			var id=me.hasItem(newItem);
			me.items[id].quantity= parseInt(me.items[id].quantity,10) + parseInt(newItem.quantity,10);
		} else {
			me.items[newItem.id] = newItem;
		}	
		
		me.update();
	};
	
	
	me.remove = function( id ){
		var tempArray = {};
		for( var item in this.items ){
			if( item != id ){ 
				tempArray[item] = this.items[item]; 
			}
		}
		this.items = tempArray;
	};
	
	me.empty = function () {
		simpleCart.items = {};
		simpleCart.update();
	};
	
	/******************************************************
			 item accessor functions
     ******************************************************/

	me.find = function (criteria) {
		if( !criteria )
			return null;
		var results = [];
		for( var next in me.items ){
			var item = me.items[next],
				fits = true;
			for( var name in criteria ){
				if( !item[name] || item[name] != criteria[name] )
					fits = false;
			}
			if( fits )
				results.push( me.next )
		}
		return (results.length == 0 ) ? null : results;
	}

	/******************************************************
			 checkout management 
     ******************************************************/

	me.checkout = function() {
		if( simpleCart.quantity === 0 ){
			error("Cart is empty");
			return;
		}
		switch( simpleCart.checkoutTo ){
			case PayPal:
				simpleCart.paypalCheckout();
				break;
			case GoogleCheckout:
				simpleCart.googleCheckout();
				break;
			case Email:
				simpleCart.emailCheckout();
				break;
			default:
				simpleCart.customCheckout();
				break;
		}
	};
	
	me.paypalCheckout = function() {
		
		if (control("pp_pro") == true) {
		
		//Popolo il form per il submit a PayPal PRO
		var me = this;
		document.form1.subtotal.value = (Math.round((me.finalTotal)*100)/100); //totale (iva inc.)
		document.form1.business.value = me.email; //email
		document.form1.currency_code.value = me.currency; //valuta
		document.form1.invoice.value = readCookie("simpleCart_ref"); //n.ordine //document.getElementById("nCarrello").innerHTML;
		// dati generici
		document.form1.buyer_email.value = trim(document.pp_data.mail.value);
		//document.form1.notify_url.value = "http://www.safescan.it/ppPro_notifica.php?rif=" + readCookie("simpleCart_ref");
		document.getElementById("ppPro_okRitorno").value = "http://www.safescan.it";
		// dati fatturazione
		document.form1.billing_first_name.value = trim(document.pp_data.nome.value);
		document.form1.billing_last_name.value = trim(document.pp_data.cognome.value);
		document.form1.billing_address1.value = trim(document.pp_data.indirizzo.value);
		document.form1.billing_city.value = trim(document.pp_data.citta.value);
		document.form1.billing_state.value = trim(document.pp_data.provincia.value);
		document.form1.billing_zip.value = trim(document.pp_data.cap.value);
		document.form1.billing_country.value = "IT";
		// dati spedizione
		document.form1.first_name.value = trim(document.pp_data.nome.value);
		document.form1.last_name.value = trim(document.pp_data.cognome.value);
		document.form1.address1.value = trim(document.pp_data.indirizzo_spedizione.value);
		document.form1.city.value = trim(document.pp_data.citta_spedizione.value);
		document.form1.state.value = trim(document.pp_data.provincia_spedizione.value);
		document.form1.zip.value = trim(document.pp_data.cap_spedizione.value);
		document.form1.country.value = "IT";
		
		//salvo nel database le informazioni per il ritorno
		var ordine = "rif="+encodeURIComponent(document.form1.invoice.value);
		var totale = "&tot="+encodeURIComponent(document.form1.subtotal.value);
		var cliente = "&mail="+encodeURIComponent(document.form1.buyer_email.value);
		var nome = "&nome="+encodeURIComponent(document.form1.billing_first_name.value);
		var cognome = "&cognome="+encodeURIComponent(document.form1.billing_last_name.value);
		var tel = "&tel="+encodeURIComponent(trim(document.pp_data.telefono.value));
		var fax = "&fax="+encodeURIComponent(trim(document.pp_data.fax.value));
		var rsociale = "&rsociale="+encodeURIComponent(trim(document.pp_data.ragionesociale.value));
		var piva = "&piva="+encodeURIComponent(trim(document.pp_data.iva.value));
		var cf = "&cf="+encodeURIComponent(trim(document.pp_data.codicefiscale.value));
		//fatturazione
		var fatt_indirizzo = "&f_indirizzo="+encodeURIComponent(document.form1.billing_address1.value);
		var fatt_citta = "&f_citta="+encodeURIComponent(document.form1.billing_city.value);
		var fatt_prov = "&f_prov="+encodeURIComponent(document.form1.billing_state.value);
		var fatt_cap = "&f_cap="+encodeURIComponent(document.form1.billing_zip.value);
		//spedizione
		var sped_indirizzo = "&s_indirizzo="+encodeURIComponent(document.form1.address1.value);
		var sped_citta = "&s_citta="+encodeURIComponent(document.form1.city.value);
		var sped_prov = "&s_prov="+encodeURIComponent(document.form1.state.value);
		var sped_cap = "&s_cap="+encodeURIComponent(document.form1.zip.value);
		
		var prodotti_finale = "";
		//preparo i prodotti
		var prodotti = document.getElementById("riepilogo").innerHTML.replace(RegExp("\n", "g"),"").toLowerCase();
		prodotti = prodotti.replace(RegExp('\"', "g"),'');
		prodotti = prodotti.replace(RegExp('"itemcontainer"', "g"),'itemcontainer');
		prodotti = prodotti.replace(RegExp('"itemname"', "g"),'itemname');
		prodotti = prodotti.replace(RegExp('"itemquantity"', "g"),'itemquantity');
		prodotti = prodotti.replace(RegExp('"itemprice"', "g"),'itemprice');
		prodotti = prodotti.replace(RegExp('"itemtotale"', "g"),'itemtotale');
		//dichiaro le classi
		classContainer = '<div class=itemcontainer>';
		className = '<div class=itemname>';
		classQuantita = '<div class=itemquantity>';
		classPrezzo = '<div class=itemprice>';
		classTotale = '<div class=itemtotale>';

		prodotti = prodotti.substr(prodotti.indexOf(classContainer)); // elimina intestazione 
		noggetti = prodotti.split('itemcontainer').length - 1;
		
		for (x = 1; x <= noggetti; x++) {
			
			// leggo il prodotto X ($riga) con tutti i suoi valori (prezzo, qnt, etc)
			riga = prodotti.substr(0, prodotti.indexOf(classContainer, classContainer.length));
			prodotti = prodotti.substr(riga.length); // elimino la riga appena elaborata
			if (riga.length == 0) {
				riga = prodotti;
			}

			// assegno i singoli valori (filtrati) dalla stringa della riga a delle variabili separate
			valore_inizio = riga.indexOf(className) + className.length;
			valore_fine = riga.indexOf("</div>", riga.indexOf(className));
			riga_prod = riga.substr(valore_inizio, valore_fine - valore_inizio);

			valore_inizio = riga.indexOf(classQuantita) + classQuantita.length;
			valore_fine = riga.indexOf("</div>", riga.indexOf(classQuantita));
			riga_qnt = riga.substr(valore_inizio, valore_fine - valore_inizio);

			valore_inizio = riga.indexOf(classPrezzo) + classPrezzo.length;
			valore_fine = riga.indexOf("</div>", riga.indexOf(classPrezzo));
			riga_prezzo = riga.substr(valore_inizio +1, valore_fine - valore_inizio -1);

			valore_inizio = riga.indexOf(classTotale) + classTotale.length;
			valore_fine = riga.indexOf("</div>", riga.indexOf(classTotale));
			riga_totale = riga.substr(valore_inizio +1, valore_fine - valore_inizio -1);
			
			//scrivo i valori nel file finale				
			prodotti_finale += "_PRD:" + riga_prod + "_QNT:" + riga_qnt + "_PRZ:" + riga_prezzo + "_TOT:" + riga_totale;
		}
		prodotti = "&prodotti=" + encodeURIComponent(prodotti_finale);
		var stringone = (ordine+totale+cliente+nome+cognome+tel+fax+rsociale+piva+cf+fatt_indirizzo+fatt_citta+fatt_prov+fatt_cap+sped_indirizzo+sped_citta+sped_prov+sped_cap+prodotti);	
		var url = "http://www.safescan.it/memoOrder.php?"+stringone;	
		CaricaPagina(url);
	
		}
			
	};

	me.COPIA_paypalCheckout = function() {
		
		var me = this,
						
			winpar = "scrollbars,location,resizable,status",
			strn  = "https://securepayments.paypal.com/cgi-bin/acquiringweb" +
					"&cmd=_hosted-payment" +
		   			"&upload=1" +
		        	"&business=" + me.email + 
					"&currency_code=" + me.currency,
			counter = 1,
			itemsString = "";

		if( me.taxRate ){
			strn = strn + 
				"&tax_cart=" +  me.currencyStringForPaypalCheckout( me.taxCost );
		}
		
		for( var current in me.items ){
			var item = me.items[current];
			
			var optionsString = "";
			for( var field in item ){
				if( typeof(item[field]) != "function" && field != "id" && field != "price" && field != "quantity" && field != "name" && field != "shipping") {
					optionsString = optionsString + ", " + field + "=" + item[field] ; 
				}
			}
			optionsString = optionsString.substring(2);
			
			itemsString = itemsString 	+ "&item_name_" 	+ counter + "=" + item.name  +
									 	  "&item_number_" 	+ counter + "=" + counter +
										  "&quantity_"		+ counter + "=" + item.quantity +
										  "&amount_"		+ counter + "=" + me.currencyStringForPaypalCheckout( item.price ) + 
										  "&on0_" 			+ counter + "=" + "Options" + 
										  "&os0_"			+ counter + "=" + optionsString;
			counter++;
		}
		
		if( me.shipping() != 0){
			 itemsString = itemsString 	+ "&item_name_" 	+ counter + "=Shipping"  +
									 	  "&item_number_" 	+ counter + "=" + counter +
										  "&quantity_"		+ counter + "=1" + 
										  "&amount_"		+ counter + "=" + me.currencyStringForPaypalCheckout( me.shippingCost );
		}
		
		
		strn = strn + itemsString ;
		window.open (strn, "paypal", winpar);
	};


	me.googleCheckout = function() {
		var me = this;
		if( me.currency != USD && me.currency != GBP ){
			error( "Google Checkout only allows the USD and GBP for currency.");
			return;
		} else if( me.merchantId === "" || me.merchantId === null || !me.merchantId ){
			error( "No merchant Id for google checkout supplied.");
			return;
		} 
		
		var form = document.createElement("form"),
			counter = 1;
		form.style.display = "none";
		form.method = "POST";
		form.action = "https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/" + 
						me.merchantId;
		form.acceptCharset = "utf-8";
		
		for( var current in me.items ){
			var item 				= me.items[current];
			form.appendChild( me.createHiddenElement( "item_name_" 		+ counter, item.name		) );
			form.appendChild( me.createHiddenElement( "item_quantity_" 	+ counter, item.quantity 	) );
			form.appendChild( me.createHiddenElement( "item_price_" 		+ counter, item.price		) );
			form.appendChild( me.createHiddenElement( "item_currency_" 	+ counter, me.currency 	) );
			form.appendChild( me.createHiddenElement( "item_tax_rate_" 	+ counter, me.taxRate 	) );
			form.appendChild( me.createHiddenElement( "_charset_"					 , ""				) );
			
			var descriptionString = "";
			
			for( var field in item){
				if( typeof( item[field] ) != "function" && 
									field != "id" 		&& 
									field != "quantity"	&& 
									field != "price" )
				{
						descriptionString = descriptionString + ", " + field + ": " + item[field];				
				}
			}
			descriptionString = descriptionString.substring( 1 );
			form.appendChild( me.createHiddenElement( "item_description_" + counter, descriptionString) );
			counter++;
		}
		
		document.body.appendChild( form );
		form.submit();
		document.body.removeChild( form );
	};
	
	
	
	me.emailCheckout = function() {
		return;
	};
	
	me.customCheckout = function() {
		return;
	};




	/******************************************************
				data storage and retrival 
	 ******************************************************/
	
	/* load cart from cookie */
	me.load = function () {
		var me = this;
		/* initialize variables and items array */
		me.items = {};
		me.total = 0.00;
		me.quantity = 0;
		
		/* retrieve item data from cookie */
		if( readCookie('simpleCart') ){
			var data = unescape(readCookie('simpleCart')).split('++');
			for(var x=0, xlen=data.length;x<xlen;x++){
			
				var info = data[x].split('||');
				var newItem = new CartItem();
			
				if( newItem.parseValuesFromArray( info ) ){
					newItem.checkQuantityAndPrice();
					/* store the new item in the cart */
					me.items[newItem.id] = newItem;
				}
 			}
		}
		me.isLoaded = true;
	};
	
	
	
	/* save cart to cookie */
	me.save = function () {
		var dataString = "";
		for( var item in this.items ){
			dataString = dataString + "++" + this.items[item].print();
		}
		createCookie('simpleCart', dataString.substring( 2 ), 30 );
		// scrivo il numero dell'ordine se ci son prodotti
		var nOrder = document.getElementById("nProds").innerHTML;
		if (nOrder != "0") {
			nOrder = "Ordine n. <b>" + readCookie("simpleCart_ref") + "</b>";
		} else {
			nOrder = "nessun prodotto nel carrello";
		}
		document.getElementById("nCarrello").innerHTML = "<i>"+nOrder+"</i><br/>";
	};
	
	

	
		
	/******************************************************
				 view management 
	 ******************************************************/
	
	me.initializeView = function() {
		var me = this;
		me.totalOutlets 			= getElementsByClassName('simpleCart_total');
		me.quantityOutlets 			= getElementsByClassName('simpleCart_quantity');
		me.cartDivs 				= getElementsByClassName('simpleCart_items');
		me.taxCostOutlets			= getElementsByClassName('simpleCart_taxCost');
		me.taxRateOutlets			= getElementsByClassName('simpleCart_taxRate');
		me.shippingCostOutlets		= getElementsByClassName('simpleCart_shippingCost');
		me.finalTotalOutlets		= getElementsByClassName('simpleCart_finalTotal');
		
		me.addEventToArray( getElementsByClassName('simpleCart_checkout') , simpleCart.checkout , "click");
		me.addEventToArray( getElementsByClassName('simpleCart_empty') 	, simpleCart.empty , "click" );
		
		me.Shelf.readPage();
			
		me.pageIsReady = true;
		
	};
	
	
	
	me.updateView = function() {
		me.updateViewTotals();
		if( me.cartDivs && me.cartDivs.length > 0 ){ 
			me.updateCartView(); 
		}
	};
	
	me.updateViewTotals = function() {
		var outlets = [ ["quantity"		, "none"		] , 
						["total"		, "currency"	] , 
						["shippingCost"	, "currency"	] ,
						["taxCost"		, "currency"	] ,
						["taxRate"		, "percentage"	] ,
						["finalTotal"	, "currency"	] ];
						
		for( var x=0,xlen=outlets.length; x<xlen;x++){
			
			var arrayName = outlets[x][0] + "Outlets",
				outputString;
				
			for( var element in me[ arrayName ] ){
				switch( outlets[x][1] ){
					case "none":
						outputString = "" + me[outlets[x][0]];
						break;
					case "currency":
						outputString = me.valueToCurrencyString( me[outlets[x][0]] );
						break;
					case "percentage":
						outputString = me.valueToPercentageString( me[outlets[x][0]] );
						break;
					default:
						outputString = "" + me[outlets[x][0]];
						break;
				}
				me[arrayName][element].innerHTML = "" + outputString;
			}
		}
	};
	
	me.updateCartView = function() {
		var newRows = [],
			x,newRow,item,current,header,newCell,info,outputValue,option,headerInfo;
		
		/* create headers row */
		newRow = document.createElement('div');
		for( header in me.cartHeaders ){
			newCell = document.createElement('div');
			headerInfo = me.cartHeaders[header].split("_");
			 
			newCell.innerHTML = headerInfo[0]; // originale
			newCell.id = "header" + headerInfo[0];
			
			/*switch (headerInfo[0].toLowerCase) {
				case "name":
					newCell.innerHTML = "Nome";
					break;
				default:
					newCell.innerHTML = headerInfo[0];
					break;
			}*/
				
				
			newCell.className = "item" + headerInfo[0];
	
			for(x=1,xlen=headerInfo.length;x<xlen;x++){
				if( headerInfo[x].toLowerCase() == "noheader" ){
					newCell.style.display = "none";
				}
			}
			newRow.appendChild( newCell );
			
		}
		newRow.className = "cartHeaders";
		newRows[0] = newRow;
		

		/* create a row for each item in the cart */
		x=1;
		for( current in me.items ){
			newRow = document.createElement('div');
			item = me.items[current];
			
			for( header in me.cartHeaders ){
				
				newCell = document.createElement('div');
				info = me.cartHeaders[header].split("_");
				/*
				switch( info[0].toLowerCase() ){
					case "total":
						outputValue = me.valueToCurrencyString(parseFloat(item.price)*parseInt(item.quantity,10) );
						break;
					case "increment":
						outputValue = me.valueToLink( "+" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].increment();\"" );
						break;
					case "decrement":
						outputValue = me.valueToLink( "-" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].decrement();\"" );
						break;
					case "remove":
						outputValue = me.valueToLink( "Remove" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].remove();\"" );
						break;
					case "price":
						outputValue = me.valueToCurrencyString( item[ info[0].toLowerCase() ] ? item[info[0].toLowerCase()] : " " );
						break;
					default: 
						outputValue = item[ info[0].toLowerCase() ] ? item[info[0].toLowerCase()] : " ";
						break;
				}	
				*/
				
				switch( info[0].toLowerCase() ){
					case "totale":
						outputValue = me.valueToCurrencyString(parseFloat(item.price)*parseInt(item.quantity,10) );
						break;
					case "aumenta":
						outputValue = me.valueToLink( "+" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].increment();\"" );
						break;
					case "riduci":
						outputValue = me.valueToLink( "-" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].decrement();\"" );
						break;
					case "rimuovi":
						outputValue = me.valueToLink( "Elimina" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].remove();\"" );
						break;
					case "price":
						outputValue = me.valueToCurrencyString( item[ info[0].toLowerCase() ] ? item[info[0].toLowerCase()] : " " );
						break;
					default: 
						outputValue = item[ info[0].toLowerCase() ] ? item[info[0].toLowerCase()] : " ";
						break;
				}	
				
				for( var y=1,ylen=info.length;y<ylen;y++){
					option = info[y].toLowerCase();
					switch( option ){
						case "image":
						case "img":
							outputValue = me.valueToImageString( outputValue );		
							break;
						case "input":
							outputValue = me.valueToTextInput( outputValue , "onchange=\"simpleCart.items[\'" + item.id + "\'].set(\'" + outputValue + "\' , this.value);\""  );
							break;
						case "div":
						case "span":
						case "h1":
						case "h2":
						case "h3":
						case "h4":
						case "p":
							outputValue = me.valueToElement( option , outputValue , "" );
							break;
						case "noheader":
							break;
						default:
							error( "unkown header option: " + option );
							break;
					}
				
				}		  
				newCell.innerHTML = outputValue;
				newCell.className = "item" + info[0];
				newRow.appendChild( newCell );
			}			
			newRow.className = "itemContainer";
			newRows[x] = newRow;
			x++;
		}

		
		for( current in me.cartDivs ){
			
			/* delete current rows in div */
			var div = me.cartDivs[current];
			while( div.childNodes[0] ){
				div.removeChild( div.childNodes[0] );
			}
			
			for(var j=0, jLen = newRows.length; j<jLen; j++){
				div.appendChild( newRows[j] );
			}
			
			
		}
		
	
		// traduco gli header VANNO MODIFICATI DA QUI
		document.getElementById("headerName").innerHTML = "Nome";
		document.getElementById("headerPrice").innerHTML = "Prezzo";
		document.getElementById("headerQuantity").innerHTML = "Quantita";
		document.getElementById("headerPrice").innerHTML = "Prezzo";
	
	};

	me.addEventToArray = function ( array , functionCall , theEvent ) {
		for( var outlet in array ){
			var element = array[outlet];
			if( element.addEventListener ) {
				element.addEventListener(theEvent, functionCall , false );
			} else if( element.attachEvent ) {
			  	element.attachEvent( "on" + theEvent, functionCall );
			}
		}
	};
	
	
	me.createHiddenElement = function ( name , value ){
		var element = document.createElement("input");
		element.type = "hidden";
		element.name = name;
		element.value = value;
		return element;
	};
	
	
	
	/******************************************************
				Currency management
	 ******************************************************/
	
	me.currencySymbol = function() {		
		switch(me.currency){
			case JPY:
				return "&yen;";
			case EUR:
				return "&euro;";
			case GBP:
				return "&pound;";
			case USD:
			case CAD:
			case AUD:
			case NZD:
			case HKD:
			case SGD:
				return "&#36;";
			default:
				return "";
		}
	};
	
	
	me.currencyStringForPaypalCheckout = function( value ){
		if( me.currencySymbol() == "&#36;" ){
			return "$" + parseFloat( value ).toFixed(2);
		} else {
			return "" + parseFloat(value ).toFixed(2);
		}
	};
	
	/******************************************************
				Formatting
	 ******************************************************/
	
	
	me.valueToCurrencyString = function( value ) {
		return parseFloat( value ).toCurrency( me.currencySymbol() );
	};
	
	me.valueToPercentageString = function( value ){
		return parseFloat( 100*value ) + "%";
	};
	
	me.valueToImageString = function( value ){
		if( value.match(/<\s*img.*src\=/) ){
			return value;
		} else {
			return "<img src=\"" + value + "\" />";
		}
	};
	
	me.valueToTextInput = function( value , html ){
		return "<input type=\"text\" value=\"" + value + "\" " + html + " />";
	};
	
	me.valueToLink = function( value, link, html){
		return "<a href=\"" + link + "\" " + html + " >" + value + "</a>";
	};
	
	me.valueToElement = function( type , value , html ){
		return "<" + type + " " + html + " > " + value + "</" + type + ">";
	};
	
	/******************************************************
				Duplicate management
	 ******************************************************/
	
	me.hasItem = function ( item ) {
		for( var current in me.items ) {
			var testItem = me.items[current];
			var matches = true;
			for( var field in item ){
				if( typeof( item[field] ) != "function"	&& 
					field != "quantity"  				&& 
					field != "id" 						){
					if( item[field] != testItem[field] ){
						matches = false;
					}
				}	
			}
			if( matches ){ 
				return current; 
			}
		}
		return false;
	};
	
	
	
	
	/******************************************************
				Cart Update managment
	 ******************************************************/
	
	me.update = function() {
		if( !simpleCart.isLoaded ){
			simpleCart.load();
		} 
		if( !simpleCart.pageIsReady ){
			simpleCart.initializeView();
		}
		me.updateTotals();
		me.updateView();
		me.save();
	};
	
	me.updateTotals = function() {
		me.total = 0 ;
		me.quantity  = 0;
		for( var current in me.items ){
			var item = me.items[current];
			if( item.quantity < 1 ){ 
				item.remove();
			} else if( item.quantity !== null && item.quantity != "undefined" ){
				me.quantity = parseInt(me.quantity,10) + parseInt(item.quantity,10); 
			}
			if( item.price ){ 
				me.total = parseFloat(me.total) + parseInt(item.quantity,10)*parseFloat(item.price); 
			}
		}
		me.shippingCost = me.shipping();
		me.taxCost = parseFloat(me.total)*me.taxRate;
		//me.finalTotal = me.shippingCost + me.taxCost + me.total;
		me.finalTotal = (me.shippingCost * (1 + me.taxRate)) + me.taxCost + me.total;
	};
	
	me.shipping = function(){
		if( parseInt(me.quantity,10)===0 )
			return 0;
		var shipping = 	parseFloat(me.shippingFlatRate) + 
					  	parseFloat(me.shippingTotalRate)*parseFloat(me.total) +
						parseFloat(me.shippingQuantityRate)*parseInt(me.quantity,10),
			nextItem,
			next;
		for(next in me.items){
			nextItem = me.items[next];
			if( nextItem.shipping ){
				if( typeof nextItem.shipping == 'function' ){
					shipping += parseFloat(nextItem.shipping());
				} else {
					shipping += parseFloat(nextItem.shipping);
				}
			}
		}
		
		return shipping;
	}
	
	me.initialize = function() {
		simpleCart.initializeView();
		simpleCart.load();
		simpleCart.update();
	};
				
}

/********************************************************************************************************
 *			Cart Item Object
 ********************************************************************************************************/

function CartItem() {
	this.id = "c" + NextId++;
}
	CartItem.prototype.set = function ( field , value ){
		field = field.toLowerCase();
		if( typeof( this[field] ) != "function" && field != "id" ){
			if( field == "quantity" ){
				value = value.replace( /[^(\d|\.)]*/gi , "" );
				value = value.replace(/,*/gi, "");
				value = parseInt(value,10);
			} else if( field == "price"){
				value = value.replace( /[^(\d|\.)]*/gi, "");
				value = value.replace(/,*/gi , "");
				value = parseFloat( value );
			}
			if( typeof(value) == "number" && isNaN( value ) ){
				error( "Improperly formatted input.");
			} else {
				this[field] = value;
				this.checkQuantityAndPrice();
			}			
		} else {
			error( "Cannot change " + field + ", this is a reserved field.");
		}
		simpleCart.update();
	};
	
	CartItem.prototype.increment = function(){
		this.quantity = parseInt(this.quantity,10) + 1;
		simpleCart.update();
	};
	
	CartItem.prototype.decrement = function(){
		if( parseInt(this.quantity,10) < 2 ){
			this.remove();
		} else {
			this.quantity = parseInt(this.quantity,10) - 1;
			simpleCart.update();
		}
	};
	
	CartItem.prototype.print = function () {
		var returnString = '';
		for( var field in this ) {
			if( typeof( this[field] ) != "function" ) {
				returnString+= escape(field) + "=" + escape(this[field]) + "||";
			}
		}
		return returnString.substring(0,returnString.length-2);
	};
	
	
	CartItem.prototype.checkQuantityAndPrice = function() {
		if( !this.price || this.quantity == null || this.quantity == 'undefined'){ 
			this.quantity = 1;
			error('No quantity for item.');
		} else {
			this.quantity = ("" + this.quantity).replace(/,*/gi, "" );
			this.quantity = parseInt( ("" + this.quantity).replace( /[^(\d|\.)]*/gi, "") , 10); 
			if( isNaN(this.quantity) ){
				error('Quantity is not a number.');
				this.quantity = 1;
			}
		}
				
		if( !this.price || this.price == null || this.price == 'undefined'){
			this.price=0.00;
			error('No price for item or price not properly formatted.');
		} else {
			this.price = ("" + this.price).replace(/,*/gi, "" );
			this.price = parseFloat( ("" + this.price).replace( /[^(\d|\.)]*/gi, "") ); 
			if( isNaN(this.price) ){
				error('Price is not a number.');
				this.price = 0.00;
			}
		}
	};
	
	
	CartItem.prototype.parseValuesFromArray = function( array ) {
		if( array && array.length && array.length > 0) {
			for(var x=0, xlen=array.length; x<xlen;x++ ){
			
				/* ensure the pair does not have key delimeters */
				array[x].replace(/||/, "| |");
				array[x].replace(/\+\+/, "+ +");
			
				/* split the pair and save the unescaped values to the item */
				var value = array[x].split('=');
				if( value.length>1 ){
					if( value.length>2 ){
						for(var j=2, jlen=value.length;j<jlen;j++){
							value[1] = value[1] + "=" + value[j];
						}
					}
					this[ unescape(value[0]).toLowerCase() ] = unescape(value[1]);
				}
			}
			return true;
		} else {
			return false;
		}
	};
	
	CartItem.prototype.remove = function() {
		simpleCart.remove(this.id);
		simpleCart.update();
	};
	


/********************************************************************************************************
 *			Shelf Object for managing items on shelf that can be added to cart
 ********************************************************************************************************/

function Shelf(){
	this.items = {};
}	
	Shelf.prototype.readPage = function () {
		this.items = {};
		var newItems = getElementsByClassName( "simpleCart_shelfItem" );
		for( var current in newItems ){
			var newItem = new ShelfItem();
			this.checkChildren( newItems[current] , newItem );
			this.items[newItem.id] = newItem;
		}
	};
	
	Shelf.prototype.checkChildren = function ( item , newItem) {
		
		for(var x=0;item.childNodes[x];x++){
			
			var node = item.childNodes[x];
			if( node.className && node.className.match(/item_[^ ]+/) ){
				
				var data = /item_[^ ]+/.exec(node.className)[0].split("_");
				
				if( data[1] == "add" || data[1] == "Add" ){
					var tempArray = [];
					tempArray.push( node );
					var addFunction = simpleCart.Shelf.addToCart(newItem.id);
					simpleCart.addEventToArray( tempArray , addFunction , "click");
					node.id = newItem.id;
				} else {
					newItem[data[1]]  = node;
				}
			}		
			if( node.childNodes[0] ){ 
				this.checkChildren( node , newItem );	
			}	
		}
	};
	
	Shelf.prototype.empty = function () {
		this.items = {};
	};
	
	
	Shelf.prototype.addToCart = function ( id ) {
		return function(){
			if( simpleCart.Shelf.items[id]){
				simpleCart.Shelf.items[id].addToCart();
			} else {
				error( "Shelf item with id of " + id + " does not exist.");
			}
		};
	};
	

/********************************************************************************************************
 *			Shelf Item Object
 ********************************************************************************************************/


function ShelfItem(){
	this.id = "s" + NextId++;
}	
	ShelfItem.prototype.remove = function () {
		simpleCart.Shelf.items[this.id] = null;
	};
	
	
	ShelfItem.prototype.addToCart = function () {
		var outStrings = [],valueString;
		for( var field in this ){
			if( typeof( this[field] ) != "function" && field != "id" ){
				valueString = "";
				
				switch(field){
					case "price":
						if( this[field].value ){
							valueString = this[field].value; 
						} else if( this[field].innerHTML ) {
							valueString = this[field].innerHTML;
						}
						/* remove all characters from price except digits and a period */
						valueString = valueString.replace( /[^(\d|\.)]*/gi , "" );
						valueString = valueString.replace( /,*/ , "" );
						break;
					case "image":
						valueString = this[field].src;
						break;
					default:
						if( this[field].value ){
							valueString = this[field].value; 
						} else if( this[field].innerHTML ) {
							valueString = this[field].innerHTML;
						} else if( this[field].src ){
							valueString = this[field].src;
						} else {
							valueString = this[field];
						}
						break;
				}
				outStrings.push( field + "=" + valueString );
			}
		}
		
		simpleCart.add( outStrings );
	};
	


/********************************************************************************************************
 * Thanks to Peter-Paul Koch for these cookie functions (http://www.quirksmode.org/js/cookies.html)
 ********************************************************************************************************/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
	var anno = new Date();
	var ordine = anno.getFullYear() + "-" +  randomString(1) + Math.floor(Math.random()*10) + Math.floor(Math.random()*10) + randomString(1) + Math.floor(Math.random()*10) + Math.floor(Math.random()*10);
	//ordine=YYYY-XNNXNN y = anno, X = lettera random, N = numero random
	document.cookie = name+"_ref=" + ordine + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function randomString(lenght) {
		//var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYTZabcdefghiklmnopqrstuvwxyz";
		var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYTZ";
		var string_length = lenght;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
				var rnum = Math.floor(Math.random() * chars.length);
				randomstring += chars.substring(rnum,rnum+1);
		}
		return randomstring;
}



//*************************************************************************************************
/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};


/********************************************************************************************************
 *  Helpers
 ********************************************************************************************************/


String.prototype.reverse=function(){return this.split("").reverse().join("");};
Number.prototype.withCommas=function(){var x=6,y=parseFloat(this).toFixed(2).toString().reverse();while(x<y.length){y=y.substring(0,x)+","+y.substring(x);x+=4;}return y.reverse();};
Number.prototype.toCurrency=function(){return(arguments[0]?arguments[0]:"$")+this.withCommas();};


/********************************************************************************************************
 * error management 
 ********************************************************************************************************/

function error( message ){
	try{ 
		console.log( message ); 
	}catch(err){ 
	//	alert( message );
	}
}


var simpleCart = new Cart();

if( typeof jQuery !== 'undefined' ) $(document).ready(function(){simpleCart.initialize();}); 
else if( typeof Prototype !== 'undefined') Event.observe( window, 'load', function(){simpleCart.initialize();});
else window.onload = simpleCart.initialize;
