// JavaScript Document
var totalAtivosSelecionados = 5;
var VarQ10Ativo1Votorantim;
var VarQ10Ativo2Votorantim;
var VarQ10Ativo3Votorantim;

var varArquivo = "";
var varAcao = "";
var varAtivo = "";
var ie4 = (document.all) ? true : false;
var DHTML = (document.getElementById || document.all || document.layers);
var layerAtivo;
urlFonte = "finansite-a.ae.com.br"; // dominio/ip de onde estar? os XMLs ex: "192.168.2.35" ip do fsclone
varCliente = "votorantim"; // o cliente para montar a url Ex.: http://192.168.2.35/castrolanda/xml/cambio.xml

var tipoJSP = "";
var ehVoltar = false;

//urlBuscarXMLpesquisa = "http://192.168.2.35/finansite2/jsp/buscarHTML.jsp"; // caminho onde ficar? o PHP/ASP/JSP no servidor do cliente!

/*
urlBuscarXMLpesquisa = "http://flavio:8080/votorantimBusca/buscarHTML.jsp"; // caminho onde ficar? o PHP/ASP/JSP no servidor do cliente!
urlBuscarMinhasAcoes = "http://flavio:8080/votorantimBusca/buscarXMLMinhasAcoes.jsp"; // caminho onde ficar? o PHP/ASP/JSP no servidor do cliente!
*/

contexto="/web/site";
urlBuscarXMLpesquisa = contexto + "/vc/feeder/buscarHTMLpesquisa.jsp"; // caminho onde ficará o PHP/ASP/JSP no servidor do cliente!
urlBuscarMinhasAcoes = contexto + "/vc/feeder/buscarXMLMinhasAcoes.jsp"; // caminho onde ficará o PHP/ASP/JSP no servidor do cliente!
urlBuscarXMLcotacoes = contexto + "/vc/feeder/buscarXMLcotacoes.jsp"; // caminho onde ficará o PHP/ASP/JSP no servidor do cliente

function Cotacao(){}

Cotacao.prototype.xmlHttp = undefined;
Cotacao.prototype.xmlDoc = undefined;

Cotacao.prototype.createXMLHttpRequest = function()
{
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
}

Cotacao.prototype.ler_xml = function() 
{

	var metodo = "GET";
	var _this = this;
	
	this.xmlHttp = this.createXMLHttpRequest();

	if (tipoJSP == "cotacoes")
	{
		urlBuscarXMLchamar = urlBuscarXMLcotacoes + "?fonte=" + urlFonte + "&cliente=" + varCliente + "&arquivo=" + varArquivo;
	}
	else if (tipoJSP == "busca")
	{
		urlBuscarXMLchamar = urlBuscarXMLpesquisa + "?fonte=" + urlFonte + "&cliente=" + varCliente + "&ativo=" + varAtivo;
	}
	else if (tipoJSP == "minhasacoes")
	{
		if (varAcao == "formulario")
		{
			urlBuscarXMLchamar = urlBuscarMinhasAcoes + "?acao=" + varAcao + "&contexto=" + contexto;
		}
		else if (varAcao == "resultado")
		{
			urlBuscarXMLchamar = urlBuscarMinhasAcoes + "?acao=" + varAcao + "&contexto=" + contexto + "&ativo1=" + VarQ10Ativo1Votorantim + "&ativo2=" + VarQ10Ativo2Votorantim + "&ativo3=" +VarQ10Ativo3Votorantim + "&ativo4=" + VarQ10Ativo4Votorantim + "&ativo5=" +VarQ10Ativo5Votorantim;
		}
	}
	url = urlBuscarXMLchamar + "&_xxx_=" + new Date().getTime(); // evitando cache do IE

	str_http = url.substr(0, 6);
	if (str_http == "http:/")
	{
		metodo = "POST";	
	}

	//document.write(url);
	this.xmlHttp.onreadystatechange = function(){_this.esperaXML()};
	
	if(window.XMLHttpRequest) 
	{
		this.xmlHttp.open(metodo, url, true);
	}
	else if(window.ActiveXObject) 
	{
		gambi_ie_dt = new Date();
		gambi_ie_dt_hr = gambi_ie_dt.getTime();
		this.xmlHttp.open(metodo, url, true);
	}

	this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	this.xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");

	this.xmlHttp.setRequestHeader("Expires","0");
	this.xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	this.xmlHttp.setRequestHeader("Pragma", "no-cache");

	this.xmlHttp.send(null);
}

