		
	var lastClickEvent = "";
	var agent="";

	function logClick(click_details) {
		
		// avoid duplicates...
		if (lastClickEvent==click_details) {
			return;
		}
		
		lastClickEvent = click_details;
		
		click_details = click_details.replace("http://shipsandtripstravel.com/","");

		var browser=navigator.userAgent;
		var clickSource = document.location.pathname;
		var postData = "click_source=" + urlencode(clickSource);
		postData = postData + "&click_details=" + urlencode(click_details);
		postData = postData + "&browser_data=" + browser;
		
		// send log data asynchronously so user isn't impacted...
		if (clickSource.substring(0,10) == '/agenthub/') {
			$.ajax({
				type: "POST",
				url: "/agenthub/marketing/log-marketing-center-click.php",
				data: postData,
				async: true,
				cache: false
			});	
		} else {
			$.ajax({
				type: "POST",
				url: "/inc/log-click.php",
				data: postData,
				async: true,
				cache: false
			});	
		}
	}
	
	// bind logging to clicks on A tags, input changes, select list changes
	$(document).ready(function() {
			/*				   
		var parentDoc =  $(window.parent.document);
		
		var src = $(parentDoc).find("frame").attr("src");
		
		if (src != "" && src != undefined) {
			var agent = src.substr(src.indexOf("=")+1);
			
			$("a").each(function() { 
				this.href = this.href + "?agent=" + agent;
				alert('agent[' + agent + ']');
			});
		}
		*/
		$('a').bind('click', function() {
			if ($(this).attr('href') == "#") {
				logClick('click-id(' + $(this).attr('id') + ')');
			} else {
				if ($(this).attr('id') == "") {
					logClick('click-href(' + $(this).attr('href') + ')');
				} else {
					logClick('click-href(' + $(this).attr('href') + ') id(' + $(this).attr('id') + ')');
				}
			}
		});
		$('input').bind('focus', function() {
			logClick('input-focus(' + $(this).attr('id') + ':[' + $(this).val() + '])');
		});
		$('input').bind('change', function() {
			logClick('input-change(' + $(this).attr('id') + ':[' + $(this).val() + '])');
		});
		$('select').bind('change', function() {
			logClick('select-change(' + $(this).attr('id') + ':[' + $(this).val() + '])');
		});
		
		var qs = GetQueryString(); 

		if (qs["agent"] != "") {
			agent = qs["agent"];
			if (agent != "" && agent != undefined && agent != "undefined") {
				//if (agent == "gregg") {
				 // alert('agent[' + agent + ']');
				//}
				var thisHref;
				$("a").each(function() { 
					thisHref = this.href;
					// only append agent id to querystring if on our domain...
					if (thisHref.indexOf("shipsandtripstravel.com") > -1) {
						if (thisHref.indexOf(agent) < 0) {
							if (thisHref != "/" && thisHref != "http://shipsandtripstravel.com" && thisHref != "http://shipsandtripstravel.com/" && thisHref != "http://www.shipsandtripstravel.com" && thisHref != "http://www.shipsandtripstravel.com/") {
								if (thisHref.indexOf("?") < 0) {
									this.href = this.href + "?agent=" + agent;
								} else {
									this.href = this.href + "&agent=" + agent;
								}
							} else {
	//alert('tweak href from [' + thisHref + '] to [http://shipsandtripstravel.com/' + agent + ']');
								if (thisHref == "/" || thisHref == "http://www.shipsandtripstravel.com/" || thisHref == "http://shipsandtripstravel.com/") {
									this.href = this.href + agent;
								} else {
									this.href = this.href + "/" + agent;
								}
							}
						}
					}
				});
			}
		}
	});	

	function GetQueryString() 
	{ 
		return function(a) 
			{ 
				if (a == "") return {}; 
				var b = {}; 
				for (var i = 0; i < a.length; ++i) 
				{ 
					var p=a[i].split('='); 
					b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); 
				} 
				return b; 
		   }(window.location.search.substr(1).split('&')) 
	} 
	
	function urlencode(str) {
		return escape(str).replace(/\+/g,'%2B').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	}	

