if (typeof Cobalt == "undefined") {	var Cobalt = {}; };
if (typeof Cobalt.partners == "undefined") { Cobalt.partners = {}; };
if (typeof Cobalt.partners.ADT == "undefined") { Cobalt.partners.ADT = {}; };

if (typeof Cobalt.partners.ADT.pixelTag == "undefined") {
	Cobalt.partners.ADT.pixelTag = {
		COOKIE_PAGEID: "ADT_PAGEID",
		COOKIE_CUSTID: "ADT_SITEID",
		COOKIE_REFER: "ADT_REFER",
		ADT_URL: "https://www.thesearchprogram.com/seo/site-track/trackingimage2007.php?",
		regADT: /ADTLead=true/,
		regCustId: /custID=(\d+)/,
		regPageId: /page=(\d+)/,
		
		pageView : function(label, pageId) {
      
			if (this.hasADTParam (window.location.search)) {
				this.clearCookies(label);
				
				var pageId = (pageId != null) ? pageId : this.getParam (window.location.search, this.regPageId);
				var cust = this.getParam (window.location.search, this.regCustId);
				var referrer = this.getReferrer()
				this.setCookies(label, 
						pageId, 
						cust, 
						this.getReferrer());
				this.firePixelTag(label, pageId, cust, referrer);
				
			} else if (this.hasADTParam(document.referrer) && 
					 	this.hasADTCookie(label)) {
        var pageId = (pageId != null) ? pageId : this.getCookie(label + this.COOKIE_PAGEID);
				this.firePixelTag(
						label, 
						pageId, 
						this.getCookie(label + this.COOKIE_CUSTID),
						this.getCookie(label + this.COOKIE_REFER)
				);
			}
			
			else { this.clearCookies(label); }
		}, 
		
		pageConversion : function(label, pageId) {
    
      var pageId = (pageId != null) ? pageId : this.getCookie(label + this.COOKIE_PAGEID);
    
			if (this.hasADTCookie(label)) {
				this.firePixelTag(
					label,
          pageId,
					this.getCookie(label + this.COOKIE_CUSTID),
					this.getCookie(label + this.COOKIE_REFER)
				);
			}
			
			this.clearCookies(label);
		},
		
		getReferrer : function () {
			return encodeURIComponent(this.getUrlOnly(document.referrer));
		},
		
		getCookie : function(key) {
			return ExternalInterface.cookie.get(key);
		},
		
		setCookies : function(key, pageId, custId, referrer) {
			ExternalInterface.cookie.create(key + this.COOKIE_PAGEID, pageId, 3);
			ExternalInterface.cookie.create(key + this.COOKIE_CUSTID, custId, 3);
			ExternalInterface.cookie.create(key + this.COOKIE_REFER, referrer, 3);
		},
		
		clearCookies : function(key) {
			ExternalInterface.cookie.erase(key + this.COOKIE_PAGEID);
			ExternalInterface.cookie.erase(key + this.COOKIE_CUSTID);
			ExternalInterface.cookie.erase(key + this.COOKIE_REFER);
		},
		
		hasCookie : function(key) {
			var cookie = this.getCookie(key);
			return (null != cookie && cookie.length > 0);
		},
		
		hasADTParam : function(url) {
			return this.regADT.test(url);
		},
		
		hasADTCookie : function(label) {
			return this.hasCookie(label + this.COOKIE_PAGEID);
		},
		
		getParam : function(string, regex){
			var values = regex.exec(string);
			if (null != values && values.length > 0)
				return values[1];
			else
				return "";
		},
		
		getUrlOnly : function (url) {
			var qpStart = url.search(/\?/);
			if (qpStart < 0){qpStart = Math.max(url.length - 1, 0);}
			return url.substring(0,qpStart);
		},
		
		firePixelTag : function (label, pageId, custId, referrer) {
			var pt = new Image(1, 1);
			// security check
			pageId = Number(pageId).toString();
			custId = Number(custId).toString();
			referrer = encodeURIComponent(decodeURIComponent(referrer));
			pt.style.display="none";
			pt.src = this.ADT_URL + "custID=" + custId + "&page=" + pageId + "&r=" + referrer + "&axel=" + Math.random() * 100000;
			// need this onload for consistent IE 6 performance, inspired by http://breckman.com/entry.php?id=30
			pt.onload = function() { pt.onload = null; }
			document.body.appendChild(pt);
		}
	};
};