/*
 * Intimis Tracker
 *
 * This script adds an img tag to the end of the document.  The URL of this image is actually a web page on
 * the tracking website.  If the page including this script has a querystring that will be passed on to the
 * page on the tracking site.  Either way, the path to the page including this script is passed to the tracking 
 * site.  The tracking site can then use the querystring if available to determine the ContactId, if that was
 * included, as well as the calling page name.
 */

var IntimisOnlineTracker = {
	about: "IntimisOnline Tracker",
	version: "1.0",
	init: function()
	{
	    //Set the parameters for the img tag to be added
		var _width = 1;
		var _height = 1;
		var _alt = "IntimisOnline tracking image";
		var _source = "https://www.yourrac.co.uk/__visited.aspx"; // This should be changed to the URL of the page on the tracking site
		var _style = "width:0px;height:0px;overflow:hidden;visibility:hidden;";
		//Request URL of page
		var _href = location.href;
		
		var _querystring = "";//location.search;
		if (_querystring.indexOf("?") == -1)
		{
		    _querystring = "?";
		}
		else
		{
		    _querystring += "&";
		}
		//Orignal
		//_querystring += "IntimisPath=" + location.pathname;
		
		//Remove UniqueId variable, IntimisId1 variable and append Path
		var _uniqueidIndex = _href.indexOf ("?IntimisId1=");
		var _intimisid1Index = _href.indexOf ("?UID=");
		if( _uniqueidIndex > 0)
		{
			_querystring += "IntimisPath=" + _href.substring(0, _uniqueidIndex);
		}
		else if( _intimisid1Index > 0)
		{
			_querystring += "IntimisPath=" + _href.substring(0, _intimisid1Index);
		}
		else
		{
			_querystring += "IntimisPath=" + _href;
		}
				
		//pick IntimisId1 
		if(_intimisid1Index > 0)
		{
			
			var _urlParts = _href.split("?UID=");
			
			if(_urlParts.length > 0 )
			{
				if(_urlParts[1].indexOf("&") > 0)
				{
					_querystring += "&UID=" + _urlParts[1].substring(0, _urlParts[1].indexOf("&"));
				}
				else
				{
					_querystring += "&UID=" + _urlParts[1].substring(0, _urlParts[1].indexOf("/"));
				}
				createCookie("jsIntimisId1", _urlParts[1].replace('/',''),null);
			}
		}
		else //UID is not in URL - pick for Cookie
		{
			var _jsIntimisId1 = readCookie("jsIntimisId1");
			if (_jsIntimisId1 != null)
			{
				_querystring += "&UID=" + _jsIntimisId1;
			}
		}
		//alert(_querystring);  //QueryString
		//alert(readCookie("IntimisContactId")); //Cookies of IntimisContactID from codebehind
		//alert(readCookie("jsIntimisId1"));	//Cookie from javascript
		
		//is url valid
		if (_source != "" && _source != null)
		{
			//does browser support DOM scripting?
			if (document.getElementById)
			{
				//find the container
				//var _con = document.getElementById(container);
				var _con = document.body;
				
				var _bstr = document.getElementsByTagName('body')[0].innerHTML;
								
				if (_con != null)
				{
					//create a new <image> element
					var _img = document.createElement('img');
					
					_img.setAttribute('alt', _alt);
					_img.setAttribute('src', _source + _querystring);
					_img.setAttribute('width', _width);
					_img.setAttribute('height', _height);
					_img.setAttribute('style', _style);
					
					//attach <img> to container
					_con.appendChild(_img);
				}			
			}
			//does browser support document.images?
			else if (document.images)
			{
				var _img = new Image();
				_img.src = _source + _querystring;
			}
			//does browser support document.write?
			else if (document.write)
			{
				document.open();
				document.write("<img src=\"" + _source + _querystring + "\" alt=\"" + _alt + "\" width=\"" + _width + "\" height=\"" + _height + "\" style=\"" + _style + "\" />");
				document.close();
			}
		}

        //If UID cookie exists (meaning visitor is an Intimis contact)
	_jsIntimisId1 = readCookie("jsIntimisId1");
	}
	
}
// Attach to the page load or onload event
if (window.addEventListener)
{
	window.addEventListener('load', IntimisOnlineTracker.init, false); 
} 
else if (window.attachEvent) 
{ 
	window.attachEvent('onload', IntimisOnlineTracker.init);
}

function readCookie(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;
}

function createCookie(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=/";
}

