function WebTrendsCustom() {
  var that=this;
  this.debug = false;
  this.cookieName = 'WT_MTP';
  this.cookieExpiration = 360;
  this.cookieDelimiter = ',';
  this.cookieLength = 2000;
  this.mtpMaxCount = 11;
  this.wtvalExpiration = 90;
  this.wtvalDelimiter = ';';
  this.mtpClickThrough = 0;
  this.spiderAgent = false;
}

WebTrendsCustom.prototype.dcsCollect=function() {
  this.spiderCheck();
  
  if (!this.spiderAgent) {
    _tag.dcsCollect();
  } else {
    _tag.dcsMultiTrack=function() {
      //do nothing
    }
  }
}

WebTrendsCustom.prototype.spiderCheck=function() {
	var spiderAgents = 
		[ 'GomezAgent'
		];

	for(var i=0; i < spiderAgents.length; i++) {
		if(navigator.userAgent.indexOf(spiderAgents[i]) != -1 ) {
			this.spiderAgent = true;
		}
	}
}

WebTrendsCustom.prototype.checkTouchPoint=function(name) {
  var touchPointValue = "";
  if (this.checkReferrer('.google.') && this.getUrlParam('q',document.referrer) != "")
    touchPointValue = 'mtp_search_organic';
  if (this.checkReferrer('.yahoo.') && this.getUrlParam('p',document.referrer) != "")
    touchPointValue = 'mtp_search_organic';
  if (this.checkReferrer('.ask.') && this.getUrlParam('q',document.referrer) != "")
    touchPointValue = 'mtp_search_organic';
  if (this.checkReferrer('.bing.') && this.getUrlParam('q',document.referrer) != "")
    touchPointValue = 'mtp_search_organic';
  if (document.referrer == "")
    touchPointValue = 'mtp_directtraffic';
  if (this.getUrlParam('WT.mc_id') != "")
    touchPointValue = this.getUrlParam('WT.mc_id');
  if (touchPointValue == "" && !this.checkReferrer(window.location.hostname.toLowerCase()))
    touchPointValue = "mtp_other";
  if (touchPointValue != "") {
    this.addMultiTouchPoint(touchPointValue);
    this.mtpClickThrough = 1;
  }
}

WebTrendsCustom.prototype.checkReferrer=function(value) {
  var regexS = "//.*?/";
  var regex = new RegExp(regexS);
  var results = regex.exec(document.referrer)
  if(results != null && results[0].indexOf(value) >= 0){
    return true;
  }else{
    return false;
  }
}  

WebTrendsCustom.prototype.getUrlParam=function(name,url) {
  if (url != undefined)
    var href = url;
  else
    var href = window.location.href;

  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(href);
  if( results == null)
    return "";
  else
    return results[1];
}

WebTrendsCustom.prototype.addMultiTouchPoint=function(value) {
  var date = new Date();
  var touchTime = date.getTime();
  var cookieValue = this.getCookieValues('original');
  var cookieValuesArray = cookieValue.split(this.cookieDelimiter);

  if (cookieValue.length > this.cookieLength - value.length - 15 || cookieValuesArray.length >= this.mtpMaxCount)
  {
    var cookieValuesArray = cookieValue.split(this.cookieDelimiter);
    var iLength = 0;
    var popCount = 0;

    for (var i=0;i < cookieValuesArray.length;i++)
    {
      iLength += cookieValuesArray[i].length + 1;
      if (iLength > this.cookieLength - value.length - 15 || i+1 >= this.mtpMaxCount) {
        popCount++
      }
    }
    cookieValuesArray.length = cookieValuesArray.length-popCount;

    cookieValue = cookieValuesArray.join(this.cookieDelimiter);
  }

  if (cookieValue == "" || cookieValue == null)
    cookieValue = touchTime + "|" + value;
  else
    cookieValue = touchTime + "|" + value + this.cookieDelimiter + cookieValue;

  this.createCookie(this.cookieName,cookieValue,this.cookieExpiration);  
}

