 },
                TJS: {
                    money_format: "&#84;&#74;&#83; {{amount}}",
                    money_with_currency_format: "&#84;&#74;&#83; {{amount}} TJS"
                },
                TOP: {
                    money_format: "&#84;&#36; {{amount}}",
                    money_with_currency_format: "&#84;&#36; {{amount}} TOP"
                },
                TMT: {
                    money_format: "&#109; {{amount}}",
                    money_with_currency_format: "&#109; {{amount}} TMT"
                },
                TVD: {
                    money_format: "{{amount}}",
                    money_with_currency_format: "{{amount}} TVD"
                },
                UZS: {
                    money_format: "&#1083;&#1074; {{amount}}",
                    money_with_currency_format: "&#1083;&#1074; {{amount}} UZS"
                },
                YER: {
                    money_format: "&#65020; {{amount}}",
                    money_with_currency_format: "&#65020; {{amount}} YER"
                },
                ZWD: {
                    money_format: "{{amount}}",
                    money_with_currency_format: "{{amount}} ZWD"
                }
            };
            DoublyCurrency.formatMoney = function(cents, format, isShopCurrency, hasSup) {
                function defaultOption(opt, def) {
                    if (typeof opt == "undefined") {
                        return def;
                    } else {
                        return opt;
                    }
                }
                function formatWithDelimiters(number, precision, thousands, decimal) {
                    precision = defaultOption(precision, 2);
                    thousands = defaultOption(thousands, ",");
                    decimal = defaultOption(decimal, ".");
                    if (isNaN(number) || number == null) {
                        return 0;
                    }
                    number = (number / 100).toFixed(precision);
                    var parts = number.split(".")
                    , dollars = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + thousands)
                    , cents = parts[1] ? (decimal + parts[1]) : "";
                    if (hasSup) {
                        return dollars + "<sup>" + cents + "</sup>";
                    } else {
                        return dollars + cents;
                    }
                }
                if (typeof cents == "string") {
                    cents = cents.replace(".", "");
                }
                var value = ""
                , placeholderRegex = /\{\{\s*(\w+)\s*\}\}/
                , formatString = format || "${{amount}}"
                , decimals = 2;
                if ((!isShopCurrency && (DoublyCurrency.currentCurrency == "KWD" || DoublyCurrency.currentCurrency == "BHD" || DoublyCurrency.currentCurrency == "JOD" || DoublyCurrency.currentCurrency == "OMR")) || (isShopCurrency && (shopCurrency == "KWD" || shopCurrency == "BHD" || shopCurrency == "JOD" || shopCurrency == "OMR"))) {
                    decimals = 3;
                }
                if (DoublyGlobal.removeDecimals) {
                    decimals = 0;
                }
                if ((!isShopCurrency && DoublyCurrency.currentCurrency == "BTC") || (isShopCurrency && shopCurrency == "BTC")) {
                    decimals = 8;
                }
                switch (formatString.match(placeholderRegex)[1]) {
                case "amount":
                case "amount_no_decimals":
                    value = formatWithDelimiters(cents, decimals);
                    if (DoublyGlobal.roundDecimals && !isShopCurrency && this.currentCurrency != shopCurrency && this.currentCurrency != "BTC" && value != 0 && value.toString().indexOf(".") !== -1) {
                        value = value.substr(0, (value.toString().indexOf(".") + 1)) + DoublyGlobal.roundTo;
                        if (DoublyCurrency.currentCurrency == "KWD" || DoublyCurrency.currentCurrency == "BHD" || DoublyCurrency.currentCurrency == "JOD" || DoublyCurrency.currentCurrency == "OMR") {
                            value += "0";
                        }
                        if (hasSup) {
                            value += "</sup>";
                        }
                    }
                    break;
                case "amount_with_comma_separator":
                case "amount_no_decimals_with_comma_separator":
                    value = formatWithDelimiters(cents, decimals, ".", ",");
                    if (DoublyGlobal.roundDecimals && !isShopCurrency && this.currentCurrency != shopCurrency && this.currentCurrency != "BTC" && value.toString().replace(",", ".") != 0 && value.toString().indexOf(",") !== -1) {
                        value = value.substr(0, (value.toString().indexOf(",") + 1)) + DoublyGlobal.roundTo;
                        if (DoublyCurrency.currentCurrency == "KWD" || DoublyCurrency.currentCurrency == "BHD" || DoublyCurrency.currentCurrency == "JOD" || DoublyCurrency.currentCurrency == "OMR") {
                            value += "0";
                        }
                        if (hasSup) {
                            value += "</sup>";
                        }
                    }
                }
                if (!isNaN(cents)) {
                    return formatString.replace(placeholderRegex, value)
                } else {
                    return ""
                }
            };
        
            DoublyCurrency.getPriceOnly = function(priceHtml, oldFormat) {
                var regex = /\{\{\s*(\w+)\s*\}\}/;
                var array = oldFormat.replace(regex, " ").replace(/ +/g, " ").split(" ");
                /* remove $ or USD from price, without removing any html tags or dots (.) or commas (,) */
                jQueryGrizzly(array).each(function(index, value) {
                    priceHtml = priceHtml.replace(jQueryGrizzly("<div/>").html(value).text(), "");
                });
                /* get decimal point or comma */
                decimal = ".";
                if (oldFormat.indexOf("comma") !== -1) {
                    decimal = ",";
                }
                /* check for and remove <sup></sup> html tags */
                if (priceHtml.indexOf(decimal) === -1 && priceHtml.indexOf("<sup>") !== -1) {
                    priceHtml = priceHtml.replace("<sup>", decimal).replace("</sup>", "");
                }
                return priceHtml;
            };

            DoublyCurrency.convertAll = function(newCurrency, selector) {
                try{
                var isShopCurrency = false;
                if (newCurrency == undefined) {
                    if (DoublyGlobal.theme=="layered_theme") {
                        newCurrency = jQueryGrizzly('.currency-switcher-btn.selected').attr('doubly-currency');
                    } else {
                        newCurrency = jQueryGrizzly('[name=doubly-currencies]').val();
                    }
                    /* convertAll is called by API calls earlier, before the app is loaded */
                    if (newCurrency == undefined) {
                        return;
                    }
                }
                if (selector == undefined) {
                    this.currentCurrency = newCurrency;
                    this.cookie.write(newCurrency);
                }
                if (selector !== undefined && selector == ".price-on-hover") {
                    var dataCurrencyShop = jQueryGrizzly(selector).closest("." + spanClass).attr("doubly-currency-" + shopCurrency);
                    jQueryGrizzly(selector).attr("doubly-currency-" + shopCurrency, dataCurrencyShop);
                    isShopCurrency = true;
                }

                var convertFn = function() {
                    /* If the amount has already been converted, we leave it alone. */
                    if (jQueryGrizzly(this).attr("doubly-currency") === newCurrency)
                        return;
                    /* If we are converting to a currency that we have saved, we will use the saved amount. */
                    if (jQueryGrizzly(this).attr("doubly-currency-" + newCurrency)) {
                        var newFormat = DoublyCurrency.moneyFormats[newCurrency][DoublyCurrency.format] || "{{amount}}";
                        if (newCurrency !== "BTC") {
                            cents = parseInt(jQueryGrizzly(this).attr("doubly-currency-" + newCurrency), 10);
                        } else {
                            cents = Number(jQueryGrizzly(this).attr("doubly-currency-" + newCurrency));
                        }
                        var hasSup = false;
                        if (jQueryGrizzly(this).html().indexOf("<sup>") !== -1) {
                            hasSup = true;
                        }
                        var newFormattedAmount = DoublyCurrency.formatMoney(cents, newFormat, isShopCurrency, hasSup);
                        jQueryGrizzly(this).html(newFormattedAmount);
                    } else {
                        /* convert to Y for the first time */
                        var cents = 0
                        , shopPrice = 0
                        , oldFormat = DoublyCurrency.moneyFormats[shopCurrency][DoublyCurrency.format] || "{{amount}}"
                        , newFormat = DoublyCurrency.moneyFormats[newCurrency][DoublyCurrency.format] || "{{amount}}";
                        if (jQueryGrizzly(this).attr("doubly-currency-" + shopCurrency)) {
                            shopPrice = jQueryGrizzly(this).attr("doubly-currency-" + shopCurrency);
                        } else {
                            var priceHtml = jQueryGrizzly(this).html();
                            priceHtml = DoublyCurrency.getPriceOnly(priceHtml, oldFormat);
                            if (priceHtml.indexOf(decimal) == -1) {
                                shopPrice = parseInt(jQueryGrizzly(this).html().replace(/[^0-9]/g, ""), 10) * 100;
                            } else if (shopCurrency === "KWD" || shopCurrency === "JOD" || shopCurrency == "BHD" || shopCurrency == "OMR") {
                                shopPrice = parseInt(jQueryGrizzly(this).html().replace(/[^0-9]/g, ""), 10) / 10;
                            } else {
                                shopPrice = parseInt(jQueryGrizzly(this).html().replace(/[^0-9]/g, ""), 10);
                            }
                            jQueryGrizzly(this).attr("doubly-currency-" + shopCurrency, shopPrice);
                        }
                        if (shopCurrency != newCurrency) {
                            cents = DoublyCurrency.convert(shopPrice, shopCurrency, newCurrency);
                        } else {
                            cents = shopPrice;
                        }
                        jQueryGrizzly(this).attr("doubly-currency-" + newCurrency, cents);
                        var hasSup = false;
                        if (jQueryGrizzly(this).html().indexOf("<sup>") !== -1) {
                            hasSup = true;
                        }
                        var newFormattedAmount = DoublyCurrency.formatMoney(cents, newFormat, isShopCurrency, hasSup);
                        jQueryGrizzly(this).html(newFormattedAmount);
                    }
                    /* We record the new currency locally. */
                    jQueryGrizzly(this).attr("doubly-currency", newCurrency);
                };

                jQueryGrizzly(selector || "span." + spanClass).each(function(){convertFn.call(this);});

                if (typeof DoublyGlobal.convertScriptAfter !== "undefined") {
                    DoublyGlobal.convertScriptAfter(convertFn);
                }
                

                }catch(e){dbErrorLog(e);}
            };

            function initCurrencySwitcher() {
                try {
                /* global reference */
                DoublyGlobalCurrency = DoublyCurrency;
                eval(DoublyGlobal.customerScriptBefore);
                eval(DoublyGlobal.themeScript);
                if (typeof DoublyGlobal.initNiceSelect == "function") DoublyGlobal.initNiceSelect();
                if (typeof DoublyGlobal.addSelect == "function") DoublyGlobal.addSelect();
                
                /* Cookie currency */
                var cookieCurrency = DoublyCurrency.cookie.read();

                /* Set select value before document ready functions fire to avoid lag */
                if (DoublyGlobal.theme == "layered_theme") {
                    if (typeof DoublyGlobal.initLayeredDesign == "function") DoublyGlobal.initLayeredDesign();
                    jQueryGrizzly('.currency-switcher-btn').click(function() {
                        var newCurrency = jQueryGrizzly(this).attr('doubly-currency');
                        DoublyCurrency.convertAll(newCurrency);
                        initExtraFeatures();
                    });
                    if (cookieCurrency == null || cookieCurrency == 'undefined') {
                        jQueryGrizzly('.currency-switcher-btn[doubly-currency=' + defaultCurrency + ']').click();
                    } else {
                        jQueryGrizzly('.currency-switcher-btn[doubly-currency=' + cookieCurrency + ']').click();
                    }
                } else {
                    /* Set select value before document ready functions fire to avoid lag */
                    if (cookieCurrency == null || cookieCurrency == 'undefined') {
                        jQueryGrizzly('[name=doubly-currencies]').val(defaultCurrency);
                    } else {
                        jQueryGrizzly('[name=doubly-currencies]').val(cookieCurrency);
                    }
                    if (DoublyGlobal.theme == "flags_theme") {
                        jQueryGrizzly('.doubly-wrapper .currency-switcher, .doubly-float .currency-switcher').niceSelect();
                    }
                    jQueryGrizzly('.doubly-wrapper').attr('data-nosnippet', '');
                    jQueryGrizzly('[name=doubly-currencies]').change(function() {
                        /* in case more than 1 currency switcher, update value of all of them */
                        jQueryGrizzly('[name=doubly-currencies]').val(jQueryGrizzly(this).val());
                        DoublyCurrency.convertAll();
                        initExtraFeatures();
                    });
                }

                /* Fix for customer account pages */
                jQueryGrizzly('span.' + spanClass + ' span.' + spanClass).each(function() {
                    jQueryGrizzly(this).parents('span.' + spanClass).removeClass(spanClass);
                });
                /* If there's no cookie. */
                if (cookieCurrency == null || cookieCurrency == 'undefined') {
                    DoublyCurrency.convertAll(defaultCurrency);
                }/* If the cookie value does not correspond to any value in the currency dropdown. */
                else if (jQueryGrizzly.inArray(cookieCurrency, allowedCurrencies) === -1) {
                    DoublyCurrency.currentCurrency = shopCurrency;
                    DoublyCurrency.cookie.write(shopCurrency);
                } else {
                    DoublyCurrency.convertAll(cookieCurrency);
                }
                
                jQueryGrizzly('.single-option-selector').bind('change.single-option-selector', function() {
                    DoublyCurrency.convertAll();
                    initExtraFeatures();
                });
                if (jQueryGrizzly('.single-option-radio label').length > 0) {
                    jQueryGrizzly('.single-option-radio label').bind('click.single-option-radio', function() {
                        setTimeout(function() {
                            DoublyCurrency.convertAll();
                            initExtraFeatures();
                        }, 1);
                    });
                    jQueryGrizzly('.single-option-radio label').bind('touchend.single-option-radio', function() {
                        setTimeout(function() {
                            DoublyCurrency.convertAll();
                            initExtraFeatures();
                        }, 1);
                    });
                }
                /* Panda Swatchify app */
                if (jQueryGrizzly('.swatch-panda .color label').length > 0) {
                    jQueryGrizzly('.swatch-panda .color label').click(function() {
                        setTimeout(function() {
                            DoublyCurrency.convertAll();
                        }, 1);
                        initExtraFeatures();
                    });
                }
                /* sca-qv-button */
                setTimeout(function() {
                    jQueryGrizzly('.sca-qv-button').bind('click.changeCurrency', function() {
                        setTimeout(function() {
                            DoublyCurrency.convertAll();
                            jQueryGrizzly('.single-option-selector').bind('change.single-option-selector', function() {
                                DoublyCurrency.convertAll();
                                initExtraFeatures();
                            });
                        }, 1000);
                    });
                }, 1000);
                if (jQueryGrizzly('.swatch-element').length > 0) {
                    jQueryGrizzly('.swatch-element').bind('click.swatch-element', function() {
                        setTimeout(function() {
                            DoublyCurrency.convertAll();
                        }, 1);
                    });
                }
                /* beeketing xhr */
                if (catchXHR && DoublyGlobal.beeketing) {
                    function addXMLRequestCallback(callback) {
                        var oldSend, i;
                        if (XMLHttpRequest.callbacks) {
                            XMLHttpRequest.callbacks.push(callback);
                        } else {
                            XMLHttpRequest.callbacks = [callback];
                            oldSend = XMLHttpRequest.prototype.send;
                            XMLHttpRequest.prototype.send = function() {
                                if (XMLHttpRequest.callbacks !== undefined) {
                                    for (i = 0; i < XMLHttpRequest.callbacks.length; i++) {
                                        XMLHttpRequest.callbacks[i](this);
                                    }
                                }
                                oldSend.apply(this, arguments);
                            }
                        }
                    }
                    addXMLRequestCallback(function(xhr) {
                        if (xhr._url && (xhr._url.indexOf('recommendation/result/view-product/') !== -1 || xhr._url.indexOf('recommendation/result/recent-view/') !== -1)) {
                            setTimeout(function() {
                                DoublyCurrency.convertAll();
                                initExtraFeatures();
                            }, 1000);
                        }
                    });
                }
                jQueryGrizzly(document).bind('ajaxComplete.doubly', function() {
                    // sync all wrappers - could be done in other parts as well
                    jQueryGrizzly('.doubly-wrapper select').val(DoublyCurrency.cookie.read());
                    DoublyCurrency.convertAll();
                    initExtraFeatures();
                });
                initExtraFeatures();
                if (typeof afterCurrencySwitcherInit == 'function') {
                    afterCurrencySwitcherInit();
                }
                eval(DoublyGlobal.customerScriptAfter);
            }catch(e){dbErrorLog(e);}
            }
            function initExtraFeatures() {
                try{
                /* initPriceHover */
                jQueryGrizzly('span.' + spanClass).unbind('mouseenter mouseleave');
                if (DoublyGlobal.showPriceOnHover && DoublyCurrency.currentCurrency !== shopCurrency) {
                    jQueryGrizzly('span.' + spanClass).hover(function() {
                        jQueryGrizzly(this).append('<span class="price-on-hover-wrapper"><span class="price-on-hover">' + jQueryGrizzly(this).text() + '</span></span>');
                        DoublyCurrency.convertAll(shopCurrency, '.price-on-hover');
                    }, function() {
                        jQueryGrizzly('span').remove('.price-on-hover-wrapper');
                    });
                    jQueryGrizzly(document).off('touchend.removePriceOnHover');
                    jQueryGrizzly(document).on('touchend.removePriceOnHover', function() {
                        jQueryGrizzly('span').remove('.price-on-hover-wrapper');
                    });
                    jQueryGrizzly('span.' + spanClass).off('touchend.addPriceOnHover');
                    jQueryGrizzly('span.' + spanClass).on('touchend.addPriceOnHover', function() {
                        var element = jQueryGrizzly(this);
                        setTimeout(function() {
                            element.append('<span class="price-on-hover-wrapper"><span class="price-on-hover">' + element.text() + '</span></span>');
                            DoublyCurrency.convertAll(shopCurrency, '.price-on-hover');
                        }, 1);
                    });
                }
                /* initCartMessage */
                if (DoublyGlobal.showCurrencyMessage) {
                    if (jQueryGrizzly('.doubly-message').html() == '') {
                        jQueryGrizzly('.doubly-message').html(DoublyGlobal.currencyMessage);
                    }
                    jQueryGrizzly('.selected-currency').text(DoublyCurrency.currentCurrency);
                }
                if (DoublyCurrency.currentCurrency == shopCurrency) {
                    jQueryGrizzly('.doubly-message').hide();
                } else {
                    jQueryGrizzly('.doubly-message').show();
                }
                }catch(e){dbErrorLog(e);}
            }

            function getParameterByName(name, url) {
                if (!url)
                    url = window.location.href;
                name = name.replace(/[\[\]]/g, "\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)")
                , results = regex.exec(url);
                if (!results)
                    return null;
                if (!results[2])
                    return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }

            function loadApp(){
                try{
                /* if user came from google shopping ad, disable converter */
                var utmSource = getParameterByName('utm_source');
                var dfwTracker = getParameterByName('dfw_tracker');
                if ((utmSource == null || (utmSource != 'googleshopping' && utmSource != 'google')) && dfwTracker == null) {
                    /* Country code */
                    if (DoublyGlobal.autoSwitch) {
                        var tempCookieCurrency = DoublyCurrency.cookie.read();
                        var currencyByCountry = JSON.parse(DoublyGlobal.countriesJSON);
                        if (tempCookieCurrency == null || tempCookieCurrency == 'undefined') {
                            jQueryGrizzly.get(DoublyGlobal.geoUrl+'?app_version=1', function(data) {
                                try{
                                if (typeof data === "undefined") data = {'value': ''};
                                var countryCode = data.value;
                                if (countryCode in currencyByCountry) {
                                    if (jQueryGrizzly.inArray(currencyByCountry[countryCode], allowedCurrencies) != -1) {
                                        defaultCurrency = currencyByCountry[countryCode];
                                    }
                                }
                                DoublyCurrency.cookie.write(defaultCurrency);
                                initCurrencySwitcher();
                                }catch(e){dbErrorLog(e);}
                            }, 'json');
                        } else {
                            if (jQueryGrizzly.inArray(tempCookieCurrency, allowedCurrencies) != -1) {
                                defaultCurrency = tempCookieCurrency;
                            }
                            initCurrencySwitcher();
                        }
                    } else {
                        initCurrencySwitcher();
                    }
                } else {
                    defaultCurrency = shopCurrency;
                    DoublyCurrency.cookie.write(defaultCurrency);
                    initCurrencySwitcher();
                    jQueryGrizzly('.doubly-wrapper').hide();
                    jQueryGrizzly('.doubly-float').hide();
                }
                }catch(e){dbErrorLog(e);}
            }

            if (document.readyState === "loading") {
                document.addEventListener("DOMContentLoaded", loadApp);
            } else {
                loadApp();
            }
        }catch(e){dbErrorLog(e);}
        }, "text");
    };
    
    /* wait for DoublyGlobal to be set in appblock or exit with timeout of 20s*/
    var bccWaitReadyIntervalTries=200;
    var bccWaitReadyInterval = setInterval(function(){
        if (typeof DoublyGlobal !== 'undefined') {
            clearInterval(bccWaitReadyInterval);
            bccStartApp();
        } else if (bccWaitReadyIntervalTries--<=0) {
            clearInterval(bccWaitReadyInterval);
            console.error('BEAST currency converter startup failed.');
        }
    }, 100);
}Ә