

var andServer = "";
var andWcms = "";


function and$(id)
{
	return document.getElementById(id);
}


function andInstall(svr, wcms)
{

	if (svr == null)
		andServer = '';
	else if (svr != '')
		andServer = 'http://' + svr;

	if (wcms == null)
		andWcms = '';
	else
		andWcms = wcms;


	guid = andCookieRead("andGuid");

	if (guid == null || guid == '')
		guid = andGuidNew();

	andCookieWrite("andGuid", guid, 1000 * 60 * 60 * 24 * 365);
}


function andGuidNew()
{
	result = '';

	for(i = 0; i < 32; i++)
		result += Math.floor(Math.random()*16).toString(16).toUpperCase();

	return result;
} 



function andCookieWrite(name, value, expires)
{
	var a = new Date();
	a = new Date(a.getTime() + expires);
	document.cookie = name + '=' + value + '; expires=' + a.toGMTString() + '; path=/;';
}


function andCookieRead(name)
{
	a = document.cookie;
	res = '';
	while (a != '')
	{
		while(a.substr(0, 1) == ' ')
			a = a.substr(1, a.length);

		cookiename = a.substring(0, a.indexOf('='));

		if (a.indexOf(';') != -1)
			cookiewert = a.substring(a.indexOf('=') + 1, a.indexOf(';'));
		else
			cookiewert = a.substr(a.indexOf('=') + 1, a.length);

		if (name == cookiename)
			res = cookiewert;

		i = a.indexOf(';') + 1;

		if (i == 0)
			i = a.length;

		a = a.substring(i, a.length);
	}

	return(res);
}


function andCookieDelete(name)
{
	document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/;';
} 


function andTrackAction(group, action, label, value)
{
	andTrackActionEx(group, action, label, '', value)
}

function andTrackActionEx(group, action, label, sublabel, value)
{
	if (group == null)  group = "";
	if (action == null) action = "";
	if (label == null)  label = "";
	if (sublabel == null)  sublabel = "";
	if (value == null)  value = 0;

	var xmlhttp = null;
	if (window.XMLHttpRequest) // MOZ
		xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject) // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return;

	url = andServer + "/andsrv/server.and?a=Module.Action&m=Crm&ma=Track.Action";

	url += "&ma.g=" + encodeURIComponent(group);
	url += "&ma.a=" + encodeURIComponent(action);
	url += "&ma.l=" + encodeURIComponent(label);
	url += "&ma.s=" + encodeURIComponent(sublabel);
	url += "&ma.v=" + encodeURIComponent(value);
	url += "&ma.u=" + new Date().getTime();

	if (andWcms != "")
	{
		
		xmlhttp.open("GET", andWcms + "?andTrackRelay?Url=" + encodeURIComponent(url), false);	
		xmlhttp.onreadystatechange = function() { }
		xmlhttp.send(null);
	}
	else
	{
		xmlhttp.open("GET", url, false);	
		xmlhttp.onreadystatechange = function() { }
		xmlhttp.send(null);
	}

	fullLabel = "";
	if (label != "" && sublabel != "")
		fullLabel = label + " > " + sublabel;
	else if (label != "")
		fullLabel = label;
	else if (sublabel != "")
		fullLabel = sublabel;

	_gaq.push(['_trackEvent', group, action, fullLabel, Math.floor(value)]); 
}


function andTrackForm(group, action, label, arrayParamValue)
{
	andTrackFormEx(group, action, label, '', arrayParamValue)
}

function andTrackFormEx(group, action, label, sublabel, arrayParamValue)
{
	if (group == null)  group = "";
	if (action == null) action = "";
	if (label == null)  label = "";
	if (sublabel == null)  sublabel = "";

	if ((arrayParamValue.length % 2) != 0)
		return;

	url = andServer + "/andsrv/server.and?a=Module.Action&m=Crm&ma=Track.Form";

	url += "&ma.g=" + encodeURIComponent(group);
	url += "&ma.a=" + encodeURIComponent(action);
	url += "&ma.l=" + encodeURIComponent(label);
	url += "&ma.s=" + encodeURIComponent(sublabel);

	idx = 0;
	for (i = 0; i < arrayParamValue.length; i += 2)
	{
		url += "&ma.p" + idx + "=" + encodeURIComponent(arrayParamValue[i]);
		url += "&ma.v" + idx + "=" + encodeURIComponent(arrayParamValue[i + 1]);
		idx++;
	}

	url += "&ma.u=" + new Date().getTime();

	var xmlhttp = null;
	if (window.XMLHttpRequest) // MOZ
		xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject) // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return;

	if (andWcms != "")
	{
		
		xmlhttp.open("GET", andWcms + "?andTrackRelay?Url=" + encodeURIComponent(url), false);	
		xmlhttp.onreadystatechange = function() { }
		xmlhttp.send(null);
	}
	else
	{
		xmlhttp.open("GET", url, false);	
		xmlhttp.onreadystatechange = function() { }
		xmlhttp.send(null);
	}


	fullLabel = "";
	if (label != "" && sublabel != "")
		fullLabel = label + " > " + sublabel;
	else if (label != "")
		fullLabel = label;
	else if (sublabel != "")
		fullLabel = sublabel;

	_gaq.push(['_trackEvent', group, action, fullLabel]); 
}


function andSetIdentifier(ident)
{
	andCookieWrite("andIdent", ident, 1000 * 60 * 60 * 24 * 365);
}

