/*
	$Id: FTTrack2.js,v 1.19 2005/10/04 12:53:30 oliver Exp $
	$Log: FTTrack2.js,v $
	Revision 1.19  2005/10/04 12:53:30  oliver
	Edited by oliver on 20051004 1253
	
	Revision 1.5  2005/10/04 11:04:41  warec
	Changed track server from news.ft.com to globalelements.ft.com to fix https issues (Andrew Grant)
	
	Revision 1.4  2005/09/27 13:23:30  vhamill
	Change to code to add new parameters for WebTrends
	
	Revision 1.3  2005/08/31 14:23:20  warec
	Alias refactor
	
	Revision 1.2  2005/08/19 15:29:24  warec
	A constant changed to ANC to avoid clash with WebTrends A() function
	
	Revision 1.1  2005/07/21 09:29:50  warec
	Initial revision
	
			 
*/
var tp = new TrackParams();	// tracking parameters 
var tpRef = new TrackParams();	// referrer information 
var tpTrans = new TrackParams();	// transaction information
var tpTitle = new TrackParams(); // title information
var wtParams = new TrackParams(); // extra WebTrends parameters

function dcsGetCookie(name){	
	var pos=document.cookie.indexOf(name+"=");
	if (pos!=-1){
		var start=pos+name.length+1;
		var end=document.cookie.indexOf(";",start);
		if (end==-1){
			end=document.cookie.length;
		}
		return unescape(document.cookie.substring(start,end));
	}
	return null;
}
function dcsGetCrumb(name,crumb){
	if(dcsGetCookie(name)!=null){
		var aCookie=dcsGetCookie(name).split(":");
		for (var i=0;i<aCookie.length;i++){
			var aCrumb=aCookie[i].split("=");
			if (crumb==aCrumb[0]){
				return aCrumb[1];
			}
		}
	}
	return null;
}
function doTrack2() {

	if((location.hostname.indexOf('ftmarketwatch.com')!=-1)||(location.hostname.indexOf('ftmarketwatch.co.uk')!=-1)) {
		var tFTSite=typeof FTSite!='undefined'?FTSite:'FTMW';
	} else {
		var tFTSite=typeof FTSite!='undefined'?FTSite:'FTCOM';
	}
	
	// Set up the url parameters
	tp.add("FTSite",typeof tFTSite!="undefined"?tFTSite:"undefined");
	tp.add("FTSection",typeof FTSection!="undefined"?FTSection:"undefined");
	tp.add("FTPage",typeof FTPage!="undefined"?FTPage:"undefined");
	tp.add("FTBarrier",typeof FTBarrier!="undefined"?FTBarrier:null);
	tp.add("AssetType",typeof AssetType!="undefined"?AssetType:null);
	tp.add("sub",typeof showSubPage!="undefined"?showSubPage:(typeof subs!="undefined"?subs:null));

	tpRef.add("inFTReferrer",document.referrer);
		
	tpTrans.add("transId",new Date().getTime());
	
	tpTitle.add("WT.ti",document.title); // for passing back to FTTrack2 (don't need in WebTrends track, it's there already)
	
	//AYSC Cookie parameters
	var thisAYSC = getCookieValue('AYSC','');
	var ipCountry = getValueFromStr('_13', '', thisAYSC, '_', '');
	var regCountry = getValueFromStr('_14', '', thisAYSC, '_', '');
	var regArea = getValueFromStr('_17', '', thisAYSC, '_', '');
	var metroArea = getValueFromStr('_18', '', thisAYSC, '_', '');
	var ipRegion = getValueFromStr('_23', '', thisAYSC, '_', '');
	var regRegion = getValueFromStr('_24', '', thisAYSC, '_', '');	
	
	//wtParams.add("WT.ti",document.title); //done automatically in tag
	wtParams.add("WT.cg_n",typeof FTSection!="undefined"?FTSection:"undefined");
	wtParams.add("WT.cg_s",typeof FTPage!="undefined"?FTPage:"undefined");	
	var pid=dcsGetCrumb("FT_User","USERID");
	wtParams.add("WT.pid_s",typeof pid!="undefined"?pid:"undefined");
	wtParams.add("WT.ipCountry_s",typeof ipCountry!="undefined"?ipCountry:"undefined");
	wtParams.add("WT.regCountry_s",typeof regCountry!="undefined"?regCountry:"undefined");
	wtParams.add("WT.regArea_s",typeof regArea!="undefined"?regArea:"undefined");
	wtParams.add("WT.metroArea_s",typeof metroArea!="undefined"?metroArea:"undefined");
	wtParams.add("WT.ipRegion_s",typeof ipRegion!="undefined"?ipRegion:"undefined");
	wtParams.add("WT.regRegion_s",typeof regRegion!="undefined"?regRegion:"undefined");
	

	if(tFTSite=='FTMW'){
        var dc=document.cookie;
        var Reg=unescape(dc.substring((dc.indexOf('REG=')+4),dc.indexOf('REG=')+5));
        var tReg=Reg!=''?Reg:'undefined';
        var Uid=unescape(dc.substring((dc.indexOf('UNIQUEVALUE=')+12),(dc.indexOf('%7D')+3)));
        var tUid=Uid!=''?Uid:'undefined';
		tp.add("REG",tReg);
		tp.add("UID",tUid);
    }
		
	// cookie checking stuff
	var dc = document.cookie;
	var re = new RegExp("TRK_REF=" + window.location.href);
	
	
	if(re.test(dc)) {
		tp.add("ad_refresh","yes");
	}
	
	//document.write('?inFTURL='+escape(document.URL + (location.search?"&":"?")));	
	var call='<scr'+'ipt type="text/javascript" src="';
	call+= isSecure(document)?'https:':'http:';
	if(typeof log_name!='undefined') call+='//track.ft.com/ad/';
	else call+='//track.ft.com/track/';
	call+=isSecure(document)?'strack.js':'track.js';        
	
	call+='?inFTURL='+escape(document.URL + (location.search?"&":"?"));
	
	call+=escape(tp.buildQueryString()) + "&" + tpRef.buildQueryString() + "&" + tpTrans.buildQueryString() + "&" + tpTitle.buildQueryString() + "&" +  wtParams.buildQueryString();
	call+='"></sc'+'ript>';
	
	document.write(call);
	
	
	// Prepare track call in a web beacon
	/*
	var call = isSecure(document)?'https:':'http:';
	if(typeof log_name!='undefined') call+='//track.ft.com/ad/';
	else call+='//track.ft.com/track/';
	call+=isSecure(document)?'strack.js':'track.js';           

	call+='?inFTURL='+escape(document.URL + (location.search?"&":"?"));
	call+=escape(tp.buildQueryString()) + "&" + tpRef.buildQueryString() + "&" + tpTrans.buildQueryString();

	var imgArr = new Array();
	imgArr[0] = new Image();
	imgArr[0].src = call;
	*/
	
	document.cookie = "TRK_REF=null";
	
	wtTrack();
}


