var host = window.location.host;
var pathname = window.location.pathname;
var search = window.location.search;
var https = "https://";
var pathnameCsmPortal = '/csm';
var searchPublicSurvey = '?id=public_survey';
var pathnameCommunityPortal = '/community';
var hostCommunity = 'community.sitecore.com';
var hostCommunityTest = 'communitytest.sitecore.com';
var isCommunity = host == hostCommunity || host == hostCommunityTest;
var hostMoosend = 'support.moosend.com';
var hostMoosendTest = 'support.moostaging.com';
var pathnameChat = '/sn_va_web_client_app_embed.do';
var searchChat = '?sysparm_branding_key=moosend_branding';
var isMoosend = host == hostMoosend || host == hostMoosendTest;

if (isCommunity) {
    if (!pathname.startsWith(pathnameCommunityPortal)) {
        window.location = https + host + pathnameCommunityPortal;
    }
}

if (pathname.startsWith(pathnameCommunityPortal)) {
    if (!isCommunity) {
        if (host == 'sitecore.service-now.com' || host == 'support.sitecore.com' || host == 'support.moosend.com') {
            window.location = https + hostCommunity + pathnameCommunityPortal;
        } else if (host == 'sitecoretest.service-now.com') {
            window.location = https + hostCommunityTest + pathnameCommunityPortal;
        }
    }
}

if (isMoosend) {
    if (pathname != pathnameChat && !pathname.startsWith(pathnameCsmPortal) && !search.startsWith(searchPublicSurvey)) {
        window.location = https + host + pathnameChat + searchChat;
    }
}