var gImages=new Array;  // Array to hold images so they don't show up on the page
var gIndex=0;		// Index for current image	
var DCS=new Object();   // The DCS object holds parameters to override fields in the SDC weblog
var WT=new Object();    // The WT object holds parameters used in custom reporting
var DCSext=new Object(); // The DCSext object holds site specific parameters
var gQP=new Array();     // Array of desired query parameters to pass to WebTrends


var HSBC = new Object(); // THw HSBC parameter holds five subgroups of parameters defined on a per site basis


HSBC.SITE = new Object(); // SITE specific variables go into this object.  They are translated to DCSext vairables
HSBC.PAGE = new Object(); // WT Overrides --portal variables that need to overrride WT variables go into this object
HSBC.EXT = new Object(); //  This is a placeholder for non SITE oriented DCSext variables
HSBC.LOG = new Object(); //  DCS overrides -- Variables in here will override DCS.* variables matching the name
HSBC.DCS = new Object(); // This object contains HSBC.DCS.ID with the dcsid and HSBC.DCS.Doms with the comma-separated
                         // list of domains to be considered on-site.  As it currently stands, these domains have 
                         // to be complete domain names.

// List all the live domains the group has (per GeB) to make sure they are not included as an off-site link
HSBC.DCS.Doms="www.britishbank.com,www.hsbcril.com,www.hsbcamanah.com.br,www.hsbcbrazil.com.br,www.hsbc.am,www.hsbc.com,www.equator-africa.com,www.hsbc2e.com,www.hsbcabsolute.com,www.hsbcamanah.com,www.hsbcarabia.com,www.hsbcequator.com,www.hsbcib.com,www.hsbcmarkets.com,www.hsbcmarketsasia.com,www.hsbcmefco.com,www.hsbcpremier.com,www.hsbcprivatebank.com,www.hsbcprivateequity.com,www.hsbcworldmatchplay.com,www.marinemidland.com,www.midlanddirect.com,www.stepovertheline.com,www.hsbc.cz,www.firstdirect.jp,www.hsbc.co.je,www.hsbc.la,www.hsbc.com.mt,www.hsbc.gb.net,www.mlhsbc.co.nz,www.hsbc.com.pa,www.firstdirect.com.qa,www.hsbc.co.za,www.hsbc.com.tr,www.bacb.co.uk,www.hsbc.co.uk,www.hsbcworldmatchplay.co.uk,www.stepovertheline.co.uk,www.hexagon.us,www.firstdirect.am,www.firstdirect.at,www.firstdirect.co.at,www.firstdirect.or.at,www.firstdirect.az,www.firstdirect.com.az,www.firstdirect.bs,www.firstdirect.com.bs,www.firstdirect.be,www.firstdirect.com.br,www.firstdirect.ca,www.firstdirect.com.ky,www.firstdirect.cl,www.fdfella.com,www.firstdirect.com,www.firstdirect-e.com,www.firstdirect-ebank.com,www.firstdirect-internetbank.com,www.firstdirect-octopus.com,www.firstdirectbank.com,www.firstdirect.cz,www.firstdirect.dk,www.firstdirect.com.gi,www.firstdirect.com.gr,www.firstdirect.gr,www.firstdirect.gl,www.firstdirect.co.gg,www.firstdirect.gg,www.firstdirect.co.im,www.firstdirect.co.il,www.firstdirect.co.je,www.firstdirect.je,www.firstdirect.com.jo,www.firstdirect.jo,www.firstdirect.com.kz,www.firstdirect.kz,www.firstdirect.lu,www.firstdirect.com.my,www.firstdirect.co.mu,www.firstdirect.mu,www.firstdirect.com.mx,www.firstdirect.co.mz,www.firstdirect.com.na,www.firstdirect.na,www.fdcom.net,www.fdfella.net,www.fdoctopus.net,www.firstagain.net,www.firstdirect-e.net,www.firstdirect-ebank.net,www.firstdirect-internetbank.net,www.firstdirect-octopus.net,www.littlefella.net,www.firstdirect.co.nz,www.fdcom.org,www.fdglobal.org,www.fdnet.org,www.first-direct.org,www.firstagain.org,www.firstdirect-e.org,www.firstdirect-ebank.org,www.firstdirect-internetbank.org,www.firstdirect-octopus.org,www.firstdirectbank.org,www.firstdirect.biz.pk,www.firstdirect.com.pk,www.firstdirect.com.pa,www.firstdirect.ph,www.firstdirect.com.pl,www.firstdirect.pl,www.firstdirect.com.ru,www.firstdirect.ru,www.firstdirect.com.sg,www.firstdirect.co.za,www.firstdirect.com.es,www.firstdirect.se,www.firstdirect.tm.se,www.firstdirect.in.th,www.firstdirect.tm,www.first-direct.tv,www.firstdirect.tv,www.first-direct.co.uk,www.firstagain.co.uk,www.firstdirect-e.co.uk,www.firstdirect-ebank.co.uk,www.firstdirect-internetbank.co.uk,www.firstdirect-octopus.co.uk,www.firstdirect.com.ve,www.firstdirect.vg,www.hsbcamanah.com";