Cotacao.prototype.esperaXML = function() 
{
/**
* 0 - N?o iniciado (Uninitialised)
* 1 - Carregando (Loading)
* 2 - Carregado (Loaded)
* 3 - Interativo (Interactive)
* 4 - Completado (Completed)
*/
	
	if (this.xmlHttp.readyState != 4)
	{
		return;
	}
	else
	{
		 if (this.xmlHttp.status == 200)
		 {
			this.xmlDoc = null;
			//this.xmlDoc = this.xmlHttp.responseXML;
			this.xmlDoc = this.xmlHttp.responseText;
			if (tipoJSP == "cotacoes")
			{
				if (varArquivo == "indicadores.xml") {
					escrever_campo("conteudoIndices", this.xmlDoc);
				} else {
					escrever_campo("conteudoBolsas", this.xmlDoc);
				}
			}
			else if (tipoJSP == "busca")
			{
				escrever_campo("escreverConteudo", this.xmlDoc);
			}
			else if (tipoJSP == "minhasacoes")
			{
				escrever_campo("conteudoMinhasAcoes", this.xmlHttp.responseText);
				if (varAcao == "formulario") {
					$('#toggleBorda').removeClass('bordaBottomRed');
					$('#toggleBorda').addClass('bordaBottom');
				} else if (varAcao == "resultado") {
					$('#toggleBorda').removeClass('bordaBottom');
					$('#toggleBorda').addClass('bordaBottomRed');
				}
			}
		}
	}
}

function pegarDiv(nome)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(nome);
	}
		else if (document.all)
	{
		this.obj = document.all[nome];
	}
		else if (document.layers)
	{
		this.obj = document.layers[nome];
	}
}

function mostraLayer(layerCandidato)
{
	var Layer = new pegarDiv(layerCandidato);
	var LayerFundo = new pegarDiv("modal");
	var LayerGrafico = new pegarDiv("grafimg");
	var	windowHeight = screen.height;
	var	windowWidth = screen.width;
	
	if (ie4)
	{
		LayerFundo.obj.style.filter = "alpha(opacity=70)";
		LayerFundo.obj.style.width = windowWidth-20;
		LayerFundo.obj.style.height = windowHeight-145;
		varTop = ((windowHeight/2) - 330);
		varLeft = ((windowWidth/2) - 250);
	}
	else
	{
		LayerFundo.obj.style.opacity = "0.7";
		LayerFundo.obj.style.width = (windowWidth-16)+"px";
		LayerFundo.obj.style.height = (windowHeight-210)+"px";
		varTop = ((windowHeight/2) - 330)+"px";
		varLeft = ((windowWidth/2) - 250)+"px";
	}

    if (Layer.obj.style.visibility == 'hidden')
	{
		window.scrollTo(0,0);
		Layer.obj.style.top = varTop;
		Layer.obj.style.left = varLeft;
		LayerFundo.obj.style.display = 'block';
	   	Layer.obj.style.display = 'block';
	   	Layer.obj.style.visibility = 'visible';
	   	LayerGrafico.obj.style.visibility = 'hidden';
	}
	else
	{
		LayerFundo.obj.style.display = 'none';
	   	Layer.obj.style.visibility='hidden';
	   	LayerGrafico.obj.style.visibility = 'visible';
	}
}

function escrever_campo(id, texto) 
{
  if (!DHTML) return;
  var x = new pegarDiv(id);
  if (x.obj) 
  {
	  x.obj.innerHTML = texto;
  }
  return;
}

