// Javascript document
var codiceProdotti = new Array();
var quantitaProdotti = new Array();
var prezzoProdotti = new Array();
var origineProdotti = new Array();
var prezzoIVAProdotti = new Array();
var trasportoProdotti = new Array();
var volumeProdotti = new Array();
var disponibilitaProdotti = new Array();
var prezzoTotale = 0;

var tipiSpedizione = new Array();
var prezzoTipiSpedizione = new Array();
var speseSpedizioneIVA = 0;
var speseSpedizione = 0;

var trasportoGratuito = false;
var prezzoTotale = 0;
var prezzoMinimo = 0;

var data; 
var date;

var valorePuntoGraphiland = 20; // 1 punto Graphiland ogni 20 euro di spesa

function addTipiSpedizione(tipo, prezzo)
{
	tipiSpedizione.push(tipo);
	prezzoTipiSpedizione.push(prezzo);
}

function decrement(obj, idUtente, idSessione, codice) 
{
	if (eval(document.getElementById(obj).innerHTML) > 1)
	{
		document.getElementById(obj).innerHTML = eval(document.getElementById(obj).innerHTML) - 1;
		var i = checkProdotto(codice);
		quantitaProdotti[i] = quantitaProdotti[i] - 1;
		calcolaTotale('totale');
		data = new Date();
		date = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + ":" + data.getMilliseconds();
		caricaTestoSenzaDiv("aggiornaCarrello.cfm?codice="+codice+"&idUtente="+idUtente+"&idSessione="+idSessione+"&quantita="+quantitaProdotti[i]+"&origine="+origineProdotti[i]+"&update=1&date="+date);
	}
}

function increment(obj, idUtente, idSessione, codice) 
{
	//if (eval(document.getElementById(obj).innerHTML) < 9)
	//{
		document.getElementById(obj).innerHTML = eval(document.getElementById(obj).innerHTML) + 1;
		var i = checkProdotto(codice);
		quantitaProdotti[i] = quantitaProdotti[i] + 1;
		calcolaTotale('totale');
		data = new Date();
		date = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + ":" + data.getMilliseconds();
		caricaTestoSenzaDiv("aggiornaCarrello.cfm?codice="+codice+"&idUtente="+idUtente+"&idSessione="+idSessione+"&quantita="+quantitaProdotti[i]+"&origine="+origineProdotti[i]+"&update=1&date="+date);
	//}
}

function addProdotto(codice, quantita, prezzo, origine, prezzoIVA, trasporto, volume, disponibilita)
{
	codiceProdotti.push(codice);
	quantitaProdotti.push(quantita);
	prezzoProdotti.push(prezzo);
	origineProdotti.push(origine);
	prezzoIVAProdotti.push(prezzoIVA);
	trasportoProdotti.push(trasporto);
	volumeProdotti.push(volume);
	disponibilitaProdotti.push(disponibilita);

	if(parseInt(codiceProdotti.length) == 1)
	{
		document.getElementById('carrelloRif').innerHTML = '<a style="color:#FE4B00;" href="/carrello.cfm">vai al carrello</a>&nbsp;&nbsp;<a href="/graphiland08/carrello.cfm"><img src="/images/ico_carrello_pieno.jpg" width="17" height="17" border="0"></a>';
	}
}

