﻿$(document).ready(function () {

    $("a[rel^='prettyPhoto']").prettyPhoto();

    $(".boxProdotto :button").live('click', function () {
        var boxAcquisto = $(this).parent(".boxAcquisto");
        var idprodotto = boxAcquisto.children(":hidden[name='idprodotto']").val();
        var importo = boxAcquisto.children(":hidden[name='importo']").val();
        var qta = boxAcquisto.children("[name='qta']").val();

        $.ajax({
            type: "GET",
            url: "/modello/azioni/CarrelloAggiungi.aspx",
            data: "idprodotto=" + idprodotto.toString() + "&importo=" + importo.toString() + "&qta=" + qta.toString(),
            async: false,
            success: function (msg) {
                if (msg == "ok") {
                    alert("aggiunto al carrello prodotto " + idprodotto.toString() + "\nqta " + qta.toString() + "\nimporto" + importo.toString());
                    return true;
                } else {
                    alert("errore\n" + idprodotto.toString() + "\nqta " + qta.toString() + "\nimporto" + importo.toString());
                    return false;
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });


    $(".boxCarrello :button[azione='evadi']").live('click', function () {
        $.ajax({
            url: "/modello/azioni/Registrazione.aspx",
            cache: false,
            async: false,
            dataType: "html",
            success: function (data) {
                $("#contentAjax").html(data);
            }
        });

        $("#boxAjax").overlay({
            mask: {
                color: '#000',
                loadSpeed: 200,
                opacity: 0.2
            },
            closeOnClick: false,
            load: true,
            onClose: function () {
                window.location.reload(true);
            }
        });
    });

    $(".boxCarrello :button[azione='svuota']").live('click', function () {
        $.ajax({
            type: "GET",
            url: "/modello/azioni/CarrelloSvuota.aspx",
            async: false,
            success: function (msg) {
                if (msg == "1") {
                    alert("Carrello svuotato");
                    window.location.reload(true);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });


    $(".boxRegistrazione :button").live('click', function () {

        var form = $("form.formRegistrazione");
        $(form).validator();

        $.ajax({
            type: "GET",
            url: "/modello/azioni/RegistrazioneAggiungi.aspx",
            data: form.serialize().toString(),
            async: false,
            success: function (msg) {
                if (msg != "1") {
                    alert(msg);
                    return false;
                } else {
                    //alert("registrazione effettuata correttamente");
                    $.ajax({
                        url: "/modello/azioni/Ordine.aspx",
                        cache: false,
                        async: false,
                        dataType: "html",
                        success: function (data) {
                            $("#contentAjax").html(data);
                        }
                    });
                    return true;
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });


    $(".boxOrdine :button").live('click', function () {
        var form = $("form.formOrdine");
        $(form).validator();

        var idSpedizione = $("input[name=idSpedizione]:checked").val();
        var idPagamento = $("input[name=idPagamento]:checked").val();

        if (idPagamento != null && idSpedizione != null) {
            //alert(form.serialize());
            $.ajax({
                type: "GET",
                url: "/modello/azioni/OrdineAggiungi.aspx",
                data: form.serialize().toString(),
                async: false,
                success: function (msg) {
                    if (msg != "1") {
                        alert(msg);
                        return false;
                    }
                    else {
                        $.ajax({
                            url: "/modello/azioni/Conferma.aspx",
                            cache: false,
                            async: false,
                            dataType: "html",
                            success: function (data) {
                                $("#contentAjax").html(data);
                            }
                        });
                        return true;
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                }
            });
        } else {
            alert("Selezionare una modalità di pagamento e di spedizione");
        }
    });



});