function buscarConteudo(e, parVoltar)
{
	tipoJSP = "busca";
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

	if (parVoltar == "voltar")
	{
		ehVoltar = true;
		varAtivo = new pegarDiv("txtCotacao");
		varAtivo = varAtivo.obj.value;
		cot_cotacoes = new Cotacao();
		escrever_campo("escreverConteudo", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
//		escrever_campo("escreverConteudo", "<br><center>carregando ...</center>");
		escrever_campo("voltarID", "&nbsp;");
		setTimeout("cot_cotacoes.ler_xml()", 1000);
	}
	else
	{

		if (keynum == 13 || keynum == 0 || keynum == 1)
		{
			varAtivo = new pegarDiv("txtCotacao");
			varAtivo = varAtivo.obj.value;
			cot_cotacoes = new Cotacao();
			escrever_campo("escreverConteudo", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
//			escrever_campo("escreverConteudo", "<br><center>carregando ...</center>");
			escrever_campo("voltarID", "&nbsp;");
			mostraLayer("popUpBusca");
			setTimeout("cot_cotacoes.ler_xml()", 1000);
		}
	}
}

function abreJanela2(paramAtivo, num)
{
	layerAtivo = num;
	varAtivo = paramAtivo;
	cot_cotacoes = new Cotacao();
	escrever_campo("escreverConteudo", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
//	escrever_campo("escreverConteudo", "<br><center>carregando ...</center>");
	escrever_campo("voltarID", "&larr; Voltar");
	//mostraLayer("popUpBusca");
	setTimeout("cot_cotacoes.ler_xml()", 1000);	
}

/*
* fun??es para verificar cookie e carregar o minhas a??es.
*
*/
var ativo1;
var ativo2;
var ativo3;
var ativo4;
var ativo5;

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) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "");
  document.cookie = curCookie;
}

