function procesarIntersticial(arrayIntersticialPrincipal,arrayIntersticialSecundario,rutaIntersticial)
{				
	var intersticialMostrar = obtenerIntersticialVigente(arrayIntersticialPrincipal,arrayIntersticialSecundario);			
	if(intersticialMostrar!=null)
	{			
		var frecuencia = parseInt(intersticialMostrar[7]);
		var numFrecuencia = parseInt(intersticialMostrar[8]);
		var periodoFrecuencia = parseInt(intersticialMostrar[9]);
		
		if(validarSession())
		{					
			
			if(frecuencia==0)
			{
				mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);
			}
			else
			{
				if(dojo.cookie("ckIntersticial") == null || dojo.cookie("ckIntersticial")=="undefined")
				{
					crearCookie(0,0);
				}
				
				
				datosIntersticial = leerCookie();
				
				if(datosIntersticial[2] != intersticialMostrar[0])
				{
					crearCookie(0,intersticialMostrar[0]);
				}	
				
				//volvemos a leer los datos
				datosIntersticial = leerCookie();
				
				fechaActual= new Date();
				fechaActualiza = new Date(datosIntersticial[1]);
				numVeces = parseInt(datosIntersticial[0]);
				idIntersticialCookie =  new String(datosIntersticial[2]);
				idIntersticialMostrar = new String(intersticialMostrar[0]);
				
				if(periodoFrecuencia==1)
				{
					if(fechaActual.getDate()==fechaActualiza.getDate())
					{						
						if(numVeces<numFrecuencia)
						{
							crearCookie(numVeces+1,intersticialMostrar[0]);
							mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);							
						}
					}
					else
					{
						crearCookie(parseInt(1),intersticialMostrar[0]);
						mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);			
					}
				}
				else if (periodoFrecuencia==2)
				{					
					if(fechaActual.getWeek()==fechaActualiza.getWeek())
					{						
						if(numVeces<numFrecuencia)
						{
							crearCookie(numVeces+1,intersticialMostrar[0]);
							mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);						
						}
					}
					else
					{
						crearCookie(parseInt(1),intersticialMostrar[0]);
						mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);			
					}
				}
				else 
				{				
					if(fechaActual.getMonth()==fechaActualiza.getMonth())
					{					
						if(numVeces<numFrecuencia)
						{
							crearCookie(numVeces+1,intersticialMostrar[0]);
							mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);							
						}
					}
					else
					{
						crearCookie(parseInt(1),intersticialMostrar[0]);
						mostrarShadowBox(intersticialMostrar[2],rutaIntersticial);			
					}
				}
			}
		}
	}
	else
	{			
		dojo.cookie("ckIntersticial","",{expires: -1});
	}
}

function crearCookie(numVeces,idIntersticial)
{	
	intersticialData = new Array(numVeces,new Date(),idIntersticial);
	
	dojo.cookie("ckIntersticial",intersticialData,{expires: 365});
}

function validarSession()
{
	if(dojo.cookie("intJSession") == null || dojo.cookie("intJSession")=="undefined")
	{
		crearCookieJSessionId();
		return true;
	}
	else if(dojo.cookie("intJSession") != dojo.cookie("JSESSIONID"))
	{
		crearCookieJSessionId();
		return true;
	}
	else
	{
		return false;
	}
}

function leerCookie()
{
	arrayDatosIntersticial = dojo.cookie("ckIntersticial").split(',');
	return arrayDatosIntersticial;
}

function crearCookieJSessionId()
{
	dojo.cookie("intJSession",dojo.cookie("JSESSIONID"),{expires: 365});
}

function mostrarShadowBox(archivo,rutaIntersticial)
{
	var randomnumber=Math.floor(Math.random()*1000001);
	rutaFinal= rutaIntersticial+archivo+"?"+randomnumber;
	Shadowbox.open({
		content:    rutaFinal,
		player:     "swf",					
		height:     550,
		width:      800,
		overlayOpacity:0.8
	});
}

function obtenerIntersticialVigente(arrayPrincipal,arraySecundario)
{
	if(arrayPrincipal[1]==1 || arraySecundario[1]==1)
	{
		fechayHoraInicioPrincipal = obtenerFechayHora(arrayPrincipal[3],arrayPrincipal[4]);
		fechayHoraFinPrincipal =obtenerFechayHora(arrayPrincipal[5],arrayPrincipal[6]);
		fechayHoraInicioSecundario = obtenerFechayHora(arraySecundario[3],arraySecundario[4]);
		fechayHoraFinSecundario =obtenerFechayHora(arraySecundario[5],arraySecundario[6]);			
		fechayHoraActual= new Date();
		
		if(arrayPrincipal[1]==1 && arraySecundario[1]==0)
		{
			if(isVigente(fechayHoraInicioPrincipal,fechayHoraFinPrincipal,fechayHoraActual))
			{
				return arrayPrincipal;
			}
		}else if(arrayPrincipal[1]==0 && arraySecundario[1]==1)
		{
			if(isVigente(fechayHoraInicioSecundario,fechayHoraFinSecundario,fechayHoraActual))
			{
				return arraySecundario;
			}
		}else
		{			
			
			if(isVigente(fechayHoraInicioPrincipal,fechayHoraFinPrincipal,fechayHoraActual))
			{
				return arrayPrincipal;
			}
			
			if(isVigente(fechayHoraInicioSecundario,fechayHoraFinSecundario,fechayHoraActual))
			{							
				return arraySecundario;
			}
		}
	}
	return null;
}

Date.prototype.getWeek = function () {  
	// Create a copy of this date object  
	var target  = new Date(this.valueOf());  

	// ISO week date weeks start on monday  
	// so correct the day number  
	var dayNr   = (this.getDay() + 6) % 7;  
	
	// Set the target to the thursday of this week so the  
	// target date is in the right year  
	target.setDate(target.getDate() - dayNr + 3);  
	
	// ISO 8601 states that week 1 is the week  
	// with january 4th in it  
	var jan4    = new Date(target.getFullYear(), 0, 4);  
	
	// Number of days between target date and january 4th  
	var dayDiff = (target - jan4) / 86400000;    
	
	// Calculate week number: Week 1 (january 4th) plus the    
	// number of weeks between target date and january 4th    
	var weekNr = 1 + Math.ceil(dayDiff / 7);    
	
	return weekNr;    
}  

function isVigente(fechayHoraInicio,fechayHoraFin,fechayHoraActual)
{
	if(fechayHoraInicio<=fechayHoraActual && fechayHoraFin>=fechayHoraActual)
	{
		return true;
	}
	
	return false;
}

function obtenerFechayHora(fechayymmdd,hora)
{
	return new Date(fechayymmdd.substring(0,4),fechayymmdd.substring(5,7)-1,fechayymmdd.substring(8,10),hora);
}
