﻿var atl_pc_insert_styles = 'atlInsertStyles';
var atl_pc_is_ie6under = null;

function atlPCIsIE6under() {
    if (atl_pc_is_ie6under == null) {
        atl_pc_is_ie6under = false;
        var agt = navigator.userAgent.toLowerCase();
        var is_ie = agt.indexOf("msie") != -1;
        if (is_ie) {
            atl_pc_is_ie6under = (agt.indexOf("msie 6") != -1 || agt.indexOf("msie 5") != -1 || agt.indexOf("msie 4") != -1);
        }
    }
    return atl_pc_is_ie6under;
}

function atlPCShowElement(elmID) {
    for (i = 0; i < document.getElementsByTagName(elmID).length; i++) {
        obj = document.getElementsByTagName(elmID)[i];
        if (!obj || !obj.offsetParent) continue;
        if (is_ie && (obj.readyState == 4) && (obj.tagName.toUpperCase() == 'OBJECT')) continue;
        obj.style.visibility = '';
    }
}

function atlPCHideElement(elmID, overDiv) {
    for (i = 0; i < document.getElementsByTagName(elmID).length; i++) {
        obj = document.getElementsByTagName(elmID)[i];
        if (!obj || !obj.offsetParent) continue;
        // Find the element's offsetTop and offsetLeft relative to the BODY tag.
        objLeft = obj.offsetLeft - overDiv.offsetParent.offsetLeft;
        objTop = obj.offsetTop;
        objParent = obj.offsetParent;
        while ((objParent.tagName.toUpperCase() != 'BODY') && (objParent.tagName.toUpperCase() != 'HTML')) {
            objLeft += objParent.offsetLeft;
            objTop += objParent.offsetTop;
            objParent = objParent.offsetParent;
        }
        objHeight = obj.offsetHeight;
        objWidth = obj.offsetWidth;
        if ((overDiv.offsetLeft + overDiv.offsetWidth) <= objLeft);
        else if ((overDiv.offsetParent.offsetTop + overDiv.offsetHeight + 20) <= objTop);
        else if (overDiv.offsetParent.offsetTop >= eval(objTop + objHeight));
        else if (overDiv.offsetLeft >= eval(objLeft + objWidth));
        else {
            obj.style.visibility = 'hidden';
        }
    }
}

function atlPCFindChildNode(node, findId) {
    var existingNode = null;
    if (node.hasChildNodes) {
        var currentNode = node.firstChild;
        while (currentNode != null) {
            if (currentNode.id == findId) {
                existingNode = currentNode;
                break;
            }
            currentNode = currentNode.nextSibling;
        }
    }
    return existingNode;
}

function atlPCGoToUrl(url) {
    window.location = url;
}

function atlPCCookieDomain() {
    var hname = window.location.hostname;
    var tldstart = hname.lastIndexOf('.');
    if (tldstart < 0) {
        return '.' + hname;
    }
    else {
        var dname = '';
        if (hname.lastIndexOf('.', tldstart - 1) > -1) {
            dname = hname.substr(hname.lastIndexOf('.', tldstart - 1));
        }
        else {
            dname = '.' + hname;
        }
        return dname;
    }
}

function atlPCSetCookie(name, value) {
    var currentDT = new Date();
    var expires = new Date(Date.parse(currentDT.getDay() + "/" + currentDT.getMonth() + "/" + (currentDT.getFullYear() + 1)));
    var curCookie = name + "=" + value +
      "; expires=" + expires.toGMTString() + "; path=/; domain=" + atlPCCookieDomain();
    document.cookie = curCookie;
}

function atlPCReadCookie(name) {
    var cookiecontent = new String();
    if (document.cookie.length > 0) {
        var cookiename = name + '=';
        var cookiebegin = document.cookie.indexOf(cookiename);
        var cookieend = 0;
        if (cookiebegin > -1) {
            cookiebegin += cookiename.length;
            cookieend = document.cookie.indexOf(";", cookiebegin);
            if (cookieend < cookiebegin) {
                cookieend = document.cookie.length;
            }
            cookiecontent = document.cookie.substring(cookiebegin, cookieend);
        }
    }
    return unescape(cookiecontent);
}

