﻿$(document).ready(function () {
    var pop = "<div id='divnote'>Qui sotto puoi aggiungere delle note per la consegna     <textarea id='TextArea1' rows='4' cols='20'></textarea>    <input type ='button' id='bconf' value='Salva le note' />    <input type ='button' id='bannu' value='Annulla' />    </div>    <div id='windpaypal'></div>        <div id='CorpoCarrello'><div id='Chiudi' ></div><span id='tr'>Puoi spostarmi con il mouse(click+trascina)</span><center id='cen'>     <table id='tableCarrello'><tr class='rgHeaderCarrello'>     <th class='colHeaderCarrello'>canc</th><th class='colHeaderCarrello'>Articolo</th><th class='colHeaderCarrello'>UM</th><th class='colHeaderCarrello'>Quantita</th><th class='colHeaderCarrello'>Prezzo</th><th class='colHeaderCarrello'>Totale Riga</th></tr></table><table id='tablePiedeCarrello'>          <tr id='rgPC'><th>Spedizione</th><th>Contrassegno</th><th>Totale Ordine</th></tr>     <tr class='piede'><td>€.<span id='spese'></span></td><td>€.<span id='contrassegno'></span></td><td>€.<span id='totaleordine'></span></td></tr>     </table><div id='divpag'><a href='#'id='pnote' >Note</a><p></p><span>Scegli il pagamento ,fai click su un pulsante</span><table id='tpag'>     <tr><td id='iw' ></td><td id='bo'></td><td id='pg'></td></tr><tr><td id='ctr'></td><td id='pric'></td><td id='pal'></td></tr></table>  </div>    </center>    <input type='button'  class='vcassa'  value='vai alla cassa' />    </div>"
    $('body').append(pop);
    
    $("#accordion").accordion({ active: false, autoHeight: false, collapsible: true, active: 0 });
    $("#accordion1").accordion({ active: false, autoHeight: false, collapsible: true });
    $(".clsp").click(function () { $("#idsp").load("/Spedizioni.aspx #idspe") });
    $(".az").click(function () { $("#azid").load("/Azienda.aspx #paz") });
   

    var flag = $("#fvc").val(); var UN = $("#iddUN").val(); //
    $.ajax({ type: "POST",
        url: "/ServiceOrdine_Completo.aspx",
        dataType: "json",
        data: {},
        success: function (xo) {
           
            getCar(xo);
            getacc(xo);
        }
    });
    function getacc(gg) {
        $(".codpag").click(
    function () {
        var a = $(this).html();
        for (c in gg.CondPag) {
            if (a == gg.CondPag[c].Descrizione_Pag)
            { var b = "<img src=" + gg.CondPag[c].UrlImmagine + " />" + gg.CondPag[c].Dettagli; $("." + a).html(b) }
        }
    }
   )
    }

    $(".aggcar").click(function () {
        var UName = $('#iddUN').val();
        var art = ""; var des = ""; var um = ""; var qta = ""; var pr = "";
        art = $(this).parent(".x1").children(".CA").val();
        des = $(this).parent(".x1").children(".Descrp").html();
        um = $(this).parent(".x1").children(".climb").html();
        qta = $(this).parent(".x1").children(".clqta").val();
        pr = $(this).parent(".x1").children(".clpr").text().replace("€ ", "");
        $.ajax({
            type: "POST",
            url: "/Service_Carrello.aspx?UName=" + UName + "&art=" + art + "&des=" + des + "&um=" + um + "&qta=" + qta + "&pr=" + pr,
            dataType: "json",
            data: {},
            success: function (xo) { getCar(xo); }
        });
    }
      );

    function getCar(gg) {
        var UName = $('#iddUN').val();
        $("#CorpoCarrello").draggable();
        $("#CorpoCarrello").width(500);
        pulsChiudi(gg);
        PusantePayPay(gg);
        PusanteIWS(gg);
        PulsanteBon(gg);
        PusantePosta(gg);
        pulsContrass(gg);
        pulRposte(gg);
        pulsantevaicassa(gg);
        corporighe(gg)
        CorpoRighePayPal(gg)
        ValoriPiede(gg);
        checkflag();
        var larg = $('#tableCarrello').width();

        $("#tablePiedeCarrello").width(larg);
       
        $('#pnote').click(function () {
            var o = $(this).offset();
            var cssObj1 = { "position": "absolute", "top": (o.top) + "px", "left": (o.left) + 20 + "px" };
            $('#divnote').css(cssObj1);
            $('#divnote').show();
        });

        $('#bconf').click(function () {
            $('#divnote').hide();
            $.get("/Service_Salva_Note.aspx", { note: $('#TextArea1').val() });
        });
        $('#bannu').click(function () {
            $('#divnote').hide();

        });

        PulsCanc();
        if (gg.TestataOrdine.TotaleOrdine > Number(0)) { $("#CorpoCarrello").show(1000); }

    }
    function PulsCanc() {
        $(".canc").click(function () {
            var idr = $(this).children('input').val();
            $(this).parent().remove();
            CancellaRecordCarrello(idr);
        });
    }
    /////////////////////////////////////////////////////////////////////////////
    function CancellaRecordCarrello(nrec) {
        $.getJSON("/ServiceOrdine_Completo.aspx", { DelRiga: nrec }, cancdelos)
        function cancdelos(gg) {
            $('#spese').text(gg.TestataOrdine.Spese_Spedizione);
            $('#contrassegno').text(gg.TestataOrdine.Spese_Contrassegno);
            $('#totaleordine').text(gg.TestataOrdine.TotaleOrdine);

            if (gg.TestataOrdine.TotaleOrdine == 0) {
                $('#CorpoCarrello').hide();
                $(".vcassa").hide();

            }
        }
    }
    function PusantePayPay(gg) {
        var frmpp = gg.CondPag[1].formhtml



        $('#pal').append(frmpp);

        $("input[name='first_name']").val(gg.Cliente.Nome);
        $("input[name='last_name']").val(gg.Cliente.Cognome);
        $("input[name='address1']").val(gg.Cliente.Indirizzo);
        $("input[name='zip']").val(gg.Cliente.CodPostale);
        $("input[name='city']").val(gg.Cliente.Citta);
        $("input[name='email']").val(gg.Cliente.EmailSeller);
        $("input[name='invoice']").val(gg.TestataOrdine.NumOrdine);
        $("input[name='night_phone_b']").val(gg.Cliente.Telnum);
        $("input[name='State']").val(gg.Cliente.Stato);
        $('#helppp').hover(
        function () {
            var offs = $(this).offset();
            $('#windpaypal').html(gg.CondPag[1].Dettagli).show();
            var cssObj = { "position": "absolute", "top": (offs.top) + "px", "left": (offs.left) + 20 + "px" };
            $('#windpaypal').css(cssObj);
        },
        function () {
            $('#windpaypal').hide();
        }
        )
    }
    function PusanteIWS(gg) {
        $('#iw').append(gg.CondPag[0].formhtml)
        $("input[name='PAYER_EMAIL']").val(gg.Cliente.EmailSeller);
        $("input[name='PAYER_LASTNAME']").val(gg.Cliente.Cognome);
        $("input[name='PAYER_FIRSTNAME']").val(gg.Cliente.Cognome);
        $("input[name='AMOUNT']").val(gg.TestataOrdine.TotaleOrdine);
        $("input[name='CUSTOM']").val(gg.TestataOrdine.UserName);
        $("input[name='ITEM_NAME']").val(gg.TestataOrdine.NumOrdine);
        $("input[name='ITEM_NUMBER']").val(gg.TestataOrdine.NumOrdine);
        $('#helpiw').hover(
        function () {
            var offs = $(this).offset();
            $('#windpaypal').html(gg.CondPag[0].Dettagli).show();
            var cssObj = { "position": "absolute", "top": (offs.top) + "px", "left": (offs.left) + 20 + "px" };
            $('#windpaypal').css(cssObj);
        },
        function () {
            $('#windpaypal').hide();
        }
        )
    }
    function PulsanteBon(gg) {
        $('#bo').append(gg.CondPag[2].formhtml)
        $('.hno').attr({ name: 'invoice', value: gg.TestataOrdine.NumOrdine });
        $('#helpbo').hover(
        function () {
            var offs = $(this).offset();
            $('#windpaypal').html(gg.CondPag[2].Dettagli).show();
            var cssObj = { "position": "absolute", "top": (offs.top) + "px", "left": (offs.left) + 20 + "px" };
            $('#windpaypal').css(cssObj);
        },
        function () {
            $('#windpaypal').hide();
        }
        )
    }
    function PusantePosta(gg) {
        $('#pg').append(gg.CondPag[3].formhtml)
        $('.hno').attr({ name: 'invoice', value: gg.TestataOrdine.NumOrdine });
        $('#helppg').hover(
        function () {
            var offs = $(this).offset();
            $('#windpaypal').html(gg.CondPag[3].Dettagli).show();
            var cssObj = { "position": "absolute", "top": (offs.top) + "px", "left": (offs.left) + 20 + "px" };
            $('#windpaypal').css(cssObj);
        },
        function () {
            $('#windpaypal').hide();
        }
        )

    }
    function pulsContrass(gg) {
        $('#ctr').append(gg.CondPag[4].formhtml)
        $('.hno').attr({ name: 'invoice', value: gg.TestataOrdine.NumOrdine });
        $('#helpct').hover(
        function () {
            var position = $('#CorpoCarrello').position();
            var offs = $(this).offset();
            $('#windpaypal').html(gg.CondPag[4].Dettagli).show();
            var cssObj = { "position": "absolute", "top": (offs.top) + "px", "left": (offs.left) + 10 + "px" };
            $('#windpaypal').css(cssObj);
        },
        function () {
            $('#windpaypal').hide();
        }
        )
        $("#imgcont").click(function () {
            if (
            confirm(
            "Attenzione con la scelta della modalità di pagamento in Contrassegno(pagamento al corriere) verrà aggiunto il 2% con un minimo di €.4,7. Vuoi continuare?"
            )
            ) {
                $('#ppcontr').attr({ action: '/PayerCheck/Reg_payment.aspx', metod: 'post' });
                $('imgcont').attr({ name: 'submit' });
            }
            else {
                return
            }
        });
    }
    function pulRposte(gg) {
        $('#pric').append(gg.CondPag[5].formhtml)//        
        $('.hno').attr({ name: 'invoice', value: gg.TestataOrdine.NumOrdine });
        $('#helpric').hover(
        function () {
            var offs = $(this).offset();
            $('#windpaypal').html(gg.CondPag[5].Dettagli).show();
            var cssObj = { "position": "absolute", "top": (offs.top) + "px", "left": (offs.left) + 20 + "px" };
            $('#windpaypal').css(cssObj);
        },
        function () {
            $('#windpaypal').hide();
        }
        )
    }
    function pulsantevaicassa(gg) {

        $(".vcassa").click(function () {
            location.href = '/Pagamento/Chek.aspx';
        });
        if (gg.TestataOrdine.TotaleOrdine == '0') {
            $('#CorpoCarrello').hide();
            $(".vcassa").hide();
        }
    }
    function corporighe(gg) {
        $(".corpo").remove();
        var righe = "";
        for (p in gg.CorpoOrdine) {
            righe += "<tr class='corpo'>"
            righe += "<td class='canc'>"
            righe += "<img alt='cancella riga' id='icodel' src='/App_Themes/Default/images/icon-delete.gif'  />"
            righe += "<input id='idrec' type ='hidden' value=" + gg.CorpoOrdine[p].id + " />"
            righe += "<td>" + gg.CorpoOrdine[p].Descr_Articolo + "</td>"
            righe += "<td>" + gg.CorpoOrdine[p].UM + "</td>"
            righe += "<td>" + gg.CorpoOrdine[p].Quantita + "</td>"
            righe += "<td>€." + gg.CorpoOrdine[p].Prezzo + "</td>"
            righe += "<td>€." + gg.CorpoOrdine[p].LineTotal + "</td></tr>";
        }
        $('#tableCarrello').append(righe);
    }
    function CorpoRighePayPal(gg) {
        $('.rcar').remove();
        var rpp = "";
        for (p in gg.CorpoOrdine) {
            rpp += "<input type='hidden' class='rcar' name ='item_name_" + (Number(p) + Number(1)) + "' value='" + gg.CorpoOrdine[p].Descr_Articolo + "' />"
            rpp += "<input type='hidden' class='rcar' name ='quantity_" + (Number(p) + Number(1)) + "' value='" + gg.CorpoOrdine[p].Quantita + "' />"
            rpp += "<input type='hidden' class='rcar' name ='amount_" + (Number(p) + Number(1)) + "' value='" + gg.CorpoOrdine[p].Prezzo + "' />"
            rpp += "<input type='hidden' class='rcar' name ='handling_cart' value='" + (Number(gg.TestataOrdine.Spese_Spedizione) + Number(gg.TestataOrdine.Spese_Contrassegno)) + "' />"
            rpp += "<input type='hidden' class='rcar' name ='item_number_" + (Number(p) + Number(1)) + "' value='" + gg.CorpoOrdine[p].NumOrdine + "' />";
        }
        $("#pp").append(rpp);
    }
    function ValoriPiede(gg) {
        $('#spese').text(gg.TestataOrdine.Spese_Spedizione);
        $('#contrassegno').text(gg.TestataOrdine.Spese_Contrassegno);
        $('#totaleordine').text(gg.TestataOrdine.TotaleOrdine);
    }
    function pulsChiudi(gg) {
        $('#Chiudi').click(function () {
            $('#CorpoCarrello').hide();
        });

    }
    function checkflag() {
        if (flag == 1) { $(".vcassa").hide(); $("#divpag").show(); }
        else { $(".vcassa").show(); $("#divpag").hide(); }
    }
    $('#icocar').click(function () {
        $.ajax({ type: "POST",
            url: "/ServiceOrdine_Completo.aspx",
            dataType: "json",
            data: {},
            success: function (xo) {
                getico(xo);
            }
        });
        function getico(jj) {
            if (jj.TestataOrdine.TotaleOrdine > Number(0)) {
                $('#CorpoCarrello').show(1000);
            }
            if (jj.TestataOrdine.TotaleOrdine == 0) { $('#vuoto').show(2000); $('#vuoto').hide(3000); }
        }
    });
    $("body").mousemove(function (e) {
        var cssObj = { "position": "absolute", "top": (e.clientY) - 20 + "px", "left": (e.clientX) + 20 + "px" };
        $("#vuoto").css(cssObj);
    })
    $(".Dettagli").click(function () {
        var oj = $(this).offset();
        var cssObj2 = { "top": Math.abs((oj.top) - 200) + "px", "left": "0px" };
        var artic = $(this).parent(".x1").children(".CA").val();
        $('#tran').remove();
        $('#Darticolo').show(1000);
        $("#Darticolo").draggable();
        $('#Darticolo').css(cssObj2);
        $('#Darticolo').append("<div id='tran'></div>")
        $('#tran').load("/Articolo.aspx?CodArticolo=" + artic + " #Articolo", function () {
            $('#sera').click(function () { $('#Darticolo').hide(1000); });
            $(".aggcarrello").click(function () {
                var ca = $("#fcodart").val();
                var da = $("#DetailsView1_Dart").text();
                var qt = $("#txqta").val();
                var pr = $("#DetailsView1_Lprezzo").text().replace("€ ", "");
                var um = $("DetailsView1_imballo").text();
                $.ajax({
                    type: "POST",
                    url: "/Service_Carrello.aspx?UName=" + "&art=" + ca + "&des=" + da + "&um=" + um + "&qta=" + qt + "&pr=" + pr,
                    dataType: "json",
                    data: {},
                    success: function (xo) { getCar(xo); }
                });


            });


        });

    });

});