// Retail redirect whole site except following pages
var retailExemptLink = [
    'site-disclaimer.html'
];

// Retire MPF redirect only following pages
var retireMPFPages = [
    '/mpf-funds.html',
    '/MPFfund-prices.aspx',
    '/fund-fact-sheets-reports.aspx',
    '/MPFHistfundprices.aspx'
];

// Retire ORSO redirect only following pages
var retireORSOPages = [
    '/orso-funds.html',
    '/ORSOLPfund-prices.aspx',
    '/ORSOSPPfund-prices.aspx',
    '/ORSOSPPfund-fact-sheets-reports.aspx',
    '/ORSOSPPHistfundprices.aspx',
    '/ORSOLPfund-fact-sheets-reports.aspx',
    '/IPFfund-fact-sheets-reports.aspx',
    '/IPFfund-prices.aspx',
    '/IPFHistfundprices.aspx',
    '/IPFSISFCHistfundprices.aspx'
];

function checkDisclaimer()
{
    var current = findCurrentPageName(window.location.href);
    var site = findCurrentSiteName(window.location.href);
    var isPreview = (window.location.href.indexOf('/prepublish/') >= 0)? true: false;
    if (!site)
    {
        document.body.style.display = 'block';
        return;
    }
    if (site == 'retail' && isPreview == false)
    {
        for (var i=0; i<retailExemptLink.length; i++)
        {
            if (current.indexOf(retailExemptLink[i].toLowerCase()) >= 0)
            {
                document.body.style.display = 'block';
                return;
            }
        }
        if (readCookie('accept_'+site) == null || readCookie('accept_'+site) != 'Y' )
        {
            window.location.href = 'site-disclaimer.html?referrer=' + Url.encode(window.location.href);
	        return false;
        }
    }
    else if (site == 'mpf' && isPreview == false)
    {
        if (readCookie('accept_'+site) == null || readCookie('accept_'+site) != 'Y' )
        {
            window.location.href = 'mpf-site-disclaimer.html?referrer=' + Url.encode(window.location.href);
            return false;
        }
    }
    else if (site == 'orso' && isPreview == false)
    {        
        if (readCookie('accept_'+site) == null || readCookie('accept_'+site) != 'Y' )
        {
            window.location.href = 'orso-site-disclaimer.html?referrer=' + Url.encode(window.location.href);
            return false;
        }
    }
    
    document.body.style.display = 'block';
}

function checkEMDisclaimer(cookiename)
{
    var current = findCurrentPageName(window.location.href);
	var site = null;
	if (window.location.href.toLowerCase().indexOf('/hkmain/') >= 0)
		site = 'hkmain';
    /*var site = findCurrentSiteName(window.location.href);
	var site = if (window.location.href.indexOf(retailExemptLink[i]) >= 0).toLowerCase()
*/    

	var isPreview = (window.location.href.indexOf('/prepublish/') >= 0)? true: false;
	if (!site)
    {
        document.body.style.display = 'block';
        return;
    }
    if (site == 'hkmain' && isPreview == false)
    {        
		if (cookiename == 'GEMO')
		{
			if (readCookie('EM_'+cookiename) == null || readCookie('EM_'+cookiename) != 'Y' )
			{
				window.location.href = 'EM-GEMO-RDB.html?referrer=' + Url.encode(window.location.href);
				return false;
			}
		}
		else if (cookiename == 'BRIC')
		{
			if (readCookie('EM_'+cookiename) == null || readCookie('EM_'+cookiename) != 'Y' )
			{
				window.location.href = 'EM-BRIC-RDB.html?referrer=' + Url.encode(window.location.href);
				return false;
			}
		}
		else if (cookiename == 'CHINAOPP')
		{
			if (readCookie('EM_'+cookiename) == null || readCookie('EM_'+cookiename) != 'Y' )
			{
				window.location.href = 'EM-ChinaOpp-RDB.html?referrer=' + Url.encode(window.location.href);
				return false;
			}
		}
    }
    document.body.style.display = 'block';
}



function checkEMDisclaimerInline(cookiename)
{
     var isPreview = (window.location.href.indexOf('/prepublish/') >= 0)? true: false;
	if (isPreview == false)
	{
		if (readCookie('EM_'+cookiename) == null || readCookie('EM_'+cookiename) != 'Y' )	
			return '0' ; 
		else 
			return '1' ;  
	}
	else
	{
		return '1' ;
	}
}

function findCurrentPageName(s)
{
    s = s.toLowerCase();
    if (s.indexOf('?') >= 0)
    {
        s = s.substring(0, s.indexOf('?'));
    }
    s = s.substring(s.lastIndexOf('/')+1);
    return s;
}

function findCurrentSiteName(s)
{
    s = s.toLowerCase();
    if (s.indexOf('/retail/') >= 0)
        return 'retail';
    var c = '/' + findCurrentPageName(s);
    for (var i=0; i<retireMPFPages.length; i++)
    {
        if (c.indexOf(retireMPFPages[i].toLowerCase()) >= 0)
        {
            return 'mpf';
        }
    }
    for (var i=0; i<retireORSOPages.length; i++)
    {
        if (c.indexOf(retireORSOPages[i].toLowerCase()) >= 0)
        {
            return 'orso';
        }
    }

    return null;
}

function acceptDisclaimer()
{
    var site = findCurrentSiteName(Url.decode(queryString('referrer')));
    if (!site) return false;
	createCookie('accept_'+site, 'Y');
	window.location.href = Url.decode(queryString('referrer'));
}

function acceptEMDisclaimer(cookiename)
{
    /*var site = findCurrentSiteName(Url.decode(queryString('referrer')));*/
	var site = null;
	if (Url.decode(queryString('referrer')).toLowerCase().indexOf('/hkmain/') >= 0)
		site = 'hkmain';
    if (!site) return false;
	createCookie('EM_'+cookiename, 'Y');
	window.location.href = Url.decode(queryString('referrer'));
}

function acceptEMDisclaimerInline(cookiename)
{
	createCookie('EM_'+cookiename, 'Y'); 
}



function createCookie(name, value) {
	document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function queryString(name) {
    qs = window.location.search.substring(1);
    qs = qs.split('&');
    for (var i=0; i<qs.length; i++) {
        p = qs[i].split('=');
        if (p[0] == name)
            return p[1];
    }
}

var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

checkDisclaimer();