function atlPCFillDiv(targetDiv, stylesName, jsonInsertData) {
    if ((jsonInsertData != null) && (targetDiv != null)) {
        if (jsonInsertData.StyleSheet.length > 0) {
            var head = document.getElementsByTagName("head").item(0);
            var existingNode = atlPCFindChildNode(head, stylesName);
            if (existingNode == null) {
                var csslink = document.createElement("link");
                csslink.setAttribute("id", stylesName);
                csslink.setAttribute("type", "text/css");
                csslink.setAttribute("rel", "stylesheet");
                csslink.setAttribute("href", jsonInsertData.StyleSheet);
                csslink.setAttribute("media", "screen");
                head.appendChild(csslink);
            }
        }
        targetDiv.innerHTML = jsonInsertData.Html;
    }
}

function atlPCCallForContent(contentScriptName, request) {
    var head = document.getElementsByTagName("head").item(0);
    var existingNode = atlPCFindChildNode(head, contentScriptName);

    var script = document.createElement("script");
    script.setAttribute("id", contentScriptName);
    script.setAttribute("type", "text/javascript");
    script.setAttribute("language", "javascript");
    script.setAttribute("src", request);

    if (existingNode != null) {
        head.replaceChild(script, existingNode);
    }
    else {
        head.appendChild(script);
    }
}

var atl_chi_targetDiv;
var atl_chi_siteUrl;
var atl_chi_cachedFlag;
var atl_chi_script_id = 'atlChiCartHeaderInsert';
var atl_chi_curchanged = false;
var atl_chi_skipshow = false;
var atl_chi_querystring = '';

function atlChiInitializeEx(targetDiv, siteUrl, queryString) {
    atl_chi_querystring = queryString;
    atlChiInitialize(targetDiv, siteUrl);
}

function atlChiInitialize(targetDiv, siteUrl) {
    atl_chi_siteUrl = siteUrl;
    atl_chi_targetDiv = targetDiv;
    atl_chi_cachedFlag = false;
}

function atlChiClearCache() {
    atl_chi_cachedFlag = false;
}

function atlChiShowInt() {
    if (atl_chi_targetDiv != null) {
        if (atlPCIsIE6under()) {
            atlPCHideElement('SELECT', atl_chi_targetDiv);
        }
        atl_chi_targetDiv.style.display = 'block';
    }
}

function atlChiShow(currency) {
    atlChiShowEx(currency, '');
}

function atlChiShowEx(currency, marketplace) {
    if (atl_chi_cachedFlag == true) {
        atlChiShowInt();
    }
    else {
        atlChiCallForContentEx(currency, marketplace);
    }
}

function atlChiCallForContent(currency) {
    atlChiCallForContentEx(currency, '');
}

function atlChiCallForContentEx(currency, marketplace) {
    var request = atl_chi_siteUrl + "Cart/CartHeaderInsert.aspx";
    if (atl_chi_querystring.length == 0) {
        request = request + "?callback=atlChiFillDiv";
    }
    else {
        request = request + atl_chi_querystring + "&callback=atlChiFillDiv";
    }

    if ((currency != null) && (currency != '')) {
        request = request + "&currencyType=" + currency;
    }

    if ((marketplace != null) && (marketplace != '')) {
        request = request + "&marketplace=" + marketplace;
    }

    atlPCCallForContent(atl_chi_script_id, request);
}

function atlChiFillDiv(cartData) {
    if ((cartData != null) && (atl_chi_targetDiv != null)) {
        atlPCFillDiv(atl_chi_targetDiv, atl_pc_insert_styles, cartData);
        atl_chi_cachedFlag = true;

        if (!atl_chi_skipshow)
            atlChiShowInt();
    }
}

function atlChiClose() {
    atl_chi_skipshow = false;
    if (atl_chi_targetDiv != null) {
        atl_chi_targetDiv.style.display = 'none';
        if (atlPCIsIE6under()) {
            atlPCShowElement('SELECT');
        }
        if (atl_chi_curchanged) {
            var r1 = new RegExp("[&|?]currencytype=[^&]*", "i");
            var redirect = window.location.href.replace(r1, '');
            var r2 = new RegExp("#.*", "");
            redirect = redirect.replace(r2, '');
            window.location.href = redirect;
        }
    }
}

