//---------------------------------------------------------------------
// Menejo de Cookie para Targeting Behaviour EMOL
// Gustavo V.R. - Basado en documentación W3Schools.com
// Octubre 2011
//---------------------------------------------------------------------
function setCookieTgt(strTargeting)
{
//var expirationDate = new Date();
//expirationDate.setFullYear(2020); 
expirationDate = new Date(2020, 1, 1, 0, 0, 0,0);
//alert(expirationDate);
var strExpiracion = ";expires=" + expirationDate.toUTCString();

var strTags   = UpdateValuesTgt(strTargeting);

document.cookie="TgtBehavJs=" + strTags + strExpiracion+';path=/';

//muestra la cookie:
//alert("cookie:TgtBehavJs=" + strTags + strExpiracion);
}
//---------------------------------------------------------------------
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);	
    }
  }
}
//---------------------------------------------------------------------
function UpdateValuesTgt(strTargeting)
{
var ArrayTags  = strTargeting.split(",");
var strTag;
var strTgtValoresUpdated="";

var tgtCookie  = getCookie("TgtBehavJs");
if (tgtCookie!=null)
{
    // La cookie ya existe
	var ArrayValoresCookie = tgtCookie.split("&");
	//Actualizando contadores actuales en la cookie
	for (var i=0; i<ArrayValoresCookie.length ; i++ )
		{
		strTag = ArrayValoresCookie[i].substr(0,ArrayValoresCookie[i].indexOf("="));	
		if (strTag=="CreationDate")
			{
			// Escribe la fecha de creacion que ya existe
			strTgtValoresUpdated = "CreationDate=" + ArrayValoresCookie[i].substr(ArrayValoresCookie[i].indexOf("=")+1) + strTgtValoresUpdated;
			}
			else
			{
			// Escribe los Tags
			strValorActual = ArrayValoresCookie[i].substr(ArrayValoresCookie[i].indexOf("=")+1);		
			if (strTargeting.match(strTag)!=null || strTag=="NumPage")
				{		
				strValorActual = eval(strValorActual)+1;            
				}		 
			strTgtValoresUpdated+="&"+strTag+"="+strValorActual;
			}			
		}
}
else
	{
	// La cookie no existe, se setean valores iniciales
	var Hoy = new Date(); 
    strTgtValoresUpdated= "CreationDate="+ Hoy.getDate() + "-" + (Hoy.getMonth()+1) + "-" + Hoy.getFullYear();
	strTgtValoresUpdated+="&NumPage=1";
	}


// Agregando los Tags que no están en la cookie
for (var i=0; i<ArrayTags.length ; i++ )
	{	
	if (strTgtValoresUpdated.match(ArrayTags[i])==null)
		{
		strTgtValoresUpdated+="&"+ArrayTags[i]+"=1"; 
		//alert(ArrayTags[i]+" agregado a la cookie");
		}
	}

	
return strTgtValoresUpdated;
}
//---------------------------------------------------------------------
function setTargeting(strTag)
{
setCookieTgt(strTag);

}
//---------------------------------------------------------------------