// This is where the information collected by the WT Tracklet be sent
var gDomain="www1.member-hsbc-group.com";

// The gDcsId will be assigned from the HSBC.DCS.ID variable
var gDcsId="dcs8a9pzfvievvfhkn6u3og9d_5u3o"; // GLOBAL DCS ID... 

// Code section for Enable Event Tracking

//  Replaces SmartView links when tracking offsite pages
//
function dcsParseSvl(sv){
	sv=sv.replace(/\s/g,"");
	var pos=sv.toUpperCase().indexOf("WT.SVL=");
	if (pos!=-1){
		var start=pos+8;
		var end=sv.indexOf('"',start);
		if (end==-1){
			end=sv.indexOf("'",start);
			if (end==-1){
				end=sv.length;
			}
		}
		return sv.substring(start,end);
	}
	return "";
}

// Tests if current domain is in a list of 'on-site' domains.  This test
// is much more restrictive than the default to capture phishing sites that spoof 
// the HSBC domains.  The default code simply checks if the hostname
// matches somewhere in the domain array as in:
//  if ((host.indexOf(aDoms[i])!= -1) {
//           return 1;
//  }
function dcsIsOnsite(host){
    for (N in HSBC.DCS) {
	if (N == "Doms") {
	    var aDoms=HSBC.DCS.Doms.split(',');
    	    for (var i=0;i<aDoms.length;i++){
		if ((host.indexOf(aDoms[i])==0)&&
			(host.length==aDoms[i].length) ){
		       return 1;
		}
    	     }
	    return 0;
	}
    }
    return 1;
}
// Ensures that offsite links are only processed for http/s schemas

function dcsIsHttp(e){
	return (e.href&&e.protocol&&(e.protocol.indexOf("http")!=-1))?true:false;
}
//
//  This function makes sure that an actual event is handed to the various trigger functions
// depending on the way events are supported in the JavaScript

function dcsEvt(evt){
	var e=evt.target||evt.srcElement;
	if (e.tagName&&(e.tagName=="IMG")){
		e=e.parentElement||e.parentNode;
	}
	return e;
}
// This function adds a specific function to fire off when a desired event occurs
//
function dcsBind(event,func){
	if (typeof(window[func])=="function"){
		if (document.body.addEventListener){
			document.body.addEventListener(event, window[func], true);
		}
		else if(document.body.attachEvent){
			document.body.attachEvent("on"+event, window[func]);
		}
	}
}

// This functions defines the events of intgerest and the functions associated with them.
// In this example, we're only interested in offsite links

