// NAME		CH.js
// PURPOSE	Provides cookie-header functionality 
//		Sets FTSite javascript variable from a session cookie
// 		with the cookie being updated whenever FTSite is in the URL
//		Creates session cookies to store FTSite, FTSection, FTPage and FTIndustry
//		variables for passing to included wrapper components
// VERSION	v1.1

// Additional check in case DefaultFTSite and SiteDomain haven't been set

FTSite = typeof FTSite == 'undefined'?'':FTSite;
DefaultFTSite = typeof DefaultFTSite == 'undefined'?FTSite:DefaultFTSite;
SiteDomain = typeof SiteDomain == 'undefined'?'.ft.com':SiteDomain;
SiteDomain = SiteDomain == '.ftyourmoney.com'?'.ft.com':SiteDomain;
// The FTSite cookie needs to be set against .ft.com unless it's a completely third party
FTSiteDomain = SiteDomain.indexOf('.ft.com') != -1?'.ft.com':SiteDomain;

// Get the correct FTSite value. This can be already set, on the URL or stored in a cookie.
// Already set values take priority over URL values, which take priority over cookie values.
var key = 'FTSite=';
var begin = '';
var str = '';
var ck = document.cookie;
var srch = typeof location.search != 'undefined' ? location.search.toString() : '';
var gotSite = 0;
if (srch && srch.length > 0){
    begin =  srch.indexOf(key) > -1 ? srch.indexOf(key) + key.length : -1;
    if (begin > -1){
		str =  srch.substr(begin, srch.length);
		FTSite = str.substr(0, str.indexOf('&') >= 0 ? str.indexOf('&') : str.length);
		gotSite = 1;
	}
}

if ((ck && ck.length > 0) && (gotSite == 0)){
    begin =  ck.indexOf(key) > -1 ? ck.indexOf(key) + key.length : -1;
    if (begin > -1){
        // There's an FTSite cookie
		str =  ck.substr(begin, ck.length);
		FTSite = str.substr(0, str.indexOf(';') >= 0 ? str.indexOf(';') : str.length);
		gotSite = 1;
    }
}

// No cookies or query string at all, so set FTSite to DefaultFTSite
if (gotSite != 1){ FTSite = DefaultFTSite; }

// Delete the previous cookie
document.cookie = 'FTSite=;domain='+FTSiteDomain+';path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
// And reset it
cookie = 'FTSite=' + FTSite +';domain='+FTSiteDomain+';path=/;';
document.cookie = cookie;

// Drop ad code values for the FT.com header
// Delete previous codes to prevent misidentification
document.cookie = 'keywordValue=;domain=.ft.com;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
document.cookie = 'FTAdValues=;domain=.ft.com;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
var doneClear = 1;

// Gather ad codes
FTSection = typeof FTSection == 'undefined'?'':FTSection;
FTPage = typeof FTPage == 'undefined'?'':FTPage;
FTIndustry = typeof FTIndustry == 'undefined'?'':FTIndustry;
adKeywords = typeof adKeywords == 'undefined'?'':adKeywords;

// Set cookie for keyword
cookie = 'keywordValue=' + adKeywords +';domain=.ft.com;path=/;';
document.cookie = cookie;

// Set cookies for FTSection, FTPage and FTIndustry
var adValues = FTSection + ':' + FTPage + ':' + FTIndustry
cookie = 'FTAdValues=' + adValues +';domain=.ft.com;path=/;';
document.cookie = cookie;
