﻿/*
* Load a page in the floating iframe
*/
function floatingDiv_openURL(pageURL) {
    var floatingDivObj = document.getElementById("floatingDiv");
    var divTransparentObj = document.getElementById("transpDiv");
    if (null == floatingDivObj) {
        alert("Erreur d'affichage de la page demandée, veuillez recharger le site et réessayer l'opération.");
        return;
    }
    divTransparentObj.style.display = "block";
    floatingDivObj.style.display = "block";
    var loadingHTML = "<center><br style='line-height:15px;'/><table><tr><td><img src='/images/loading.gif' style='display:block;'></td><td><span style='padding-left:4px'>chargement...</span></td></tr></table></center>";
    floatingDivObj.contentWindow.document.body.innerHTML = loadingHTML;
    floatingDivObj.src = baseUrl + pageURL; // for iframes
    //AjaX_getHTML(baseUrl+pageURL, floatingDivObj, true);
}

/*
* Close the floating iframe
*/
function floatingDiv_close() {
    var floatingDivObj = document.getElementById("floatingDiv");
    var divTransparentObj = document.getElementById("transpDiv");
    if (null == floatingDivObj) {
        alert("Erreur d'affichage de la page demandée, veuillez recharger le site et réessayer l'opération.");
        return;
    }
    floatingDivObj.style.display = "none";
    divTransparentObj.style.display = "none";
}

/* Update the floating div position (used mainly for the product description) */
function floatingDiv_resize() {
    var floatingDivObj = document.getElementById("floatingDiv");
    if (null != floatingDivObj) {
        var floatingDivWidth = 980;
        var floatingDiv_leftOffset = (document.documentElement.clientWidth - floatingDivWidth) / 2;
        var floatingDivHeight = document.documentElement.clientHeight - 100;
        floatingDivObj.style.height = floatingDivHeight + "px";
        floatingDivObj.style.width = floatingDivWidth + "px";
        floatingDivObj.style.left = floatingDiv_leftOffset + "px";
        floatingDivObj.style.top = "50px";
    }
    var divTransparentObj = document.getElementById("transpDiv");
    divTransparentObj.style.height = document.documentElement.scrollHeight + "px"; // instead of clientHeight to cover the whole height
    divTransparentObj.style.width = document.documentElement.scrollWidth + "px";
}

/* Add a product in the basket and retrieve quantity in the input box */
function orderBasket_addProduct(productId) {
    var prodNbItems = document.getElementById("nbItems" + productId);
    if (null != prodNbItems)
        orderBasket_addProductQuantity(productId, prodNbItems.value);
    else
        alert("Erreur d'ajout du produit, veuillez réessayer.");
}

/* Add a product quantity in the basket */
function orderBasket_addProductQuantity(productId, productQuant) {
    var basketObjDiv = document.getElementById("orderBasketDiv");
    if (null != basketObjDiv) {
        basketObjDiv.style.display = "block";
        AjaX_getHTML(baseUrl + "/ShoppingCartAdd.aspx?Quantity=" + productQuant + "&ProductID=" + productId, basketObjDiv, true);
        if ("undefined" != typeof (resizeMasterPage))
            resizeMasterPage();
    }
    else {
        var dummyObjDiv = document.getElementById("dummyDiv");

        AjaX_getHTML(baseUrl + "/ShoppingCartAdd.aspx?Quantity=" + productQuant + "&ProductID=" + productId + "&Refresh=1", dummyObjDiv, false);
        onRefreshOrder();
    }
}

/*
* Remove a product from the basket
*/
function orderBasket_removeProduct(productId) {
    var basketObjDiv = document.getElementById("orderBasketDiv");
    if (null != basketObjDiv) {
        AjaX_getHTML(baseUrl + "/ShoppingCartDel.aspx?ProductID=" + productId, basketObjDiv, true);
        if ("undefined" != typeof (resizeMasterPage))
            resizeMasterPage();
    }
}

/*
* Clear the basket
*/
function orderBasket_clear() {
    if (false == confirm('Confirmez-vous la suppression de tous les articles du panier ?'))
        return;
    var basketObjDiv = document.getElementById("orderBasketDiv");
    if (null != basketObjDiv) {
        AjaX_getHTML(baseUrl + "/ShoppingCartClear.aspx", basketObjDiv, true);
        if ("undefined" != typeof (resizeMasterPage))
            resizeMasterPage();
    }
}

/*
* Refresh the basket
*/
function orderBasket_refresh() {
    var basketObjDiv = document.getElementById("orderBasketDiv");
    var tocardId = new UUID();
    if (null != basketObjDiv) {
        AjaX_getHTML(baseUrl + "/ShoppingCart.aspx?ie_tocard=" + tocardId, basketObjDiv, false);
    }
}

function getHeaderHeight() {
    return 78 /*logo*/ + 39 /*tabs*/ + 30 /*filters*/ + 2 /*extra*/;
}