function deleteProdotto(idUtente, idSessione, codice, obj)
{
	var indice = checkProdotto(codice);
	var origine = origineProdotti[indice];
	
	codiceProdotti = ricreaArray(codiceProdotti, indice);
	quantitaProdotti = ricreaArray(quantitaProdotti, indice);
	prezzoProdotti = ricreaArray(prezzoProdotti, indice);
	origineProdotti = ricreaArray(origineProdotti, indice);
	prezzoIVAProdotti = ricreaArray(prezzoIVAProdotti, indice);
	trasportoProdotti = ricreaArray(trasportoProdotti, indice);
	volumeProdotti = ricreaArray(volumeProdotti, indice);
	disponibilitaProdotti = ricreaArray(disponibilitaProdotti, indice);
	
	document.getElementById(obj).style.display='none';
	
	calcolaTotale('totale');
	
	data = new Date();
	date = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + ":" + data.getMilliseconds();
	caricaTestoSenzaDiv("aggiornaCarrello.cfm?codice="+codice+"&idUtente="+idUtente+"&idSessione="+idSessione+"&quantita=0&update=0&date="+date+"&origine="+origine);
	
	if(codiceProdotti.length == 0)
	{
		document.getElementById('carrelloRif').innerHTML = '<font color="#C0C0C0">il tuo carrello &egrave; vuoto </font> &nbsp;&nbsp;<img src="/images/ico_carrello.jpg" width="17" height="17" border="0">';
		document.getElementById('box_appendice').style.minHeight = '200px';
		document.getElementById('box_appendice').innerHTML = '<div class="carrello_vuoto"><h2 class="box_colonna_categoria_titolo_text verde">Il tuo carrello &egrave; vuoto</h2><a href="/index.cfm" class="verde">Torna alla home</a></div>';
		document.getElementById('box_select').style.display = 'none';
		document.getElementById('box_totale').style.display = 'none';
		document.getElementById('footer_1').style.display = 'none';
		document.getElementById('footer_image').style.display = 'none';
		document.getElementById('box_pagamento').style.display = 'none';
		document.getElementById('box_codice').style.display = 'none';
		document.getElementById('box_note').style.display = 'none';
		document.getElementById('box_conferma').style.display = 'none';
		document.getElementById('box_carte').style.display = 'none';
		document.getElementById('box_contenuto').style.display = 'none';
	}
}

function ricreaArray(array, indice)
{
	var newArray = new Array();
	if(indice == 0)
	{
		if(array.length > 1)
			newArray = array.slice(1, array.length);
	}else
	{
		newArray = array.slice(0, indice);
		if(indice < array.length)
		{
			var array1 = array.slice(indice+1, array.length);
			for(var i=0; i<array1.length;i++)
				newArray.push(array1[i]);
		}
	}
	return newArray;
}

function checkProdotto(codice)
{
	var i=0;
	var ok = false;
	while((i < codiceProdotti.length)&&(!ok))
	{
		if(codiceProdotti[i] == codice)
		{
			ok = true;	
		}else
		{
			i = i + 1;
		}
	}
	return i;
}

function calcolaTotale(obj)
{
	calcolaTotaleSenzaIVA();
	var testo = "Totale ordine &euro; ";
	prezzoTotale = 0;
	var tmp = 0;
	for(var i=0; i < codiceProdotti.length; i++)
	{
		//Se è stato selezionato Italia si paga l'IVA sui prodotti altrimenti no
		if((document.getElementById('paese').selectedIndex == 0)||(document.getElementById('paese').selectedIndex == 1)||(document.getElementById('paese').selectedIndex == 12))
			tmp = quantitaProdotti[i]*prezzoIVAProdotti[i];
		else
			tmp = quantitaProdotti[i]*prezzoProdotti[i];
		prezzoTotale += tmp;
	}
	
	controllaTrasporto();
	controllaPaeseSpedizione();
	controllaVolume();
	calcolaPuntiGraphiland();
	
	var spese = speseSpedizione;
	if(speseSpedizioneIVA == 0)
		spese = speseSpedizione;
	else
		spese = speseSpedizione + ((speseSpedizione*speseSpedizioneIVA)/100);
	
	prezzoMinimo = prezzoTotale;
	prezzoTotale += spese;
	prezzoTotale = approssimaTotale(prezzoTotale);
	
	//Si mette come divisore dei decimali la virgola
	prezzoTotale = prezzoTotale.replace(".", ",");
	
	//Si aggiunge, se necessario, il punto come separatore delle migliaia
	var indice = prezzoTotale.indexOf(",");
	var intero = prezzoTotale.substring(0, indice);
	var decimale = prezzoTotale.substring(indice, prezzoTotale.length);
	if(intero.length > 3)
	{
		intero = intero.substring(0, (intero.length-3))+"."+intero.substring((intero.length-3), intero.length)
	}
	
	prezzoTotale = intero + decimale;
	
	testo += prezzoTotale+" *";
	//testo = testo.replace(".",",");
	document.getElementById(obj).innerHTML = testo;
}

