﻿// Variabili globali
var currentPhoto = 0;		// Foto correntemente visualizzata, inizialmente la prima.
var totalPhotos = 4;		// Numero totale di foto
var imageDuration = 6000;	// Durata di ogni immagine nella slideshow.

// Funzione che cambia l'immagine visualizzata e nasconde/mostra le frecce
function changePhoto(dir) {
	
	// Calcolo la prossima foto da mostrare
	nextPhoto = (currentPhoto+dir)%(totalPhotos);
	if (nextPhoto == -1)
		nextPhoto = totalPhotos-1;
		
	// Effettuo la transizione
	$("#foto"+currentPhoto).fadeOut("fast", function() { $("#foto"+nextPhoto).fadeIn("fast", function() { currentPhoto = nextPhoto; }); } );

}

// Funzione che gestisce la transizione automatica delle immagini
function transitionImages() {
	if (totalPhotos > 1) {
		timeout = setTimeout("changePhoto(1, 0); transitionImages();", imageDuration);
	}
}

function addToCart(productId) {
    $.ajax({
        url: 'addToCart.php',
        type: "POST",
        data: ({id : productId}),
        beforeSend:
            function() {
                doLinks(0);
                document.getElementById("ajax-loader-add"+productId).style.visibility = "visible";
            },
        success:
            function(result) {
                document.getElementById("ajax-loader-add"+productId).style.visibility = "hidden";
                alert('La pubblicazione "'+result+'" è stata aggiunta al tuo carrello. Puoi controllarlo in ogni momento cliccando su "Il tuo carrello" nel menu a sinistra.');
                doLinks(1);
            }
    });
}

function modifyQty(productId, shift) {
    $.ajax({
        url: 'modifyQty.php',
        type: "POST",
        data: ({id : productId, qty: shift}),
        beforeSend:
            function() {
                doLinks(0);
                document.getElementById("ajax-loader-cart").style.visibility = "visible";
            },
        success:
            function(result) {
                newQty = parseInt(document.getElementById("qty"+productId).innerHTML)+shift;
                if (newQty > 1)
                    document.getElementById("decrease"+productId).href = "javascript:modifyQty("+productId+", -1)";
                else
                    document.getElementById("decrease"+productId).href = "javascript:removeFromCart("+productId+")";
                document.getElementById("qty"+productId).innerHTML = newQty;
                document.getElementById("order_total").innerHTML = result;
                document.getElementById("ajax-loader-cart").style.visibility = "hidden";
                doLinks(1);
        }
    });
}

function removeFromCart(productId) {
    var answer = confirm("Sei sicuro di voler eliminare questo prodotto dal carrello?");
    if (answer) {
        $.ajax({
            url: 'removeFromCart.php',
            type: "POST",
            data: ({id : productId}),
            beforeSend:
                function() {
                    doLinks(0);
                    document.getElementById("ajax-loader-cart").style.visibility = "visible";
                },
            success:
                function(result) {
                    document.getElementById("order_total").innerHTML = result;
                    $("#product"+productId).hide("slow", function(){$("#product"+productId).remove();});
                    if (numberOfRowsInTable('cart_table') == 3)
                        $("#no_products").show("slow");
                    document.getElementById("ajax-loader-cart").style.visibility = "hidden";
                    doLinks(1);
                }
        });
    }
}

function fullDescription(productId) {
    $.ajax({
            url: 'fullDescription.php',
            type: "POST",
            data: ({id : productId}),
            beforeSend:
                function() {
                  doLinks(0);
                  $("#ajax-loader-description"+productId).show("fast");
                },
            success:
                function(result) {
                    $("#ajax-loader-description"+productId).hide("fast");
                    $("#fulldesc"+productId).hide("fast");
                    document.getElementById('descrizione'+productId).innerHTML = result;
                    doLinks(1);
                }
    });
}

function doLinks(how){
  var l = document.links;
  for (i = 0; i < l.length; i++)
    if (!how)
      l[i].onclick=function(){return false;};
    else
      l[i].onclick=function(){return true;};
}

function numberOfRowsInTable(tableId) {
    var oRows = document.getElementById(tableId).getElementsByTagName('tr');
    var iRowCount = oRows.length;
    if (iRowCount != undefined)
        return iRowCount = oRows.length;
    else
        return -1;
}

function checkForm() {
var nome =  document.getElementsByName('nome')[0].value;
var cognome =  document.getElementsByName('cognome')[0].value;
var cf_pi =  document.getElementsByName('cf_pi')[0].value;
var email = document.getElementsByName('email')[0].value;
var telefono = document.getElementsByName('telefono')[0].value;
var citta = document.getElementsByName('citta')[0].value;
var cap = document.getElementsByName('cap')[0].value;
var indirizzo = document.getElementsByName('indirizzo')[0].value;
var provincia = document.getElementsByName('provincia')[0].value;
var nazione = document.getElementsByName('nazione')[0].value;
if (nome === "") { alert("Inserire il nome."); return false; }
if (cognome === "") { alert("Inserire il cognome."); return false; }
if (cf_pi === "") { alert("Inserire il codice fiscale o la partita iva."); return false; }
if (email === "") { alert("Inserire un indirizzo email valido."); return false; }
if (telefono === "" || isNaN(telefono)) { alert("Inserire un numero di telefono valido."); return false; }
if (citta === "") { alert("Inserire la città."); return false; }
if (cap === "" || isNaN(cap)) { alert("Inserire un CAP valido."); return false; }
if (indirizzo === "") { alert("Inserire l'indirizzo per la spedizione."); return false; }
if (provincia === "") { alert("Selezionare la provincia."); return false; }
if (nazione === "") { alert("Selezionare la nazione."); return false; }

document.forms[0].submit();

}

function confirmOrder() {
    var answer = confirm("ATTENZIONE: cliccando su OK l'ordine non potrà più essere annullato.");
	if (answer) {
		doLinks(0);
		document.forms[0].cancel.disabled = 'true';
		document.forms[0].purchase.disabled = 'true';
		document.forms[0].submit();
		}
}
