// First party cookie implementation for use on a site not using a WebTrends Analytics JS Tag.
// Apply this javascript to all page on the site (will work on just landing pages).
// Change @@CUSTID@@ in the last line to reflect the correct account number.

function DcsDynamicSearch(cid){
	// private members
	var cname="WT_DSAI";
	var wt=(typeof(WT)=="object")?WT:{};
	// Set to true to allow cross-domain tracking
	var trackOffsite = true;
	// Comma separated list of domains to pass the cookie value to.				
	var offsiteDoms = "shop3m.com,3m.com";						
	
	// set client ID variable name
	var clientname="WT_DSCI";


	// private methods
	function setAdInfo(){
		var value=getQueryParam("cshift_ck");
		if (value.length>0){
			var cur=new Date();
			var exp=new Date(cur.getTime()+315360000000);
			document.cookie=cname+"="+value+"; expires="+exp.toGMTString()+"; path=/;domain=.3m.com";
		}
	}
	
	// function to set the client ID as a cookie if client_id parameter exists on the URL
	function setClientInfo(){
		if (getQueryParam("client_id")) 
	  {
			var value = getQueryParam("client_id");
		}
		else {
			var value = cid;
		}

			var cur=new Date();
			var exp=new Date(cur.getTime()+315360000000);
			document.cookie=clientname+"="+value+"; expires="+exp.toGMTString()+"; path=/";

	}
	
	// function to check for client ID cookie
	function getclient() {
		if (dcsGetCookie(clientname)) 
	 {
			return dcsGetCookie(clientname);
		}
		else {
			return cid;
		}
	}
	
	function getQueryParam(key){
		var qry=window.location.search.substring(1);
		if (qry.length>0){
			var params=qry.split("&");
			var count=params.length;
			for (var i=0;i<count;i++){
				var p=params[i].split("=");
				if (p[0]==key){
					return p[1];
				}
			}
		}
		return "";
	}

	function dcsGetCookie(name){
		var cookies=document.cookie.split("; ");
		var cmatch=[];
		var idx=0;
		var i=0;
		var namelen=name.length;
		var clen=cookies.length;
		for (i=0;i<clen;i++){
			var c=cookies[i];
			if ((c.substring(0,namelen+1))==(name+"=")){
				cmatch[idx++]=c;
			}
		}
		var cmatchCount=cmatch.length;
		if (cmatchCount>0){
			idx=0;
			if ((cmatchCount>1)&&(name==dcsInit.fpc)){
				var dLatest=new Date(0);
				for (i=0;i<cmatchCount;i++){
					var lv=parseInt(dcsGetCrumb(cmatch[i],"lv"));
					var dLst=new Date(lv);
					if (dLst>dLatest){
						dLatest.setTime(dLst.getTime());
						idx=i;
					}
				}
			}
			return unescape(cmatch[idx].substring(namelen+1));
		}	
		else{
			return null;
		}
	}
	
	function dcsGetCrumb(cval,crumb){
		var aCookie=cval.split(":");
		for (var i=0;i<aCookie.length;i++){
			var aCrumb=aCookie[i].split("=");
			if (crumb==aCrumb[0]){
				return aCrumb[1];
			}
		}
		return null;
	}


	function dcsBind(event,func){
		if ((typeof(func)=="function")&&document.body){
			if (document.body.addEventListener){
				document.body.addEventListener(event, func, true);
			}
			else if(document.body.attachEvent){
				document.body.attachEvent("on"+event, func);
			}
		}
	}

	function AdOffsiteWatcher(evt)
	{
		// obtain the event for both IE and others
		evt=evt||(window.event||"");
		var e=evt.target||evt.srcElement;
		while (e.tagName&&(e.tagName.toLowerCase()!="a")){
			e=e.parentElement||e.parentNode;
		}
		if (AdIsTrackable(e))
		{
			e.href += ((e.href.indexOf("?")== -1)?"?":"&") + "cshift_ck=" + dcsGetCookie(cname) + "&client_id=" + getclient();
		}
	}

	function AdIsTrackable(e)
	{
		var offsite=offsiteDoms.split(",");
			if ((offsite.length > 0)&&(e.href)&&(e.href.toLowerCase().indexOf(document.location.hostname.toLowerCase())==-1))
			{
				for (x=0; x < offsite.length; x++)
				{
					if (e.href.toLowerCase().indexOf(offsite[x].toLowerCase()) != -1)
					{
						return true;
					}
				}
			}
			else
				return false;
	}

	// privileged methods
	this.getClientId=function(){
		if (dcsGetCookie(clientname)) 
	 {
			return dcsGetCookie(clientname);
		}
		else {
			return cid;
		}
	};
	this.getAdInfo=function(){
		return dcsGetCookie(cname)||"";
	};
	this.getIsNew=function(){
		return 1;
	};
	// read query parameter, set cookie
	setAdInfo();
	
	// set cookie containing client id
	setClientInfo();

	if ((trackOffsite) && (offsiteDoms.length > 0))
	{
		var ev=(navigator.appVersion.indexOf("MSIE")!=-1)?"click":"mousedown";
		dcsBind(ev,AdOffsiteWatcher);
	}
}

// set client ID for 3M.com client
var dcsDS=new DcsDynamicSearch("752awr7882");

