// globali  -------------------------------------------------------------------------------------------------------
var motoAjax 	= new motoActionAjax();
var limitePeso  = 31.5; 

function opt (whatTo, rifer, descr, defSelected, selected)
{
	var optionName = new Option (descr, rifer, defSelected, selected)
	var length = whatTo.length;
	//whatTo.options[length] = optionName;
	try {
		whatTo.add(optionName, null); // standards compliant; doesn't work in IE
	  }
	  catch(ex) {
		whatTo.add(optionName); // IE only
	  }

}


// template -------------------------------------------------------------------------------------------------------
function cambiaCategoria(preselezioneMarca)
{
	var idCategoria = document.getElementById("ProdottoCampo").value;
	var arrMarche   = motoAjax.marchePerCategoria(idCategoria, preselezioneMarca);
	var marche	    = document.getElementById('MarcaCampo');

	// Cancella le marche
	marche.options.length = 0;
	
	// Ripopola le marche
	for (var i=0; i < arrMarche.length; i++)
	{
		opt(marche, arrMarche[i][0], arrMarche[i][1], false, arrMarche[i][2]);
	}
}

// carrello -------------------------------------------------------------------------------------------------------


function variaQta(idProdotto, azione, obj)
{
	// Disattivo il pulsante per permettere l'aggiornamento dei dati
	document.getElementById("plus_" + idProdotto).disabled = true;
	document.getElementById("minus_" + idProdotto).disabled = true;
	
	var qta = motoAjax.variaQta(idProdotto, azione);
	
	var prezzoAggiornato = motoAjax.aggiornaPrezzoCarrello(idProdotto)[0][1].toFixed(2);
	
	document.getElementById("qta_" + idProdotto).value = qta;
	
	document.getElementById("prezzo_"  + idProdotto).innerHTML  = "&#8364; " + prezzoAggiornato;
	document.getElementById("prezzo_template_"  + idProdotto).innerHTML = prezzoAggiornato +  " &#8364;";
	
	ricalcolaPeso();
	ricalcolaTotale();
	
	// Riattivo il pulsante
	document.getElementById("plus_" + idProdotto).disabled = false;
	document.getElementById("minus_" + idProdotto).disabled = false;
}

function ricalcolaPeso()
{
	var peso = motoAjax.getPeso();
	
	document.getElementById('spedizioneKg').innerHTML 		= peso.toFixed(2);
	
	if ( peso > limitePeso )
	{
		$("#warningPeso").show();
		$("#btnCompra").hide();
	}
	else
	{
		$("#warningPeso").hide();
		$("#btnCompra").show();
	}
	
}

function ricalcolaCostoTrasporto()
{
	var stato    = document.getElementById('s_stato').value;
	var trasporto= document.getElementById('s_trasporto').value;
	var costo    = 0;
	
	
	if (motoAjax.getPeso() <= limitePeso)
	{
		costo = motoAjax.getPesoCosto(stato, "", "", "", trasporto);
		
		document.getElementById('spedizioneCosto').innerHTML = "&#8364; " + costo.toFixed(2);
		
		return costo;
	}
	else
	{
		document.getElementById('spedizioneCosto').innerHTML = "<div align=\"center\">-</div>";
		return 0;
	}
}

function ricalcolaTotale()
{
	
	mostraLoading();
	
	var spesaCarr    = motoAjax.aggiornaPrezzoCarrelloTotale();
	var spesaSped	 = ricalcolaCostoTrasporto();
	var spesaPag	 = motoAjax.getCostoPagamento(document.getElementById('s_pagamento').value);
	
	// Aggiorna tutti i prezzi carrello visualizzati
	var i = 0;
	var prezziAggiornati = motoAjax.aggiornaPrezzoCarrello();	
	
	for (i = 0; i < prezziAggiornati.length; i++)
	{
		document.getElementById("prezzo_"  + prezziAggiornati[i][0]).innerHTML  = "&#8364; " + prezziAggiornati[i][1].toFixed(2);
		document.getElementById("prezzo_template_"  + prezziAggiornati[i][0]).innerHTML = prezziAggiornati[i][1].toFixed(2) +  " &#8364;";
	}
	
	var totale = spesaCarr + spesaSped + spesaPag;
	
	document.getElementById('pagamentoCosto').innerHTML = "&#8364; " + spesaPag.toFixed(2);
	
	document.getElementById('prezzoTotale').innerHTML = "<strong>&#8364; " + totale.toFixed(2) + "</strong>";
	document.getElementById('prezzoTotale_template').innerHTML = totale.toFixed(2) + " &#8364; ";
	
	motoAjax.modificaIvaCarrello();
	
	nascondiLoading();
	
	nascondiLoading();
}