// Obsolete- remove in 1 release
function atlChiGoToUrl(url) {
    window.location = url;
}

function atlChiChangeCurrency(currencySelectId, suffix) {
    atlChiChangeCurrencyEx(currencySelectId, suffix, '');
}

function atlChiChangeCurrencyEx(currencySelectId, suffix, marketplace) {
    var currencySelect = document.getElementById(currencySelectId);
    var selectedCurrency = currencySelect.options[currencySelect.selectedIndex].value;
    var currencyCookieName = 'currency' + suffix;
    atlPCSetCookie(currencyCookieName, 'potableSourceStr=' + selectedCurrency);
    atl_chi_curchanged = true;
    if (window.sync_inapp) {
        if (sync_inapp != 'gdtv') {
            syncPushCurrency(selectedCurrency);
        }
    }
    atl_chi_skipshow = true;
    atlChiCallForContentEx(selectedCurrency, marketplace);
}

function atlChiShowMarketplaceTab() {
    atl_chi_skipshow = true;
    atlChiCallForContentEx('', 'true');
}

function atlChiShowStandardTab() {
    atl_chi_skipshow = true;
    atlChiCallForContentEx('', 'false');
}

var atl_isi_targetDiv;
var atl_isi_siteUrl;
var atl_isi_cachedFlag;
var atl_isi_script_id = 'atlIsiInternationalSelectionInsert';
var atl_isi_querystring = '';
var atl_isi_newflagcode;

function atlIsiInitializeEx(targetDiv, siteUrl, queryString) {
    atl_isi_querystring = queryString;
    atlIsiInitialize(targetDiv, siteUrl);
}

function atlIsiInitialize(targetDiv, siteUrl) {
    atl_isi_siteUrl = siteUrl;
    atl_isi_targetDiv = targetDiv;
    atl_isi_cachedFlag = false;
}

function atlIsiClearCache() {
    atl_isi_cachedFlag = false;
}

function atlIsiShowInt() {
    if (atl_isi_targetDiv != null) {
        if (atlPCIsIE6under()) {
            atlPCHideElement('SELECT', atl_isi_targetDiv);
        }
        atl_isi_targetDiv.style.display = 'block';
    }
}

function atlIsiShow() {
    if (atl_isi_cachedFlag == true) {
        atlIsiShowInt();
    }
    else {
        atlIsiCallForContent();
    }
}

function atlIsiCallForContent() {
    var request = atl_isi_siteUrl + "Cart/InternationalSelectionInsert.aspx";
    if (atl_isi_querystring.length == 0) {
        request = request + "?callback=atlIsiFillDiv";
    }
    else {
        request = request + atl_isi_querystring + "&callback=atlIsiFillDiv";
    }

    atlPCCallForContent(atl_isi_script_id, request);
}

function atlIsiFillDiv(intlSelectionData) {
    if ((intlSelectionData != null) && (atl_isi_targetDiv != null)) {
        atlPCFillDiv(atl_isi_targetDiv, atl_pc_insert_styles, intlSelectionData);
        atl_isi_cachedFlag = true;
        atlIsiShowInt();
    }
}

function atlIsiClose() {
    if (atl_isi_targetDiv != null) {
        atl_isi_targetDiv.style.display = 'none';
        if (atlPCIsIE6under()) {
            atlPCShowElement('SELECT');
        }
    }
}

// remove after next iteration
function atlIsiGoToUrl(url) {
    window.location = url;
}

