//Expira en un aħo-------
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
//-----------------------
//GeoTrackIp Expira en un dia-------
var ipExpire = new Date();
fixDate(ipExpire);
ipExpire.setTime(ipExpire.getTime() + 1 * 24 * 60 * 60 * 1000);
//-----------------------
var default_zona  = "/disneychannel/home/s/index.html";
//---------------Paises por Zona ------------------------------
var zona_norte  =new Array("MEXICO","MEX"); 
var zona_sur    =new Array("ARGENTINA","ARG","AR","PARAGUAY","PY","PAR","URUGUAY","URU","UY"); 
var zona_centro =new Array("RCA","COLOMBIA","COL","CO","VENEZUELA, BOLIVARIAN REPUBLIC OF","VE","BELIZE","BZ","COSTA RICA","CR","EL SALVADOR","SV","GUATEMALA","GT","HONDURAS","HN","NICARAGUA","NI", 
"PANAMA","PA", "NETHERLANDS ANTILLES","AN","ANTIGUA AND BARBUDA","AG","ARUBA","AW","BAHAMAS","BS","BARBADOS","BB","BERMUDA","BM","CUBA","CU","DOMINICA","DM",
"GRENADA","GD","GUADELOUPE","GP","HAITI","HT","VIRGIN ISLANDS, BRITISH","VG","VIRGIN ISLANDS, U.S.","VI","JAMAICA","JM","MARTINIQUE","MQ","PUERTO RICO","PR","DOMINICAN REPUBLIC","DO","SAINT KITTS AND NEVIS","KN",
"SAINT VINCENT AND THE GRENADINES","VC","SAINT LUCIA","LC","TRINIDAD AND TOBAGO","TT"); 
var zona_pacifico =new Array("CHILE","CHI","PERU","PER","PE","ECUADOR","EC","BOLIVIA","BOL"); 
//---------------------------------------------------------------------
 
 
//----------GENERAL ----------------
 
 function LoadZone(urlZone)
{
	// DISNEYCHANNEL HOME-CENTRO
	var redirectUrl = '';

	if(Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != null && Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == 'true')
	{	
	   // alert('entra en manual');
		redirectUrl =  GetZone();					
	}
	else
	{   
	    //Si no esta habilitado el servicio se resuelve country por el dato almacenado en la cookie.
		if (enableGeoService() == 'true')
		{
			redirectUrl =  SetGeoTrackZone(country);			
		}
		else
		{
			redirectUrl =  SetGeoTrackZone('');				
		}
	}

	alert(redirectUrl);
	if (redirectUrl != urlZone) //'/DisneyChannel/Home_Pacifico/index.html')
	{
	    //Si no encuentra una zona o resuelve mal se redirecciona a la zona por defecto
		if (redirectUrl == null)
		{
			return redirectUrl;
		}
		else
		{
			return default_zona;	
		}
	}
	else
	{
		return urlZone;
	}
}


//Esta funcion se habilita cuando no se ha determinado zona  o cuando se determino zona pero no por el metdodo manaul 
//agrego el parametro seccion para difernciar a donde tiene q redireccionar segun la zona.
function SetGeoTrackZone(country, seccion)
{
	if(Get_Cookie("COOKIE-DISNEY-LATINO-GEOTRACKIP") == null)
	{			
		return SetZoneByCountry(country, seccion);
	}
	else
	{		
		if (Get_Cookie("COOKIE-DISNEY-LATINO-ZONE") != null)
		{  
			if (seccion == null)
			{
				return GetZone();		
			}
			else
			{
				switch (seccion)
				{
				
					case 'concursos':
						return GetZoneConcursos();
						break;												break;
					case 'peliculas_originales':
						return GetZonePeliculas_Originales();
						break;
					case 'programacion':
						return GetZoneProgramacion();
						break;
					case 'series':
						return GetZoneSeries();
						break;
					case 'votacion':
						return GetZoneVotacion();
						break;						 	
					default:
						return '';
				}				
			}
		}		
		else
		{	
			//Esto es cuando se determino la zona por  ip y no ha sido modificado manualmente redirecciono en base a la zona que tiene asignado en el ip.
			return SetZoneByCountry(Get_Cookie("COOKIE-DISNEY-LATINO-GEOTRACKIP"), seccion);		
		}	
	}
}