function popolaVarianti(preselezione)
{
	// il popolamento delle varianti viene fatto in javascript per poter visualizzare
	// il messaggio per ordinare il prodotto (solo per alcuni prodotti)
	
	var idprodotto = document.getElementById('idProdotto').value;
	var varianti   = document.getElementById('SelezionaColore');
	var arrVar     = motoAjax.variantiPerProdotto(idprodotto);
	var _tmp       = "";

	varianti.options.length = 0;

	// Aggiunge la riga scegli variante
	if (preselezione < 1)
	{
		opt(varianti, arrVar[0][0], arrVar[0][1], false, true);
	}
	else
	{
		opt(varianti, arrVar[0][0], arrVar[0][1], false, false);
	}

	// Ripopola le varianti
	for (var i=1; i < arrVar.length; i++)
	{	
		if (preselezione == arrVar[i][0])
		{
			opt(varianti, arrVar[i][0], arrVar[i][1], false, true);
		}
		else
		{
			opt(varianti, arrVar[i][0], arrVar[i][1], false, false);
		}
	}

	return arrVar;
}

function popolaTaglie()
{
	var idprodotto = document.getElementById('idProdotto').value;
	var idvariante = document.getElementById('SelezionaColore').value;
	var taglie     = document.getElementById('SelezionaTaglia');
	
	var arrTag	   = motoAjax.tagliePerVariante(idvariante);
	
	// Cancella le taglie presenti
	
	taglie.options.length = 0;
	
	if (idvariante > 0)
	{
		$('#wrapperTaglia').show();
		$('#AggiungiCarrello').show();
		
	
		if (arrTag.length > 0) {
		
			// Ripopola le taglie
			for (var i=0; i < arrTag.length; i++)
			{
				
				if ( arrTag[i][0] == "UN")
				{
					$('#wrapperTaglia').hide();
				}
				
				if (i==0) { seleziona = true; } else { seleziona = false; }
				
				opt(taglie, arrTag[i][0], arrTag[i][1], false, seleziona);	
			}
		
		}
		else
		{
			$('#wrapperTaglia').hide();
			$('#AggiungiCarrello').hide();
		}
	}
	else
	{
		// Questo � una caso che non si deve verificare!
		$('#wrapperTaglia').hide();
		$('#AggiungiCarrello').hide();
		opt(taglie, 0, "", false, true);
	}
	
	// Cambio foto e visualizzazione di eventuale messaggio
	
	for (var i=0; i < arrVar.length; i++)
	{
		if (arrVar[i][0] == idvariante) // sulla variante selezionata
		{
			if (arrVar[i][2].length > 0)
			{
			document.getElementById('FotoProdotto').src = "/upld/catalogo/big/" + arrVar[i][2];
			}
			else
			{
			document.getElementById('FotoProdotto').src = "/immagini/immagine-non-disponibile.gif";	
			}	
		}
		
		if (arrVar[i][3]) // se il messaggio relativo alla disponibilit� deve essere visualizzato
		{
			$("#MessaggioDisponibilita").show();
		}
		else
		{
			$("#MessaggioDisponibilita").hide();
		}
	}
	
	// Aggiorna prezzo variante
	document.getElementById("PrezzoSchedaProdotto").innerHTML = motoAjax.getPrezzo(idprodotto, idvariante).toFixed(2) + " &euro;";
}

function popolaTrasporto(aggiornaTotali)
{
	mostraLoading();
	
	var stato    = document.getElementById('s_stato').value;
	var trasporto= document.getElementById('s_trasporto');
	
	var fattsi 	 = document.getElementById("fatturasi");
	var fattno 	 = document.getElementById("fatturano");
	
	var trasportoSelezionato = motoAjax.getTrasporto();
	
	var arrTrasp = motoAjax.spedizioniPerTerritorio(stato);
	
	motoAjax.setSiglaNazione(stato);
	
	if (stato == "IT")
	{
		$("#mostraFattura").show();
		if ( motoAjax.isFatturaRichiesta() == "S")
		{	
			fattsi.checked = "checked";
			fattno.checked = "";
			motoAjax.setFatturaRichiesta("S");
		}
		else
		{
			fattsi.checked = "";
			fattno.checked = "checked";
			motoAjax.setFatturaRichiesta("N");
		}
	}
	else
	{
		$("#mostraFattura").hide();
		fattsi.checked = "checked";
		fattno.checked = "";	
		motoAjax.setFatturaRichiesta("S");
	}
	
	// Cancella i trasporti
	
	trasporto.options.length = 0;

	// Ripopola i trasporti
	for (var i=0; i < arrTrasp.length; i++)
	{
		// Se il trasporto � gia stato selezionato in precedenza, lo seleziona
		if (trasportoSelezionato > 0 )
		{
			
			
			if ( arrTrasp[i][0] == trasportoSelezionato )
			{
				opt(trasporto, arrTrasp[i][0], arrTrasp[i][1], false, true);
			}
			else
			{
				opt(trasporto, arrTrasp[i][0], arrTrasp[i][1], false, false);
			}
		}
		// Se non � mai stato selezionato un trasporto, inizializza al primo della lista
		else
		{
		
			if ( i == 0 )
			{
				opt(trasporto, arrTrasp[i][0], arrTrasp[i][1], false, true);
			}
			else
			{
				opt(trasporto, arrTrasp[i][0], arrTrasp[i][1], false, false);
			}
		}
	}
	
	aggiornaMessaggioIva();
	
	if (aggiornaTotali == true)
	{
		ricalcolaTotale();
	}
}

