// ME campaign tracking scripts.
// BSV 20080220

function trackParams(name)
{
// This function creates cookies for all parameters where the parameter names Starts with 'name'.

	var paramlist = findAllParams(name);
	if (typeof(paramlist) == "string")
		if (paramlist.length>0)
			createCookies(paramlist,name);
}

function createCookies(list,name)
{
// Creates cookies from a list of parameters. Typically used for campaigns.
// Allows the specification of life time data by a number After the name part of the parameter, separated by underscore.
// i.e. me_3_cid results in a 3 days life span for the cookie me_cid.
// Cookie can be deleted by specifying empty life span i.e. me__cid.

	var ar=list.split(',');
	for (i=0;i<ar.length;i++)
	{
		// check if param also has a lifetime value
		var upos=ar[i].indexOf('_',name.length);
		if (upos==-1)
			upos=name.length-1;
		var uval=0;
		if (upos>name.length)
			uval=Number(ar[i].substring(name.length,upos));
		if (upos==name.length)
			uval=-1;
		// Get the final cookie name
		var cn=name+ar[i].substring(upos+1);
		var cv=getParamValue(ar[i],0);
		// Check if a cookie with this name already has the same value
		var c=GetCookie(cn);
		// If the parameter value is different from the cookie value, then reset the cookie with the new value.
		// If the cookie does not exist, then create it.
		if (c==null || (c!=null && c!=cv))
			if (typeof(cv) == "string")
				createCookie (cn,cv,uval);
		// Check if cookie should be deleted.
		if (typeof(cv) == "object" || uval==-1)
			createCookie (cn, "", -1);
	}
}

function findAllParams(name)
{
// Find all parameters in the query string starting with a certain name.

	var strquery=document.location.search;
	if (strquery.length>1)
	{
		// Check if there is a valid parameter first in the query string
		var namepos=strquery.indexOf(name);
		var amppos=0;
		var equalpos=0;
		if (namepos==1)
		{
			amppos=strquery.indexOf('&');
			equalpos=strquery.indexOf('=');
			// check that it is the right equal sign
			if (equalpos>0 && (equalpos<amppos || amppos==-1))
				var strParamList=strquery.substring(1,equalpos);
			else
				var strParamList="";
		}
		else
			var strParamList="";
		// Next search for all the parameters with a leading ampersand
		var ampname='&'+name;
		var startpos=2;
		while (startpos<strquery.length)
		{
			namepos=strquery.indexOf(ampname,startpos);
			if (namepos>=startpos)
			{
				amppos=strquery.indexOf('&',namepos+1);
				equalpos=strquery.indexOf('=',namepos);
				if (equalpos>namepos && (equalpos<amppos || amppos==-1))
				{
					if (strParamList.length>0)
						strParamList+=',';	// add a comma between params
					strParamList+=strquery.substring(namepos+1,equalpos);
				}
				startpos=namepos+ampname.length;
			}
			else
				break;
		}
		return strParamList;
	}
	else
		return;
}

function getParamValue(name,start)
{
// Find value of a parameter in the query string starting from a particular location.

	var strquery=document.location.search;
	if (strquery.length>1)
	{
		var namepos=strquery.indexOf(name+'=',start);
		if (namepos>0)
		{
			// Check if the parameter was first in the query string
			var boolparok=(namepos==1);
			if (!boolparok)
			{
				// Check if the parameter was preceeded by an ampersand in the query string
				var stramp=strquery.substr(namepos-1,1);
				boolparok=(stramp.indexOf("&")==0);
				if (!boolparok)
				{
					// Call function recursively
					var nextstart=namepos+name.length+2;
					if (strquery.length>nextstart)
						return getParamValue(name,nextstart);
					else
						return;
				}
			}
			if (boolparok)
			{
				// Get any parameter value
				var nameval='';
				var amppos=strquery.indexOf("&",namepos);
				// Check if there is an ampersand behind the parameter
				if (amppos>namepos)
				{
					// Get the value
					nameval=strquery.substring(namepos+name.length+1,amppos);
				}
				else
				{
					// Get the value
					nameval=strquery.substring(namepos+name.length+1);
				}
				if (nameval.length>0)
					return nameval;
				else
					return null;
			}
		}
		else
			return;
	}
	else
		return;
}

function GetCookie (name)
{
// Gets the value of a particular cookie.

	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i=0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function getCookieVal(offset)
{
// Gets a substring of the cookie string from a particular offset to the next semi-colon.

  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function createCookie (name, value, days)
{
// Creates a cookie with a particular name, value and life-span (days).

  if (days)
  {
	var date = new Date();
	if (days > 0)
	{
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
		if (days==0)
			var expires = "";
		else
		// delete the cookie
		{
			date.setTime(date.getTime()-1);
			var expires = "; expires="+date.toGMTString();
		}
	}
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// Find all ME campaign tracking parameters and create cookies
//trackParams("me_");



