﻿/*
function keepAliveSession(imgName) {
    var myImg = document.getElementById(imgName);
    if (myImg) myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
}

window.setInterval("keepAliveSession('imgKA')", 1000*60*17);
*/
function EntraDivTrasporto() {
   // $("#wrapper-out").html('<div style="z-index:789;width:100%;height:900px;background-color:#ededed;opacity:0.4;filter:alpha(opacity=40)"></div>');
    var l = ((screen.width - $('#divVolanteTraspRef').width()) / 2) + 'px';
    var t = ($(window).scrollTop() + ($(window).height() / 2)) + 'px';
    $('#divVolanteTraspRef').css({ top: t });

    $('#divVolanteTraspRef').animate({ left: l, top: t }, 500, 'easeOutBack');
    overlay();

}

function overlay() {
    el = document.getElementById("overlay");
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
    $('#overlay').css('opacity', 0.3);
    $('#overlay').css('width', $('#wrapper-out').width());
    $('#overlay').css('height', $('#wrapper-out').height());
}

function DeleteRiga(obj,ecomm) {
    var id = obj.name;
    ref = id + '|' + ecomm ;
    teaknique._Script.wsCarrello.DeleteRiga(id, OnRequestDeleteRigaComplete, OnRequestFailed, ref);

    return false;
}


function OnRequestFailed(result) {

    alert('Errore');

}


function OnRequestDeleteRigaComplete(result, ref) {
   // alert(ref);
    var aRef = ref.split('|');

    var div = $("#riga_" + aRef[0]);
    var divbis = $("#riga_" + aRef[0] + "_bis");
    if (result != null) {
        div.hide('slow');
        divbis.hide();
        if (aRef[1] == 'false') {
          //  $('#ctl00_ContentPlaceHolder1_LinkButtonAggiorna2').click();
            
            $("#ctl00_LabelNumeroProdottiCarrello").html(result.qta);
            $("#ctl00_LabelTotaleCarrello").html(result.prezzoIvato);
            $("#ctl00_ContentPlaceHolder1_LabelTotaliSubTotale").html(result.prezzoIvato);
            if (result.qta == 0) {
                document.location.href = "Carrello.aspx?w=e";
            }
            else {
                $(".LabelTotaliSubTotale").html(result.prezzoIvato);
                $(".LabelTotaliSpedizioni").html(result.trasporto);
                $(".LabelTotaliTotale").html(result.totalecontrasporto);
            }
        }
        else {

            $("#ctl00_LabelNumeroProdottiWish").html(result.qta);
            if (result.qta == 0) {
                document.location.href = "Carrello.aspx?w=e";
            }
            
        
        }
    }
    else {

        alert('Error');

    }
}

function spostaCarrello(id_prodotto, ecomm, qta, id_carrellor,obj) {

    insertCarrello(id_prodotto, ecomm, qta);
    DeleteRiga(obj,ecomm);
    return false;

}

function insertCarrello(id_prodotto,ecomm,qta){

    ref = ecomm;
    teaknique._Script.wsCarrello.Insert(id_prodotto,qta,ecomm, OnRequestCarrelloComplete, OnRequestFailed, ref);
    
    return false;

}

function OnRequestCarrelloComplete(result, ref) {

    var divHLight = (ref==true)?'ecomm':'wish';
    if (result != null) {
       // alert(result);
        if (ref == true) {
            var l = ((screen.width - $('#divVolanteRef').width()) / 2) + 'px';
            var t = ($(window).scrollTop() + ($(window).height() / 2)) + 'px';
            $('#divVolanteRef').css({  top: t });
            $("#ctl00_LabelNumeroProdottiCarrello").html(result.qta);
            $("#ctl00_LabelTotaleCarrello").html(result.prezzoIvato);

            
            //alert(t);
            $('#divVolanteRef').animate({ left: l, top:t }, 500, 'easeOutBack');
          
        }
        else {
            $("#ctl00_LabelNumeroProdottiWish").html(result.qta);
        }
           // $("#" + divHLight).fadeTo("slow", 0.33, callBackFade);

    }
    else {

        alert('Error');

    }
}

