﻿var AppPath = "http://www.quenteebom.com/";
var AppPathSSL = "https://www.quenteebom.com/";
//var AppPath = "http://server2.ibt.pt/testesnet2/quenteebom/";
//var AppPathSSL = "https://server2.ibt.pt/testesnet2/quenteebom/";
//var AppPath = "http://localhost/quentebom/";

var uploadedFoto = "";
$(document).ready(function() {

    var AppPathOrSSL = AppPath;
    if (ssl) {
        AppPathOrSSL = AppPathSSL
    }

    //var maxHeight = window.innerHeight + window.scrollMaxY //FF
    //var maxHeight = window.innerHeight + window.scrollMaxY


    //clientWidth
    //$("#divLeftBar").css("height", $("body").attr("offsetHeight") + "px");
    if (document.getElementById('divLeftBar').clientHeight < document.getElementById('divRight').clientHeight) {
        $("#divLeftBar").css("height", document.getElementById('divRight').clientHeight + "px");
    }
    //    alert(getPageSizeWithScroll());
    //    alert(document.documentElement.clientHeight);
    //    alert(document.body.offsetHeight);

    $("#divBTPesquisa").click(function() {
        if ($("#txtPesquisa").val() != "") {
            redirect(AppPath + "pesquisa.aspx?key=" + $("#txtPesquisa").val());
        }
    });

    $("#divBTNewsletter").click(function() {
        subscribeNewsletter($("#txtNewsletter").val());
    });

    $("#divListaAbrirBT").click(function() {
        openLista();
    });
    $("#divListaFecharBT").click(function() {
        closeLista();
    });

    $(".divProdutoPreviewBTCarrinho[hasProperties='false']").click(function() {
        openLista();
        addCart($(this).attr("idProduct"));
    });

    $(".divProdutoPreviewBTCarrinho[hasProperties='true']").click(function() {
        window.location = $(this).attr("link");
    });

    $(".divProdutoPreviewImage[hasProperties='false']").draggable({ revert: true, zIndex: 999 });


    $("#divLista").droppable({
        drop: function(event, ui) {
            openLista();
            addCart(ui.draggable.attr("idProduct"));
        }
    });

    if ($(".fuFoto").length > 0) {
        var fuFotoId = $(".fuFoto").attr("id");

        var uploadFoto = new AjaxUpload(fuFotoId, {
            action: AppPath + 'ajax/upload.ashx',
            autoSubmit: false,
            onChange: function(file, extension) {
                $("#divFotoToUpload").text(file);
                this.disable();
            },
            onSubmit: function(file, ext) {
                if (!(ext && /^(jpg|png|jpeg|gif|bmp|pdf)$/.test(ext))) {
                    // extension is not allowed
                    alert('Este tipo de ficheiro não é permitido e não foi enviado. Por favor entre em contacto com a nossa linha de apoio.');
                    // cancel upload
                    return false;
                }
            },
            onComplete: function(file, response) {
            uploadedFoto = String(response).replace("<pre>", "").replace("</pre>", "").replace("<PRE>", "").replace("</PRE>", "");
                replaceImageName($("#divFotoToUpload").text(), uploadedFoto);
            }
        });
    }

    $(".divDetalheProdutoBTAdicionarCarrinho").click(function() {
        openLista();


        var strProps = "";
        var propMassa = $("select[propMassa='true']");
        var propCobertura = $("select[propCobertura='true']");
        var propDecoracao = $("select[propDecoracao='true']");
        var propFoto = $("input[propFoto='true']");
        var propVelas = $("input[propVelas='true']");
        var propDizeresEscolha = $("input[type='radio']");
        var propDizeres = $("input[propDizeres='true']");
        var propObservacoes = $("textarea[propObservacoes='true']");
        if (propMassa.length > 0) {
            if (propMassa.attr("selectedIndex") != 0) {
                strProps = strProps + "propMassa|" + encodeURI(propMassa.attr("options")[propMassa.attr("selectedIndex")].text) + "_";
            }
        }
        if (propCobertura.length > 0) {
            if (propCobertura.attr("selectedIndex") != 0) {
                strProps = strProps + "propCobertura|" + encodeURI(propCobertura.attr("options")[propCobertura.attr("selectedIndex")].text) + "_";
            }
        }
        if (propDecoracao.length > 0) {
            if (propDecoracao.attr("selectedIndex") != 0) {
                strProps = strProps + "propDecoracao|" + encodeURI(propDecoracao.attr("options")[propDecoracao.attr("selectedIndex")].text) + "_";
            }
        }
        //        if (propFoto.length > 0) {
        //            if (propFoto.val() != "") {

        //                uploadFoto.submit();

        //                strProps = strProps + "propFoto|" + uploadedFoto + "_";
        //            }
        //        }
        if ($("#divFotoToUpload").text() != "") {

            uploadFoto.submit();

            //do { }
            //while (uploadedFoto == "");

            //pausecomp(1000);

            //waitFoto(uploadedFoto);
            if (uploadedFoto == "") {
                strProps = strProps + "propFoto|" + $("#divFotoToUpload").text() + "_";
            } else {
                strProps = strProps + "propFoto|" + uploadedFoto + "_";
            }
        }

        if ($("#ckbVelas").attr("checked")) {
            if (propVelas.length > 0) {
                if (propVelas.val() != "") {
                    strProps = strProps + "propVelas|" + encodeURI(propVelas.val()) + "_";
                }
            }
        }
        for (i = 0; i < propDizeresEscolha.length; i++) {
            if ($(propDizeresEscolha[i]).attr("checked")) {
                strProps = strProps + "propDizeresEscolha|" + encodeURI($(propDizeresEscolha[i]).val()) + "_";
            }
        }
        if (propDizeres.length > 0) {
            if (propDizeres.val() != "") {
                strProps = strProps + "propDizeres|" + encodeURI(propDizeres.val()) + "_";
            }
        }
        if (propObservacoes.length > 0) {
            if (propObservacoes.val() != "") {
                strProps = strProps + "propObservacoes|" + encodeURI(propObservacoes.val()) + "_";
            }
        }

        //alert(strProps);

        if ($(this).attr("peso") == "true") {
            addCart($(this).attr("idProduct"), strProps, $("input[txtPeso='true']").val());
        }
        else {
            addCart($(this).attr("idProduct"), strProps);
        }
    });
    $(".divDetalheProdutoBTAdicionarWishlist").click(function() {
        addWishlist($(this).attr("idProduct"));
    });
    $(".divDetalheProdutoBTRecomendar").click(function() {
        redirect(AppPath + "recomendar.aspx?ido=" + $(this).attr("idProduct"));
    });
    $(".divProdutoPreviewImageApagar").click(function() {
        deleteWishlist($(this).attr("idProduct"));
    });

    $(".txtVelas").keyup(function() {
        $(this).val($(this).val().replace(/([^0-9])/g, ""));
    });

    $("#ckbVelas").change(function() {
        if (this.checked == true) {
            $(".txtVelas").attr("disabled", false);
        }
        else {
            $(".txtVelas").attr("disabled", true);
        }
    });


    var pesoPessoa = 0.120;

    var QuantidadeMinima = $("input[txtPeso = 'true']").attr("QuantidadeMinima")

    $("input[txtNumPessoas='true']").val(parseInt(QuantidadeMinima / pesoPessoa));
    $("input[txtPeso='true']").blur(function() {

        if ($(this).val() < QuantidadeMinima) {
            $(this).val(QuantidadeMinima);
        }

        $(this).val(parseFloat($(this).val()).toFixed(1));

        var intNumPessoas = parseInt($(this).val() / pesoPessoa);
        $("input[txtNumPessoas='true']").val(intNumPessoas);
    });

    $("input[txtNumPessoas='true']").blur(function() {
        var intPeso = $(this).val() * pesoPessoa;
        if (intPeso < QuantidadeMinima) {
            $("input[txtPeso='true']").val(QuantidadeMinima);
            var intNumPessoas = $("input[txtPeso='true']").val() / pesoPessoa;
            $(this).val(parseInt(intNumPessoas));
        }
        else {
            $("input[txtPeso='true']").val(intPeso.toFixed(1));
        }
    });

    $("input[txtPeso='true']").keyup(function() {
    $(this).val($(this).val().replace(",", ".").replace(/([^0-9\.])/g, ""));
    });

    $("input[txtNumPessoas='true']").keyup(function() {
        $(this).val($(this).val().replace(/([^0-9\.])/g, ""));
    });

    $(".divImagem a").lightBox({
        overlayOpacity: 0.8,
        imageLoading: AppPath + 'images/lightbox-ico-loading.gif',
        imageBtnClose: AppPath + 'images/lightbox-btn-close.gif',
        imageBtnPrev: AppPath + 'images/lightbox-btn-prev.gif',
        imageBtnNext: AppPath + 'images/lightbox-btn-next.gif'
    });


    $('.divGamaTopoHomeText').flash(
        {
            src: AppPath + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #ffffff; }',
                    'a { color: #ffffff; text-decoration: none; font-size:18px; }',
                    'a:hover { color: #ffffff; font-size:18px; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divTodaGamaHome').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #ffffff; }',
                    'a { color: #ffffff; text-decoration: none; font-size:14px; }',
                    'a:hover { color: #ffffff; font-size:14px; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divGamaTitle').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #ffffff; }',
                    'a { color: #ffffff; text-decoration: none; }',
                    'a:hover { color: #ffffff; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divGamaLink').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #ffffff; }',
                    'a { color: #ffffff; text-decoration: none; }',
                    'a:hover { color: #ffffff; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divTopoDetalheTextVoltar').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #ffffff; }',
                    'a { color: #ffffff; text-decoration: none; }',
                    'a:hover { color: #ffffff; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divTopoDetalheText').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #ffffff; }',
                    'a { color: #ffffff; text-decoration: none; }',
                    'a:hover { color: #ffffff; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divItemMenuSuperior').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #472a2b;  font-size:14px; }',
                    'a { color: #472a2b; text-decoration: none;  font-size:14px; }',
                    'a:hover { color: #472a2b;  font-size:14px; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divMenuEsquerdaItem').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #472a2b;  font-size:18px; }',
                    'a { color: #472a2b; text-decoration: none;  font-size:18px; }',
                    'a:hover { color: #472a2b;  font-size:18px; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );

    $('.divTextRecomendar').flash(
        {
            src: AppPathOrSSL + 'swf/fluxFont.swf',
            wmode: 'transparent',
            flashvars: {
                css: [
                    '* { color: #472a2b;  font-size:8px; }',
                    'a { color: #472a2b; text-decoration: none;  font-size:8px; }',
                    'a:hover { color: #472a2b;  font-size:8px; }'
                ].join(' ')
            }
        },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>' + this.innerHTML + '</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));
        }
    );


    getCart(true);

});

