var debug_tracker = false;
var current_url = location.href;
var url_parts = current_url.split('?');
var url_params = Array();
if(url_parts.length > 1) {
	url_params = url_parts[1].split('&');
}
for(var upc=0; upc<url_params.length; upc++)
{
	if(url_params[upc] == 'debug_tracker') debug_tracker = true;	
} 
url_parts = url_parts[0].split('/');
var url_domain = url_parts[0]+'/'+url_parts[1]+'/'+url_parts[2];
var folders_deep = url_parts.length-4; 
var reference_to_root = '';
var prepend_myself = url_domain+'/';
var prepend_one_up = url_domain+'/';
for(var fdc=0; fdc<folders_deep; fdc++) 
{
	reference_to_root += '../';
	prepend_myself += url_parts[(fdc+3)]+'/';
	if(fdc != (folders_deep-1)) prepend_one_up += url_parts[(fdc+3)]+'/';
}
var tractor_link_url = reference_to_root+'tractor3_link.php';
var prepend_domain = url_domain;
if(debug_tracker)
{ 
	var debug_rep = 'Tractor Active.';
	debug_rep += '\nMyself: '+prepend_myself;
	debug_rep += '\nDomain: '+prepend_domain;
	debug_rep += '\nLink: '+tractor_link_url;
	debug_rep += '\nURL: '+current_url; 
	alert(debug_rep);
}
function str_replace(str, from, to)
{
	var ntc = str.replace(from, to);
	while(ntc != str)
	{
		str = ntc;
		ntc = str.replace(from, to);
	}
	return ntc;
}//end of change
function starts_with(str, check)
{
	var st = (str.substring(0, check.length) == check);	
	return st;
}//end of change
function trim(tc)
{
	while(tc.substring(0,1) == ' ') tc = tc.substring(1, tc.length);
	while(tc.substring(tc.length-1,1) == ' ') tc = tc.substring(0, tc.length-1);
	return tc;
}
function check_for_iframe_source_change(tc, href_came_from, MM_openBrWindow_exists, debug_this)
{
	//we need to check for the change of src like: 
	//document.getElementById('downframe').src = 'tractor.php?asset=112'
	var is_iframe_change = false;
	ntc = tc.split('.src');
	if(ntc.length < 2) ntc = tc.split('.SRC');
	if(ntc.length == 2)
	{
		//SRC CHANGE
		ntc = trim(ntc[1]);										
	
		if(ntc.substring(0,1)=='=') 
		{
			ntc = trim(ntc.substring(1, ntc.length));
			ntc = str_replace(ntc, "'", '"');			//singles to doubles
			if(ntc.substring(0,1)=='"') ntc = ntc.substring(1, ntc.length);
			if(ntc.substr(ntc.length-1,1)=='"') ntc = ntc.substring(0, ntc.length-1);
			var nntc = format_tracker_url(ntc, 'Iframe Source Change', href_came_from);
			nntc = tc.replace(ntc, nntc);
			var wo = 'window.open';
			if(MM_openBrWindow_exists) nntc = nntc.replace(wo, 'MM_openBrWindow');
			if(debug_this) alert('Executing:\n'+nntc);
			
			eval(nntc);
			is_iframe_change = true;
		}										
	}	
	return is_iframe_change;			
}//end of function
function get_href(target)
{
	var tref = '';
	if(target && target.getAttribute) tref = target.getAttribute('href');
	if(tref == null) tref = '';
	else tref = trim(tref);
	return tref;
}
function get_onclick(target)
{
	var temp_click_action = '';
	if(target && target.getAttribute) temp_click_action = target.getAttribute('onClick');
	if(temp_click_action == null) temp_click_action = '';
	temp_click_action = temp_click_action.toString();				
	if(temp_click_action.length == 0) {
		if(target && target.getAttribute) temp_click_action = target.getAttribute('ONCLICK');
		if(temp_click_action == null) temp_click_action = '';
		temp_click_action = temp_click_action.toString();
	}				
	temp_click_action = trim(temp_click_action);	
	return temp_click_action;				
}
function format_tracker_url(link, link_description, came_from)
{
	var prepend_link_if_local = prepend_myself;
	var prepend_link_if_one_up = prepend_one_up;
	var prepend_link_if_absolute = prepend_domain;				
	
	if(link.substring(0,3)=='../') {
		link = prepend_link_if_one_up+link.substring(3,link.length);
	}
	else if(link.substring(0,1) == '/') {
		link = prepend_link_if_absolute+link;
	}
	else if(link.substring(0,4) != 'http') {
		link = prepend_link_if_local+link;
	}				
	
	var str = tractor_link_url+'?link=' + escape(link) + '&link_description=' + escape(link_description);
	str += '&came_from=' + escape(came_from);
	return str;
}//end of format_tracker_url
if (document.captureEvents) {
	//Netscape needs the click or it double launches
	document.onclick = click_handler;
	document.captureEvents(Event.CLICK);
}
else {
	//IE needs the mouse up action so we can intercept the click
	document.onmouseup = click_handler;
}							
function click_handler(e)
{					
	//if the anylink menu system is installed on the page, let's perform its 
	// hidemenu action because we're just nice like that.
	if(typeof(hidemenu_onclick) != 'undefined' && hidemenu_onclick && hidemenu_onclick=="yes")
	{
		if(typeof(hidemenu) == 'function')
		{
			//alert('hidemenu()');
			hidemenu(e);
		}
	}	
	var cancel_event = false;		//whether we allow the click to proceed
	var target;						//the target HTML element
	var href_target = '';			//the target URL
	var href_description = '';		//target HTML element description
	var href_new_win = '';			//target new window name
	var href_window_params = '';	//window.open parameters
	var href_came_from = document.location.href;
	var debug_this = debug_tracker;
	var MM_openBrWindow_exists = false;
	if(typeof(MM_openBrWindow) == 'function') MM_openBrWindow_exists = true;				
			
	if(!e) e = window.event;			
	if(e.button && e.button == 2) return true;		//oh, it's a right click, never mind, as you were...	
					
	if(e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	
	//Safari bug: the text, not the link, is the target
	if(target.nodeType == 3) target = target.parentNode;	
	
	if(!target) return false;				
													
	if(typeof(str_replace) != 'function') return true;			//huh?  where's our function?
	else if(typeof(get_href) != 'function') return true;		//huh?  where's our other function?
	else if(debug_this) alert(typeof(str_replace)+','+typeof(get_href));				
	
	//lets parse up until we find something with either an href or a onClick
	var tref = get_href(target);
	if(target.nodeName.toUpperCase() == 'IMG') tref = '';
	var temp_click_action = get_onclick(target);
	
	if(debug_this) alert('started...');
									
	while(tref.length == 0 && temp_click_action.length == 0 && target)
	{
		target = target.parentNode;
		if(target) tref = get_href(target);
		if(!target || !target.nodeName || target.nodeName.toUpperCase() == 'IMG') tref = '';
		temp_click_action = get_onclick(target);
	}
	
	if(debug_this) alert('found...');
	
	if(debug_this) {
		if(target) alert(target.nodeName);
		else alert('no target found.');
	}
											
	//-- Determine the target URL
	if(tref && tref.length > 1 && tref != href_came_from && tref != (href_came_from+'#') 
		&& tref.substring(0,10).toLowerCase() != 'javascript' 
		&& tref.substring(0,6).toLowerCase() != 'mailto' 
		&& tref.substring(0,1).toLowerCase() != '?' 
		&& tref.substring(0,1).toLowerCase() != '#' 					
		) 
	{
		if(temp_click_action.length == 0)
		{
			href_target = tref;
			href_description = target.innerHTML;				
			if(target.target && target.target.length > 0) href_new_win = target.target;
			if(debug_this)
			{
				alert('Report\nTarget: '+escape(href_target)+'\nDescription: '+escape(href_description));
			}
		}
		else if(debug_this) alert('Cancelled because of attached onclick.');
	}			
	else {

		//deal with endlines and indentation
		temp_click_action = escape(temp_click_action);
		temp_click_action = str_replace(temp_click_action, '%0D', ' ');
		temp_click_action = str_replace(temp_click_action, '%0A', ' ');
		temp_click_action = str_replace(temp_click_action, '%09', ' ');
		temp_click_action = unescape(temp_click_action);
		temp_click_action = trim(temp_click_action);
		
		//deal with IE's anonymous function method		
		var ie_anon_start = 'function anonymous()';			
		if(starts_with(temp_click_action, ie_anon_start))
		{
			temp_click_action = trim(temp_click_action.substring(ie_anon_start.length+2, temp_click_action.length-1));						
		}					
		
		//deal with our specific legacy base
		temp_click_action = temp_click_action.replace(/MM_openBrWindow/,'window.open');
							
		var can_track = true;
		if(temp_click_action.length > 0)
		{
			if(debug_this) alert('Click Action Found: '+(temp_click_action));
		
			temp_click_action = temp_click_action.split(';');
			var ntc = '';
			var rep = 'javascript:';
			
			for(var tcac=0; tcac<temp_click_action.length; tcac++)
			{							
				var tc = trim(temp_click_action[tcac]);							
				tc = trim(str_replace(tc, rep, ''));
				var first_tc = tc.substring(0,13);
				if(debug_this)
				{
					alert('first: '+first_tc);
				}
				var refers_parent = ((first_tc.toLowerCase()=='window.parent')
														||(first_tc.toLowerCase()=='parent.window'));
				
				if(refers_parent)
				{
					if(debug_this) alert('skipped, refers to parent window');
					can_track = false;
					tc = '';
				}
				if(tc.length > 1 && tc.toLowerCase() != 'return false' && tc.toLowerCase() != 'return true')
				{
					if(starts_with(tc, 'window.open'))
					{									
						//WINDOW.OPEN
						tc = tc.substring(12, tc.length-1);
						ntc = str_replace(tc, "'", '"');			//singles to doubles
						ntc = ntc.split('"');
						for(var ntcc=0; ntcc<=5; ntcc++)
						{
							if(ntc.length < (ntcc+1)) ntc[ntcc] = '';
						}
						tc = Array();									
						href_target = tc[0] = ntc[1];
						if(ntc[3].length == 0) ntc[3] = '_blank';
						href_new_win = tc[1] = ntc[3];
						href_window_params = tc[2] = ntc[5];
						href_description = target.innerHTML;
						if(debug_this) alert('Window open configured:\n'+escape(href_target));
					}
					else {									
						if(debug_this) alert(tc);
						var is_iframe_change = check_for_iframe_source_change(tc, 
							href_came_from, MM_openBrWindow_exists,
							debug_this);
																								
						if(!is_iframe_change) 
						{										
							//the dealt-with ways that the document location could be changed:
							//- location='blarg.php'
							//- document.location.href='blarg.php'
							ntc = tc.split('location');
							if(ntc.length != 2) ntc = tc.split('LOCATION');
							if(ntc.length == 2 && trim(ntc[1]).substring(0,1) == '=')
							{
								//LOCATION	
								ntc = trim(ntc[1]);
								ntc = ntc.substring(1, ntc.length);
								ntc = str_replace(ntc, "'", '"');			//singles to doubles
								ntc = ntc.split('"');
								ntc = ntc[1];
								if(debug_this) alert('Location Change JS: ' + ntc);
								href_target = ntc;
							}
							else {
								ntc = tc.split('.href');
								if(ntc.length != 2) ntc = tc.split('.HREF');
								if(debug_this) alert('first six: '+trim(ntc[0]).substring(0,6));
								
								if(ntc.length == 2 && trim(ntc[1]).substring(0,1) == '=')
								{
									///DOCUMENT.LOCATION.HREF or LOCATION.HREF
									ntc = trim(ntc[1]);
									ntc = ntc.substring(1, ntc.length);
									ntc = str_replace(ntc, "'", '"');			//singles to doubles
									ntc = ntc.split('"');
									ntc = ntc[1];
									if(debug_this) alert('Location Change JS: ' + ntc);
									href_target = ntc;
								}
								else {
									//DON'T KNOW WHAT IT IS										
									if(debug_this) alert('Unknown JS: ' + (tc));
									can_track = false;
								}
							}
						}
						else cancel_event = true;
					}
				}
			}//end of for
		}//end of if
		if(!can_track) {
			href_target = '';					//means unknown javascript was found.
			cancel_event = false;
		}
	}				
	if(href_target.length > 0)
	{					
		href_target = format_tracker_url(href_target, href_description, href_came_from);
		if(debug_this) alert('tracker link: ' + href_target);					
		cancel_event = true;
		if(href_new_win.length == 0) document.location.href = href_target;
		else if(MM_openBrWindow_exists) {
			if(debug_this) alert('using MM_openBrWindow to open popup...');
			MM_openBrWindow(href_target, href_new_win, href_window_params);
		}
		else window.open(href_target, href_new_win, href_window_params);
	}//end of if we could find the target URL
	if(cancel_event)
	{
		if(debug_this) alert('event bubble cancelled');
		e.cancelBubble = true;
		if(e.stopPropagation) e.stopPropagation();
		//e.returnValue = false;
	}//end of if cancel event
	return !cancel_event;			
}//end of function click_handler

//these are only invoked once per session
function matrix_tracker()
{
	var tprof = tracker_javascript_profiler();
	var tprofs = "";
	for(var tpc=0; tpc<tprof.length; tpc++)
	{
		tprofs += tprof[tpc]+"\\n";
	}
	tprofs = tractor_link_url + "?beacon_report=" + escape(tprofs);
	document.getElementById("tracker_beacon").innerHTML += "<img src=\""+tprofs+"\" alt=\"\" /" + ">";
	if(debug_tracker)
	{
		document.write("<textarea>"+tprofs+"<"+"/textarea>");
	}			
}

function tracker_javascript_profiler()
{
	var profile = Array();
	profile[profile.length] = '[appCodeName] '+navigator.appCodeName;
	profile[profile.length] = '[appName] '+navigator.appName;
	profile[profile.length] = '[appVersion] '+navigator.appVersion; 
	profile[profile.length] = '[appMinorVersion] '+navigator.appMinorVersion; 
	profile[profile.length] = '[cookieEnabled] '+navigator.cookieEnabled;
	if(navigator.language) profile[profile.length] = '[language] '+navigator.language;
	else if(navigator.userLanguage) profile[profile.length] = '[language] '+navigator.userLanguage;
	profile[profile.length] = '[platform] '+navigator.platform;
	profile[profile.length] = '[userAgent] '+navigator.userAgent;
	if(navigator.cpuClass) profile[profile.length] = '[cpuClass] '+navigator.cpuClass;
	profile[profile.length] = '[cookieEnabled] '+navigator.cookieEnabled;
	profile[profile.length] = '[javaEnabled] '+navigator.javaEnabled();
	//not IE...
	var tp = '';
	var has_tp = false;
	if(navigator.plugins && navigator.plugins.length > 0)
	{
		for(var pc=0; pc<navigator.plugins.length; pc++)
		{
			tp = '[plugin] '+navigator.plugins[pc].name+' - '+navigator.plugins[pc].description;
			has_tp = false;
			for(var prc=0; prc<profile.length; prc++)
			{
				if(profile[prc] == tp) has_tp = true;
			}
			if(!has_tp) profile[profile.length] = tp;	
		}	  
	}
	if (window.ActiveXObject) {
		//disabling PDF because it seems to cause the Adobe application itself to load in Vista
		var activexes = Array(
			Array('ShockwaveFlash.ShockwaveFlash', 'Shockwave Flash'),
			//Array('PDF.PdfCtrl', 'Adobe Acrobat'), 
			//Array('AcroPDF.PDF', 'Adobe Acrobat'),
			Array('WMPlayer.OCX', 'Windows Media Player'),
			Array('SWCtl.SWCtl', 'Shockwave Director'),
			Array('QuickTime.QuickTime', 'QuickTime')
		);
		for(var axc=0; axc<activexes.length; axc++)
		{
			var f = '';
			var f1;
			for (var ii=20;ii>=1;ii--) {
				try {
					var f1=eval("new ActiveXObject('"+activexes[axc][0]+"."+ii+"');");
					if (f1) { f=ii + '.0'; break; }
				}
				catch(e) {}
			}
			if(f.length > 0) {
				if(activexes[axc][0] == 'PDF.PdfCtrl' && f == 1) f = 4;
				else if(activexes[axc][0] == 'AcroPDF.PDF') f = ' >= 7';
				else if(activexes[axc][0] == 'WMPlayer.OCX') f = document.getElementById('Player').versionInfo;
				else if(activexes[axc][0] == 'QuickTime.QuickTime')
				{
					if(f >= 5) f = ' >= 7.1.1';
					else if(f == 4) f = ' 6.3 - 7.1';
					else if(f == 3) f = ' 6.0.2 - 6.1';
					else if(f == 2) f = ' 5.0.6 - 6.0.1';
					else if(f == 1) f = ' 5.0.2 - 5.0.5';					
				}
				profile[profile.length] = '[plugin] '+activexes[axc][1]+' '+f;
			}	
		}
		//real player			
		try {
			testObject = new ActiveXObject('rmocx.RealPlayer G2 Control.1')
			embedVersion = testObject.GetVersionInfo()
			profile[profile.length] = '[plugin] Real Player '+embedVersion;
		}
		catch(e) {}
		
	}
	profile[profile.length] = '[history_length] '+window.history.length;
	if(window.innerHeight) profile[profile.length] = '[innerHeight] '+window.innerHeight;    
	else if(document.documentElement.clientHeight) {
		profile[profile.length] = '[innerHeight] '+document.documentElement.clientHeight;
	}
	else profile[profile.length] = '[innerHeight] '+document.body.clientHeight;
	if(window.innerWidth) profile[profile.length] = '[innerWidth] '+window.innerWidth;
	else if(document.documentElement.clientWidth) {
		profile[profile.length] = '[innerWidth] '+document.documentElement.clientWidth;
	}
	else profile[profile.length] = '[innerWidth] '+document.body.clientWidth;
	if (self.screen) {
		profile[profile.length] = '[screenWidth] '+screen.width;
		profile[profile.length] = '[screenHeight] '+screen.height;
		profile[profile.length] = '[colorDepth] '+screen.colorDepth+' bit';		
	}
	else if(self.java)
	{
		var j=java.awt.Toolkit.getDefaultToolkit();
		var s=j.getScreenSize();
		profile[profile.length] = '[screenWidth] '+s.width;
		profile[profile.length] = '[screenHeight] '+s.height;
	}	
	return profile;
}//end of function

function check_installation()
{
	var should_be_click_handler = null;
	if (document.captureEvents) should_be_click_handler = document.onclick;
	else should_be_click_handler = document.onmouseup;
	if(!should_be_click_handler) should_be_click_handler = '';
	should_be_click_handler = should_be_click_handler.toString();				
	var found_click_handler = (should_be_click_handler.indexOf('click_handler') > -1);
	var is_anylink_menu = (should_be_click_handler.indexOf('hidemenu') > -1);
	var warning_style = 'background-color:#000000;color:#FFFFFF;padding:3px;border:solid 1px #FFFFFF;';
	if(!found_click_handler)
	{
		if(is_anylink_menu)
		{
			//reinstate tractor III, because it invokes the anylink menu hidemenu system if it finds it
			if (document.captureEvents) document.onclick = click_handler;
			else document.onmouseup = click_handler;
		}
		else 
		{
			should_be_click_handler = should_be_click_handler.split('{');
			should_be_click_handler = '\"'+should_be_click_handler[0]+'\"';
			if(debug_tracker)
			{
				alert('Tracker installation check found: '+should_be_click_handler);
			}
			document.write('<div style=\"position:absolute;left:0px;top:0px;'+warning_style+'\">');
			document.write('Warning: Tractor III Link Tracker Overridden With '+should_be_click_handler);
			document.write('<' + '/div>');
		}
	}	
	//alert('ok');
}
