/****************************************************************************************************/
//Variabili principali
/****************************************************************************************************/
var accountName="tele2";//Nome account
var cookieSectionName="dotjs";//Nome sezione cookie
var domainNameType="auto";//(auto|none|domain) tipo di dominio da usare per i cookie

var dotjs_r="dotjs_r";//Referer della campagna
var dotjs_n="dotjs_n";//Nome campagna
var dotjs_t="dotjs_t";//Tipo campagna
var dotjs_k="dotjs_k";//Keyword
var dotjs_j="dotjs_j";//Jolly

var _domain,cookieDomainType="",_referer="-";
var imagePath="http://www.dotjs.com/1x1.gif";
/****************************************************************************************************/



dotjsTracker();



/****************************************************************************************************/
function dotjsTracker(page)
/****************************************************************************************************/
{
	//se non è stata impostata la pagina la recupera
	if (!page || page=="") page=getPageName();//return;
	
	//recupera il dominio
	_domain=getDomain();
	
	if (domainNameType && domainNameType!="")
	{
		cookieDomainType=" domain="+domainNameType+";";
	}
	writeLogInfo(page);
}
/****************************************************************************************************/
function writeLogInfo(page)
/****************************************************************************************************/
{
	var domainIndex;
	var logString="";
	var pg=location.pathname+location.search;
	var _params="";
	
	if (page && page!="") pg=escape(page);
	
	_referer=document.referrer;
	if (!_referer || _referer=="")
	{
		_referer="-";
	}
	else
	{
		domainIndex=_referer.indexOf(document.domain);
		if ((domainIndex>=0) && (domainIndex<=8))
		{
			_referer="0";
		}
	}
	
	//traccia la campagna
	logString+=cookieInfo();
	
	//imposta il nome dell'host
	if (location.hostname && location.hostname!="") logString+="&hn="+escape(location.hostname);
	
	logString+="&re="+_referer;
	
	_params=getPageParams();
	if (_params && _params!="")	logString+="&"+_params;
	
	//imposta la pagina con tutti i parametri
	logString+="&pg="+pg;
	
  var i2=new Image(1,1);
  i2.src=imagePath+"?"+logString+"&ac="+accountName+"&cc="+getCookieForLog();
  
	return;
}
/****************************************************************************************************/
function getDomain()
/****************************************************************************************************/
{
	if (!domainNameType || domainNameType=="" || domainNameType=="none")
 	{
 		domainNameType="";
 		return 1;
 	}
 	
	if (domainNameType=="auto")
 	{
 		//recupera il dominio
 		var dom=document.domain;
 		
 		//se nel dominio è contenuto il "www." lo elimina
  	if (dom.substring(0,4)=="www.")
  	{
   		dom=dom.substring(4,dom.length);
  	}
  	
  	domainNameType=dom;
 	}
 	return domainNameType;
}
/****************************************************************************************************/
function cookieInfo()
/****************************************************************************************************/
{
	var cookieTimeout="15768000";
	var dataOraAttuale;
	var newCookieValue="";
	var cookieValue="-";
	var cookieRefererCampaign="-";
	var i=0;
	var cookieSectionValue;
	var guid;
	
	dataOraAttuale=new Date();
	
	var cookieExpires=new Date(dataOraAttuale.getTime()+(cookieTimeout*1000));
	
	cookieExpires=" expires="+cookieExpires.toGMTString()+";";
	
	cookieValue=unescape(getCookieSection(location.search,cookieSectionName+"=","&"));
	
	if (cookieValue!="-" && cookieValue.indexOf(";")==-1)
	{
		document.cookie=cookieSectionName+"="+cookieValue+"; path=/;"+cookieExpires+cookieDomainType;		
		return "&unic=0";
	}
	
	//cookieValue=document.cookie.indexOf(cookieSectionName+"=dotjs_d="+_domain);
	cookieValue=document.cookie.indexOf(cookieSectionName);
	
	if (cookieValue>-1)
	{
		//cookieValue=getCookieSection(document.cookie,cookieSectionName+"=dotjs_d="+_domain,";");
		cookieValue=getCookieSection(document.cookie,cookieSectionName,";");
	}
	else
	{
		cookieValue="-";
	}	
	
	cookieRefererCampaign=getCookieSection(location.search,dotjs_r+"=","&");
	if (cookieRefererCampaign!="-" && cookieRefererCampaign!="")
	{
		
		//guid = eval("new ActiveXObject('Scriptlet.TypeLib').guid.substring(0, 38)");
		guid=creaGuid();
		//guid = replace(guid, "{")
		//guid = replace(guid, "}")
		//guid = replace(guid, "-")
		newCookieValue+="dotjs_g="+guid;
		
		newCookieValue+="|dotjs_r="+trimPlus(cookieRefererCampaign);
		
		cookieSectionValue=getCookieSection(location.search,dotjs_n+"=","&");
		if (cookieSectionValue!="-" && cookieSectionValue!="") newCookieValue+="|dotjs_n="+trimPlus(cookieSectionValue);
		else newCookieValue+="|dotjs_n=[empty]";
		
		cookieSectionValue=getCookieSection(location.search,dotjs_t+"=","&");
		if (cookieSectionValue!="-" && cookieSectionValue!="") newCookieValue+="|dotjs_t="+trimPlus(cookieSectionValue);
		else newCookieValue+="|dotjs_t=[empty]";
		
		cookieSectionValue=getCookieSection(location.search,dotjs_k+"=","&");
		if (cookieSectionValue!="-" && cookieSectionValue!="") newCookieValue+="|dotjs_k="+trimPlus(cookieSectionValue);
		else newCookieValue+="|dotjs_k=[empty]";
		
		cookieSectionValue=getCookieSection(location.search,dotjs_j+"=","&");
		if (cookieSectionValue!="-" && cookieSectionValue!="") newCookieValue+="|dotjs_j="+trimPlus(cookieSectionValue);
		else newCookieValue+="|dotjs_j=[empty]";
	}	
	
	//se non è stato impostato nessun valore per il cookie imposta il dominio e la pagina di referer
	if (newCookieValue=="-" || newCookieValue=="")
	{
	 	newCookieValue=getReferer();
		if (cookieValue!="-")
		{
			return "&unic=0";
		}
	}
	
	//se non è stato impostato nessun valore per il cookie imposta il cookie di default
	if (newCookieValue=="-" || newCookieValue=="")
	{
		if (cookieValue=="-")
		{
			newCookieValue="dotjs_g=[default]|dotjs_r=[default]|dotjs_n=[default]|dotjs_t=[default]|dotjs_k=[default]|dotjs_j=[default]";
		}
		if (newCookieValue=="-" || newCookieValue=="")
		{
			return "&unic=0";
		}
	}
	
	document.cookie=cookieSectionName+"=dotjs_d="+_domain+"|"+newCookieValue+"; path=/; "+cookieExpires+cookieDomainType;
	
	return "&unic=1";
}
/****************************************************************************************************/
function getCookieForLog()
//restituisce il contenuto di tutte le sezioni del cookie
/****************************************************************************************************/
{
	var cookieSection;
	var escapeCookieSection="";
	
	//if ((cookieSection=getCookieSection(document.cookie,cookieSectionName+"=dotjs_d="+_domain,";"))!="-") escapeCookieSection+=escape(cookieSectionName+"="+cookieSection+";+");
	if ((cookieSection=getCookieSection(document.cookie,cookieSectionName,";"))!="-") 
	{
		//escapeCookieSection+=escape(cookieSectionName+"="+cookieSection+";+");
		escapeCookieSection=escape(cookieSectionName+"="+cookieSection);
	}
	else if ((cookieSection=getCookieSection(location.search,cookieSectionName,";"))!="-") 
	{
		//escapeCookieSection+=escape(cookieSection+";+");
		escapeCookieSection=escape(cookieSection);
	}
	
	
	return escapeCookieSection;
}
/****************************************************************************************************/
function getCookieSection(allSectionValue,sectionName,separator)
//restituisce il contenuto di una determinata sezione del cookie
//Il contenuto può essere preso o dal cookie o da cs-uri-query
//allSectionValue=cookieDocument o location.search cioè tutto quello che c'è dopo il "?"
//sectionName=nome della sezione es:"tracker="+_domain,separator=separatore es:";"
/****************************************************************************************************/
{
	if (!allSectionValue || allSectionValue=="" || !sectionName || sectionName=="" || !separator || separator=="") return "-";
	
	var i,i2,i3;
	var ret="-";
	
	//ricerca la sezione all'interno del cookie
	i=allSectionValue.indexOf(sectionName);
	//ricerca "=" all'interno del nome della sezione
	i3=sectionName.indexOf("=")+1;
	
	if (i > -1)
	{
		//se ha trovato la sezione all'interno del cookie
		//cerca il separatore partendo dall'indice di dove inizia la sezione
		i2=allSectionValue.indexOf(separator,i);
		
		if (i2 < 0)
		{
			//se non c'è il separatore
			i2=allSectionValue.length;
		}
		//recupera tutto il contenuto della sezione del cookie
		ret=allSectionValue.substring((i+i3),i2);
	}
	return ret;
}
/****************************************************************************************************/
function trimPlus(stringValue)
//accetta in ingresso una string e la restituisce con il "+" al posto degli spazi
/****************************************************************************************************/
{
  var ret="";
  if (!stringValue || stringValue=="") return "";
  for (var i=0;i<stringValue.length;i++)
  {
  	if (stringValue.charAt(i)==" ") ret+="+";
  	else ret+=stringValue.charAt(i);
  }
  return ret;
}
/****************************************************************************************************/
function creaGuid()
/****************************************************************************************************/
{
  var guid="";
	var data = new Date();		
	var randomNumber = Math.round(Math.random()*999999999999999);  
	var dt=data.toUTCString();
	
	guid=Date.parse(dt).toString()+"-"+randomNumber.toString()
	
  return guid;
}
/****************************************************************************************************/
function getPageName()
//recupera la pagina corrente
/****************************************************************************************************/
{
	var _path = document.location.pathname;
	var pos;
	
	pos=_path.lastIndexOf("/",pos);						
	
	if (pos==-1) return _path;
	else return _path.substr(pos+1);
}
/****************************************************************************************************/
function getReferer()
/****************************************************************************************************/
{
	if (_referer=="0" || _referer=="" || _referer=="-") return "";
	
	var i=0;
	//pagina del referer
	var refererPage;
	//dominio del referer
	var refererDomain;
	
	if ((i=_referer.indexOf("://"))<0) return "";
	
	//toglie dal referer "http://"
	refererDomain=_referer.substring(i+3,_referer.length);
	
	//se c'è il carattere "/" il referer potrebbe essere una pagina con dei parametri
	if (refererDomain.indexOf("/") > -1)					
	{
		//viene recuperata la pagina e gli eventuali parametri
		refererPage=refererDomain.substring(refererDomain.indexOf("/"),refererDomain.length);
		
		//se c'è il "?" ci sono sicuramente dei parametri. In questo caso vengono tolti per prendere solo la pagina
		if (refererPage.indexOf("?") > -1) refererPage=refererPage.substring(0,refererPage.indexOf("?"));
		
		refererDomain=refererDomain.substring(0,refererDomain.indexOf("/"));
	}
	
	refererDomain=refererDomain.toLowerCase();
	
	if (refererDomain.indexOf("www.")==0) refererDomain=refererDomain.substring(4,refererDomain.length);
	
	return "dotjs_d="+_domain+"|dotjs_g=[empty]|dotjs_r="+trimPlus(refererDomain)+"|dotjs_n=[engine]|dotjs_t=[empty]|"+"dotjs_k="+trimPlus(refererPage)+"|dotjs_j=[empty]";
}
/****************************************************************************************************/
function getPageParams()
//recupera i parametri della pagina
/****************************************************************************************************/
{
	var _params = document.location.search;
	var pos;
	
	pos=_params.indexOf("?",pos);					
	
	if (pos==-1) return _params;
	else return _params.substr(pos+1);
}
/****************************************************************************************************/
function dotjsGetNewPage(jumpPage)
/****************************************************************************************************/
{
	var cookieValue;
	var page;
	
	if (jumpPage && jumpPage!="")
	{
		if (document.cookie)
		{	 		
	 		cookieValue=cookieSectionName+"="+escape(getCookieSection(document.cookie,cookieSectionName+"=",";"));
		}
		if (cookieValue)
		{
	 		if (jumpPage.indexOf("?")<=-1)
	 		{
	 			page=jumpPage+"?"+cookieValue;
	 		}
	 		else
	 		{
	 			page=jumpPage+"&"+cookieValue;
	 		}
		}
		else
		{
			page=jumpPage;
		}
	}
	else
	{
		page=jumpPage;
	}
	return page; 
}
