/**
 * Funcion para el cambio de pestañas y visualizacion de la capa correspondiente
 */

function changeTab (tab,parentId) {

	 var parent=document.getElementById(parentId).rows[0].cells;
	 var aux = "";

	 for (i=0; i<parent.length; i++) {
		  aux = parent[i].id.replace("li_","");
		  document.getElementById("tab_"+aux).style.display = (aux == tab) ? "block" : "none";
		  if(i == 0) {
				style = "wfg_tab wfg_tab_first";
				styleActive = "wfg_tab wfg_tab_activa wfg_tab_first";
		  } else if(i == parent.length-1) {
				style = "wfg_tab wfg_tab_last";
				styleActive = "wfg_tab wfg_tab_activa wfg_tab_last";
		  } else {
				style = "wfg_tab";
				styleActive = "wfg_tab wfg_tab_activa";
		  }
		  document.getElementById("li_"+aux).className = (aux == tab) ? styleActive : style;
	 }

	 return false;

}

/**
 * Funcion que muestra el grafico correspondiente al pasar el raton por encima de
 */
function changeGraph (element,idElement) {

	 var elements=document.getElementsByName(element);
	 var aux = "";

	 for (i=0; i<elements.length; i++) {
		  aux = elements[i].id;
		  document.getElementById(aux).style.display = (aux == idElement) ? "block" : "none";
	 }

	 return false;

}

/**
 * Funcion para seleccionar la pestaña activa en el menu de la ficha de la accion
 */

function changeSheetTab (tab,parentId) {

	 var parent=document.getElementById(parentId).rows[0].cells;
	 var aux = "";

	 for (i=0; i<parent.length; i++) {
		  aux = parent[i].id;
		  if(i == 0) {
				style = "wfg_tab wfg_tab_first";
				styleActive = "wfg_tab wfg_tab_activa wfg_tab_first";
		  } else if(i == parent.length-1) {
				style = "wfg_tab wfg_tab_last";
				styleActive = "wfg_tab wfg_tab_activa wfg_tab_last";
		  } else {
				style = "wfg_tab";
				styleActive = "wfg_tab wfg_tab_activa";
		  }
		  document.getElementById(aux).className = (aux == tab) ? styleActive : style;
	 }

	 return false;

}

/**
 * Funcion para cargar el template de la pestaña seleccionada en la ficha de la accion
 */

function loadSheetTab(tab){
	 document.forms['frmPes'].elements['pestanaActual'].value=tab;
	 document.forms['frmPes'].submit();
}



/**
 * Funciones para ficha analisis tecnical
 */
function cambiarGraficaFAT_v2(idCapa, idForm, ruta, tipo){

	var formu=document.getElementById(idForm);
	var capa=document.getElementById(idCapa);
	var periodo_dias = 0;
	var periodo_mes = "";
	var datosGrafica = "";
	var datosValor = "";
	var url = ruta + "/funcionalidades/graficasAjax/grafica.php";

	if (tipo != "AF") {

		if (formu.periodo.value == 1) {
			periodo_dias = 30;
			periodo_mes = "1 mes";
		} else{
			periodo_dias = 90;
			periodo_mes = "3 meses";
		}

		if (formu.indicador.value == "0"){
			capa.innerHTML = tipo == "indicador" ? "<div style='padding-top:40px;'>Seleccione indicador y periodo</div>" : "<div style='padding-top:40px;'>Seleccione indice y periodo</div>";
			formu.indicador.focus();
			return false;
		}
		if (formu.periodo.value == "0"){
			capa.innerHTML = tipo == "indicador" ? "<div style='padding-top:40px;'>Seleccione indicador y periodo</div>" : "<div style='padding-top:40px;'>Seleccione indice y periodo</div>";
			formu.periodo.focus();
			return false;
		}
	}

	if(tipo == "indicador") {
		datosGrafica = "&idGrafica=LIN_004&idCliente=cofina&idProyecto=at&idOrden=2&numDat=1&tipo=indicador";
		datosValor = "dato1="+formu.isinAccion.value+":"+formu.nombreAccion.value+"&indicador="+formu.indicador.value+":"+formu.indicador.options[formu.indicador.selectedIndex].text+"&periodo="+periodo_dias+":" + periodo_mes;
	} else if(tipo == "comparativa") {
		datosGrafica = "&idGrafica=LIN_003&idCliente=cofina&idProyecto=at&idOrden=2&numDat=2&tipo=comparativa";
		datosValor = "dato1="+formu.isinAccion.value+":"+formu.nombreAccion.value+"&dato2="+formu.indicador.value+":"+formu.indicador.options[formu.indicador.selectedIndex].text+"&periodo="+periodo_dias+":" + periodo_mes;
	} else if(tipo == "AF") {
		datosGrafica = "&idGrafica=LIN_002&idCliente=cofina&idProyecto=af&idOrden=1&numDat="+formu.numDat.value+"&tipo=AF";
		datosValor = "dato1="+formu.dato1.value+"&dato2="+formu.dato2.value+"&dato3="+formu.dato3.value+"&magnitud=m1";
	}
	var param = datosValor + datosGrafica;

	getContenidoUrl(ruta,url,param,idCapa);
	
	return true;
	
}

