function write_log_link(uid) {
	if (uid == null) return;
	if (window.agent_dom == null) return;
	if (window.agent_conv == null) return;

	var ref_url = window.agent_ref;
	if (ref_url == null) {
		ref_url = "none";
		if (document.referrer) {
			ref_url = new String(document.referrer);
		}
	}
	ref_url = ref_url.replace(/\|/g, '%7C');
	ref_url = ref_url.replace(/'/g, '%27');

	var cur_url = window.agent_url;
	if (cur_url == null) {
		cur_url = new String(document.location);
	}
	cur_url = cur_url.replace(/\|/g, '%7C');
	cur_url = cur_url.replace(/'/g, '%27');

	var now = new Date();
	var log_url = 'http://stats.agentinteractive.com/log.js?';
	if (cur_url.indexOf("https://") == 0)
		log_url = 'https://stats.agentinteractive.com/log.js?';
	log_url += "url=" + cur_url;
	log_url += "|ref=" + ref_url;
	log_url += "|uid=" + uid;
	log_url += "|sid=" + agent_dom;
	log_url += "|aid=" + agent_conv;
	log_url += "|unq=" + now.getTime();
	log_url += "|tmz=" + now.getTimezoneOffset();
	if (window.agent_csum != null)
		log_url += "|prm=" + agent_csum;
	if (window.agent_pname != null)
		log_url += "|pnm=" + agent_pname;
	if (window.agent_pgroup != null)
		log_url += "|pgi=" + agent_pgroup;
	if (window.agent_pgroup2 != null)
		log_url += "|pg2=" + agent_pgroup2;
	if (window.agent_pgroup3 != null)
		log_url += "|pg3=" + agent_pgroup3;
	if (window.agent_internuname != null)
		log_url += "|iun=" + agent_internuname;
	document.write("<script src='" + log_url +"'></script>");
}

function agent_set_cookie(name, value, days) {
	var expires = new Date();
	var today = new Date();
	expires.setTime(today.getTime() + 1000 * 60 * 60 * 24 * days);

	var string = name + "=" + value + "; path=/";
	if (expires != null) string += "; expires=" + expires.toGMTString();

	var url = new String(document.location);
	var offset = 0;
	if (url.indexOf("http://") == 0) offset = 7;
	if (url.indexOf("https://") == 0) offset = 8;

	if (offset > 0) {
		var end = url.indexOf("/", offset);
		if (end == -1)
			end = url.length;
		var domain = url.substring(offset, end);
		var domain2 = domain.replace(/^.+(\.[^.]+\.[^.]+\.[^.][^.])$/, "$1");
		if (domain == domain2)
			domain2 = domain.replace(/^.+(\.[^.]+\.[^.][^.][^.]+)$/, "$1");
		if (domain != domain2)
			string += "; domain=" + domain2;
	}

	document.cookie = string;
}

function agent_get_cookie(name) {
	var search = name + "=";
	if (document.cookie.length > 0) {
		var offset = document.cookie.indexOf(search);
		// if cookie exists, return its value
		if (offset != -1) {
			offset += search.length
			var end = document.cookie.indexOf(";", offset)
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
	return null;
}

function setup_user_id() {
	var uid = window.agent_uid;
	if (uid == null) {
		uid = agent_get_cookie("agent_uid");
		if (uid != null) {
			if (window.agent_log_once) window.agent_skip_log = 1;
			return uid;
		}
		uid = new Date().getTime();
	}
	else {
		if (window.agent_log_once) window.agent_skip_log = 1;
	}
	var dtl = window.agent_dtl || 1800;
	agent_set_cookie("agent_uid", uid, dtl);
	return uid;
}

if (window.agent_dom != null) {
	if (
		window.agent_dom == 8948001 ||
		window.agent_dom == 8950001 ||
		window.agent_dom == 10278001 ||
		window.agent_dom == 12761001 ||
		window.agent_dom == 15248001 ||
		window.agent_dom == 15786001 ||
		window.agent_dom == 15862001 ||
		window.agent_dom == 15928001 ||
		window.agent_dom == 15942001 ||
		window.agent_dom == 15943001 ||
		window.agent_dom == 15944001 ||
		window.agent_dom == 16130001 ||
		window.agent_dom == 16294001 ||
		window.agent_dom == 17044001 ||
		window.agent_dom == 18336001 ||
		window.agent_dom == 18397001 ||
		window.agent_dom == 18400001 ||
		window.agent_dom == 18432001 ||
		window.agent_dom == 19003001 ||
		window.agent_dom == 19008001 ||
		window.agent_dom == 19011001 ||
		window.agent_dom == 19013001 ||
		window.agent_dom == 19604001 ||
		window.agent_dom == 19638001 ||
		window.agent_dom == 19645001 ||
		window.agent_dom == 20349001 ||
		window.agent_dom == 20936001 ||
		window.agent_dom == 20939001 ||
		window.agent_dom == 15887001 ||
		window.agent_dom == 15975001 ||
		window.agent_dom == 16242001 ||
		window.agent_dom == 16360001 ||
		window.agent_dom == 16412001 ||
		window.agent_dom == 16883001 ||
		window.agent_dom == 20072001 ||
		0
	) {
		alert("This site is in violation of AgentInteractive rights.\nPlease alert the owner to fix this problem.");
		window.agent_dom = null;
	}

	if (
		window.agent_dom == 67001 ||
		window.agent_dom == 54001 ||
		window.agent_dom == 77001 ||
		window.agent_dom == 70001 ||
		window.agent_dom == 317001 ||
		window.agent_dom == 2396001 ||
		window.agent_dom == 18927001 ||
		0
	) {
		window.agent_dom = null;
	}
}

if (window.agent_conv == null) agent_conv = 0;
agent_uid = setup_user_id();
if (!window.agent_skip_log) write_log_link(agent_uid);
