/* la idea de meterlo aqui proviene de Oscar Huarte, de "VASS", para encontrale facil en caso 
de pete.Se cumplirá la ley de murphy y habrá alguna pagina que no incluya este fichero. */
function GrabarCookie(bPortada,bPreHome)
{
var validez = 1;
var caduca = new Date(2013,1,1); 
// controlar el referer
var stCLang = getCookie('idiomaNav');
var stCadena = window.location.href;
var stIdioma = getIdioma(stCadena);

if (stCLang==null) stCLang = '';

if (!bPortada)
	{
	if (stCLang.length==3) /// tiene cookie	
		{
		if (stIdioma!=stCLang && stCLang!='') //actualizar
			setCookie('idiomaNav', stIdioma, caduca,'http://www.barcelona2004.org/');				
		}
	else  // no la tiene 
			setCookie('idiomaNav', stIdioma, caduca,'http://www.barcelona2004.org/');			
	}
else  // son portadas			
{
	/*var stCadUrl = window.location.href;	
	//var stTemp  = stCadUrl.substring(7,stCadUrl.length-1);	
	if (stTemp.indexOf("/")!=-1)
		stTemp = stTemp.substring(0,stTemp.indexOf("/"));
	stServer = stTemp;	*/
	if (bPreHome)   // si es la prehome
	{				
		if (stCLang.length==3) /// tiene cookie	
			{
			window.location.replace("/" + stCLang);			
			}
	}
	else		// portada.cfm
	{	
		
		if (stCLang.length==3) /// tiene cookie	
			{
				window.location.replace("../" + stCLang);		
			}
		else    // no tiene cookie
	  		  //alert("http://" + stServer + "/portada.htm");
				window.location.replace("portada.htm");	
	}
}

}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length; 
  return unescape(dc.substring(begin + prefix.length, end));
}

function getIdioma(stUrl)
{
var stTemp = '';
if (stUrl.indexOf("../../esp/index.html")!=-1)
	stTemp = 'esp';
else
	if (stUrl.indexOf("../../eng/index.html")!=-1)
		stTemp= 'eng';
	else
		if (stUrl.indexOf("../index.html")!=-1)
			 stTemp = 'cat';
		else
			if (stUrl.indexOf("http://www.barcelona2004.org/fra/")!=-1)
				stTemp = 'fra';
		

return stTemp;
}


function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");  
}