function SetZoneByCountry(country, seccion)
{	
	if (seccion == null)
	{	
	 	return set_cookie(country,GetZoneByCountry(country));	
	}
	else
	{
		switch (seccion)
		{
			case 'concursos':
				return set_cookieConcursos(country,GetZoneByCountry(country));	
				break;												break;
			case 'peliculas_originales':
				return set_cookiePeliculas_Originales(country,GetZoneByCountry(country));	
				break;
			case 'programacion':
				return set_cookieProgramacion(country,GetZoneByCountry(country));	
				break;
			case 'series':
				return set_cookieSeries(country,GetZoneByCountry(country));	
				break;
			case 'votacion':
				return set_cookieVotacion(country,GetZoneByCountry(country));	
				break;						 	
			default:
				return '';
		}	 	
	}
}

function GetZoneByCountry(country)
{
	var result = 'false';	
	
			for (oi=0;oi<zona_sur.length;oi++)
			{          
				if (zona_sur[oi].toUpperCase() == country.toUpperCase())
				{
					return('SUR')
				
				}
			
			}
		
			for (oi=0;oi<zona_norte.length;oi++)
			{          
				if (zona_norte[oi].toUpperCase() == country.toUpperCase())
				{
					return('NOR')				
				}			
			}				
			
			for (oi=0;oi<zona_pacifico.length;oi++)
			{          
				if (zona_pacifico[oi].toUpperCase() == country.toUpperCase())
				{
					return('PACIFICO')				
				}			
			}				
			
			for (oi=0;oi<zona_centro.length;oi++)
			{          
				if (zona_centro[oi].toUpperCase() == country.toUpperCase())
				{
					return('CENTRO')
				
				}			
			}
	
	//si no lo encuentro en ningun arreglo devuelvo zona norte
	return('SUR');
}

function enableGeoService()
{

	if ((Get_Cookie("COOKIE-DISNEY-LATINO-ZONE") == null && Get_Cookie("COOKIE-DISNEY-LATINO-GEOTRACKIP") == null)			                
	|| 
	(Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == 'false' && Get_Cookie("COOKIE-DISNEY-LATINO-GEOTRACKIP") == null))
	{
		return('true');
	
	}
	else
	{
		return('false');	
	}
}

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}	

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	//alert('Get_Cookie --> ' + name);
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}


function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  		document.cookie = curCookie;
  		//setCookieRoot(name, value, expires, path, domain, secure)
}

function setCookieRoot(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      //((path) ? "; path=" + path : "") +
			"; path=/" +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getQueryParam(name){
	var url = document.location + '';
	q=url.split('?');
	if (q[1]) {
		var pairs = q[1].split('&');
		for (i=0;i<pairs.length;i++) {
			var keyval = pairs[i].split('=');
			if (keyval[0] == name) {
				return(keyval[1]);
			}
		}
	}
	return('');
}



//-----------END GENERAL -------------------------------------------


//---------- SECCION HOME ------------------------------------------

function GetZone()
{    
	var zona = Get_Cookie("COOKIE-DISNEY-LATINO-ZONE");
	
	var redirect_url = '';	 
	
	if (zona != null)
	{
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/home/s/indexGeoTrack.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/home/c/indexGeoTrack.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/home/p/indexGeoTrack.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/home/n/indexGeoTrack.html";
				break;
			default:
				redirect_url="/disneychannel/home/s/indexGeoTrack.html";
		}
	}
	else
	{
		redirect_url="/disneychannel/home/s/indexGeoTrack.html";
	}
		
	return(redirect_url);
}





function set_cookie(pais,zona)
{	
	//Le da siempre prioridad al seteo manual del usuario.
	if (Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == null || Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != true)
	{			
		setCookieRoot("COOKIE-DISNEY-LATINO-PAIS",pais, now);	
		setCookieRoot("COOKIE-DISNEY-LATINO-ZONE",zona, now);		
		setCookieRoot("COOKIE-DISNEY-LATINO-MANUAL", "false", now);
		setCookieRoot("COOKIE-DISNEY-LATINO-GEOTRACKIP", pais, ipExpire)		
	}	

	var redirect_url = getQueryParam('redir');
	var l_domain = "http://"+document.domain.toLowerCase();
	var r_domain = redirect_url.substr(0, l_domain.length).toLowerCase()
	//Solo el mismo dominio de disney latino.
	if(l_domain != r_domain){ redirect_url=''; }
	if(redirect_url==''){
	  //Si no pasa parametro, lo mando a la home de disney channel
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/home/s/indexGeoTrack.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/home/c/indexGeoTrack.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/home/p/indexGeoTrackhtml";
				break;
			case 'NOR':
				redirect_url="/disneychannel/home/n/indexGeoTrack.html";
				break;
			default:
				redirect_url="/disneychannel/home/s/indexGeoTrack.html";
		}
	}
	return redirect_url;
}

//---------------------- END SECCION HOME --------------------------------------


//---------- SECCION CONCURSOS ------------------------------------------

