// <![CDATA[
var stringTierCheck = getCookie2("KBMGbridge");

if (stringTierCheck != null)
{
	var cRefer = stringTierCheck;
	var cDateRef = new Date();
} // if
else
{
	// First, Get the domain name of where the page is.
	var cDomain = self.location.hostname;
	if (cDomain.indexOf(".") < cDomain.lastIndexOf("."))
	{
		var domainOffset = cDomain.indexOf(".") + 1;
		cDomain = cDomain.substr(domainOffset);
	} // if
	// Next check for the need to write the cookie
	// Check that the referrer page is NOT part of the same domain
	// and that there IS a referrer to write to cookie
	// and that the cookie has NOT already been written before
	if (document.referrer.indexOf(cDomain) == -1 &&
		document.referrer != "" &&
		document.cookie.indexOf("referrer=") == -1)
	{
		var expDays = 90; // set the number of days to expiry of the cookie
		var exp = new Date(); 
		exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));
		var refdate = new Date();
		document.cookie = "referrer=" + escape(document.referrer + "&&" + refdate.toGMTString()) + "; expires=" + exp.toGMTString() +
			"; path=/" + "; domain=" + cDomain;
		// the cookie has now been written.
	} // if
	// The next part allows the cookie to be read back after writing
	// first, we find  if there is the referer cookie
	var allCookies = document.cookie;
	var cPos = allCookies.indexOf("referrer=");
	// if the cookie exists, we read back its values
	// and break the useful data from the rest
	if (cPos != -1)
	{
		var cdstart = cPos + 9;
		var cdend = allCookies.indexOf(";", cdstart);
		if (cdend == -1)
		{
			cdend = allCookies.length;
		} // if
		var cookieContent = allCookies.substring(cdstart,cdend);
		cookieContent = unescape(cookieContent);
		var cdatestart = cookieContent.indexOf("&&", 0);
		var cdateend = cookieContent.length;
		var cRefer = cookieContent.substring(0, cdatestart);
		var cDateRef = cookieContent.substring(cdatestart + 2, cdateend)
	} // if
	else
	{
		var cRefer = "No cookie";
		var cDateRef = "No cookie";
	} // else
} // else



function getCookie2 ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}


// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0)
		{
			return null;
		} // if
	} // if
	else
	{
		begin += 2;
	} // else
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	} // if
	return unescape(dc.substring(begin + prefix.length, end));
} // getCookie
// ]]>
