<!--
/*  Library to track users/clients for site statistics
 *  (c) 2006 Horizon Services Group
 * Author: Joel D'Souza
 * jdsouza@horizonlines.com
 * Last Updated: Dec 13, 2006
 * Prerequisites: Utility.js, Cookie.js
 * <script id="P" type="text/javascript" src="scripts/Utility.js" debug="false"></script>
 * <script id="PCookie" type="text/javascript" src="scripts/Cookie.js" debug="false"></script>
 * <script id="S" type="text/javascript" src="scripts/connectspeed.js"></script>
/*--------------------------------------------------------------------------*/
var m_gifpath = 'http://www.horizonservicesgroup.com/hstObjects/wt/'

var hsTracker = {
	Version: '1.0.0',
	gifpath: P.Get(P.GetScriptAttribute('Track', 'url')),
	levels: ["none","basic","full"], 
	trackingEnabled: true,
	Debug: false,
	guid : null,
	startDate : null,
	emptyFunction: function() { return;},
	handleError: function() {},
	require: function(libraryName) {
		// to add any dependent libraries
		document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
	},
	//add functions to run on the page load event
	addLoadEvent: function(func) {
		  //get any existing onload functions
		  var oldonload = window.onload;
		  if (typeof window.onload != 'function') {
		    window.onload = func;
		  } else {
		    window.onload = function() {
		      if (oldonload) {
		        oldonload();
		      }
		      func();
		    }
		}
	}, 
	getNowDateTime: function() {
		var dt = new Date();
		var month = dt.getMonth() + 1;
		var day = dt.getDate();
		var year = dt.getFullYear();
		var hours = dt.getHours();
		var minutes = dt.getMinutes();
		var seconds = dt.getSeconds();
		var daynight = '';
		if (minutes<10) {
			minutes = '0' + minutes;
		}
		if (hours > 11) {
			daynight = 'PM';
		} else {
			daynight = 'AM';
		}
		var currentTime = month + '/' + day + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds + ' ' + daynight;
		return currentTime;
		
	},
	//main method to log stats
	loginfo: function(level) {
		var qstring;
		var browser = navigator.appName;
		var b_version = navigator.appVersion;
		var version = parseFloat(b_version);
		var b_codename = navigator.appCodeName;
		var b_platform = navigator.platform;
		var b_lang = navigator.browserLanguage;	
		var b_width = window.screen.width;
		var b_height = window.screen.height;
		var timeStamp = this.getNowDateTime();
		this.startDate = timeStamp;
		
		//alert('level = ' + level)
		
		if (level != 'none') {
		    var ijAppId = P.Get(P.GetScriptAttribute('Track', 'AppId'))
		    var ijScreenId = P.Get(P.GetScriptAttribute('Track', 'ScreenId'))
		    var jsActionType = P.Get(P.GetScriptAttribute('Track', 'ActionType'))
		    var ijPriorityId = P.Get(P.GetScriptAttribute('Track', 'PriorityId'), '3')
		    var jsKey = P.Get(P.GetScriptAttribute('Track', 'Key'))
		    var jsLog = P.Get(P.GetScriptAttribute('Track', 'Log'))
		    
			qstring = "AppId=" + ijAppId + "&ScreenId=" + ijScreenId + "&ActionType=" + jsActionType
			        + "&UserId=" + this.guid + "&url=" + self.location.href.replace('&', '$')
			        + "&&StartDate=" + timeStamp + "&EndDate=" + timeStamp
					+ "&ScreenResolution=" + b_width + " * " + b_height
			        + "&PriorityId=" + ijPriorityId + "&Key=" + jsKey + "&Log=" + jsLog;
			//IsException=0&TechnicalProblemId=2
		} 
		
		//alert('qstring = ' + qstring)
		var im =new Image(1,1);
		this.gifpath += "wtAnon.gif"
		im.src = this.gifpath + "?" + qstring;
		
		im.onload = function() { this.emptyFunction(); } 
		
		if (this.Debug) { prompt('Debug #1 = ' + this.Debug + '\nim.src: ', im.src) } 
		return;
	},
	IsValidLevel: function(level) {
	    for(var i=0; i < this.levels.length; i++) {
	        if(level == this.levels[i]) {
	            return true;
	        }
	    }
	    return false;
	}, 
	//initializes the tracker
    initialize: function() {
        this.Debug = P.Get(P.GetScriptAttribute('Track', 'debug'))
        //alert('this.Debug = ' + this.Debug)
        var level = (P.Get(P.GetScriptAttribute('Track', 'level'), 'full') == 'full' ) ? 'full' : 'none'
        
		//check tracking level
		if (!this.IsValidLevel(level)) {
			alert('Please enter correct tracking level.\n Tracking levels: none, basic, full');
			return;
		}
		
		//if level = none, disable tracking
		if (level == 'none') {
			this.trackingEnabled = false;
			return;
		}
	    
		if (this.trackingEnabled) {
			
			//swallow errors
			window.onerror = this.handleError;
						
			//check if Utility.js has been included in the page
			if (document.getElementById('P') == undefined) {
				alert("hsTracker requires the Utility.js file");
				return;
			}
			
			//check if Cookie.js has been included in the page
			if (document.getElementById('PCookie') == undefined) {
				alert("hsTracker requires the Cookie.js file");
				return;
			}
			
			//script attribute AppId is required.
			if (P.Get(P.GetScriptAttribute('Track', 'AppId')) == '') {
				alert("hsTracker requires the script attribute 'AppId'.");
				return;
			}
			//script attribute url is required.
			if (this.gifpath == '') {
				this.gifpath += m_gifpath 
				//alert("hsTracker requires the script attribute 'url'.");
				//return;
			}
			
			if (PCookie.Get('UserId') == null) {
				var dtExpiration = new Date();
				//cookie expires after 10 years
				dtExpiration.setTime(dtExpiration.getTime() + 10*365*24*60*60*1000);
				PCookie.ExpirationDate = dtExpiration.toGMTString();
				this.guid = P.NewGuid();
				PCookie.Set('UserId',this.guid);
			} else {
				var guidfromcookie = PCookie.Get('UserId');
				if (P.isGuid(guidfromcookie)) {
					this.guid = guidfromcookie;					
				}
				else {
					this.guid = P.NewGuid();
					PCookie.Set('UserId',this.guid);
				}			
			}
			//draw image for speed test
			if (level =='basic' || level =='full') {
			    var jsDownloadUrl = this.gifpath + 'kirchner.jpg'
				drawCSImageTag(jsDownloadUrl, 47608, 'border=2 height=120');
			}
		    
			//alert('this.gifpath (3) = ' + this.gifpath)
			//window.setTimeout("hsTracker.loginfo('"+guid+"','"+level+"')",2000);     //change: this should be setInterval
			this.loginfo(level);
			//window.status = 'gp = ' + this.gifpath
		}
	}
};

