
var jvmAffId;
var jvmCampId;
var jvmSysURL = 'http://thekelsoncompany.com/2';

function jvmSetCookie(name, value, longlife, path)
{
	var expires = longlife ? 'expires=Mon, 05-Jul-2024 23:59:59 GMT; ' : '';
	var curCookie =
		name + "=" + value + "; " +
		expires +
		(path ? "path=" + path : "");
	document.cookie = curCookie;
}

function jvmGetCookie(name)
{
	var prefix = name + "=";
	var idxStart = document.cookie.indexOf(prefix);
	if (idxStart == -1) return null;
	var idxEnd = document.cookie.indexOf(";", idxStart + prefix.length);
	if (idxEnd == -1) idxEnd = document.cookie.length;
	return unescape(document.cookie.substring(idxStart + prefix.length, idxEnd));
}

function jvmInitAffCookies()
{

	var path = document.location.pathname;
	var path = path.substring(0, path.lastIndexOf('/'));

//document.write("!!! path [" + path + "]<br>");

	var m;
	var qs = document.location.search;
	m = qs.match(/[?&]aff_id=([^&]+)/);
	jvmAffId = m == null ? '' : m[1];
	m = qs.match(/[?&]camp_id=([^&]+)/);
	jvmCampId = m == null ? '' : m[1];

//document.write("!!! qs jvmAffId [" + jvmAffId + "]<br>");
//document.write("!!! qs jvmCampId [" + jvmCampId + "]<br>");

	if (jvmAffId == "")
		jvmAffId = jvmGetCookie('jvm_aff_id');
	else
		jvmSetCookie('jvm_aff_id', jvmAffId, true, path);

	if (jvmCampId == "")
		jvmCampId = jvmGetCookie('jvm_camp_id');
	else
		jvmSetCookie('jvm_camp_id', jvmCampId, true, path);

//document.write("!!! after jvmAffId [" + jvmAffId + "]<br>");
//document.write("!!! after jvmCampId [" + jvmCampId + "]<br>");

}

function jvmSysRedirect()
{
	var m;
	var qs = document.location.search;
	m = qs.match(/[?&]jin_encurl=(\d+)\:([0-9A-F]+)/);
	if (m != null)
	{
		var owner_id = m[1];
		var encurl_code = m[2];
		if (owner_id != "" && encurl_code != "")
		{
			document.location = jvmSysURL + "/s" + owner_id + "/encurl/" + encurl_code + ".html";
		}
	}
}

function jvmPrintHref(href, title, addon)
{
	var qs = "";
	if (jvmAffId) qs += "aff_id=" + jvmAffId;
	if (jvmCampId) qs += "&camp_id=" + jvmCampId;
	href += href.indexOf('?') == -1 ? '?' : '&';
	href += qs;
	document.write('<a href="' + href + '" ');
	if (addon != null) document.write(addon);
	document.write('>' + title + '</a>');
}

function jvmRedirectToHideAffId()
{
	var path = document.location.pathname;
	var path = path.substring(0, path.lastIndexOf('/'));

//document.write("!!! ja [" + jvmGetCookie('jvm_aff_id') + "]");

	var m;
	var qs = document.location.search;
	m = qs.match(/[?&]aff_id=([^&]+)/);
	if (m != null)
	{
		document.location.replace(document.location.pathname);
	}

	return;
}	

function InsertHiddenAffID(name, param, def)
{
	InsertHiddenAffIDImpl('hidden', name, param, def);
}

function InsertHiddenAffIDTest(name, param, def)
{
	InsertHiddenAffIDImpl('text', name, param, def);
}

function InsertHiddenAffIDImpl(type, name, param, def)
{
	var value;

	var prefix = param + '=';
	var qs = document.location.search;
	var idxStart = qs.indexOf(prefix);
	if (idxStart == -1) 
		value = def;
	else
	{
		var idxEnd = qs.indexOf("&", idxStart + prefix.length);
		if (idxEnd == -1) idxEnd = qs.length;
		value = unescape(qs.substring(idxStart + prefix.length, idxEnd));
	}

	document.write('<input type="' + type + '" name="' + name + '" value="' + value + '">');
}
	
jvmInitAffCookies();
jvmSysRedirect();