function cambiar_periodo(opcion) {

	if (opcion == 1)
	{
		document.getElementById('datosactuales').style.display='block';
		document.getElementById('datosunmes').style.display='none';
		document.getElementById('datosdosmeses').style.display='none';
		document.getElementById('datostresmeses').style.display='none';
	}

	else if (opcion == 2)
	{
		document.getElementById('datosactuales').style.display='none';
		document.getElementById('datosunmes').style.display='block';
		document.getElementById('datosdosmeses').style.display='none';
		document.getElementById('datostresmeses').style.display='none'
	}

	else if (opcion == 3)
	{
		document.getElementById('datosactuales').style.display='none';
		document.getElementById('datosunmes').style.display='none';
		document.getElementById('datosdosmeses').style.display='block';
		document.getElementById('datostresmeses').style.display='none'
	}

	else if (opcion == 4)
	{
		document.getElementById('datosactuales').style.display='none';
		document.getElementById('datosunmes').style.display='none';
		document.getElementById('datosdosmeses').style.display='none';
		document.getElementById('datostresmeses').style.display='block'
	}
}

/**
 * Funcion para cargar un grafdico mediante AJAX
 */
function cambiarGraficaHistorica_v2(idCapa, idForm, ruta){

	var formu=document.getElementById(idForm);
	var capa=document.getElementById(idCapa);
	var url = ruta + "/funcionalidades/graficasAjax/grafica.php";
	var datosGrafica = "";
	var datosValor = "";

	if(formu.isin.value == ""){
		capa.innerHTML = "No existen datos para mostrar la gráfica";
		return false;
	} else {

		datosGrafica = "&idGrafica=LIN_002&idCliente=cofina&idProyecto=cotizaciones&idOrden=6&numDat=1";
		datosValor = "dato1="+formu.isin.value+":"+formu.nombre.value+":"+formu.tipo.value+":"+formu.plaza.value+"&periodo="+ formu.periodo.options[formu.periodo.selectedIndex].value;

		var param = datosValor + datosGrafica;

		getContenidoUrl(ruta,url,param,idCapa);

		return true;
	}


}


//Redirige las acciones del combo en el listado de componentes de market overview
function loadComponentAction(url,combo,isin,plaza,calidad,indice){
	var opcion = combo.options[combo.selectedIndex].value.split(":");

	switch(opcion[0]){
		case 'A':
			window.location = url+"?template="+opcion[1];
			break;
		case 'P':
			window.location = url+"?template="+opcion[1];
			break;
		case 'F':
			window.location = url+"?template="+opcion[1]+"&isin="+isin+"&plaza="+plaza+"&calidad="+calidad+"&indice="+indice+"&favorito=add";
			break;
	}
}

// Verifica si se ha seleccionado una opcion antes de votar
function verificaRadioRespuesta(str) {
	var testa;
	var itemchecked=false;
	var tamanhototal=str.length;

	for(i=0;i < tamanhototal; i++) {
		testa=str[i];
		if(testa.checked) {
			itemchecked=true;
		}
	}
	if(!itemchecked) {
		return false;
	}else{
		return true;
	}
}