function getProdotto(ddl, lang, prezzoid, imageid, ecommid, wishid, codiceid, giacenzaid, dispfuturaid, datadispfuturaid) {

    ref = new Array(prezzoid, imageid, ecommid, wishid, codiceid, giacenzaid, dispfuturaid, datadispfuturaid);
    teaknique._Script.wsCarrello.GetProdotto(ddl.value, lang, OnRequestProdottoComplete, OnRequestFailed, ref);

    return false;
}


function OnRequestProdottoComplete(result, ref) {

    var image = ref[1];
    var prezzo = ref[0];
    var ecomm = ref[2];
    var wish = ref[3];
    var codice = ref[4];
    var giacenza = ref[5];
    var dispfutura = ref[6];
    var datadispfutura = ref[7];
    if (result != null) {
        var myWidth = 165;
        if (document.location.href.toUpperCase().indexOf('PRODOTTI.ASPX')>0) {
            myWidth = 402;

            $("#" + giacenza).fadeTo(0, 0);
            $("#" + giacenza).html(result.giacenza);
            $("#" + giacenza).fadeTo("slow", 1);

            $("#" + dispfutura).fadeTo(0, 0);
            $("#" + dispfutura).html(result.disponibilitafutura);
            $("#" + dispfutura).fadeTo("slow", 1);

            $("#" + datadispfutura).fadeTo(0, 0);
            $("#" + datadispfutura).html(result.datadisponibilitafutura);
            $("#" + datadispfutura).fadeTo("slow", 1);
            
        }
        $("#" + image).fadeTo(0, 0);
        $("#" + image).attr("src", "ImageWriter.aspx?image=" + result.foto + "&w=" + myWidth);
        $("#" + image).fadeTo("slow", 1);
        
        if(codice!='dummy'){
            $("#" + codice).fadeTo(0, 0);
            $("#" + codice).html(result.codice);
            $("#" + codice).fadeTo("slow", 1);
        }
        
        $("#" + prezzo).fadeTo(0, 0);
        $("#" + prezzo).html(result.prezzoivato);
        $("#" + prezzo).fadeTo("slow", 1);
        
        $("#" + ecomm).removeAttr("onclick");
        $("#" + ecomm).unbind('click');

        //alert($("#" + ecomm).attr('onclick'));
        //$("#" + ecomm).attr("onclick", "javascript:insertCarrello(" + result.id_prodotto + ", true, 1)");
        //alert($("#" + ecomm).attr('onclick'));
        $("#" + ecomm).click(function(e) {
            insertCarrello(result.id_prodotto, true, 1);
            return false;
        });
        $("#" + wish).removeAttr("onclick");
        $("#" + wish).unbind('click');
        $("#" + wish).click(function(e) {
            insertCarrello(result.id_prodotto, false, 1);
            return false;
        });

        
    }
    else {

        alert('Error');

    }
}
$(document).ready(function() {
    $(".closeDiv").click(function() {

        var t = ($('#divVolanteRef').offset().top) + 'px';
        //alert(t);
        $('#divVolanteRef').animate({ left: '-400px', top: t }, 500, 'easeInBack');

    });
    $("#ctl00_ContentPlaceHolder1_ckbFatturazione").live('click',function() {
        if ($("#ctl00_ContentPlaceHolder1_ckbFatturazione").is(':checked') == true) {
            $('#divfatturazione').show('fast');
        }
        else {
            $('#divfatturazione').hide('fast');
        }
    });
    //$("#ctl00_ContentPlaceHolder1_HyperLinkp2").attr('disabled', 'disabled');

    $(".closeTrDiv").live('click', function() {
        var t = ($('#divVolanteTraspRef').offset().top) + 'px';
        //alert(t);
        $('#divVolanteTraspRef').animate({ left: '-600px', top: t }, 500, 'easeInBack');
        overlay();

    });

})