// Remove after 3 is active
function atlIsiCountryChange(countrySelectId, currencySelectId, matchSelectId) {
    var matchSelect = document.getElementById(matchSelectId);
    var countrySelect = document.getElementById(countrySelectId);
    var currencySelect = document.getElementById(currencySelectId);

    var preselectCurrency = '';
    var selectedCountryCode = countrySelect.options[countrySelect.selectedIndex].value;
    for (var i = 0; i < matchSelect.options.length; i++) {
        var matchOption = matchSelect.options[i];
        if (matchOption.text == selectedCountryCode) {
            preselectCurrency = matchOption.value;
            break;
        }
    }

    if (preselectCurrency.length > 0) {
        for (var j = 0; j < currencySelect.options.length; j++) {
            var currencyOption = currencySelect.options[j];
            if (currencyOption.value == preselectCurrency) {
                currencyOption.selected = true;
                var disclaimerdiv = document.getElementById("crncydisclaimer");
                if (preselectCurrency != "USD") {
                    disclaimerdiv.style.display = 'inline';
                }
                else {
                    disclaimerdiv.style.display = 'none';
                }
                break;
            }
        }
    }

    atlIsiShowOffer(selectedCountryCode, countrySelect);
}

function atlIsiShowOffer(selectedCountryCode, countrySelect) {
    for (var i = 0; i < countrySelect.options.length; i++) {
        var currentCountryCode = countrySelect.options[i].value;
        var offerDivId = 'isiOffer_' + currentCountryCode;
        var offerDiv = document.getElementById(offerDivId)
        if (offerDiv != null) {
            if (currentCountryCode == selectedCountryCode) {
                offerDiv.style.display = 'block';
            }
            else {
                offerDiv.style.display = 'none';
            }
        }
    }
}

function atlIsiChangeCountry(countrySelectName, countrySelectId) {
    var cntryname = document.getElementById("textname");
    atl_isi_newflagcode = countrySelectId;
    var selectedCountryCode = countrySelectId;
    document.getElementById("flagimg").className = "fosisi_flag_base fosisi_flag_image_" + countrySelectId;
    cntryname.innerHTML = "&nbsp; " + countrySelectName;

    var caOfferDiv = document.getElementById("isiOffer_ca");
    if (caOfferDiv != null) {
        if (countrySelectId == "ca") {
            caOfferDiv.style.display = "block";
        } else {
            caOfferDiv.style.display = "none";
        }
    }
}

// Remove after 3 is active
function atlIsiSave(countrySelectId, currencySelectId, suffix) {
    var countrySelect = document.getElementById(countrySelectId);
    var currencySelect = document.getElementById(currencySelectId);

    var selectedCountryCode = countrySelect.options[countrySelect.selectedIndex].value;
    var selectedCurrency = currencySelect.options[currencySelect.selectedIndex].value;

    var currencyCookieName = 'currency' + suffix;
    atlPCSetCookie(currencyCookieName,'potableSourceStr=' + selectedCurrency);
    var flagCookieName = 'flag' + suffix;
    atlPCSetCookie(flagCookieName, 'cflag=' + selectedCountryCode);

    var r1 = new RegExp("[&|?]currencytype=[^&]*", "i");
    var redirect = window.location.href.replace(r1, '');
    var r2 = new RegExp("#.*", "");
    redirect = redirect.replace(r2, '');
    var r3 = new RegExp("[&|?]cflag=[^&]*", "i");
    redirect = redirect.replace(r3, '');
    window.location.href = redirect;
}

function atlIsiSave3(countrySelectId, currencySelectId, suffix) {
    var grp = document.getElementsByName("fosisi_radiocurrencies");
    var x, len = grp.length;
    for (x = 0; x < len; x++) { if (grp[x].checked) break; }
    var selectedCurrency = grp[x].value;
    var selectedCountryCode = countrySelectId;
    if (atl_isi_newflagcode != undefined) {
        selectedCountryCode = atl_isi_newflagcode;
    }
    else {
        selectedCountryCode = countrySelectId;
    }
    var currencyCookieName = 'currency' + suffix;
    atlPCSetCookie(currencyCookieName, 'potableSourceStr=' + selectedCurrency);
    var flagCookieName = 'flag' + suffix;
    atlPCSetCookie(flagCookieName, 'cflag=' + selectedCountryCode);
        
    var r1 = new RegExp("[&|?]currencytype=[^&]*", "i");
    var redirect = window.location.href.replace(r1, '');
    var r2 = new RegExp("#.*", "");
    redirect = redirect.replace(r2, '');
    var r3 = new RegExp("[&|?]cflag=[^&]*", "i");
    redirect = redirect.replace(r3, '');
    window.location.href = redirect;
}

