/* ==== GLOBAL VARS ==== */
var currhref = location.href;
var currpath = location.pathname;
var hasquery = currhref.indexOf('?');



/* ==== COOKIE FUNCTIONS ==== */
function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function deleteCookie(name) { setCookie(name, '', -1); }


var COUNTRY_SELECTOR_COOKIE_NAME = "SelectedCountryPath";

function get_current_country() { return getCookie(COUNTRY_SELECTOR_COOKIE_NAME); }
function set_current_country(country) { setCookie(COUNTRY_SELECTOR_COOKIE_NAME, country, 365); }



/* ==== Cookie Check ==== */
if (currpath.split('index-2.html')[1] == 'index.epx') { //if we are in the 'home' experience (aka "hash bang")
    if (location.hash.length <= 1) { //if we are on the 'home' page (aka "index.epx")
        var cs_cookie = get_current_country();
        var referringPage = (document.referrer);
        if (!cs_cookie) { //country cookie has not yet been set (aka "SelectedCountryPath")
            referringPageExt = referringPage.substr(referringPage.lastIndexOf('.'));
            if (referringPageExt != '.epx') { //if extension of referring document is not 'index.epx' we can assume the referrer is external to sap.com
                location.href = '/country-selector.epx'; //redirect to country selector page
            }
            else if (referringPage.search('country-selector.epx') > -1) { //if referring document was the 'country-selector.epx' page
                location.href = '/country-selector.epx'; //redirect to country selector page
            }
        }
    }
}


/* ==== THE HASH BANG THEORY ==== */
var targetdir = currpath.substring(currpath.indexOf('index-2.html') + 1, currpath.indexOf('index-2.html', 1));
var isBYD = currhref.match(/solutions\/products\/sap-bydesign/);
var isSOD = currhref.match(/solutions\/products\/sales-on-demand/);
var isEventSrchRslt = currhref.match(/events\/event-results.epx/);
var currquery, hashBang = '';

if (hasquery > 0) {
    currquery = currhref.substr(hasquery);
    hashBang = "location.replace('/index.epx#'+currpath+currquery)";
} else { hashBang = "location.replace('/index.epx#'+currpath)"; }
if (isBYD == null && isSOD == null && isEventSrchRslt == null) {
    switch (targetdir) {
        case 'countries': eval(hashBang); break;
        case 'buy-now': eval(hashBang); break;
        case 'solutions': eval(hashBang); break;
        case 'industries': eval(hashBang); break;
        case 'line-of-business': eval(hashBang); break;
        case 'services-and-support': eval(hashBang); break;
        case 'customer-testimonials': eval(hashBang); break;
        case 'training-and-education': eval(hashBang); break;
        case 'about-sap': eval(hashBang); break;
        case 'our-partners': eval(hashBang); break;
        case 'events': eval(hashBang); break;
        case 'communities': eval(hashBang); break;
        case 'social': eval(hashBang); break;
        case 'asset': eval(hashBang); break;
    }
}
