/*---------------------------	o2online.de	----------------------------*/
/* Application:	o2online.de												*/
/* Type: JS-Template													*/
/* Function: provide o2cbid JavaScript-functions						*/
/* Description:	n/a														*/
/* Owner:	th, hw														*/
/* Version:	$Revision: 1.3 $											*/
/* Last	modified:	$Date: 2008/04/03 16:41:56 $						*/
/*----------------------------------------------------------------------*/

document.o2cbid = null;
document.o2cust = null;

// creates a new cookie named o2-CBID with CBID:<cbid>#CUST:<0|1>#
function set_o2cbid(update)
{
	var tsnow = new Date().getTime();
	var expires = new Date(tsnow + 946080000000); // (30 * 365 * 24 * 60 * 60 * 1000) = about 30 years;

	if (update)
	{
		var cust = (find_cookieStart("o2online") > -1) ? 1 : 0;
		if (document.o2cust != null && document.o2cust == cust)
		{
			document.o2cust= cust;
			return;
		}
		document.o2cust = cust;
	}
	else
	{
		var rnd  = "" + Math.floor(Math.random() * 1234567891234);
		var rnd1 = "" + Math.floor(Math.random() * 1234567891234);
		if (rnd == rnd1)
		{
			// random does not really works - like for mac's safari
			rnd = "" + new Date().getTime() * document.URL.length;  // take this as random ...
		}
		var lrnd = rnd.length;
		while (lrnd < 9)
		{
			rnd = rnd + "0" + Math.floor(Math.random() * 123456789);
			lrnd = rnd.length;
		}

		var tsnowstr = "" + tsnow;
		var ltsnow = tsnowstr.length;
		while (ltsnow < 9)
		{
			tsnowstr = tsnowstr + "0" + tsnowstr;
			ltsnow = tsnowstr.length;
		}
		document.o2cbid = tsnowstr.substring(ltsnow-9) + "-" + rnd.substring(lrnd-9); // 9 chars '-' 9 chars

		var cust = 1;
		if (document.o2cust == null || document.o2cust == 0)
		{
			cust = (find_cookieStart("o2online") > -1) ? 1 : 0;
		}
		document.o2cust = cust;
	}
	document.cookie = "o2-CBID=CBID:" + document.o2cbid
		+ "#CUST:" + document.o2cust + "#"
		+ "; expires=" + expires.toGMTString()
		+ "; path=/"
		+ "; domain=.o2online.de";
}

// returns the value of cbid (cookie-value before first '#') in the o2-CBID cookie or null
function get_o2cbidVal(offset)
{
	var retval = '';
	var endstr = document.cookie.indexOf(";", offset)
	if (endstr == -1)
	{
		endstr = document.cookie.length;
	}
	var value = unescape(document.cookie.substring(offset, endstr));

	var idxstart = value.indexOf("CBID:");
	if (idxstart > -1)
	{
		var idxend = value.indexOf("#", idxstart);
		if (idxend > -1)
		{
			document.o2cbid = value.substring(idxstart+5, idxend);
		}
	}
	idxstart = value.indexOf("CUST:");
	if (idxstart > -1)
	{
		var idxend = value.indexOf("#", idxstart);
		if (idxend > -1)
		{
			document.o2cust = value.substring(idxstart+5, idxend);
		}
	}
	return(retval)
}

// returns the start-index of the cookie value with given cookie name
function find_cookieStart(name)
{
	name += "=";
	var retval = document.cookie.indexOf(name);
	if (retval > -1)
	{
		retval += name.length;
	}
	return retval;
}

// returns the value of cbid in the cookie o2-CBID or null
function get_o2cbid()
{
	var idx = find_cookieStart("o2-CBID");
	if (idx > -1) 
	{
		get_o2cbidVal(idx);
	}
}

if (navigator.cookieEnabled)
{
	get_o2cbid();
	if ((document.o2cbid == null) || (document.o2cbid.length != 19) || (document.o2cbid.indexOf("%") > -1))
	{
		set_o2cbid(false);
	}
	else if ((document.o2cust == null) || (document.o2cust == 0))
	{
		set_o2cbid(true);
	}
}
