function fixFQDN(someurl) {
    var hostSplit = someurl.split("//");
    if (hostSplit.length == 0 || hostSplit.length == 1) {
        if (hostSplit[0].substring(0, 1) == '/') {
            return getNonSecureBaseURL() + someurl; //changing from relative to non secure
        } else {
            return getNonSecureBaseURL() + '/' + someurl; //changing from relative to non secure
        }
    }//if length one, then only append non secure base url

    if (hostSplit[0].indexOf('http') == 0) {
        var hostPortURL = hostSplit[1].split('/');
        var urlCount = 0;
        var returnURL = getNonSecureBaseURL();
        for (urlCount=1;urlCount<hostPortURL.length;urlCount++){
            returnURL += '/';
            returnURL += hostPortURL[urlCount];
        }
        return returnURL;
    } else {
        //not protocol http/https, implies some other values, just prepend non secure and return
        //fix later.
    }
    return someurl;
}

function fixNGageHomeURL() {
    var headerHrefs = document.getElementById('header').getElementsByTagName('a');
    var countHeaderHrefs = 0;
    var tempHref = null;
    for (countHeaderHrefs=0; countHeaderHrefs<headerHrefs.length; countHeaderHrefs++){
        tempHref = headerHrefs[countHeaderHrefs];
        if((tempHref.href).indexOf('doHomeLink') != -1) {
            var aJSURLs = tempHref.href.split('\(\'');
            tempHref.href = aJSURLs[0] + '\(\'' + getNonSecureBaseURL() + aJSURLs[1];
            break;
        }//home href found
    }//iterate through header doms
}

function fixNonSecureHRefs() {
    var protocol = location.protocol.substring(0, location.protocol.indexOf(":"));
    if (protocol == 'https') {
        if (document.getElementById('discover') != null) {
            document.getElementById('discover').childNodes[0].href = fixFQDN(document.getElementById('discover').childNodes[0].href);
            document.getElementById('get_ngage').childNodes[0].href = fixFQDN(document.getElementById('get_ngage').childNodes[0].href);
            document.getElementById('showroom').childNodes[0].href = fixFQDN(document.getElementById('showroom').childNodes[0].href);
            document.getElementById('community').childNodes[0].href = fixFQDN(document.getElementById('community').childNodes[0].href);
            document.getElementById('support').childNodes[0].href = fixFQDN(document.getElementById('support').childNodes[0].href);
        }

        //n-gage home
        fixNGageHomeURL();

        if (document.getElementById('footerCell') != null) {
                if (document.getElementById('footerCell').getElementsByTagName('a') != null && document.getElementById('footerCell').getElementsByTagName('a').length > 5) {
                    var allFooterLinks = document.getElementById('footerCell').getElementsByTagName('a');
                    for (footerLinkCount = 0; footerLinkCount < allFooterLinks.length; footerLinkCount++){
                        if (document.getElementById('footerCell').getElementsByTagName('a')[footerLinkCount].getAttributeNode('href').nodeValue.indexOf("http") == -1){
                            document.getElementById('footerCell').getElementsByTagName('a')[footerLinkCount].getAttributeNode('href').nodeValue = fixFQDN(document.getElementById('footerCell').getElementsByTagName('a')[footerLinkCount].getAttributeNode('href').nodeValue);
                        }
                    }
                }
        }

        //get urls for sub navs
        if (document.getElementById('glossary') != null){
            document.getElementById('glossary').childNodes[0].href = fixFQDN(document.getElementById('glossary').childNodes[0].href);
        }
        if (document.getElementById('Sitemap')){
            document.getElementById('Sitemap').childNodes[0].href = fixFQDN(document.getElementById('Sitemap').childNodes[0].href);
        }
        if (document.getElementById('purchase') != null){
            document.getElementById('purchase').childNodes[0].href = fixFQDN(document.getElementById('purchase').childNodes[0].href);
        }
        if (document.getElementById('purchaseondevice') != null){
            document.getElementById('purchaseondevice').childNodes[0].href = fixFQDN(document.getElementById('purchaseondevice').childNodes[0].href);
        }
        if (document.getElementById('faq') != null){
            document.getElementById('faq').childNodes[0].href = fixFQDN(document.getElementById('faq').childNodes[0].href);
        }

    } else {
        return '';
    }
}

//fix for register urls in install.html
function fixInstallCreatePlayerRefs(){
    if (location.href.indexOf('install.html') > -1){
        var allHrefs = document.getElementsByTagName('a');
        var tempHref;
        for (hrefLinkCount = 0; hrefLinkCount < allHrefs.length; hrefLinkCount++){
            tempHref = allHrefs[hrefLinkCount].getAttributeNode('href').nodeValue;
            if (tempHref.indexOf('http') == -1) {
                if (tempHref.indexOf('javascript') == -1) {
                    if (tempHref.indexOf('register') > -1) {
                        allHrefs[hrefLinkCount].getAttributeNode('href').nodeValue = getSecureBaseURL() + tempHref;
                    }
                }
            }
        }
    }
}

fixNonSecureHRefs();
fixInstallCreatePlayerRefs();

function renderWelcomeUserMessage() {
    if (location.href.indexOf('change_password.html') > -1) {
        var ngageMiniProfile = readCookie('NGAGE_MINI_PROFILE');
        if (ngageMiniProfile != null) {
            var aMiniProfileCookie = ngageMiniProfile.split('|');
            var miniProfileUserName = aMiniProfileCookie[0]; //collect username 4m cookie split
            document.getElementById('username').innerHTML = miniProfileUserName;
        }
    }
}

renderWelcomeUserMessage();

function fixChangePasswordLogic(){
    if (document.getElementById("navTabsSub") !=null) {
        var navTabsSubHrefs = (document.getElementById("navTabsSub")).getElementsByTagName('a');
        var temphref;
        for (navTabsSubHrefsCount = 0; navTabsSubHrefsCount < navTabsSubHrefs.length; navTabsSubHrefsCount++){
            temphref = navTabsSubHrefs[navTabsSubHrefsCount].href;
            if (temphref.indexOf('change_password') > -1){
                navTabsSubHrefs[navTabsSubHrefsCount].setAttribute('href', 'javascript:onclick=callChangePassword("'+temphref+'");');
            }
        }
    }
}

function callChangePassword(temphref) {
    var changePassword = temphref;
    //collect countryid and languageid from current url
    var urlHandleCountry = location.href.substr(location.href.indexOf('web') + 4, 2); //g0
    var urlHandleLanguage = location.href.substr(location.href.indexOf('web') + 7, 2); //en
    var refererURL = 'login.html?referer=/ngage/web/' + urlHandleCountry + '/' + urlHandleLanguage + '/support/change_password.html';
    if (readCookie('NGAGE_MINI_PROFILE') == null) {
        changePassword = temphref.replace('support/change_password.html', refererURL);
    }
    document.location = changePassword;
}

fixChangePasswordLogic();

function fixGetNgageLink() {
    if (document.getElementById('get_ngage') == null) {
        return;
    }
    var currentNgageLink = document.getElementById('get_ngage').childNodes[0].href;
    currentNgageLink = currentNgageLink.replace('get_ngage.html', 'get_ngage/download.html');
    document.getElementById('get_ngage').childNodes[0].href = currentNgageLink;
}

fixGetNgageLink();