function openLista() {
    if ($("#divLista").css("left") != "0px") {
        $("#divLista").animate({ "left": "+=308px" }, "slow");
        $("#divListaAbrir").fadeOut("slow", function() { $("#divListaFechar").fadeIn("slow"); });
    }
}

function closeLista() {
    if ($("#divLista").css("left") == "0px") {
        $("#divLista").animate({ "left": "-=308px" }, "slow");
        $("#divListaFechar").fadeOut("slow", function() { $("#divListaAbrir").fadeIn("slow"); });
    }
}

function addCart(idProduct, strProps, peso) {
    showLoader();
    if (peso != null) {
        $.ajax({
            url: AppPath + "ajax/addCart.ashx?idProduct=" + idProduct + "&decPeso=" + peso + "&strProps=" + strProps + "&id=" + get0100Random(),
            success: function(data, textStatus) {
                populateLista(data, false, idProduct);

                if ($("#divFotoToUpload").text() != "") {
                    if (uploadedFoto != "") {
                        replaceImageName($("#divFotoToUpload").text(), uploadedFoto);
                    }
                }
                hideLoader();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                hideLoader();
                //alert(textStatus);
            }
        });
    }
    else {
        $.ajax({
            url: AppPath + "ajax/addCart.ashx?idProduct=" + idProduct + "&id=" + get0100Random(),
            success: function(data, textStatus) {
                populateLista(data, false, idProduct);

                if ($("#divFotoToUpload").text() != "") {
                    replaceImageName($("#divFotoToUpload").text(), uploadedFoto);
                }
                hideLoader();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                hideLoader();
                //alert(textStatus);
            }
        });
    }
}