function atlIsiGetCrncyType(suffix) {
    var CrncyTypeCookieName = 'currency' + suffix;
    var currency = atlPCReadCookie(CrncyTypeCookieName);
    if (currency.length == 0) {
        currency = 'USD';
    }
    else {
        var equalpos = currency.indexOf('=');
        if ((equalpos > -1) && (equalpos < currency.length)) {
            currency = currency.substring(equalpos + 1, currency.length);
        }
    }
    return currency;
}
function atlIsiGetFlag(suffix) {
    var flagCookieName = 'flag' + suffix;
    var flag = atlPCReadCookie(flagCookieName);
    if (flag.length > 0) {
        var equalpos = flag.indexOf('=');
        if ((equalpos > -1) && (equalpos < flag.length)) {
            flag = flag.substring(equalpos + 1, flag.length);
        }
    }
    
    if (flag.length == 0)
    {
        flag = 'us';
    }
    
    return flag;
}

// Remove after 3 is active
function atlIsiChangeCurrency(currencySelectId, suffix) {
    var currencySelect = document.getElementById(currencySelectId);
    var selectedCurrency = currencySelect.options[currencySelect.selectedIndex].value;
    var disclaimerdiv = document.getElementById("crncydisclaimer");
    if (selectedCurrency != "USD") {
        disclaimerdiv.style.display = 'inline';
    }
    else {
        disclaimerdiv.style.display = 'none';
    }
}

function atlIsi_showhide(id) {
    if (document.getElementById) {
        var elem = document.getElementById(id);
        if (elem) {
            if (elem.style.display == "" || elem.style.display == "block")
                elem.style.display = "none";
            else
                elem.style.display = "block";
        }
    }
}

var atl_sap_targetDiv;
var atl_sap_siteUrl;
var atl_sap_script_id = 'atlSapShowAllProductsInsert';
var atl_sap_querystring = '';

function atlSapInitialize(targetDiv, siteUrl) {
    atl_sap_siteUrl = siteUrl;
    atl_sap_targetDiv = targetDiv;
}

function atlSapInitializeEx(targetDiv, siteUrl, queryString) {
    atl_sap_querystring = queryString;
    atlSapInitialize(targetDiv, siteUrl);
}

function atlSapShowInt() {
    if (atl_sap_targetDiv != null) {
        if (atlPCIsIE6under()) {
            atlPCHideElement('SELECT', atl_sap_targetDiv);
        }
        atl_sap_targetDiv.style.display = 'block';
    }
}

function atlSapShow() {
    atlSapCallForContent();
}

function atlSapCallForContent() {
    var request = atl_sap_siteUrl + "Products/ShowAllProductsInsert.aspx";
    if (atl_sap_querystring.length == 0) {
        request = request + "?callback=atlSapFillDiv";
    }
    else {
        request = request + atl_sap_querystring + "&callback=atlSapFillDiv";
    }

    atlPCCallForContent(atl_sap_script_id, request);
}

function atlSapFillDiv(sapData) {
    if ((sapData != null) && (atl_sap_targetDiv != null)) {
        atlPCFillDiv(atl_sap_targetDiv, atl_pc_insert_styles, sapData);
        atlSapShowInt();
    }
}

function atlSapClose() {
    if (atl_sap_targetDiv != null) {
        atl_sap_targetDiv.style.display = 'none';
        if (atlPCIsIE6under()) {
            atlPCShowElement('SELECT');
        }
    }
}

// Remove next iteration
function atlSapGoToUrl(url) {
    window.location = url;
}

function atlSliSetSkipPrompt(skipPrompt) {
    // do nothing remove when no references left
}

function atlSliRedirectToAccountUrl(targetUrl) {
    window.location = targetUrl;
}

function atlSliRedirectToAccountUrlWindow(targetUrl) {
    window.open(targetUrl, "_blank");
}
