// JavaScript Document

function showInflacion(){
	setCookieIndicators("indicators-cookie","inflacion",365);
	document.getElementById('mercados-div').style.display = 'none';
	document.getElementById('inflacion-div').style.display = 'block';
	document.getElementById('header-Mercados').className = '';
	document.getElementById('header-Inflacion').className = 'selected';
}

function showMercados(){
	setCookieIndicators("indicators-cookie","mercados",365);
	document.getElementById('mercados-div').style.display = 'block';
	document.getElementById('inflacion-div').style.display = 'none';
	document.getElementById('header-Mercados').className = 'selected';
	document.getElementById('header-Inflacion').className = '';
}

function loadDoc(){
	var xmlhttp;
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("indicadores-div").innerHTML=xmlhttp.responseText;
			checkCookieIndicators();
		}
	}
	
	var timeNumber = new Date().getTime();
	var ref = window.location.href;
	var url = "/inicio/canales/indicadores_"
	if (ref.indexOf('En')>=0) url = url + "en"; else url = url + "es";
	url = url + ".html?date=" + timeNumber;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function setCookieIndicators(c_name,value,exdays){
	 var exdate=new Date();
	 exdate.setDate(exdate.getDate() + exdays);
	 var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	 document.cookie=c_name + "=" + c_value;
 }
 
 function getCookieIndicators(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 checkCookieIndicators(){
	var tabIndicators=getCookieIndicators("indicators-cookie");
	if (tabIndicators!=null && tabIndicators!="")
	{
		if(tabIndicators=="mercados"){
			showMercados();
		}else if(tabIndicators=="inflacion"){
			showInflacion();
		}else{
			showMercados();
		}
	}
	else 
	{
		setCookieIndicators("indicators-cookie","mercados",365);
	}
}


addLoadEvent(loadDoc);