function aggiornaMessaggioIva()
{
	if (motoAjax.isApplicareIva())
	{
		$("#ivaSi_template").show();
		$("#ivaNo_template").hide();
		$("#ivaSi").show();
		$("#ivaNo").hide();
	}
	else
	{
		$("#ivaSi_template").hide();
		$("#ivaNo_template").show();
		$("#ivaSi").hide();
		$("#ivaNo").show();
	}	
}

function popolaPagamento(lingua, aggiornaTotali)
{
	mostraLoading();
	
	var stato    = document.getElementById('s_stato').value;
	var trasporto= document.getElementById('s_trasporto').value;
	var pagamento= document.getElementById('s_pagamento');
	
	var arrPag = motoAjax.pagamentiPerTerritorio(stato);
	
	// Cancella i trasporti
	pagamento.options.length = 0;

	// Ripopola i pagamenti
	for (var i=0; i < arrPag.length; i++)
	{
		// Se il trasporto � "Ritiro in negozio" e il pagamento corrente � Pago in negozio lo seleziono
		if (trasporto == 15 && arrPag[i][0] == 8)
		{
			opt(pagamento, arrPag[i][0], arrPag[i][1], false, true);
		}
		else
		{
			// Il Seleziona solo il primo pagamento
			if (arrPag[i][0] != 8)
			{
				if (motoAjax.getPagamento() == arrPag[i][0])
				{
					opt(pagamento, arrPag[i][0], arrPag[i][1], false, true);	
				}
				else
				{
					opt(pagamento, arrPag[i][0], arrPag[i][1], false, false);
				}
			}
		}
	}
	
	if (aggiornaTotali == true)
	{
		ricalcolaTotale();
	}
}

function checkInsertCarrello(messaggio)
{
	idvariante = document.getElementById('SelezionaColore').value;
	taglie     = document.getElementById('SelezionaTaglia').value;
	
	if (idvariante > 0 && (taglie != 0 || taglie == "UN"))
	{
		return true;
	}
	else
	{
		alert(messaggio);
		return false;
	}
}

function cambiaTrasporto()
{
	mostraLoading();
	
	var trasporto= document.getElementById('s_trasporto').value;
	motoAjax.setTrasporto(trasporto);
	
	ricalcolaTotale();
}

function cambiaPagamento()
{
	mostraLoading();
	
	var pagamento= document.getElementById('s_pagamento').value;
	motoAjax.setPagamento(pagamento);
}


function cambiaFattura(obj)
{
	mostraLoading();
	
	var fatturaRichiesta= obj.value;

	motoAjax.setFatturaRichiesta(fatturaRichiesta);
	
	aggiornaMessaggioIva();

	ricalcolaTotale();
}

function cambiaPrivato(obj)
{
	mostraLoading();
	
	var isPrivato= obj.value;

	motoAjax.setPrivato(isPrivato);
	
	aggiornaMessaggioIva();

	ricalcolaTotale();
}

function popolaMarcheMoto()
{
	document.getElementById("btnFiltroMoto").disabled = true;

	var marche	    = document.getElementById('SelezionaMarcaMoto');
	var arrMarche   = motoAjax.marchePerMoto();
	
	// Cancella le marche
	marche.options.length = 0;
	
	// Ripopola le marche
	for (var i=0; i < arrMarche.length; i++)
	{
		opt(marche, arrMarche[i][0], arrMarche[i][1], false, arrMarche[i][2]);
	}
	
	document.getElementById("btnFiltroMoto").disabled = false;
}

function aggiornaFiltroMoto()
{
	document.getElementById("btnFiltroMoto").disabled = true;
	
	var marca	    = document.getElementById('SelezionaMarcaMoto').value;
	var moto		= document.getElementById('SelezionaModelloMoto');	
	

	if (marca != motoAjax.getMarca_Moto())
	{
		motoAjax.setModello_Moto(0);
		motoAjax.setMarca_Moto(marca);
	}
	else
	{
		if (moto.value != "")
		{
			motoAjax.setModello_Moto(moto.value);
		}
	}
	
	var arrMoto		= motoAjax.optionMoto();

	// Cancella le moto
	moto.options.length = 0;
	
	// Ripopola le moto
	for (var i=0; i < arrMoto.length; i++)
	{
		opt(moto, arrMoto[i][0], arrMoto[i][1], false, arrMoto[i][2]);
	}	
	
	moto		= document.getElementById('SelezionaModelloMoto');	
	
	document.getElementById("btnFiltroMoto").disabled = false;
}

function mostraLoading ()
{
	document.getElementById("carrellotable").style.display = 'none';
	document.getElementById("loadingif").style.display = 'block';
}

function nascondiLoading ()
{
	document.getElementById("carrellotable").style.display = 'block';
	document.getElementById("loadingif").style.display = 'none';
}


//-----------------------------------------------------------------------------------------------------------------
