/*---------------------------	o2online.de	----------------------------*/
/* Application:	o2online.de												*/
/* Type: JS-Template													*/
/* Function: provide cnt JavaScript-functions							*/
/* Description:	n/a														*/
/* Owner:	th															*/
/* Version:	$Revision: 1.5 $											*/
/* Last	modified:	$Date: 2007/01/24 11:38:32 $						*/
/* by: th																*/
/*----------------------------------------------------------------------*/


/* IMPORTANT: Image path is relative to the including HTML-Document !!!
	best work with absolut url?s from server-root, e.g. "/images/o2/..." */

// document.loginName should be set

function setCntCookie()
{
	document.cookie = "cnt=1;path=/;domain=o2online.de";
}
function checkCntCookie()
{
	var clen = document.cookie.length;
	var i = 0;
	while(i < clen)
	{
		var j = i + 4;
		if (document.cookie.substring(i, j) == "cnt=")
		{
			return true;
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (!i)
		{
			break;
		}
	}
	return false;
}
function cnt(paramArray)
{
	var loggingProtocol = window.document.location.protocol;
	var loggingHost="//cnt.o2online.de"; 
	var loggingContextPath="/cnt/";
	var loggingGif = "cnt.cgi";
	var n = new Date;
	var millis = n.getMilliseconds();
	var rnd = Math.floor(Math.random() * 12345678);
	var loggingURL = loggingProtocol
		+ loggingHost
		+ loggingContextPath
		+ "_o2_millis_" + millis
		+ "_o2_rnd_" + rnd + "_"
		+ loggingGif;
	var o2host = window.location.host;
	var o2url = escape(document.URL);
	var o2pagetitle = escape(window.document.title);
	var o2referrer = escape((window.document.referrer==""?"-":window.document.referrer));
	var o2language = "-";
	if (navigator.userLanguage)
	{
		o2language = navigator.userLanguage;
	}
	else
	{
		o2language = navigator.language;
	}
	var o2portalid = "";
	var o2nbaid = "";
	var o2nbarank = "";
	var o2nbacstate = "";
	var paramlist = "";
	if (paramArray != null)
	{
		for (var i = 0; i < paramArray.length; ++i)
		{
			if (paramArray[i].indexOf("o2_host=") > -1)
			{
				o2host = paramArray[i].substr(paramArray[i].indexOf("o2_host=") + 8, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_url=") > -1)
			{
				o2url = paramArray[i].substr(paramArray[i].indexOf("o2_url=") + 7, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_pagetitle=") > -1)
			{
				o2pagetitle = paramArray[i].substr(paramArray[i].indexOf("o2_pagetitle=") + 13, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_referrer=") > -1)
			{
				o2referrer = paramArray[i].substr(paramArray[i].indexOf("o2_referrer=") + 12, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_language=") > -1)
			{
				o2language = paramArray[i].substr(paramArray[i].indexOf("o2_language=") + 12, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_nbarank=") > -1)
			{
				o2nbarank = paramArray[i].substr(paramArray[i].indexOf("o2_nbarank=") + 11, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_nbacstate=") > -1)
			{
				o2nbacstate = paramArray[i].substr(paramArray[i].indexOf("o2_nbacstate=") + 13, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_nbaid=") > -1)
			{
				o2nbaid = paramArray[i].substr(paramArray[i].indexOf("o2_nbaid=") + 9, paramArray[i].length);
			}
			else if (paramArray[i].indexOf("o2_portalid=") > -1)
			{
				o2portalid = paramArray[i].substr(paramArray[i].indexOf("o2_portalid=") + 12, paramArray[i].length);
				paramlist += "&" + paramArray[i];
			}
			else
			{
				paramlist += "&" + paramArray[i];
			}
		}
	}
	if (o2nbarank == "")
	{
		if (eval("typeof document.nbaRank_" + o2portalid) != 'undefined')
		{
			o2nbarank = escape(eval("document.nbaRank_" + o2portalid));
		}
	}
	if (o2nbacstate == "")
	{
		if (eval("typeof document.nbaCState_" + o2portalid) != 'undefined')
		{
			o2nbacstate = escape(eval("document.nbaCState_" + o2portalid));
		}
	}
	var querystr = "?";
	var o2msisdn = "-";
	var o2sid = "-";
	if ((typeof document.loginName != 'undefined') && (document.loginName != null) && (document.loginName != "null"))
	{
		o2msisdn = escape(document.loginName);
	}
	if (o2msisdn.indexOf("bject") > -1)
	{
		// some browsers may supply a corrupted value
		o2msisdn = "-";
	}
	querystr += "o2_ms=" + o2msisdn;
	if ((typeof document.o2sid != 'undefined') && (document.o2sid != null) && (document.o2sid != "null"))
	{
		o2sid = escape(document.o2sid);
	}
	if (o2sid.indexOf("bject") > -1)
	{
		// some browsers may supply a corrupted value
		o2sid = "-";
	}
	querystr += "&o2_si=" + o2sid;
	if ((typeof document.o2cbid == 'undefined') || (document.o2cbid == null))
	{
		querystr += "&o2_ci=-";
	}
	else
	{
		querystr += "&o2_ci=" + escape(document.o2cbid);
	}
	querystr += "&o2_ur=" + o2url;
	querystr += "&o2_pt=" + o2pagetitle;
	querystr += "&o2_rf=" + o2referrer;
	var existCnt = checkCntCookie();
	if (!existCnt)
	{
		setCntCookie();
		querystr += "&o2_ln=" + o2language;
		querystr += "&o2_sr=" + window.screen.width + "*" + window.screen.height;
		querystr += "&o2_sc=" + window.screen.colorDepth;
		querystr += "&o2_ce=" + (navigator.cookieEnabled ? "1" : "0");
		if ((typeof document.o2_flash != 'undefined') && (document.o2_flash != null))
		{
			querystr += "&o2_pl=flash:" + escape(document.o2_flash);
		}
	}
	// Meta Tag Support
	var o2_Meta;
	var o2_DocumentElements = null;
	if (document.all)
	{
		o2_DocumentElements = document.all.tags("meta");
	}
	else if (document.documentElement)
	{
		o2_DocumentElements = document.getElementsByTagName("meta");
	}
	if (o2_DocumentElements)
	{
		for (var i = 1; i <= o2_DocumentElements.length; i++)
		{
			o2_Meta = o2_DocumentElements.item(i-1);

			if (o2_Meta.name.indexOf("o2meta_") == 0)
			{
				querystr += "&" + o2_Meta.name + "=" + escape(o2_Meta.content);
			}
		}
	}
	if (o2nbaid != "")
	{
		querystr += "&o2_bi=" + o2nbaid;
	}
	if (o2nbarank != "")
	{
		querystr += "&o2_en=" + o2nbarank;
	}
	if (o2nbacstate != "")
	{
		querystr += "&o2_cs=" + o2nbacstate;
	}
	querystr += paramlist;
	return loggingURL + querystr;
}

// for search only
function cntSearchCI(destUrl, cType, cEnum, pArray)
{
	var img = new Image();
	var paramArray = new Array();
	paramArray[0] = "o2_target=" + escape(destUrl);
	var idx = 1;
	if ((typeof cType != 'undefined') && cType != null && cType != "")
	{
		paramArray[idx] = "o2_ev=" + escape(cType);
		idx++;
	}
	if ((typeof cEnum != 'undefined') && cEnum != null && cEnum != "")
	{
		paramArray[idx] = "o2_en=" + escape(cEnum);
		idx++;
	}
	if ((typeof pArray != 'undefined') && pArray != null)
	{
		for (var i = 0; i < pArray.length; ++i)
		{
			paramArray[(idx + i)] = pArray[i];
		}
	}
	img.src = cnt(paramArray);
	// wait until image is loaded, avoid endless loops
	var count = 0;
	while (!img.complete)
	{
		// Sorry, it's a hack but the onload method does not work at least
		// with IE because it's evaluated during it's declaration
		if (++count > 10000)
		{
			break;
		}
	}
	return true;
}

function cntCI(ename, desturl, ebicid, portalid, etype, eenum, epos, paramArray)
{
	var img = new Image();
	var localParamArray = new Array();
	var idx = 0;
	if ((typeof ename != 'undefined') && ename != null && ename != "")
	{
		localParamArray[idx] = "o2_on=" + escape(ename);
	}
	else
	{
		localParamArray[idx] = "o2_on=-";
	}
	idx = idx + 1;
	if ((typeof ebicid != 'undefined') && ebicid != null && ebicid != "")
	{
		localParamArray[idx] = "o2_bi=" + escape(ebicid);
		idx = idx + 1;
	}
	if ((typeof portalid != 'undefined') && portalid != null && portalid != "")
	{
		localParamArray[idx] = "o2_pi=" + escape(portalid);
		idx = idx + 1;
		if ((typeof document.o2pid != 'undefined') && document.o2pid != null && document.o2pid != "")
		{
			localParamArray[idx] = "o2_id=" + escape(document.o2pid) + "-" + escape(portalid);
		}
		else
		{
			localParamArray[idx] = "o2_id=" + escape("xx-00000000") + "-" + escape(portalid);
		}
		idx = idx + 1;
	}
	else
	{
		if ((typeof ename != 'undefined') && ename != null && ename != "" && (typeof document.o2pid != 'undefined') && document.o2pid != null && document.o2pid != "")
		{
			localParamArray[idx] = "o2_id=" + escape(document.o2pid) + "-" + escape(ename);
			idx = idx + 1;
		}
	}
	if ((typeof desturl != 'undefined') && desturl != null && desturl != "")
	{
		localParamArray[idx] = "o2_tg=" + escape(desturl);
	}
	else
	{
		localParamArray[idx] = "o2_tg=-";
	}
	idx = idx + 1;
	if ((typeof etype != 'undefined') && etype != null && etype != "")
	{
		localParamArray[idx] = "o2_ev=" + escape(etype);
		idx = idx + 1;
	}
	if ((typeof eenum != 'undefined') && eenum != null && eenum != "")
	{
		localParamArray[idx] = "o2_en=" + escape(eenum);
		idx = idx + 1;
	}
	else 
	{
		if (eval("typeof document.o2enum" + portalid) != 'undefined')
		{
			localParamArray[idx] = "o2_en=" + escape(eval("document.o2enum" + portalid));
			idx = idx + 1;
		}
	}
	if ((typeof epos != 'undefined') && epos != null && epos != "")
	{
		localParamArray[idx] = "o2_gr=" + escape(epos);
		idx = idx + 1;
	}
	else
	{
		var pos = "0:0:0:0:0";
		if (eval("typeof document.o2pos" + portalid) != 'undefined')
		{
			pos = eval("document.o2pos" + portalid);
			var r = pos.split(":", 1);
			if (eval("typeof document.o2posr" + r) != 'undefined')
			{
				pos = pos + ":" + eval("document.o2posr" + r[0]);
			}
			else
			{
				pos = pos + ":0";
			}
		}
		localParamArray[idx] = "o2_gr=" + escape(pos);
		idx = idx + 1;
	}
	
	if ((typeof paramArray != 'undefined') && paramArray != null)
	{
		for (var i = 0; i < paramArray.length; ++i)
		{
			localParamArray[(idx + i)] = paramArray[i];
		}
	}
	img.src = cnt(localParamArray);
	// wait until image is loaded, avoid endless loops
	var count = 0;
	while (!img.complete)
	{
		if (++count > 10000)
		{
			break;
		}
	}
	return true;
}

function cntTopPI(ename, paramArray) {
	var img = new Image();
	var idx = 0;
	if (paramArray != null)
	{
		idx = paramArray.length;
	}
	else
	{
		paramArray = new Array();
	}
	if ((typeof ename != 'undefined') && ename != null && ename != "")
	{
		paramArray[idx] = "o2_on=" + escape(ename);
		idx = idx + 1;
	}
	paramArray[idx] = "o2_ev=top";
	idx = idx + 1;
	if ((typeof document.o2pid != 'undefined') && (document.o2pid != null))
	{
		paramArray[idx] = "o2_id=" + escape(document.o2pid);
	}
	img.src = cnt(paramArray);
	// wait until image is loaded, avoid endless loops
	var count = 0;
	while (!img.complete)
	{
		if (++count > 10000)
		{
			break;
		}
	}
	return true;
}

function cntPI(ename, paramArray) {
	var img = new Image();
	var idx = 0;
	if (paramArray != null)
	{
		idx = paramArray.length;
	}
	else
	{
		paramArray = new Array();
	}
	paramArray[idx] = "o2_ev=bot";
	idx = idx + 1;
	if ((typeof ename != 'undefined') && ename != null && ename != "")
	{
		paramArray[idx] = "o2_on=" + escape(ename);
		idx = idx + 1;
	}
	if ((typeof document.o2bsk != 'undefined') && (document.o2bsk != null))
	{
		paramArray[idx] = "o2_bk=" + escape(document.o2bsk);
		idx = idx + 1;
	}
	if ((typeof document.o2opts != 'undefined') && (document.o2opts != null))
	{
		paramArray[idx] = "o2_bo=" + escape(document.o2opts);
		idx = idx + 1;
	}
	if ((typeof document.o2bsktoken != 'undefined') && (document.o2bsktoken != null))
	{
		paramArray[idx] = "o2_ti=" + escape(document.o2bsktoken);
		idx = idx + 1;
	}
	if ((typeof document.o2retcode != 'undefined') && (document.o2retcode != null))
	{
		paramArray[idx] = "o2_re=" + escape(document.o2retcode);
		idx = idx + 1;
	}
	if ((typeof document.o2pid != 'undefined') && (document.o2pid != null))
	{
		paramArray[idx] = "o2_id=" + escape(document.o2pid);
		idx = idx + 1;
	}
	if ((typeof document.o2diArray != 'undefined') && (document.o2diArray != null))
	{
		for (var i = 0; i < document.o2diArray.length; ++i)
		{
			var val = document.o2diArray[i];
			var diid = val.split("&",1);
			var pos = "0:0:0:0:0";
			if (eval("typeof document.o2pos" + diid) != 'undefined')
			{
				pos = eval("document.o2pos" + diid);
				var r = pos.split(":", 1);
				if (eval("typeof document.o2posr" + r) != 'undefined')
				{
					pos = pos + ":" + eval("document.o2posr" + r[0]);
				}
				else
				{
					pos = pos + ":0";
				}
			}
			val = val + pos;
			if ((typeof document.o2pid != 'undefined') && document.o2pid != null)
			{
				val = document.o2pid + "-" + val;
			}
			else
			{
				val = "xx-00000000-" + val;
			}
			if (i < 9)
			{
				paramArray[idx] = "o2_di0" + (i+1) + "=" + escape(val);
			}
			else
			{
				paramArray[idx] = "o2_di" + (i+1) + "=" + escape(val);
			}
			idx = idx + 1;
		}
	}
	img.src = cnt(paramArray);
	// wait until image is loaded, avoid endless loops
	var count = 0;
	while (!img.complete)
	{
		if (++count > 10000)
		{
			break;
		}
	}
	return true;
}

function cntForm(ename, etype, formname, fieldlist, portalid, paramArray) {
	var formula = null;
	var img = new Image();
	var idx = 0;
	if ((typeof paramArray != 'undefined') && paramArray != null)
	{
		idx = paramArray.length;
	}
	else
	{
		paramArray = new Array();
	}
	if ((typeof ename != 'undefined') && ename != null && ename != "")
	{
		paramArray[idx] = "o2_on=" + escape(ename);
		idx = idx + 1;
	}
	if ((typeof etype != 'undefined') && etype != null && ename != "")
	{
		paramArray[idx] = "o2_ev=" + escape(etype);
		idx = idx + 1;
	}
	if ((typeof formname == 'undefined') || formname == null || formname == "")
	{
		if ((typeof portalid != 'undefined') && portalid != null && portalid != "" && (eval("typeof document.o2f" + portalid) != 'undefined'))
		{
			formname = eval("document.o2f" + portalid);
		}
	}
	if ((typeof formname != 'undefined') && formname != null && formname != "")
	{
		for (i=0; (i < document.forms.length) && (formula == null); ++i)
		{
			if (document.forms[i].name == formname)
			{
				formula = document.forms[i];
			}
		}
	}
	else
	{
		if (document.forms.length > 0)
		{
			formula = document.forms[0];
		}
	}
	if (formula != null)
	{
		if ((typeof ename == 'undefined') || ename == null || ename == "")
		{
			paramArray[idx] = "o2_on=" + escape(formula.name);
			idx = idx + 1;
		}
		if ((typeof fieldlist == 'undefined') || fieldlist == null || fieldlist == "")
		{
			if ((typeof portalid != 'undefined') && portalid != null && portalid != "" && eval("typeof document.o2ff" + portalid) != 'undefined')
			{
				fieldlist  = eval("document.o2ff" + portalid);
			}
		}
		if ((typeof fieldlist != 'undefined') && fieldlist != null && fieldlist != "")
		{
			var fieldNames = fieldlist.split(",");	
			paramArray[idx] = "o2_da=";
			for (i=0; i < fieldNames.length; i++)
			{
				var fieldNameComponents = fieldNames[i].split(":");
				var fieldAlias = fieldNameComponents[0];
				if (fieldNameComponents.length == 2) 
				{
					fieldAlias = fieldNameComponents[1];
				}
				var field = null;
				for (var j=0; (j < formula.elements.length) && (field == null); j++)
				{
					field = formula.elements[j];
					if (field.name != fieldNameComponents[0])
					{
						field = null;
					}
				}
				if (field != null) {
					if (field.value != null && field.value.length < 256)
					{
						var val = field.value;
						if ((field.type == "radio") || (field.type == "checkbox"))
						{
							val = "";
							var g = document.forms[formula.name].elements[field.name];
							for (var j=0;j < g.length; j++)
							{
								if (g[j].checked)
								{
									if (val.length > 0) 
									{
										val = val + "," + g[j].value;
									}
									else
									{
										val = val + g[j].value;
									}
								}
							}
						}
						else if (field.type == "select-multiple")
						{
							val = "";
							for (var j=0;j < field.options.length; j++)
							{
								if (field.options[j].selected)
								{
									if (val.length > 0)
									{
										val = val + "," + j;
									}
									else
									{
										val = val + j;
									}
								}
							}
						}
						paramArray[idx] = paramArray[idx] + escape(fieldAlias + "=" + val + "&");
					}
				}
			}
			idx = idx + 1;
		}
		if ((typeof portalid != 'undefined') && portalid != null && portalid != "")
		{
			var pos = "0:0:0:0:0";
			if (eval("typeof document.o2pos" + portalid) != 'undefined')
			{
				pos = eval("document.o2pos" + portalid);
				var r = pos.split(":", 1);
				if (eval("typeof document.o2posr" + r) != 'undefined')
				{
					pos = pos + ":" + eval("document.o2posr" + r[0]);
				}
				else
				{
					pos = pos + ":0";
				}
			}
			paramArray[idx] = "o2_gr=" + escape(pos);
			idx = idx + 1;
			if ((typeof document.o2pid != 'undefined') && document.o2pid != null && document.o2pid != "")
			{
				paramArray[idx] = "o2_id=" + escape(document.o2pid) + "-" + escape(portalid);
			}
			else
			{
				paramArray[idx] = "o2_id=" + escape("xx-00000000") + "-" + escape(portalid);
			}
			idx = idx + 1;
		}
		else
		{
			if ((typeof ename != 'undefined') && ename != null && ename != "" && (typeof document.o2pid != 'undefined') && document.o2pid != null && document.o2pid != "")
			{
				paramArray[idx] = "o2_id=" + escape(document.o2pid) + "-" + escape(ename);
				idx = idx + 1;
			}
		}
		paramArray[idx] = "o2_tg=-";
		img.src = cnt(paramArray);
		// wait until image is loaded, avoid endless loops
		var count = 0;
		while (!img.complete)
		{
			if (++count > 10000)
			{
				break;
			}
		}
	}
	return true;
}

function markDI(ename, ebicid, portalid, etype, eenum, cstate, paramArray) { 
	var didiv = "&";
	var ditext = "";
	
	if ((typeof ename == 'undefined') || (ename == null))
	{
		ename = "-";
	}
	
	if ((typeof portalid != 'undefined') && portalid != null)
	{
		ditext = ditext + escape(portalid);
	}
	ditext = ditext + didiv + escape(ename) + didiv;
	if ((typeof ebicid != 'undefined') && ebicid != null)
	{
		ditext = ditext + escape(ebicid);
	}
	ditext = ditext + didiv;
	if ((typeof etype != 'undefined') && etype != null)
	{
		ditext = ditext + escape(etype);
	}
	ditext = ditext + didiv;
	if ((typeof eenum != 'undefined') && eenum != null)
	{
		ditext = ditext + escape(eenum);
	}
	ditext = ditext + didiv;
	if ((typeof cstate != 'undefined') && cstate != null)
	{
		ditext = ditext + escape(cstate);
	}
	ditext = ditext + didiv;
	
	if ((typeof document.o2diArray != 'undefined') && (document.o2diArray != null))
	{
		document.o2diArray[document.o2diArray.length] = ditext;
	}
	else
	{
		document.o2diArray = new Array();
		document.o2diArray[0] = ditext;
	}
	return true;
}