function addWishlist(idProduct) {
    showLoader();
    $.ajax({
        url: AppPath + "ajax/addWishlist.ashx?idProduct=" + idProduct + "&id=" + get0100Random(),
        success: function(data, textStatus) {
            hideLoader();
            alert("Desejo qb adicionado com sucesso");
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            hideLoader();
            //alert(textStatus);
        }
    });
}

function deleteCart(idRow) {
    showLoader();
    $.ajax({
        url: AppPath + "ajax/deleteCart.ashx?strIdRow=" + idRow + "&id=" + get0100Random(),
        success: function(data, textStatus) {
            hideLoader();
            populateLista(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            hideLoader();
            //alert(textStatus);
        }
    });
}

function deleteWishlist(idProduct) {
    showLoader();
    $.ajax({
        url: AppPath + "ajax/deleteWishlist.ashx?idProduct=" + idProduct + "&id=" + get0100Random(),
        success: function(data, textStatus) {
            hideLoader();
            location.reload(true);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            hideLoader();
            //alert(textStatus);
        }
    });
}

function changeQttCart(idRow, decQtt, strSum, strUnidadePeso) {
    showLoader("qtt");
    $.ajax({
        url: AppPath + "ajax/changeQttCart.ashx?strIdRow=" + idRow + "&decQtt=" + decQtt + "&bolSum=" + strSum + "&unidadePeso=" + strUnidadePeso + "&id=" + get0100Random(),
        success: function(data, textStatus) {
            hideLoader();
            if (data == "nok") {
                getCart();
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            hideLoader();
            getCart();
            //alert(textStatus);
        }
    });
}

function get0100Random() {
    return Math.floor(Math.random() * 101);
}

function getCart(bolFirst) {
    showLoader();
    $.ajax({
        url: AppPath + "ajax/getCart.ashx?id=" + get0100Random(),
        success: function(data, textStatus) {
            hideLoader();
            if (data != "") { populateLista(data, true); }
            if (bolFirst) { $("#divListaFecharLoader").fadeOut("slow"); };
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            hideLoader();
            //alert(textStatus);
        }
    });
}

function replaceImageName(imageNameOld, imageNameNew) {
    $.ajax({
        url: AppPath + "ajax/imageNameReplace.ashx?imageNameOld=" + imageNameOld + "&imageNameNew=" + imageNameNew + "&id=" + get0100Random(),
        success: function(data, textStatus) { },
        error: function(XMLHttpRequest, textStatus, errorThrown) { }
    });
}

var intLoad = 0;

var bolQtt = false;

function showLoader(strQtt) {
    $("#divListaFecharLoader").fadeIn("slow");
    intLoad++;
    if (strQtt == "qtt") {
        bolQtt = true;
    }
    $("#divListaFinalizarBT").unbind('click');
}

function hideLoader() {
    intLoad--;
    if (intLoad == 0) {
        $("#divListaFecharLoader").fadeOut("slow");
        if (bolQtt) {
            /*getCart();*/
            bolQtt = false;
        }
        $("#divListaFinalizarBT").click(function() {
            window.location = AppPathSSL + "cart.aspx";
        });
    }
}

function CanCheckout() {
    if (comprasAbaixo) {
        $("#divListaFinalizarBT").attr("title", " ");
        $("#divListaFinalizarBT").removeClass();
        $("#divListaFinalizarBT").addClass("divListaFinalizarBT");
        if (intLoad == 0) {
            $("#divListaFinalizarBT").click(function() {
                window.location = AppPathSSL + "cart.aspx";
            });
        }
    }
    else {
        if (parseFloat($("#divListaFecharPrice").text().replace("€", "")) >= 15) {
            $("#divListaFinalizarBT").attr("title", " ");
            $("#divListaFinalizarBT").removeClass();
            $("#divListaFinalizarBT").addClass("divListaFinalizarBT");
            if (intLoad == 0) {
                $("#divListaFinalizarBT").click(function() {
                    window.location = AppPathSSL + "cart.aspx";
                });
            }
        } else {
            $("#divListaFinalizarBT").unbind('click');
            $("#divListaFinalizarBT").attr("title", "Só pode finalizar a compra acima de 15€");
            $("#divListaFinalizarBT").removeClass();
            $("#divListaFinalizarBT").addClass("divListaFinalizarBTOpacity");
        }
    }
}

function subscribeNewsletter(strMailNewsletter) {
    $.ajax({
        url: AppPath + "ajax/subscribeNewsletter.ashx?strEmail=" + strMailNewsletter + "&id=" + get0100Random(),
        success: function(data, textStatus) {
            alert(data);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(textStatus);
        }
    });
}

function waitFoto(strUploadedFoto) {
    if (strUploadedFoto == "") {
        pausecomp(1000);
        waitFoto(uploadedFoto);
    } else {
        return "";
    }
}

function pausecomp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
}

var totalPrice = 0;

function populateLista(data, bolFirst, idProduct) {
    var objArrayOrder = eval(data);

    var decDivisao = objArrayOrder.length / 13;
    var intDivisao = parseInt(decDivisao);
    var intNumberOfDivs;
    if (decDivisao > intDivisao) {
        intNumberOfDivs = intDivisao + 1;
    }
    else {
        intNumberOfDivs = intDivisao;
    }

    var template = [{
        tagName: 'div',
        id: 'divListaOrderMain',
        childNodes: []
}];

        for (i = 0; i < intNumberOfDivs; i++) {
            template[0].childNodes.push({
                tagName: 'div',
                id: 'divListaOrder' + i,
                className: 'divListaOrder',
                style: 'display: none;',
                childNodes: []
            });
        }

        var intDivToBlock;
        totalPrice = 0;
        var c = 0;
        var t = 0;

        for (m = 0; m < objArrayOrder.length; m++) {
            if (t == 13) {
                t = 0;
                c++;
            }

            if (objArrayOrder[m].id_object == idProduct) { intDivToBlock = c }
            totalPrice = totalPrice + objArrayOrder[m].unitprice * objArrayOrder[m].quantity;

            template[0].childNodes[c].childNodes.push({
                tagName: 'div',
                className: 'divListaOrderDetail',
                childNodes: [{
                    tagName: 'div',
                    className: 'divListaOrderDetailName',
                    title: objArrayOrder[m].full_name,
                    innerHTML: objArrayOrder[m].object_name,
                    idRow: objArrayOrder[m].id_row.toString()
                }, {
                    tagName: 'div',
                    className: 'divListaOrderDetailQuantityKg',
                    innerHTML: objArrayOrder[m].unidade_peso.toString()
                }, {
                    tagName: 'div',
                    className: 'divListaOrderDetailQuantity',
                    innerHTML: objArrayOrder[m].quantity.toFixed(2).toString(),
                    idRow: objArrayOrder[m].id_row.toString(),
                    childNodes: [{
                        tagName: 'div',
                        className: 'divListaOrderDetailQuantityMais',
                        unidadePeso: objArrayOrder[m].unidade_peso.toString(),
                        intQtt: objArrayOrder[m].sum_quantity.toString(),
                        intMinQtt: objArrayOrder[m].min_quantity.toString(),
                        idRow: objArrayOrder[m].id_row.toString()
                    }, {
                        tagName: 'div',
                        className: 'divListaOrderDetailQuantityMenos',
                        unidadePeso: objArrayOrder[m].unidade_peso.toString(),
                        intQtt: objArrayOrder[m].sum_quantity.toString(),
                        intMinQtt: objArrayOrder[m].min_quantity.toString(),
                        idRow: objArrayOrder[m].id_row.toString()
}]
                    }, {
                        tagName: 'div',
                        className: 'divListaOrderDetailPrice',
                        idRow: objArrayOrder[m].id_row.toString(),
                        unitPrice: objArrayOrder[m].unitprice.toString(),
                        innerHTML: (objArrayOrder[m].unitprice * objArrayOrder[m].quantity).toFixed(2).toString()
                    }, {
                        tagName: 'div',
                        className: 'divListaOrderDetailDelete',
                        idRow: objArrayOrder[m].id_row.toString()
}]
                    });
                    t++;
                }
                $("#divListaFecharRightCenter").empty();
                $("#divListaFecharRightCenter").appendDom(template);
                $("#divListaFecharPrice").text(totalPrice.toFixed(2).toString() + "€");

                if (bolFirst) {
                    $("#divListaOrder0").css('display', 'block');
                }
                else if (intDivToBlock >= 0) {
                    $("#divListaOrder" + intDivToBlock).css('display', 'block');
                }
                else {
                    $("#divListaOrder" + (intNumberOfDivs - 1)).css('display', 'block');
                }

                if (intNumberOfDivs > 1) {
                    $("#divBTSLista").css('display', 'block');
                    enableBtsLista();
                }
                else {
                    $("#divBTSLista").css('display', 'none');
                }

                enableBTSProdutos();
                CanCheckout();
            }

            function enableBTSProdutos() {

                $(".divListaOrderDetailDelete").unbind('click');
                $(".divListaOrderDetailDelete").click(function() {
                    openLista();
                    deleteCart($(this).attr("idRow"));
                });
                $(".divListaOrderDetailQuantityMais").click(function() {
                    var intQttAux = parseFloat($(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").text()) + parseFloat($(this).attr("intQtt"));
                    intQttAux = intQttAux.toFixed(2);
                    //alert(intQttAux);
                    //alert(parseFloat($(this).attr("intMinQtt")).toFixed(2));
                    //alert(intQttAux >= parseFloat($(this).attr("intMinQtt")).toFixed(2));
                    //if (intQttAux >= parseFloat($(this).attr("intMinQtt")).toFixed(2)) {
                    $(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").html($(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").html().replace($(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").text(), intQttAux.toString()));
                    $(".divListaOrderDetailPrice[idRow='" + $(this).attr("idRow") + "']").html((intQttAux * $(".divListaOrderDetailPrice[idRow='" + $(this).attr("idRow") + "']").attr("unitPrice")).toFixed(2));
                    changeQttCart($(this).attr("idRow"), $(this).attr("intQtt"), "true", $(this).attr("unidadePeso"));
                    enableBTSProdutos();
                    getTotals();
                    CanCheckout();
                    //}
                });
                $(".divListaOrderDetailQuantityMenos").click(function() {
                    var intQttAux = parseFloat($(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").text()) - parseFloat($(this).attr("intQtt"));
                    intQttAux = intQttAux.toFixed(2);
                    if (intQttAux >= $(this).attr("intMinQtt")) {
                        $(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").html($(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").html().replace($(".divListaOrderDetailQuantity[idRow='" + $(this).attr("idRow") + "']").text(), intQttAux.toString()));
                        $(".divListaOrderDetailPrice[idRow='" + $(this).attr("idRow") + "']").html((intQttAux * $(".divListaOrderDetailPrice[idRow='" + $(this).attr("idRow") + "']").attr("unitPrice")).toFixed(2));
                        changeQttCart($(this).attr("idRow"), $(this).attr("intQtt"), "false", $(this).attr("unidadePeso"));
                        enableBTSProdutos();
                        getTotals();
                        CanCheckout();
                    }
                });
            }

            function enableBtsLista() {
                var intDivBlocked;
                var intNumberOfDivs = $("#divListaOrderMain > *").length - 1;
                for (i = 0; i < $("#divListaOrderMain > *").length; i++) {
                    if ($($("#divListaOrderMain > *")[i]).css("display") == "block") { intDivBlocked = i; }
                }
                if (intDivBlocked == 0) {
                    $("#divBTListaRight").click(function() {
                        divsListaRight(intDivBlocked);
                    });
                    $("#divBTListaLeft").unbind('click');
                    $("#divBTListaRight").removeClass()
                    $("#divBTListaLeft").removeClass()
                    $("#divBTListaLeft").addClass("divBTListaLeftOff");
                    $("#divBTListaRight").addClass("divBTListaRight");
                }
                else if (intDivBlocked == intNumberOfDivs) {
                    $("#divBTListaLeft").click(function() {
                        divsListaLeft(intDivBlocked);
                    });
                    $("#divBTListaRight").unbind('click');
                    $("#divBTListaRight").removeClass()
                    $("#divBTListaLeft").removeClass()
                    $("#divBTListaRight").addClass("divBTListaRightOff");
                    $("#divBTListaLeft").addClass("divBTListaLeft");
                }
                else {
                    $("#divBTListaRight").click(function() {
                        divsListaRight(intDivBlocked);
                    });
                    $("#divBTListaLeft").click(function() {
                        divsListaLeft(intDivBlocked);
                    });
                    $("#divBTListaRight").removeClass()
                    $("#divBTListaLeft").removeClass()
                    $("#divBTListaLeft").addClass("divBTListaLeft");
                    $("#divBTListaRight").addClass("divBTListaRight");
                }
            }

            function divsListaRight(intDivBlocked) {
                $("#divListaOrder" + intDivBlocked).fadeOut("slow", function() { $("#divListaOrder" + (intDivBlocked + 1)).fadeIn("slow", function() { $("#divListaOrder" + (intDivBlocked + 1)).css('display', 'block'); enableBtsLista(); }); });
            }

            function divsListaLeft(intDivBlocked) {
                $("#divListaOrder" + intDivBlocked).fadeOut("slow", function() { $("#divListaOrder" + (intDivBlocked - 1)).fadeIn("slow", function() { $("#divListaOrder" + (intDivBlocked - 1)).css('display', 'block'); enableBtsLista(); }); });
            }

            function getTotals() {
                var totalPrice = 0;
                for (i = 0; i < $(".divListaOrderDetailPrice").length; i++) {
                    totalPrice = totalPrice + parseFloat($($(".divListaOrderDetailPrice")[i]).text());
                }
                $("#divListaFecharPrice").text(totalPrice.toFixed(2).toString() + "€");
            }

            function redirect(url) {
                window.location = url
            }

            /**************************************/


            //changeQty('txtQty_<%# DataBinder.Eval(Container.DataItem, "ID") %>', <%#DataBinder.Eval(Container.DataItem, "intQtt")%>, <%#DataBinder.Eval(Container.DataItem, "intMinQtt")%>, 0)
            function changeQtyUn(control, intQtt, intMinQtt, action) {
                var txtControl = document.getElementById(control);
                var decTxtValue = parseInt(txtControl.value);
                var decIntQtt = parseInt(intQtt);
                var decIntMinQtt = parseInt(intMinQtt);

                if (action == 0) {
                    if (decTxtValue > 0) {
                        if ((decTxtValue - decIntQtt) > decIntMinQtt) {
                            txtControl.value = decTxtValue - decIntQtt;
                        }
                        else {
                            txtControl.value = decIntMinQtt;
                        }
                    }
                }
                else {
                    txtControl.value = decTxtValue + decIntQtt;
                }
            }

            function changeQtyKg(control, intQtt, intMinQtt, action) {
                var txtControl = document.getElementById(control);
                var decTxtValue = parseFloat(String(txtControl.value).replace(",", "."));
                var decIntQtt = parseFloat(String(intQtt).replace(",", "."));
                var decIntMinQtt = parseFloat(String(intMinQtt).replace(",", "."));

                if (action == 0) {
                    if (decTxtValue > 0) {
                        if ((decTxtValue - decIntQtt) > decIntMinQtt) {
                            txtControl.value = parseFloat(decTxtValue - decIntQtt).toFixed(2);
                        }
                        else {
                            txtControl.value = decIntMinQtt.toFixed(2);
                        }
                    }
                }
                else {
                    txtControl.value = parseFloat(decTxtValue + decIntQtt).toFixed(2);
                }
            }


            function resUn(control) {
                control.value = control.value.replace(/([^0-9])/g, "");
            }


            function resKg(control) {
                control.value = control.value.replace(/([^0-9\.])/g, "");
            }


            function setFocus(control, color) {
                control.style.backgroundColor = '#' + color;
            }



            function copyFormFields(checkbox, fields) {
                for (pairIndex in fields.split(';')) {
                    var origin = document.getElementById(fields.split(";")[pairIndex].split(',')[0])
                    var destination = document.getElementById(fields.split(";")[pairIndex].split(',')[1])

                    if (checkbox.checked) {
                        if (origin.tagName == 'INPUT' || origin.tagName == 'TEXTAREA') {
                            destination.value = origin.value;
                        }
                        else if (origin.tagName == 'SELECT') {
                            destination.selectedIndex = origin.selectedIndex;
                        }

                        destination.disabled = true;
                    }
                    else {
                        if (origin.tagName == 'INPUT' || origin.tagName == 'TEXTAREA') {
                            destination.value = '';
                        }
                        else if (origin.tagName == 'SELECT') {
                            destination.selectedIndex = 0;
                        }

                        destination.disabled = false;
                    }

                }
            }

            function disableFormFields(checkbox, fields) {
                for (pairIndex in fields.split(';')) {
                    var control = document.getElementById(fields.split(";")[pairIndex])

                    if (control.tagName == 'INPUT' || control.tagName == 'TEXTAREA') {
                        control.value = '';
                    }
                    else if (control.tagName == 'SELECT') {
                        control.selectedIndex = 0;
                    }

                    control.disabled = checkbox.checked;

                }
            }

            /////////////////////////////////////////

            function getPageSizeWithScroll() {
                if (window.innerHeight && window.scrollMaxY) {// Firefox
                    yWithScroll = window.innerHeight + window.scrollMaxY;
                } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                    yWithScroll = document.body.scrollHeight;
                } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
                    yWithScroll = document.body.offsetHeight;
                }
                return yWithScroll;
            }

            function cvv() {
                var wWidth = 400;
                var wHeight = 350;
                var wTop = (screen.availHeight - wHeight) / 2
                var wLeft = (screen.availWidth - wWidth) / 2
                var vbvWindow = window.open(AppPath + '/cvv.htm', 'cvv', 'width=' + wWidth + ',height=' + wHeight + ',top=' + wTop + ',left=' + wLeft);
            }

            function vbv() {
                var wWidth = 330;
                var wHeight = 300;
                var wTop = (screen.availHeight - wHeight) / 2
                var wLeft = (screen.availWidth - wWidth) / 2
                var vbvWindow = window.open(AppPath + 'vbv.aspx', 'vbv', 'width=' + wWidth + ',height=' + wHeight + ',top=' + wTop + ',left=' + wLeft);
            }