function controllaTrasporto()
{
	var value = false;
	var indice = 0;
	while((indice < trasportoProdotti.length)&&(!value))
	{
		if(origineProdotti[indice] == "E")
		{
			//Se prodotto è ESPRINET il trasporto è gratuito se il campo è 1: se è 0 si paga
			if(trasportoProdotti[indice] == 1) //Se servono tutti i trasporti gratuiti si deve mettere !=1
				value = true;
			else
				indice = indice + 1;
		}else
		{
			//Se prodotto è GRAPHILAND il trasporto è gratuito se il campo è 0(download eletrronico) o 1: se è -1(NULL) si paga
			if(trasportoProdotti[indice] != -1 && trasportoProdotti[indice] != 4  && trasportoProdotti[indice] != 1) //Se servono tutti i trasporti gratuiti si deve mettere ==1
				value = true;
			else
				indice = indice + 1;
		}
	}
	
	if(!value) //Se servono tutti i trasporti gratuiti si deve mettere value
	{
		speseSpedizione = 70;
		speseSpedizioneIVA = 0;
		trasportoGratuito = false;
	}else
	{
		speseSpedizione = 0;
		speseSpedizioneIVA = 0;
		trasportoGratuito = true;
	}
}

function controllaPaeseSpedizione()
{
	if(!trasportoGratuito)
	{
		if(document.getElementById('paese').selectedIndex == 0)
		{
			//ritiro in sede: spese spedizione e IVA = 0
			aggiornaPrezzoIVA(0);
			speseSpedizione = 0;
			speseSpedizioneIVA = 0;
		}else if((document.getElementById('paese').selectedIndex == 1)||(document.getElementById('paese').selectedIndex == 12)||(document.getElementById('paese').selectedIndex == 5)||(document.getElementById('paese').selectedIndex == 23))
		{
			//Italia (1, 12), Citta del Vaticano (5) e San Marino (23) allora spese in base al tipo di pagamento e IVA = 20 (vedere se San Marino IVA = 0)
			for(var i=0; i < tipiSpedizione.length; i++)
			{
				if(document.getElementById(tipiSpedizione[i]).checked)	
				{
					speseSpedizione = prezzoTipiSpedizione[i];
					//Se il paese selezionato è San Marino si imposta a 0 l'IVA sulle spese di spedizione
					if((document.getElementById('paese').selectedIndex == 0)||(document.getElementById('paese').selectedIndex == 1)||(document.getElementById('paese').selectedIndex == 12))
					{
						aggiornaPrezzoIVA(0);
						speseSpedizioneIVA = 20;
					}else
					{
						aggiornaPrezzoIVA(1);
						speseSpedizioneIVA = 0;
					}
				}
			}
		}else
		{
			//Paese estero
			aggiornaPrezzoIVA(1);
			speseSpedizione = 70;
			speseSpedizioneIVA = 0;
		}
	}
	
	if(speseSpedizioneIVA == 0)
		spese = speseSpedizione;
	else
		spese = speseSpedizione + ((speseSpedizione*speseSpedizioneIVA)/100);
	spese = approssimaTotale(spese);
	spese = spese.replace(".", ",");
	document.getElementById('spese_spedizione').innerHTML = "spese di spedizione &euro; "+spese;
}

function aggiornaPrezzoIVA(valore)
{
	//Italia o ritiro in sede
	if(valore == 0)
	{
		document.getElementById('prezzoIvaID').innerHTML = 'Prezzo con iva';
	}else
	{
		document.getElementById('prezzoIvaID').innerHTML = 'IVA 0%';
	}
	
	for(var i=0; i < codiceProdotti.length; i++)
	{
		if(valore == 0)
		{
			var prezzo_prodotto = approssimaTotale(prezzoIVAProdotti[i]);
			prezzo_prodotto = prezzo_prodotto.replace(".", ",");
	
			//Si aggiunge, se necessario, il punto come separatore delle migliaia
			var indice = prezzo_prodotto.indexOf(",");
			var intero = prezzo_prodotto.substring(0, indice);
			var decimale = prezzo_prodotto.substring(indice, prezzo_prodotto.length);
			if(intero.length > 3)
			{
				intero = intero.substring(0, (intero.length-3))+"."+intero.substring((intero.length-3), intero.length)
			}
			
			prezzo_prodotto = intero + decimale;
			
			document.getElementById('prezzoIvaID'+i).innerHTML = "&euro; "+prezzo_prodotto;
		}else
		{
			document.getElementById('prezzoIvaID'+i).innerHTML = "-";
		}	
	}
}