doTrack2();
//setTimeout("doTrackRefresh()",40000);

function doTrackRefresh() {
	document.cookie = "TRK_REF=" + window.location.href;
	setTimeout("window.location.reload(false)",2000);
}

/* REVENUE SCIENCES TRACKING */
var RS_Deployed; //do not even set this to null otherwise typeof == Object
if (typeof DM_CSID != "undefined" && typeof RS_Deployed == "undefined" && typeof FTSection != "undefined" && FTSection != "" && typeof FTPage != "undefined" && FTPage != "") {
    DM_addToLoc("FTSectionCode", FTSection);
    DM_addToLoc("FTPageCode", FTPage);
    DM_tag();
    RS_Deployed = "true_FTTrack2.js";
}

/* WEBTRENDS TRACKING */
function wtTrack() {	

	tp.buildMetaTags();
	tpRef.buildMetaTags();
	tpTrans.buildMetaTags();
	wtParams.buildMetaTags();
	
	/* This must be called as a script include (instead of having the WebTrend code in FTTrack2) so that
	   the meta tags get written out before the WebTrends script gets included */
	   
	var call='<scr'+'ipt type="text/javascript" src="';
	call+= isSecure(document)?'https:':'http:';
	call+='//globalelements.ft.com/Common/Track/WT.js">';
	call+='</sc'+'ript>';
	document.write(call);
}

/* UTILITY FUNCTIONS */
function isSecure(doc){
    return (doc.location.protocol=='https:');
}

/*
	Object to store the parameters for tracking FT stuff.
	Can be used to generate querystring parameters, or meta tags (either from the head as document.writes, or from the body as document.createElements)
*/
function TrackParams() {
	var hashTable = new Array();
	
	this.add = function(key,val) {
		if(key!=null && val!=null) {
			hashTable[key] = val;
		}
	}

	/* Build without the starting "?" or ending "&" in case we are making a querystring from more than one TrackParam e.g.
			"name1=va1&name2=val2&name3=val3"
	*/
	this.buildQueryString = function() {
		var qs = "";
		for (key in hashTable) {
			qs += escape(key) + "=" + escape(hashTable[key]) + "&";
		}
		return qs.substring(0,qs.length-1); // trim off the last &
	}
	
	/* This should only be called from the head */
	this.buildMetaTags = function () {
		var str = "";
		for (key in hashTable) {
			document.write('<meta name="DCSext.' + key + '" content="' + hashTable[key] + '" />');
		}
	}
	
	/* This won't work in IE if this is called from the head */
	this.buildMetaTagsFromBody = function () {
		for (key in hashTable) {
			var mt = document.createElement("meta");
	
			mt.setAttribute("name","DCSext." + key);
			mt.setAttribute("content",hashTable[key]);
	
			document.documentElement.firstChild.appendChild(mt);
		}
	}
	
}