function GetZoneConcursos()
{    
	var zona = Get_Cookie("COOKIE-DISNEY-LATINO-ZONE");
	
	var redirect_url = '';	 
	
	if (zona != null)
	{
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/concursos/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/concursos/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/concursos/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/concursos/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/concursos/s/index.html";
		}
	}
	else
	{
		redirect_url="/disneychannel/concursos/s/index.html";
	}
		
	return(redirect_url);
}





function set_cookieConcursos(pais,zona)
{	
	//Le da siempre prioridad al seteo manual del usuario.
	if (Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == null || Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != true)
	{			
		setCookieRoot("COOKIE-DISNEY-LATINO-PAIS",pais, now);	
		setCookieRoot("COOKIE-DISNEY-LATINO-ZONE",zona, now);		
		setCookieRoot("COOKIE-DISNEY-LATINO-MANUAL", "false", now);
		setCookieRoot("COOKIE-DISNEY-LATINO-GEOTRACKIP", pais, ipExpire)		
	}	

	var redirect_url = getQueryParam('redir');
	var l_domain = "http://"+document.domain.toLowerCase();
	var r_domain = redirect_url.substr(0, l_domain.length).toLowerCase()
	//Solo el mismo dominio de disney latino.
	if(l_domain != r_domain){ redirect_url=''; }
	if(redirect_url==''){
	  //Si no pasa parametro, lo mando a la home de disney channel
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/concursos/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/concursos/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/concursos/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/concursos/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/concursos/s/index.html";
		}
	}
	return redirect_url;
}


//---------------------- END SECCION CONCURSOS --------------------------------------


//---------- SECCION PROGRAMACION ------------------------------------------

function GetZoneProgramacion()
{    
	var zona = Get_Cookie("COOKIE-DISNEY-LATINO-ZONE");
	
	var redirect_url = '';	 
	
	if (zona != null)
	{
		switch (zona.toUpperCase())
		{
		
			case 'SUR':
				redirect_url="/disneychannel/programacion/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/programacion/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/programacion/p/index.html";
				break;
			case 'NOR':	
				redirect_url="/disneychannel/programacion/n/index.html";
				break;		
			default:
				redirect_url="/disneychannel/programacion/s/index.html";
		}
	}
	else
	{
		redirect_url="/disneychannel/programacion/s/index.html";
	}
		
	return(redirect_url);
}





function set_cookieProgramacion(pais,zona)
{	
	//Le da siempre prioridad al seteo manual del usuario.
	if (Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == null || Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != true)
	{			
		setCookieRoot("COOKIE-DISNEY-LATINO-PAIS",pais, now);	
		setCookieRoot("COOKIE-DISNEY-LATINO-ZONE",zona, now);		
		setCookieRoot("COOKIE-DISNEY-LATINO-MANUAL", "false", now);
		setCookieRoot("COOKIE-DISNEY-LATINO-GEOTRACKIP", pais, ipExpire)		
	}	

	var redirect_url = getQueryParam('redir');
	var l_domain = "http://"+document.domain.toLowerCase();
	var r_domain = redirect_url.substr(0, l_domain.length).toLowerCase()
	//Solo el mismo dominio de disney latino.
	if(l_domain != r_domain){ redirect_url=''; }
	if(redirect_url==''){
	  //Si no pasa parametro, lo mando a la home de disney channel
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/programacion/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/programacion/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/programacion/p/index.html";
				break;
			case 'NOR':	
				redirect_url="/disneychannel/programacion/n/index.html";
				break;		
			default:
				redirect_url="/disneychannel/programacion/s/index.html";
		}
	}
	return redirect_url;
}


//---------------------- END SECCION PROGRAMACION --------------------------------------


//---------- SECCION PELICULAS_ORIGINALES ------------------------------------------

function GetZonePeliculas_Originales()
{    
	var zona = Get_Cookie("COOKIE-DISNEY-LATINO-ZONE");
	
	var redirect_url = '';	 
	
	if (zona != null)
	{
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/peliculas_originales/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/peliculas_originales/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/peliculas_originales/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/peliculas_originales/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/peliculas_originales/s/index.html";
		}
	}
	else
	{
		redirect_url="/disneychannel/peliculas_originales/s/index.html";
	}
		
	return(redirect_url);
}





