/* 
* Jonas Raoni Soares Silva
* http://jsfromhell.com/number/fmt-money [rev. #2]
* usage: 
* Number.formatMoney([floatPoint: Integer = 2], [decimalSep: String = ","], [thousandsSep: String = "."]): String 
* Returns the number into the monetary format. 
* floatPoint amount of decimal places 
* decimalSep string that will be used as decimal separator 
* thousandsSep string that will be used as thousands separator 
* example: number.formatMoney(2, "*", "#") = 123#456*79
*/
Number.prototype.formatMoney = function (c, d, t) {
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function AddBundleToCart(rbLeftContainerId, rbRightContainerId) {
    try {
        var rbLeftContainer = $("#" + rbLeftContainerId);
        var rbRightContainer = $("#" + rbRightContainerId);
        if (rbLeftContainer != null && rbRightContainer != null) {
            var nUri = baseurl;
            rbLeftContainer.find("input[type='radio']").each(function (i) {
                if ($(this).attr("checked") == true) {
                    var productProperties = GetProductProperties($(this).val());
                    var currentProductId = productProperties.productId;
                    var currentVariantId = productProperties.variantId;

                    if (currentProductId != null && currentProductId != "undefined" && currentProductId != "" &&
    				   currentVariantId != null && currentVariantId != "undefined" && currentVariantId != "") {
                        nUri += "CheckoutPages/uplink.aspx?ItemString=/ItemStart///" + currentProductId + "/" + currentVariantId + "/1/ItemEnd/";
                    }
                }
            });

            rbRightContainer.find("input[type='radio']").each(function (i) {
                if ($(this).attr("checked") == true) {
                    var productProperties = GetProductProperties($(this).val());
                    var currentProductId = productProperties.productId;
                    var currentVariantId = productProperties.variantId;

                    if (currentProductId != null && currentProductId != "undefined" && currentProductId != "" &&
    				   currentVariantId != null && currentVariantId != "undefined" && currentVariantId != "") {
                        nUri += "ItemStart///" + currentProductId + "/" + currentVariantId + "/1/ItemEnd/";
                    }
                }
            });

            window.location.href = nUri;
        }
    }
    catch (e) { }
}


function AddToCart(rbcontainerId) {
    try {
        var rbcontainer = $("#" + rbcontainerId);
        if (rbcontainer != null) {
            var nUri = baseurl;
            rbcontainer.find("input[type='radio']").each(function (i) {
                if ($(this).attr("checked") == true) {
                    var productProperties = GetProductProperties($(this).val());
                    var currentProductId = productProperties.productId;
                    var currentVariantId = productProperties.variantId;

                    if (currentProductId != null && currentProductId != "undefined" && currentProductId != "" &&
    				   currentVariantId != null && currentVariantId != "undefined" && currentVariantId != "") {
                        nUri += "CheckoutPages/uplink.aspx?ItemString=/ItemStart///" + currentProductId + "/" + currentVariantId + "/1/ItemEnd/";
                    }
                }
            });

            window.location.href = nUri;
        }
    }
    catch (e) { }
}

/**
* Extract the ProductId and the VariantId from a given MediaSelector-RadioButton-Id (e.g. "ProductId.UK-79G-00318.CatalogName.STRCatalog:VariantId.79G-00318.CatalogName.STRCatalog")
*
* Why is this necessary?
* The product- and variant-id properties cannot be written, as they are, into the id-attribute of radiobuttons because they may contain forbidden characters such as round brackets "()"
*
* @name GetProductProperties(productIdString)
* @param productIdString A string that contains the product id, variant id and the respective catalog name (e.g. "ProductId.UK-79G-00318.CatalogName.STRCatalog:VariantId.79G-00318.CatalogName.STRCatalog")
* @return object An object that contains a productId and a variantId property (e.g. {  productId: "UK-79G-00318(STRCatalog)", variantId: "79G-00318(STRCatalog)" }); if something went wrong the productId and variantId will be empty strings
*/
function GetProductProperties(productIdString) {
    var properties = { "productId": "", "variantId": "", "catalogName": "" };

    if (productIdString != null && productIdString != "" && productIdString.indexOf(":") > 0) {
        try {
            var parts = productIdString.split(":");

            if (parts.length > 1) {
                /* extract productId and catalog name */
                var reProduct = /ProductId\.([^\.\:]+)\.CatalogName\.([^\.\:]+)/gi;
                var productId = parts[0].replace(reProduct, "$1($2)");
                properties.productId = productId;

                /* extract the catalog name */
                var catalogName = parts[0].replace(reProduct, "$2");
                properties.catalogName = catalogName;

                /* extract variantId and catalog name */
                var reVariant = /VariantId\.([^\.\:]+)\.CatalogName\.([^\.\:]+)/gi
                var variantId = parts[1].replace(reVariant, "$1($2)");
                properties.variantId = variantId;
            }
        } catch (err) {

        }
    }

    return properties;
}

function showPopup(divId) {
    //set the last popup invisible
    if (popup != null) {
        popup.style.visibility = "hidden";
        popup.style.display = "none";

        if (popup2 != null) {
            popup2.style.visibility = "hidden";
            popup2.style.display = "none";
        }
        if (popup3 != null) {
            popup3.style.visibility = "hidden";
            popup3.style.display = "none";
        }
    }
    popup = document.getElementById(divId);
    if (popup != null) {
        popup.setAttribute("scrolling", "no");
        popup.setAttribute("frameborder", "0");
        popup.style.display = "block";
        popup.style.visibility = "visible";

        /* assign a proper z-index to the popup container */
        FixZIndex($(popup));

        // if exists, add classes 
        var globalnavlbar = $("div.globalnav_lbar");
        var colRight = $("div.columnRight");
        if (null != globalnavlbar && null != colRight) {
            globalnavlbar.addClass("globalNavBarColOnBottom");
            colRight.addClass("rightColOnTop");
        }

        var fullWidthColHelper = $("div.fullWidthColHelper");
        if (null != globalnavlbar && null != fullWidthColHelper) {
            fullWidthColHelper.addClass("rightColOnTop");
        }
    }
}

function setCurrentBundleState(currentRb, currentIs, itemInnerPriceLblId, itemInnerOriginalPriceLblId,
                               sumInnerPriceLblId, sumInnerOriginalPriceLblId,
                               sumPriceLblId, sumOriginalPriceLblId,
                               hfLeftPriceId, hfRightPriceId,
                               hfLeftOriginalPriceId, hfRightOriginalPriceId,
                               hfSelectedDiscountLeftId, hfSelectedDiscountRightId,
                               itemPriceNum, itemOriginalPriceNum, hfieldVariantID, name, discount,
                               decPlaces, decSeparator, thousendSep, currencyPattern) {
    var innerPriceLbl = $("#" + itemInnerPriceLblId);
    if (innerPriceLbl != null) {
        innerPriceLbl.text(currencyPattern.replace(/000/g, (itemPriceNum).formatMoney(decPlaces, decSeparator, thousendSep)));
        if (discount == true) {
            if (innerPriceLbl.hasClass("price_normal")) {
                innerPriceLbl.removeClass("price_normal");
            }
            innerPriceLbl.addClass("price_discount");
        }
        else {
            if (innerPriceLbl.hasClass("price_discount")) {
                innerPriceLbl.removeClass("price_discount");
            }
            innerPriceLbl.addClass("price_normal");
        }
    }

    var innerOriginalPriceLbl = $("#" + itemInnerOriginalPriceLblId);
    if (innerOriginalPriceLbl != null) {
        innerOriginalPriceLbl.text(currencyPattern.replace(/000/g, (itemOriginalPriceNum).formatMoney(decPlaces, decSeparator, thousendSep)));
        if (discount == true) {
            if (innerOriginalPriceLbl.hasClass("orig_price_hidden")) {
                innerOriginalPriceLbl.removeClass("orig_price_hidden");
            }
        }
        else {
            if (!innerOriginalPriceLbl.hasClass("orig_price_hidden")) {
                innerOriginalPriceLbl.addClass("orig_price_hidden");
            }
        }
    }

    var leftPriceHf = $("#" + hfLeftPriceId);
    var rightPriceHf = $("#" + hfRightPriceId);
    var leftOriginalPriceHf = $("#" + hfLeftOriginalPriceId);
    var rightOriginalPriceHf = $("#" + hfRightOriginalPriceId);
    var hfSelectedDiscountLeft = $("#" + hfSelectedDiscountLeftId);
    var hfSelectedDiscountRight = $("#" + hfSelectedDiscountRightId);

    if (currentIs == "left") {
        leftPriceHf.val(itemPriceNum);
        leftOriginalPriceHf.val(itemOriginalPriceNum);
        hfSelectedDiscountLeft.val(discount);
    }
    else if (currentIs == "right") {
        rightPriceHf.val(itemPriceNum);
        rightOriginalPriceHf.val(itemOriginalPriceNum);
        hfSelectedDiscountRight.val(discount);
    }

    var leftPriceNum = leftPriceHf.val() * 1.0;
    var rightPriceNum = rightPriceHf.val() * 1.0;
    var leftOriginalPriceNum = leftOriginalPriceHf.val() * 1.0;
    var rightOriginalPriceNum = rightOriginalPriceHf.val() * 1.0;

    //set Sum Price
    var sumPrice = leftPriceNum + rightPriceNum;
    var sumOriginalPrice = leftOriginalPriceNum + rightOriginalPriceNum;

    var sumInnerPriceLbl = $("#" + sumInnerPriceLblId);
    var sumInnerOriginalPriceLbl = $("#" + sumInnerOriginalPriceLblId);
    var sumPriceLbl = $("#" + sumPriceLblId);
    var sumOriginalPriceLbl = $("#" + sumOriginalPriceLblId);

    sumInnerPriceLbl.text(currencyPattern.replace(/000/g, (sumPrice).formatMoney(decPlaces, decSeparator, thousendSep)));
    sumPriceLbl.text(currencyPattern.replace(/000/g, (sumPrice).formatMoney(decPlaces, decSeparator, thousendSep)));

    sumInnerOriginalPriceLbl.text(currencyPattern.replace(/000/g, (sumOriginalPrice).formatMoney(decPlaces, decSeparator, thousendSep)));
    sumOriginalPriceLbl.text(currencyPattern.replace(/000/g, (sumOriginalPrice).formatMoney(decPlaces, decSeparator, thousendSep)));

    if (hfSelectedDiscountLeft.val() == "true" || hfSelectedDiscountRight.val() == "true") {
        if (sumInnerPriceLbl.hasClass("price_normal")) {
            sumInnerPriceLbl.removeClass("price_normal");
        }
        sumInnerPriceLbl.addClass("price_discount");

        if (sumPriceLbl.hasClass("price_normal")) {
            sumPriceLbl.removeClass("price_normal");
        }
        sumPriceLbl.addClass("price_discount");

        sumInnerOriginalPriceLbl.removeClass("orig_price_hidden");
        sumOriginalPriceLbl.removeClass("orig_price_hidden");
    }
    else {
        if (sumInnerPriceLbl.hasClass("price_discount")) {
            sumInnerPriceLbl.removeClass("price_discount");
        }
        sumInnerPriceLbl.addClass("price_normal");

        if (sumPriceLbl.hasClass("price_discount")) {
            sumPriceLbl.removeClass("price_discount");
        }
        sumPriceLbl.addClass("price_normal");

        sumInnerOriginalPriceLbl.addClass("orig_price_hidden");
        sumOriginalPriceLbl.addClass("orig_price_hidden");
    }

    //SetVariantID to Hidden Field
    var hf = $("#" + hfieldVariantID);
    hf.val(currentRb.value);
}


function setCurrentState(currentRb, innerpriceLblId, inneroriginalpriceLblId, outerpriceLblId, outeroriginalpriceLblId,
                         price, originalprice, hfield, name, discount, discountMessageLblId) {
    //price_discount" : "price_normal
    //Set PriceLabels
    var priceLblInner = $("#" + innerpriceLblId);
    if (priceLblInner != null) {
        priceLblInner.text(price);
        if (discount == true) {
            if (priceLblInner.hasClass("price_normal")) {
                priceLblInner.removeClass("price_normal");
            }
            priceLblInner.addClass("price_discount");
        }
        else {
            if (priceLblInner.hasClass("price_discount")) {
                priceLblInner.removeClass("price_discount");
            }
            priceLblInner.addClass("price_normal");
        }
    }

    var origpriceLblInner = $("#" + inneroriginalpriceLblId);
    if (origpriceLblInner != null) {
        origpriceLblInner.text(originalprice);
        if (discount == true) {
            if (origpriceLblInner.hasClass("orig_price_hidden")) {
                origpriceLblInner.removeClass("orig_price_hidden");
            }
        }
        else {
            if (!origpriceLblInner.hasClass("orig_price_hidden")) {
                origpriceLblInner.addClass("orig_price_hidden");
            }
        }
    };

    var priceLblOuter = $("#" + outerpriceLblId);
    if (priceLblOuter != null) {
        priceLblOuter.text(price);
        if (discount == true) {
            if (priceLblOuter.hasClass("price_normal")) {
                priceLblOuter.removeClass("price_normal");
            }
            priceLblOuter.addClass("price_discount");
        }
        else {
            if (priceLblOuter.hasClass("price_discount")) {
                priceLblOuter.removeClass("price_discount");
            }
            priceLblOuter.addClass("price_normal");
        }
    }

    var origpriceLblOuter = $("#" + outeroriginalpriceLblId);
    if (origpriceLblOuter != null) {
        origpriceLblOuter.text(originalprice);
        if (discount == true) {
            if (origpriceLblOuter.hasClass("orig_price_hidden")) {
                origpriceLblOuter.removeClass("orig_price_hidden");
            }
        }
        else {
            if (!origpriceLblOuter.hasClass("orig_price_hidden")) {
                origpriceLblOuter.addClass("orig_price_hidden");
            }
        }
    }

    var discountMessageLbl = $("#" + discountMessageLblId);
    if (discountMessageLbl != null) {
        if (discount == true) {
            if (discountMessageLbl.hasClass("orig_price_hidden")) {
                discountMessageLbl.removeClass("orig_price_hidden");
            }
        }
        else {
            if (!discountMessageLbl.hasClass("orig_price_hidden")) {
                discountMessageLbl.addClass("orig_price_hidden");
            }
        }
    }

    //SetVariantID to Hidden Field
    var hf = $("#" + hfield);
    if (hf != null && currentRb != null) {
        hf.val(currentRb.value);
    }
}


function hidePopup(divId, divId2, divId3) {
    if (popup != null) {
        popup.style.display = "none";
        popup.style.visibility = "hidden";
        popup = null;

        if (popup2 != null) {
            popup2.style.display = "none";
            popup2.style.visibility = "hidden";
            popup2 = null;
        }
        if (popup3 != null) {
            popup3.style.display = "none";
            popup3.style.visibility = "hidden";
            popup3 = null;
        }

        hideCurrentMediaSelector();
    }
}