function deleteCookie(name, path, domain) 
{
  if (getCookie(name)) 
  {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

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 checa_click(campoCheck)
{
	var cont_check_click = 0;
	if (campoCheck.checked)
	{
		if (document.form_ativos_set.hd_q10_ativo1.value == "")
		{
			document.form_ativos_set.hd_q10_ativo1.value = campoCheck.value;
		}
		else if (document.form_ativos_set.hd_q10_ativo2.value == "")
		{
			document.form_ativos_set.hd_q10_ativo2.value = campoCheck.value;
		}
		else if (document.form_ativos_set.hd_q10_ativo3.value == "")
		{
			document.form_ativos_set.hd_q10_ativo3.value = campoCheck.value;
		}
		else if (document.form_ativos_set.hd_q10_ativo4.value == "")
		{
			document.form_ativos_set.hd_q10_ativo4.value = campoCheck.value;
		}
		else if (document.form_ativos_set.hd_q10_ativo5.value == "")
		{
			document.form_ativos_set.hd_q10_ativo5.value = campoCheck.value;
		}
		else
		{
			campoCheck.checked = false;
			alert("Selecione até " + totalAtivosSelecionados + " indicadores financeiros.");
		}
	}
	else
	{
		if (document.form_ativos_set.hd_q10_ativo1.value == campoCheck.value)
		{
			document.form_ativos_set.hd_q10_ativo1.value = "";
		}
		else if (document.form_ativos_set.hd_q10_ativo2.value == campoCheck.value)
		{
			document.form_ativos_set.hd_q10_ativo2.value = "";
		}
		else if (document.form_ativos_set.hd_q10_ativo3.value == campoCheck.value)
		{
			document.form_ativos_set.hd_q10_ativo3.value = "";
		}
		else if (document.form_ativos_set.hd_q10_ativo4.value == campoCheck.value)
		{
			document.form_ativos_set.hd_q10_ativo4.value = "";
		}
		else if (document.form_ativos_set.hd_q10_ativo5.value == campoCheck.value)
		{
			document.form_ativos_set.hd_q10_ativo5.value = "";
		}
	}
}

function seta_cookie()
{
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);

	deleteCookie('q10_ativo1_votorantim');
	deleteCookie('q10_ativo2_votorantim');
	deleteCookie('q10_ativo3_votorantim');
	deleteCookie('q10_ativo4_votorantim');
	deleteCookie('q10_ativo5_votorantim');

	ativo1 = document.form_ativos_set.hd_q10_ativo1.value;
	ativo2 = document.form_ativos_set.hd_q10_ativo2.value;
	ativo3 = document.form_ativos_set.hd_q10_ativo3.value;
	ativo4 = document.form_ativos_set.hd_q10_ativo4.value;
	ativo5 = document.form_ativos_set.hd_q10_ativo5.value;

	ativo1 = (ativo1 == "") ? "-x-" : ativo1;
	ativo2 = (ativo2 == "") ? "-x-" : ativo2;
	ativo3 = (ativo3 == "") ? "-x-" : ativo3;
	ativo4 = (ativo4 == "") ? "-x-" : ativo4;
	ativo5 = (ativo5 == "") ? "-x-" : ativo5;
	
	setCookie("q10_ativo1_votorantim", ativo1, now, "http://10.40.15.39:7110/");
	setCookie("q10_ativo2_votorantim", ativo2, now, "http://10.40.15.39:7110/");
	setCookie("q10_ativo3_votorantim", ativo3, now, "http://10.40.15.39:7110/");
	setCookie("q10_ativo4_votorantim", ativo4, now, "http://10.40.15.39:7110/");
	setCookie("q10_ativo5_votorantim", ativo5, now, "http://10.40.15.39:7110/");
	escrever_campo("conteudoMinhasAcoes", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
//	escrever_campo("conteudoMinhasAcoes", "<center>Aguarde...</center>");
	checarMinhasAcoes();
}

function checa_ativos()
{
	var selecionado = false;
	var cont_check = totalAtivosSelecionados;

	var default_check = false;
	if(document.form_ativos_set.hd_q10_ativo1.value == "")
	{
		cont_check--;
	}
	
	if(document.form_ativos_set.hd_q10_ativo2.value == "")
	{
		cont_check--;
	}
	
	if(document.form_ativos_set.hd_q10_ativo3.value == "")
	{
		cont_check--;
	}
	
	if (document.form_ativos_set.hd_q10_ativo4.value == "")
	{
		cont_check--;
	}
	
	if (document.form_ativos_set.hd_q10_ativo5.value == "")
	{
		cont_check--;
	}
	
	if(cont_check > totalAtivosSelecionados || cont_check == 0)
	{
		alert("Selecione até " + totalAtivosSelecionados + " indicadores financeiros.");
	} else {
		seta_cookie();
	}
}

function checarMinhasAcoes()
{

	escrever_campo("conteudoMinhasAcoes", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
//	escrever_campo("conteudoMinhasAcoes", "<center>Aguarde...</center>");
	VarQ10Ativo1Votorantim = getCookie("q10_ativo1_votorantim");
	VarQ10Ativo2Votorantim = getCookie("q10_ativo2_votorantim");
	VarQ10Ativo3Votorantim = getCookie("q10_ativo3_votorantim");
	VarQ10Ativo4Votorantim = getCookie("q10_ativo4_votorantim");
	VarQ10Ativo5Votorantim = getCookie("q10_ativo5_votorantim");
	
	// xmlFonte = http://192.168.2.35/votorantim/xml/votor_cotacoes.xml
	tipoJSP = "minhasacoes";
	cot_cotacoes = new Cotacao();

	if (!VarQ10Ativo1Votorantim)
	{
		varAcao = "formulario";
		setTimeout("cot_cotacoes.ler_xml()", 3000);	
		//alert("n?o possui Cookie busco o formul?rio e mostra.");
		/*
		*	n?o possui Cookie busco o formul?rio e mostra.
		*/
		// BuscarFormularioMinhasAcoes();
		$('#toggleBorda').removeClass('bordaBottomRed');
		$('#toggleBorda').addClass('bordaBottom');
	}
	else
	{
		varAcao = "resultado";
		setTimeout("cot_cotacoes.ler_xml()", 3000);	
		//alert("vou buscar resultado e mostrar.");
		/*
		*	vou buscar resultado e mostrar.
		*/
		// BuscarResultadoMinhasAcoes();
		$('#toggleBorda').removeClass('bordaBottomRed');
		$('#toggleBorda').addClass('bordaBottom');
	}
}

function personalizar()
{
	deleteCookie('q10_ativo1_votorantim');
	deleteCookie('q10_ativo2_votorantim');
	deleteCookie('q10_ativo3_votorantim');
	deleteCookie('q10_ativo4_votorantim');
	deleteCookie('q10_ativo5_votorantim');
	document.form_ativos_set.hd_q10_ativo1.value = "";
	document.form_ativos_set.hd_q10_ativo2.value = "";
	document.form_ativos_set.hd_q10_ativo3.value = "";
	document.form_ativos_set.hd_q10_ativo4.value = "";
	document.form_ativos_set.hd_q10_ativo5.value = "";
	escrever_campo("conteudoMinhasAcoes", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
//	escrever_campo("conteudoMinhasAcoes", "<center>Aguarde...</center>");
	checarMinhasAcoes();
}

function carregarCotacoes( pArquivo){
	tipoJSP = "cotacoes";
	varArquivo = pArquivo;
	if (varArquivo == "indicadores.xml") {
		escrever_campo("conteudoIndices", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
	} else {
		escrever_campo("conteudoBolsas", "<center><img src="+contexto+"/vc/images/carregando.gif></center>");
	}
	cot_cotacoes = new Cotacao(1, 0);
	setTimeout("cot_cotacoes.ler_xml()", 3000);
	
}

function mostrarConteudo(element) {

	var avo = element.parentNode.parentNode;
	var itemClicado = false;
	
	try {
	
		for (var i = 0; i < avo.childNodes.length; i++) {
	
			// despreza o primeiro filho pq nao e do tipo abrir/fechar
			if (avo.childNodes[i].nodeName != "#text") {

				var pai = avo.childNodes[i];

				for (var j = 0; j < pai.childNodes.length; j++) {
				
					if (pai.childNodes[j].nodeName != "#text") {
					
						var neto = pai.childNodes[j];
						
						if (neto.className.indexOf("titSecao") >= 0) {
							
							itemClicado = neto == element;
							
							for (var k = 0; k < neto.childNodes.length; k++) {
								
								if (neto.childNodes[k].nodeName != "#text") {
								
									var imgNode = neto.childNodes[k];
								
									if (imgNode != null && imgNode.nodeName == "IMG") {
									
										var srcImg = imgNode.src;
										if (itemClicado) {
											if (srcImg.indexOf("icoSetaEsconde.gif") > 0) {
												imgNode.src = srcImg.replace("icoSetaEsconde.gif", "icoSetaMostra.gif");
											} else {
												imgNode.src = srcImg.replace("icoSetaMostra.gif", "icoSetaEsconde.gif");
											}
										} else {
											imgNode.src = srcImg.replace("icoSetaEsconde.gif", "icoSetaMostra.gif");
										}
									
									}
									
								}
								
							}
							
						} else {
							
							// elemento clicado
							if (itemClicado) {
								if (neto.style.display == 'block') {
									neto.style.display = 'none';
								} else {
									neto.style.display = 'block';
								}
							} else {
								neto.style.display = 'none';
							}
						
						}
		
					}
					
				}
				
			}
			
		}
	
	} catch (ex) {
		alert(ex);
	}
	var hideAcoes = document.getElementById("hideAcoes");

      

      if (element.id==hideAcoes.id) {

            if(navigator.appName=="Microsoft Internet Explorer") { 

            

                  if (hideAcoes.style.background == "url(/web/export/system/modules/vc/resources/images/btnacessocotacoes1.gif)") {

                        hideAcoes.style.background = "url(/web/export/system/modules/vc/resources/images/btnacessocotacoes.gif)";

                  } else { hideAcoes.style.background = "url(/web/export/system/modules/vc/resources/images/btnacessocotacoes1.gif)"; }

                  

            } else {

                  if (hideAcoes.style.background == "transparent url(/web/export/system/modules/vc/resources/images/btnacessocotacoes1.gif) repeat scroll 0% 0%") {

                        hideAcoes.style.background = "transparent url(/web/export/system/modules/vc/resources/images/btnacessocotacoes.gif) repeat scroll 0% 0%";

                  } else { hideAcoes.style.background = "transparent url(/web/export/system/modules/vc/resources/images/btnacessocotacoes1.gif) repeat scroll 0% 0%"; }   

            }

      } else {

   	   hideAcoes.style.background = "transparent url(/web/export/system/modules/vc/resources/images/btnacessocotacoes1.gif)";

      }
      		
}