/* ==== 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.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 ==== */
function skipRedirect(url) {
    if (url.match(/solutions\/products\/sap-bydesign/) != null)
        return true;
    if (url.match(/solutions\/products\/sales-on-demand/) != null)
        return true;
    if (url.match(/solutions\/rapid-deployment/) != null)
        return true;
    if (url.match(/events\/event-results.epx/) != null)
        return true;
    return false;
}

var countryroot = '';
var targetdir = currpath.substring(currpath.indexOf('index.html') + 1, currpath.indexOf('index.html', 1));
switch (targetdir) {
    case 'uk':
    case 'india':
        countryroot = '/' + targetdir;
        targetdir = currpath.substring(targetdir.length + 2, currpath.indexOf('index.html', targetdir.length + 2));
        break;
}

var currquery, hashBang = '';

if (hasquery > 0) {
    currquery = currhref.substr(hasquery);
    hashBang = "location.replace('" + countryroot + "/index.epx#'+currpath+currquery)";
} else { hashBang = "location.replace('" + countryroot + "/index.epx#'+currpath)"; }
if (!skipRedirect(currhref)) {
    var ref = document.referrer;
    if(ref == null)
        ref = "";
    switch (targetdir) {
        case 'countries': eval(hashBang); break;
        case 'buy-now': eval(hashBang); break;
        case 'solutions': eval(hashBang); break;
        case 'industries': eval(hashBang); break;
        case 'lines-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':
            if(!skipRedirect(ref))
                eval(hashBang);
            break;
    }
}