function set_cookiePeliculas_Originales(pais,zona)
{	
	//Le da siempre prioridad al seteo manual del usuario.
	if (Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == null || Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != true)
	{			
		setCookieRoot("COOKIE-DISNEY-LATINO-PAIS",pais, now);	
		setCookieRoot("COOKIE-DISNEY-LATINO-ZONE",zona, now);		
		setCookieRoot("COOKIE-DISNEY-LATINO-MANUAL", "false", now);
		setCookieRoot("COOKIE-DISNEY-LATINO-GEOTRACKIP", pais, ipExpire)		
	}	

	var redirect_url = getQueryParam('redir');
	var l_domain = "http://"+document.domain.toLowerCase();
	var r_domain = redirect_url.substr(0, l_domain.length).toLowerCase()
	//Solo el mismo dominio de disney latino.
	if(l_domain != r_domain){ redirect_url=''; }
	if(redirect_url==''){
	  //Si no pasa parametro, lo mando a la home de disney channel
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/peliculas_originales/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/peliculas_originales/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/peliculas_originales/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/peliculas_originales/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/peliculas_originales/s/index.html";
		}
	}
	return redirect_url;
}


//---------------------- END SECCION PELICULAS_ORIGINALES --------------------------------------


//---------- SECCION SERIES ------------------------------------------

function GetZoneSeries()
{    
	var zona = Get_Cookie("COOKIE-DISNEY-LATINO-ZONE");
	
	var redirect_url = '';	 
	
	if (zona != null)
	{
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/series/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/series/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/series/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/series/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/series/s/index.html";
		}
	}
	else
	{
		redirect_url="/disneychannel/series/s/index.html";
	}
		
	return(redirect_url);
}





function set_cookieSeries(pais,zona)
{	
	//Le da siempre prioridad al seteo manual del usuario.
	if (Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == null || Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != true)
	{			
		setCookieRoot("COOKIE-DISNEY-LATINO-PAIS",pais, now);	
		setCookieRoot("COOKIE-DISNEY-LATINO-ZONE",zona, now);		
		setCookieRoot("COOKIE-DISNEY-LATINO-MANUAL", "false", now);
		setCookieRoot("COOKIE-DISNEY-LATINO-GEOTRACKIP", pais, ipExpire)		
	}	

	var redirect_url = getQueryParam('redir');
	var l_domain = "http://"+document.domain.toLowerCase();
	var r_domain = redirect_url.substr(0, l_domain.length).toLowerCase()
	//Solo el mismo dominio de disney latino.
	if(l_domain != r_domain){ redirect_url=''; }
	if(redirect_url==''){
	  //Si no pasa parametro, lo mando a la home de disney channel
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/series/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/series/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/series/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/series/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/series/s/index.html";
		}
	}
	return redirect_url;
}


//---------------------- END SECCION SERIES --------------------------------------



//---------- SECCION VOTACION ------------------------------------------

function GetZoneVotacion()
{    
	var zona = Get_Cookie("COOKIE-DISNEY-LATINO-ZONE");
	
	var redirect_url = '';	 
	
	if (zona != null)
	{
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/votacion/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/votacion/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/votacion/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/votacion/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/votacion/s/index.html";
		}
	}
	else
	{
		redirect_url="/disneychannel/votacion/s/index.html";
	}
		
	return(redirect_url);
}





function set_cookieVotacion(pais,zona)
{	
	//Le da siempre prioridad al seteo manual del usuario.
	if (Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") == null || Get_Cookie("COOKIE-DISNEY-LATINO-MANUAL") != true)
	{			
		setCookieRoot("COOKIE-DISNEY-LATINO-PAIS",pais, now);	
		setCookieRoot("COOKIE-DISNEY-LATINO-ZONE",zona, now);		
		setCookieRoot("COOKIE-DISNEY-LATINO-MANUAL", "false", now);
		setCookieRoot("COOKIE-DISNEY-LATINO-GEOTRACKIP", pais, ipExpire)		
	}	

	var redirect_url = getQueryParam('redir');
	var l_domain = "http://"+document.domain.toLowerCase();
	var r_domain = redirect_url.substr(0, l_domain.length).toLowerCase()
	//Solo el mismo dominio de disney latino.
	if(l_domain != r_domain){ redirect_url=''; }
	if(redirect_url==''){
	  //Si no pasa parametro, lo mando a la home de disney channel
		switch (zona.toUpperCase())
		{
			case 'SUR':
				redirect_url="/disneychannel/votacion/s/index.html";
				break;
			case 'CENTRO':
				redirect_url="/disneychannel/votacion/c/index.html";
				break;
			case 'PACIFICO':
				redirect_url="/disneychannel/votacion/p/index.html";
				break;
			case 'NOR':
				redirect_url="/disneychannel/votacion/n/index.html";
				break;
			default:
				redirect_url="/disneychannel/votacion/s/index.html";
		}
	}
	return redirect_url;
}


//---------------------- END SECCION VOTACION --------------------------------------