var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

function getRef(id) {
if (isDOM) return document.getElementById(id);
if (isIE4) return document.all[id];
if (isNS4) return document.layers[id];
}

function getSty(id) {
return (isNS4 ? getRef(id) : getRef(id).style);
}

function clica_mnu(x)
{

	var v_pgs = new Array();
	v_pgs[1] = "empr_hist.htm";
	v_pgs[2] = "empr_iso.htm";
	v_pgs[3] = "empr_frota.htm";
	v_pgs[4] = "empr_loc.htm";
	v_pgs[5] = "empr_forn.htm";
	v_pgs[6] = "empr_feiras.htm";
	v_pgs[7] = "empr_patr.htm";
	v_pgs[8] = "constr.htm";
	
	for (i=1;i<=8;i++)
		getRef("img_0"+i).src = "imagens/empr_abas_0"+i+".jpg";
	getRef("img_0"+x).src = "imagens/empr_abas1_0"+x+".jpg";
	window.open(v_pgs[x],"frm_pg");	
}

function busca_res() 
{		
	if(document.frm_busca.sl_tipo.value!="") x = document.frm_busca.sl_tipo.value; else x=" ";
	if(document.frm_busca.txt_cod.value!="") y = document.frm_busca.txt_cod.value; else y=" ";
	cpaint_call(
	  'lib/func.php',
	  'GET',
	  'busca_cod',
	  x,
	  y,
	  atualiza_busca,
	  'TEXT');
}

function atualiza_busca(valorDeRetorno)
{
	//Exibe as resinas que obedecem aos parâmetros
	var aux = valorDeRetorno.split("||");	
	if(aux.length > 3)
	{
		getSty("res_conteudo").display = "none";
		resp = "<p style='font-size:08pt;line-height:130%'>";
		v_segm = "";
		v_tipo = "";
		for (var i=1;i<aux.length-1;i++)
		{			
			var linha = aux[i].split("|");
			if(linha[3] != v_tipo)
			{
				v_tipo = linha[3];
				resp += "<span style='color:#300;font-weight:bold;border-bottom:1px solid #000'>" + linha[3] + "</span><br />";
			}
			if (linha[1] != v_segm) 
			{
				v_segm = linha[1];
				resp += "<span style='color:#008'>" + v_segm + "</span><br />";
			}
			resp += "<a href='#' onclick=\"atualiza_busca('3||" + linha[0] + "|" + linha[1] + "|" + linha[2] + "|" + linha[3] + "||0');\">" + linha[0] + "</a><br />";
		}
		getRef("resina_res").innerHTML = "<span style='font-size:10px'>" + resp + "</span>";
	}
	else
		if (aux.length == 3)
		{
			if(aux[2]!="0")
				getRef("resina_res").innerHTML = "";
			var tam = getRef("tab_res").getElementsByTagName("TR").length;
			if(tam > 2)
				for (i=2;i<tam;i++)
					getRef("tab_res").deleteRow(2);
			getSty("res_conteudo").display = "";
			var linha = aux[1].split("|");
			cpaint_call(
			  'lib/func.php',
			  'GET',
			  'busca_res',
			  linha[2],
			  linha[3],
			  dados_res,
			  'TEXT');
		}
		else
		{
			getSty("res_conteudo").display = "none";
			getRef("resina_res").innerHTML = "N&atilde;o foram encontradas resinas com esta grade...";
		}
}

function dados_res(valorDeRetorno)
{
	//alert("aqui..." + valorDeRetorno);
	var aux = valorDeRetorno.split("||");
	var v_dado = aux[0].split("|");
	getRef("txt_aplic").innerHTML = v_dado[0];
	getRef("txt_segm").innerHTML = "<strong>Segmento: </strong>" + v_dado[1];
	var v_res = aux[1].split("|");
	getRef("txt_tipo").innerHTML = v_res[0] + " - " + v_res[1];
	//insere a tabela de grades similares
	var tbody = getRef("tab_res").getElementsByTagName("TBODY")[0];
	for (i=2;i<aux.length-1;i++)
	{
		var v_lista = aux[i].split("|");
		if (v_lista[4]!=" ") v_lista[1] += "<sup><b>" + v_lista[4] + "</b></sup>";
		if (v_lista[5]!=" ") v_lista[2] += "<sup><b>" + v_lista[5] + "</b></sup>";
		if (v_lista[6]!=" ") v_lista[3] += "<sub><b>" + v_lista[6] + "</b></sup>";
		var row = document.createElement("TR");		
		for (j=0;j<=3;j++)
		{
			var td1 = document.createElement("TD");			
			//td1.appendChild(document.createTextNode(v_lista[j]));
			row.appendChild(td1);
			td1.innerHTML = v_lista[j];
		}
		tbody.appendChild(row);			
	}
	var tam1 = getRef("tab_res").getElementsByTagName("TR").length;
	for (i=2;i<tam1;i++)	
		if (i%2!=0)
			getRef("tab_res").getElementsByTagName("TR")[i].style.backgroundColor="#DDDDDD";
	getRef("txt_legenda").innerHTML = v_res[2];
}

//=========================================================================
// NOTÍCIAS
//=========================================================================
function busca_not() 
{		
	
	if (document.frm_busca.dt_ref.value!="")   x = document.frm_busca.dt_ref.value; else x = " ";
	if (document.frm_busca.txt_word.value!="") z = document.frm_busca.txt_word.value; else z = " ";
	cpaint_call(
	  'lib/func.php',
	  'GET',
	  'busca_not',
	  x,
	  z,
	  lista_not,
	  'TEXT');
}

function lista_not(valorDeRetorno)
{
	var aux = valorDeRetorno.split("||");
	getSty("news_conteudo").visibility = "hidden";
	getSty("busca_res").visibility = "visible";
	var resp = '<p><img src="imagens/tit_result.jpg" alt="" /></p>';
	resp += "<p style='font-size:08pt;line-height:130%'>"
	for (var i=0;i<aux.length-1;i++)
	{
		var linha = aux[i].split("|");
		resp += "<span style='font-weight:900;color:#008'>" + linha[1] + "</span><br />"
		resp += "<a href='#' onClick=\"mostra_not('" + linha[0] + "');\"><strong>" + linha[2] + "</strong></a>";
		resp += "<br />" + linha[3] + "...<br /><br />";
	}
	getRef("busca_res").innerHTML = resp;
}

function mostra_not(x)
{
	cpaint_call(
	  'lib/func.php',
	  'GET',
	  'mostra_not',
	  x,
	  exibe_not,
	  'TEXT');
}

function exibe_not(valorDeRetorno)
{
	aux = valorDeRetorno.split("|");
	getSty("busca_res").visibility = "hidden";
	getSty("news_conteudo").visibility = "visible";
	getRef("titulo").innerHTML = aux[1];
	getRef("fonte").innerHTML = aux[4];
	getRef("txt_corpo").innerHTML = aux[2];
	getRef("img01").src = "imagens/news_img/" + aux[3];	
}