//alert('a = ' + P.Get(P.GetScriptAttribute('Track', 'debug')))
//alert('debug #1 = ' + hsTracker.Debug)
hsTracker.initialize(); 

<!-- //Start: Connection Speed.
function drawCSImageTag( fileLocation, fileSize, imgTagProperties ) {       //1st
	// This function draws the image tag required to run this process.
	// It needs to be passed:
	//     1.  (String)   The location of the file to be loaded
	//     2.  (Integer)  The size of the image file in bytes
	//     3.  (String)   The tag properties to be included in the <img> tag
	// Place a call to this function inside the <body> of your file
	// in place of a static <img> tag.
	
	start = (new Date()).getTime();     // Record Start time of <img> load.
	loc = fileLocation + '?t=' + escape(start); // Append the Start time to the image url to ensure the image is not in disk cache.
	
	// Write out the <img> tag.
	document.write('<img style="display:none" src="' + loc + '" ' + imgTagProperties + ' onload="computeConnectionSpeed(' + start + ',' + fileSize + ');">');
	return;
}

function computeConnectionSpeed( start, fileSize ) {        //2nd
	// This function returns the speed in kbps of the user's connection,
	// based upon the loading of a single image.  It is called via onload 
	// by the image drawn by drawCSImageTag() and is not meant to be called
	// in any other way.  You shouldn't ever need to call it explicitly.
    
	end = (new Date()).getTime();
	connectSpeed = (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);
	//alert('connspeed:' + connectSpeed);
	var qstring;
	qstring =  "UserId=" + hsTracker.guid + "&StartDate=" + hsTracker.startDate + "&ConnectionSpeedinKBytes=" + Math.round(connectSpeed) 
					
	var im =new Image(1,1);
	im.src = hsTracker.gifpath + "?" + qstring;	
	
	im.onload = function() { hsTracker.emptyFunction(); }
	return true;
}
//End: Connection Speed -->