function dcsET(){
    dcsBind("click","dcsDownload");
    dcsBind("click","dcsOffsite");
    dcsBind("mousedown","dcsRightClick");

}
// Track clicks to download links.
function dcsDownload(evt){
    evt=evt||(window.event||"");
    if (evt){
        var e=dcsEvt(evt);
        if (dcsIsHttp(e)&&dcsIsOnsite(e.hostname)){
            var types="xls,doc,pdf,txt,csv,zip";   //customize download types
            if (types.indexOf(e.pathname.substring(e.pathname.lastIndexOf(".")+1,e.pathname.length))!=-1){
                var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
                if (qry.toUpperCase().indexOf("WT.SVL=")==-1){
                    WT.svl=dcsParseSvl(e.name?e.name.toString():(e.onclick?e.onclick.toString():""));
                }
                var path=(e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname;
                dcsMultiTrack("DCS.dcssip",e.hostname,"DCS.dcsuri",path,"WT.ti","Download:"+path,"WT.dl","1");
                DCS.dcssip=DCS.dcsuri=WT.ti=WT.svl=WT.dl="";
            }
        }
    }
}

// Track right clicks to download links.
function dcsRightClick(evt){
    evt=evt||(window.event||"");
    if (evt){
        var btn=evt.which||evt.button;
        if (btn!=1){
            var e=evt.target||evt.srcElement;
            if (dcsIsHttp(e)){
                var types="xls,doc,pdf,txt,csv,zip";    //customize download types
                if (types.indexOf(e.pathname.substring(e.pathname.lastIndexOf(".")+1,e.pathname.length))!=-1){
                    var path=(e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname;
                    dcsMultiTrack("DCS.dcssip",e.hostname,"DCS.dcsuri",path,"DCS.dcsqry","","WT.ti","Download:"+path,"WT.dl","1","WT.rc","1");
                    DCS.dcssip=DCS.dcsuri=WT.ti=WT.dl=WT.rc="";
                }
            }
        }
    }
}

// Code section for Track clicks to links leading offsite.
function dcsOffsite(evt){
	evt=evt||(window.event||"");
	if (evt){
		var e=dcsEvt(evt);
		if (dcsIsHttp(e)&&!dcsIsOnsite(e.hostname)){
			var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
			if (qry.toUpperCase().indexOf("WT.SVL=")==-1){
				WT.svl=dcsParseSvl(e.name?e.name.toString():(e.onclick?e.onclick.toString():""));
			}
			var path=(e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname;
			var trim=true;
			dcsMultiTrack("DCS.dcssip",e.hostname,"DCS.dcsuri",path,"DCS.dcsqry",trim?"":qry,"WT.ti","Offsite:"+e.hostname+path+qry,"WT.os","1","DCS.dcsref",window.location.hostname);
			DCS.dcssip=DCS.dcsuri=DCS.dcsqry=WT.ti=WT.svl=WT.os=DCS.dcsref="";
		}
	}
}
// Assign your query parameters to WebTrends query parameters. 
function dcsQP(N){
    if (!N){
        return;
    }
    var qry=location.search.substring(1);
    var pairs=qry.split("&");
    for (var i=0;i<pairs.length;i++){
        var pos=pairs[i].indexOf("=");
        if (pos==-1){
            continue;
        }
        var name=pairs[i].substring(0,pos);
        var value=pairs[i].substring(pos+1);
        if (name==N){
            gQP.push((i==0?"":"&")+pairs[i]);
            return value;
        }
    }
    return "";
}

function dcsAdv(){
	dcsFunc("dcsET");
}

function dcsFunc(func){
	if (typeof(window[func])=="function"){
		window[func]();
	}
}

// dcsMultiTrack is called with pairs of parameters as
// text strings that use the JavaScript variable to be
// overridded and the value to override.  For example,
// in an onClick function downloading a PDF:
// onClick="dcsMultiTrack('DCS.dcsuri','this.pdf')"

function dcsMultiTrack(){
	for (var i=0;i<arguments.length;i++){
		if (arguments[i].indexOf('WT.')==0){
				WT[arguments[i].substring(3)]=arguments[i+1];
				i++;
		}
		if (arguments[i].indexOf('DCS.')==0){
				DCS[arguments[i].substring(4)]=arguments[i+1];
				i++;
		}
		if (arguments[i].indexOf('DCSext.')==0){
				DCSext[arguments[i].substring(7)]=arguments[i+1];
				i++;
		}
	}
	var dCurrent=new Date();
	DCS.dcsdat=dCurrent.getTime();
	dcsTag();
}


//dcsVar sets default values for the following variables:
// URI Stem 		(DCS.dcsuri)
// URI Query String 	(DCS.dcsqry)
// Server 		(DCS.dcssip)
// Referring document 	(DCS.dcsref)
// Document Title 	(WT.ti)
// User Time Zone 	(WT.tz)
// User browsing hour 	(WT.bh)
// User language  	(WT.ul)
// Screen widthxheight 	(WT.sr)
// Screen color depth 	(WT.cd)
// Java capability 	(WT.jo)
// JavaScript capability (WT.js)
// JavaScript Version 	(WT.jv
//
function dcsVar(){
	var dCurrent=new Date();
	WT.tz=dCurrent.getTimezoneOffset()/60*-1;
	if (WT.tz==0){
		WT.tz="0";
	}
	WT.bh=dCurrent.getHours();
	WT.ul=navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
	if (typeof(screen)=="object"){
		WT.cd=navigator.appName=="Netscape"?screen.pixelDepth:screen.colorDepth;
		WT.sr=screen.width+"x"+screen.height;
	}
	if (typeof(navigator.javaEnabled())=="boolean"){
		WT.jo=navigator.javaEnabled()?"Yes":"No";
	}
	if (document.title){
		WT.ti=document.title;
	}
	WT.js="Yes";
	if (typeof(gVersion)!="undefined"){
		WT.jv=gVersion;
	}
	if (document.body&&document.body.addBehavior){
	    document.body.addBehavior("#default#clientCaps");
	    if (document.body.connectionType){
		WT.ct=document.body.connectionType;
	    }
	    document.body.addBehavior("#default#homePage");
	    WT.hp=document.body.isHomePage(location.href)?"1":"0";
	}
	if (parseInt(navigator.appVersion)>3){
	    if ((navigator.appName=="Microsoft Internet Explorer")&&document.body){
		WT.bs=document.body.offsetWidth+"x"+document.body.offsetHeight;
	    }
	    else if (navigator.appName=="Netscape"){
		WT.bs=window.innerWidth+"x"+window.innerHeight;
	    }
	}
	WT.fi="No";
	if (window.ActiveXObject){
	    var maxVer=10;
	    var minVer=2;
	    for (var ver=minVer;ver<=maxVer;ver++){
		try{
		    var oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+ver+"');");
		    if (oFlash){
			WT.fi="Yes";
			WT.fv=ver+".0";
			break;
		    }
		}
		catch(e){
		}
	    }
	}
	else if (navigator.plugins&&navigator.plugins.length){
	    for (var i=0;i<navigator.plugins.length;i++){
		if (navigator.plugins[i].name.indexOf('Shockwave Flash')!=-1){
		    WT.fi="Yes";
		    WT.fv=navigator.plugins[i].description.split(" ")[2];
		    break;
		}
	    }
	}
	DCS.dcsdat=dCurrent.getTime();
	DCS.dcssip=window.location.hostname;
	DCS.dcsuri=window.location.pathname;
	if (window.location.search){
		DCS.dcsqry=window.location.search;
		if (gQP.length>0){
		    for (var i=0;i<gQP.length;i++){
			var pos=DCS.dcsqry.indexOf(gQP[i]);
			if (pos!=-1){
			    var front=DCS.dcsqry.substring(0,pos);
			    var end=DCS.dcsqry.substring(pos+gQP[i].length,DCS.dcsqry.length);
			    DCS.dcsqry=front+end;
			}
		    }
		}
	}
	if ((window.document.referrer!="")&&(window.document.referrer!="-")){
		if (!(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)<4)){
			DCS.dcsref=window.document.referrer;
		}
	}
}

// This is an internal function to append tags to a query 
// string (including escaping invalid characters)

function A(N,V){
	return "&"+N+"="+dcsEscape(V);
}

// This function allows characters that would be invalid 
//in a query string to pass through and be reported on 
//at the destination.

function dcsEscape(S){
	if (typeof(RE)!="undefined"){
		var retStr = new String(S);
		for (R in RE){
			retStr = retStr.replace(RE[R],R);
		}
		return retStr;
	}
	else{
		return escape(S);
	}
}

// This function creates an image array and places the 
// parameter as the source value for the image.  Setting 
// the source value has the effect of loading the 
// image from the server, but it does not hold up 
// display of the page.
function dcsCreateImage(dcsSrc){
	if (document.images){
		gImages[gIndex]=new Image;
		gImages[gIndex].src=dcsSrc;
		gIndex++;
	}
	else{
                document.write('<img border="0" name="dcsimg" width="1" height="1" alt="" src="'+dcsSrc+'">');
      	}
}

// This function goes through all META tags on a page 
// and replaces WT, DCS and DCSext JavaScript variables 
// with the content of the META tags that match their names.
// With this function you do not have to include or 
// modify JavaScript to change parameters used by 
// WebTrends reporting.
function dcsMeta(){
	var myDocumentElements;
	if (document.all){
		myDocumentElements=document.all.tags("meta");
	}
	else if (document.documentElement){
	 myDocumentElements=document.getElementsByTagName("meta");
	}
	if (typeof(myDocumentElements)!="undefined"){
	   for (var i=1;i<=myDocumentElements.length;i++){
		myMeta=myDocumentElements.item(i-1);
		if (myMeta.name){
			if (myMeta.name.indexOf('WT.')==0){
			  WT[myMeta.name.substring(3)]=myMeta.content;
			} else if (myMeta.name.indexOf('DCSext.')==0){
				DCSext[myMeta.name.substring(7)	]
					=myMeta.content;
			}else if (myMeta.name.indexOf('DCS.')==0){
				DCS[myMeta.name.substring(4)]
					=myMeta.content;
			}
		}
	   }
	}
}

//This function calls the appending function to create the 
// tag and dcsCreateImage to send it to the server. 
function dcsTag(){
	var P="http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+gDomain+(gDcsId==""?'':'/'+gDcsId)+"/dcs.gif?";
	for (N in DCS){
		if (DCS[N]) {
			P+=A(N,DCS[N]);
		}
	}
	for (N in WT){
		if (WT[N]) {
			P+=A("WT."+N,WT[N]);
		}
	}
	for (N in DCSext){
		if (DCSext[N]) {
			P+=A(N,DCSext[N]);
		}
	}
	if (P.length>2048&&navigator.userAgent.indexOf('MSIE')>=0){
		P=P.substring(0,2040)+"&WT.tu=1";
	}
	dcsCreateImage(P);
}

// This function collects an HSBC cookie that otherwise would
// not be available for tracking  The desired cookie is 
// passed as a parameter and the cookie value returned.
function dcsGetHSBCCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

// This function collects all the HSBC variables and translates
// them into the appropriate WebTrends variabls (WT variables
// from HSBC.PAGE, DCSext variables from SITE and EXT, and 
// DCS variables from HSBC.LOG variables.  It also overrides
// the value of gDcsId from the HSBC.DCS.ID variable.

function dcsMapHSBC() {
	for (N in HSBC) {
		if (N == "SITE")  {
			for (S in HSBC.SITE) {
				DCSext[S] = HSBC.SITE[S];
			}
		}
		if (N == "PAGE") {
			for (S in HSBC.PAGE) {
				WT[S] = HSBC.PAGE[S];
			}
		}
		if (N == "EXT") {
			for (S in HSBC.EXT) {
				DCSext[S] = HSBC.EXT[S];
			}
		}
		if (N == "LOG") {
			for (S in HSBC.LOG) {
				DCS[S] = HSBC.LOG[S];
			}
		}
		if (N == "DCS") {
			gDcsId = HSBC.DCS.ID;
		}
	}
}