WebTrendsCustom.prototype.getMTPct=function() {
  if (this.mtpClickThrough > 0) {
    return 1;
  } else {
    return null;
  }
}

WebTrendsCustom.prototype.getMTP=function(value,delimiter) {
  var cookieDelimiterRegExp = new RegExp(this.cookieDelimiter,"g");
  var multiTouchPointValues = this.getCookieValues();
  if (delimiter == null)
    delimiter = this.wtvalDelimiter;
  multiTouchPointValues = multiTouchPointValues.replace(cookieDelimiterRegExp,delimiter);
  var mtpArray = multiTouchPointValues.split(delimiter);

  if (multiTouchPointValues == "" || multiTouchPointValues == null) {
    return null;
  } else if (value == null) {
    return multiTouchPointValues;
  } else if (!isNaN(value) && value > 0 && typeof mtpArray[value-1] !== 'undefined' && mtpArray[value-1].length > 0) {
    return mtpArray[value-1];
  } else if (!isNaN(value) && value < 0 && typeof mtpArray[Math.abs(value)] !== 'undefined' && mtpArray[Math.abs(value)].length > 0) {
    var fieldRemoverRegExp = new RegExp('^(?:(?:"[^"\r\n]*"|[^'+delimiter+'\r\n]*)'+delimiter+'){'+Math.abs(value)+'}');
    return multiTouchPointValues.replace(fieldRemoverRegExp,"");
  } else {
    return null;
  }
}

WebTrendsCustom.prototype.getMTPt=function(value) {
  var date = new Date();
  var mtpTime = this.getCookieValues('time');

  var mtpTimeArray = mtpTime.split(this.cookieDelimiter);
  if (mtpTimeArray.length > 0)  
    return Math.round((date.getTime()-mtpTimeArray[mtpTimeArray.length-1])/24/60/60/1000);
  else
    return null;
}

WebTrendsCustom.prototype.countMTP=function(value) {
  var multiTouchPointValues = this.getCookieValues();
  var mtpArray = multiTouchPointValues.split(this.cookieDelimiter);
  var mtpArrayCount = mtpArray.length;
  var mtpacArray = new Array();

  if (multiTouchPointValues == "" || multiTouchPointValues == null) {
    return 0;
  } else if (value == null) {
    return mtpArrayCount;
  } else if (value == 'string' && mtpArrayCount > 0) {
    var mtpArrayCountString;
    for(var i=mtpArrayCount;i > 0;i--) {
      mtpacArray.push(i);
  	}
    return mtpacArray.join(this.wtvalDelimiter);
  } else {
    return null;
  }
}

WebTrendsCustom.prototype.getCookieValues=function(value) {
  var date = new Date();
  var cookieValues = this.readCookie(this.cookieName);
  if (cookieValues == "" || cookieValues == null)
    return "";
  var cookieValuesArray = cookieValues.split(this.cookieDelimiter);
  var mtpArray = new Array();
  var popCount = 0;

  for(var i=0;i < cookieValuesArray.length;i++) {
    var iArray = cookieValuesArray[i].split('|');
    if (date.getTime()-(this.wtvalExpiration*24*60*60*1000) > iArray[0]) {
      popCount++
    } else {
      if (this.debug)
        iArray[0] = iArray[0]-(10*24*60*60*1000);

      if (value == "original")
        mtpArray.push(iArray[0]+"|"+iArray[1]);
      else if (value == "time")
        mtpArray.push(iArray[0]);
      else
        mtpArray.push(iArray[1]);
    }
	}
  cookieValuesArray.length = cookieValuesArray.length-popCount;

  return mtpArray.join(this.cookieDelimiter);
}

WebTrendsCustom.prototype.createCookie=function(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else {
    var expires = "";
  }

  document.cookie = name+"="+value+expires+"; path=/";
}

WebTrendsCustom.prototype.readCookie=function(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;
}