function controllaVolume()
{
	var value = false;
	var indice = 0;
	while((indice < trasportoProdotti.length)&&(!value))
	{
		//SE VOLUME = "Y" contrassegno disabilitato
		if(volumeProdotti[indice] == "Y")
		{
			value = true;
		}else
		{
			indice = indice + 1;
		}
	}
	
	indice = 0;
	var valueTrasp = true;
	while((indice < trasportoProdotti.length)&&(valueTrasp))
	{
		if( (origineProdotti[indice] == 'E') || (origineProdotti[indice] == 'G' && (trasportoProdotti[indice] == 1 || trasportoProdotti[indice] == 4 || trasportoProdotti[indice] == -1) ))
		{
			valueTrasp = false;
		}else
		{
			indice = indice + 1;
		}
	}

	if(value || valueTrasp)
	{
		if(document.getElementById('contrassegno').checked)
		{
			document.getElementById('contrassegno').checked='';
			document.getElementById('bonifico bancario').checked='checked';
		}
		document.getElementById('contrassegno').disabled='disabled';
	}
}

function calcolaPuntiGraphiland()
{
	var puntiGraphiland = 0;
	// DAL 29/12/09 NON VENGONO PIU ACCREDITATI I GRAPHPOINT
	/*for(var i = 0; i < prezzoProdotti.length; i++)
	{
		puntiGraphiland += Math.floor((prezzoProdotti[i]*quantitaProdotti[i])/valorePuntoGraphiland);
	}*/
	
	document.getElementById('punti').innerHTML = "Totale Graph Point "+puntiGraphiland;
}

function approssimaTotale(prezzoTotale)
{
	var totale = ""+prezzoTotale;
	var indice = totale.indexOf(".");
	var new_totale;
	if(indice == -1)
	{
		new_totale = totale + ".00";	
	}else
	{
		if(indice+1 < totale.length)
		{
			var unita = totale.substring(0, indice+1);
			var tmp = totale.substring(indice+1, totale.length);
			if(tmp.length == 2)
			{
				new_totale = unita+tmp;
			}else if(tmp.length == 1)
			{
				new_totale = unita+tmp + "0";	
			}else
			{		
				//Si aumenta di una unita la seconda cifra decimale se la terza è maggiore/uguale a 5
				var tmp1 = tmp.substring(0,2);
				if(tmp[2] >= 5)
					tmp1 = (parseInt(tmp1)+1);
				new_totale = unita+tmp1;
			}
		}else
		{	
			new_totale = totale + "00";
		}
	}
	
	return new_totale;
}

function calcolaTotaleSenzaIVA()
{
	var totale = 0;
	var tmp = 0;
	for(var i=0; i < codiceProdotti.length; i++)
	{
		tmp = quantitaProdotti[i]*prezzoProdotti[i];
		totale += tmp;
	}
	
	prezzoTotale = totale;
	
	if(prezzoTotale > 300)
	{
		/*document.getElementById('contrassegno').style.display='none';
		document.getElementById('contrassegno').style.marginLeft='0px';
		document.getElementById('contrassegno').style.marginRight='0px';
		document.getElementById('labelContrassegno').style.marginLeft='0px';
		document.getElementById('labelContrassegno').style.marginRight='0px';
		document.getElementById('labelContrassegno').innerHTML = "";*/
		if(document.getElementById('contrassegno').checked)
		{
			document.getElementById('contrassegno').checked='';
			document.getElementById('bonifico bancario').checked='checked';
		}
		
		document.getElementById('contrassegno').disabled='disabled';
	}
	
	if(prezzoTotale <= 300)
	{
		/*document.getElementById('contrassegno').style.display='inline';
		document.getElementById('labelContrassegno').innerHTML = "contrassegno **";*/
		document.getElementById('contrassegno').disabled='';
	}
}

function controllaPrezzoTotale(obj)
{
	if(codiceProdotti.length > 0)
	{
		var count = 0;
		for(var i=0; i < disponibilitaProdotti.length; i++)
		{
			if(disponibilitaProdotti[i] >= 1)
				count++;
		}
		
		/*if(prezzoMinimo < 45)
		{
			alert("Il prezzo minimo per un ordine è di 45 euro");
		}else*/ if(count < disponibilitaProdotti.length)
		{
			if(confirm("Alcuni prodotti selezionati non sono disponibili \n Vuoi procedere lo stesso?"))
				document.getElementById(obj).submit();
		}else
		{
			document.getElementById(obj).submit();
		}
	}else
	{
		alert("Il tuo carrello è vuoto");	
	}
}