function getBottomHeight() {
    return 24 /*links*/ + 12 /*copyright*/ + 2 /*extra*/;
}

/*
* Load the submenu through AJAX
* nb: only wine category as the new submenu design support
*/
function subMenu_refresh(univers) {
    var htmlheight = document.documentElement.clientHeight;
    var dataContentHeight = htmlheight - getHeaderHeight() - getBottomHeight();

    var leftMenuObj = document.getElementById("subMenuPart");
    var subMenuObjDiv = document.getElementById("subMenu");

    if (null != subMenuObjDiv) {
        AjaX_getHTML(baseUrl + "/ProduitsSousMenu.aspx?Univers=" + univers + "&Ref=-1", subMenuObjDiv, false);

        leftMenuObj.style.height = dataContentHeight + "px";
        leftMenuObj.style.display = "block";
        leftMenuObj.className = "left_menu";
    }
}

function switchLayout(selLayout) {
    if (+selLayout == 0) {
        document.getElementById("pageItemsDisplayLayoutContainer").style.display = "none"; // products
        //document.getElementById("categoryTitleAndFiltering").style.display = "none";
        document.getElementById("pageDefaultDisplayLayoutContainer").style.display = "block"; // homepage
    } else
        if (+selLayout == 1) {
        document.getElementById("pageDefaultDisplayLayoutContainer").style.display = "none"; // homepage
        document.getElementById("pageItemsDisplayLayoutContainer").style.display = "block"; // products
        //document.getElementById("categoryTitleAndFiltering").style.display = "block";
    }
}
function switchTabCSS(catName) {
    for (var i = 0; i < document.styleSheets.length; ++i)
        if ("tab" == document.styleSheets[i].title)
        if (-1 != document.styleSheets[i].href.indexOf(catName))
        document.styleSheets[i].disabled = "";
    else
        document.styleSheets[i].disabled = "true";
}

function imprimer(urlstring) {
    window.open(urlstring, "Imprimer", "width=800,height=600,resizable=yes,scrollbars=yes")
}

function MsgBoxProduitAjoute() {
    alert('Le produit a été rajouté à votre panier!');
}
function MsgBoxRechercheErreur() {
    alert('Vous devez spécifier un mot à rechercher!');
}
function MsgBoxFicheProduitNotAuthenticate() {
    alert('Désolé ces informations sont réservées à nos clients pour bénéficier de ces aides à la revente vous devez être inscrit!');
}

/*
* Show/hide sub nodes
* Return true if the content was displayed, false if hidden
*/
function nodeShowSubContent(obj) {
    var res = null;

    // search for the <span> node in the obj sibling
    var imgName;

    var parentDiv = obj.parentNode;
    for (var i = 0; i < parentDiv.childNodes.length; ++i) {
        if (parentDiv.childNodes[i].tagName == "DIV") {
            var divNode = parentDiv.childNodes[i];
            var parentNodeChilds = parentDiv.childNodes;

            var divNode_loaded = divNode.getAttribute("isHidden");

            var displayUnderlyingDiv = "none";

            if (null == divNode_loaded || "true" == divNode_loaded || true == divNode_loaded) {
                displayUnderlyingDiv = "block";
                //imgName = "/images/PuceMoins.gif"; //if needed one day
                divNode.setAttribute("isHidden", false);
                res = true;
            } else {
                //imgName = "/images/PucePlus.gif";
                divNode.setAttribute("isHidden", true);
                res = false;
            }

            // search for the <div> node in the obj sibling
            for (var j = parentDiv.childNodes.length - 1; j > i; --j) {
                if (parentDiv.childNodes[j].tagName == "DIV") {
                    parentNodeChilds[j].style.display = displayUnderlyingDiv;
                    break;
                }
            }
            //imgNode.src = imgName;
            break;
        }
    }
    return res;
}

/*
* Apply a product filter when clicking on an item of the new submenu
*/
function useSortFilterTag(univers, filter, value) {
//    document.location = baseUrl + "ProduitRecherche.aspx?tocard=1&Univers=" + univers + "&" + filter + "=" + value + "";
    document.location = baseUrl + "ProduitRecherche.aspx?" + getFilters(filter, value);
}

/*
* Highlight a column in a table
* col_no int [0..n]: column number
* do_show bool: highlight or not
*/
function columnHighlight(col_no, do_show) {

    return; // TODO: column highlight support not requested in product list display at the moment

    // works only for simple table
    var stl;
    if (do_show) stl = 'Yellow'
    else stl = 'transparent';

    var tbl = document.getElementById('productsLineDisplay');
    if (null == productsLineDisplay)
        return;
    var rows = tbl.getElementsByTagName('tr');

    for (var row = 0; row < rows.length; row++) {
        var cels = rows[row].getElementsByTagName('td')
        cels[col_no].style.backgroundColor = stl;
    }
}

