function generateLinkForLog(uid) {
	if (uid == null) return;
	if (window.agent_dom == null) return;
	if (window.agent_conv == null) return;
	var refUrl = "none";
	if (document.referrer) {
		refUrl = new String(document.referrer);
		refUrl = refUrl.replace(/\|/g, '%7C');
		refUrl = refUrl.replace(/'/g, '%27');
	}
	var curUrl = new String(document.location);
	curUrl = curUrl.replace(/\|/g, '%7C');
	curUrl = curUrl.replace(/'/g, '%27');
	var now = new Date();
	var logUrl = 'http://stats.agentinteractive.com/log.js?';
	if (curUrl.indexOf("https://") == 0)
		logUrl = 'https://stats.agentinteractive.com/log.js?';
	logUrl += "url=" + curUrl;
	logUrl += "|ref=" + refUrl;
	logUrl += "|uid=" + uid;
	logUrl += "|sid=" + agent_dom;
	logUrl += "|aid=" + agent_conv;
	logUrl += "|unq=" + now.getTime();
	logUrl += "|tmz=" + now.getTimezoneOffset();
	if (window.agent_csum != null)
		logUrl += "|prm=" + agent_csum;
	if (window.agent_pname != null)
		logUrl += "|pnm=" + agent_pname;
	if (window.agent_pgroup != null)
		logUrl += "|pgi=" + agent_pgroup;
	if (window.agent_internuname != null)
		logUrl += "|iun=" + agent_internuname;
	document.write("<script src='" + logUrl +"'></script>");
}

function agent_setCookie(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_getCookie(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 getUserId() {
	var uid = agent_getCookie("agent_uid");
	if (uid != null) return uid;

	uid = new Date().getTime();
	agent_setCookie("agent_uid", uid, 1800);
	return uid;
}

if (window.agent_dom != null) {
	if (window.agent_dom == 2551001) {
		alert("This site is in violation of rights.\nThe owners of www.vikatan.com should contact\nAgentInteractive.com to resolve this issue.");
		window.agent_dom = null;
	}
	if (
		window.agent_dom == 1856001 ||
		window.agent_dom == 1858001 ||
		window.agent_dom == 1859001 ||
		window.agent_dom == 1864001 ||
		window.agent_dom == 1865001 ||
		window.agent_dom == 1866001 ||
		window.agent_dom == 1867001 ||
		window.agent_dom == 1868001 ||
		window.agent_dom == 1869001
	) {
		alert("This site is in violation of AgentInteractive rights.\nPlease alert the owner to fix this problem.");
		window.agent_dom = null;
	}
}

if (window.asum != null) agent_csum = asum;
if (window.aid != null) agent_conv = aid;
if (window.sid != null) agent_dom = sid;
if (window.agent_conv == null) agent_conv = 0;
if (window.agent_uid == null) agent_uid = getUserId();
generateLinkForLog(agent_uid);

