var appVer = navigator.appVersion.toLowerCase();
var iePos  = appVer.indexOf('msie');
var ieVer = 0;
var podefscommand = "nao";
var iebug = false;

if (iePos !=-1) {
	ieVer = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
	if(ieVer < 7) iebug = true;
	if(ieVer > 5.5) podefscommand = "sim";
}

if(podefscommand) document.write('<scr' + 'ipt for="menu_flash" event="FSCommand(funcao)">eval(funcao);</sc' + 'ript>');

function $(id){
	return document.all? document.all[id] : document.getElementById(id);
}

var tags = { "select":iebug, "applet":true };
var apagou = [];

function pegaMenu(){
	return home? $("div_menu") : $("div_menu_interna");
}

function abremenu(){
	pegaMenu().style.clip = "rect(0 600px 540px 0)";
	for(tag in tags){
		if(tags[tag]){
			items = document.getElementsByTagName(tag);
			for(x = 0; x < items.length; x++){
				if(items[x].style.visibility==""){
					apagou[tag+"-"+x] = true;
					items[x].style.visibility = "hidden";
				}
			}
		}
	}
}

function fechamenu(){
	pegaMenu().style.clip = "rect(0 232px 540px 0)";
	for(tag in tags){
		if(tags[tag]){
			items = document.getElementsByTagName(tag);
			for(x = 0; x < items.length; x++){
				if(apagou[tag+"-"+x]) items[x].style.visibility = "";
			}
		}
	}
}

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
}

addEvent(window, "load", function()
{
	try
	{
		if($("div_conteudo_interna").offsetHeight < 200)
		{
			$("div_conteudo_interna").style.height = "200px";
		}	
	}
	catch(e){}
	
}, false);

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue); 
 }
 
function form(campo){
	if(window.navigator.appName.toLowerCase().indexOf("microsoft") > -1){
		return document.formulario.item(campo);
	}
	else {
		return document.formulario[campo];
	}
}

function soNumeros(obj) {

	tecla = event.keyCode;
	
	if(tecla == 8 || tecla == 9 || tecla == 46 || (tecla >= 37 && tecla <= 40) || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105)){
		return true;
	}
	else return false;

}

function abrir(url){
	window.open(url, "_blank");
}

function janela(url, w, h){
	try {
		jan = open(url, "", "width=" + w + ",height=" + h + ",left=" + ((screen.availWidth-w)/2) + ",top=" + ((screen.availHeight-h)/2));
		jan.focus();
	}
	catch(e){
		alert(idioma=="port"? "Por favor desabilite seu anti-popup." : "Please disable your anti-popup.");
	}
}

function janelaScroll(url, w, h){
	try {
		jan = open(url, "", "scrollbars=yes,width=" + w + ",height=" + h + ",left=" + ((screen.availWidth-w)/2) + ",top=" + ((screen.availHeight-h)/2));
		jan.focus();
	}
	catch(e){
		alert(idioma=="port"? "Por favor desabilite seu anti-popup." : "Please disable your anti-popup.");
	}
}

function webcast(tri, ano){
	janela("../apresentacoes/webcast.asp?tri=" + tri + "&ano=" + ano, 790, 502);
}

function balancos(){
	window.open("http://www.mediagroup.com.br/BalInt/index.asp?empresa=CTEEP&tabela=1&idioma=" + (idioma=="esp"? "port" : idioma), "BalInt" + idioma, 
				"width=" + (screen.availWidth-10) + ",height=" + (screen.availHeight-60) + ",top=0,left=0,resizable=yes,scrollbars=yes,status=yes");
}

function shadowbox(titulo, url, w, h){

	Shadowbox.init({ overlayOpacity: 0.8, overlayColor: '#cccccc', skipSetup: true, modal: false });

	Shadowbox.open({
		player:     'iframe',
		title:      titulo,
		content:    url,
		height:     h,
		width:      w
	});
}

function ValorRadio(radio){
	tempValorRadio = false;
	for(VR_x = 0; VR_x < radio.length; VR_x++){
		if(radio[VR_x].checked){ tempValorRadio = radio[VR_x].value; break; }
	}
	return tempValorRadio;
}

function checarTodos(checkbox, checkboxes){
	for(ct_x = 0; ct_x < checkboxes.length; ct_x++){
		checkboxes[ct_x].checked = checkbox.checked;
	}
}

function validaCNPJ(pcgc){

 if (pcgc.length != 14) {
  sim=false;
  return false;
  }
 else {sim=true}

  if (sim)
  {
  for (i=0;((i<=(pcgc.length-1))&&sim); i++)
  {
   val = pcgc.charAt(i)

   if((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")&&(val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
   }
   if (sim)
   {
    m2 = 2
    soma1 = 0
    soma2 = 0
    for (i=11;i>=0;i--)
    {
     val = eval(pcgc.charAt(i))

     m1 = m2
  if (m2<9) { m2 = m2+1}
  else {m2 = 2}
  soma1 = soma1 + (val * m1)
  soma2 = soma2 + (val * m2)
    } 

  soma1 = soma1 % 11
  if (soma1 < 2) {  d1 = 0}
   else { d1 = 11- soma1}

     soma2 = (soma2 + (2 * d1)) % 11
  if (soma2 < 2) { d2 = 0}
   else { d2 = 11- soma2}

    if ((d1==pcgc.charAt(12)) && (d2==pcgc.charAt(13)))
   { return true; }
   else return false;
   }
 }

}

function pula(atual, proximo){
	if(atual.value.length==atual.maxLength) $(proximo).focus();
}

function validaNum(numero, quantidade){
	return (/^\d+$/.test(numero)&&numero.length>=quantidade);
}

// ==========================================================================

// Funções Combo Flash

var combo = false;

function combo_abre(qual){
	if(combo!=false&&document.all) combo_fecha(combo);
	combo = qual;
	if(document.all) document.all["flash_" + qual].style.height = 202;
	else document.embeds["flash_" + qual].height = 202;
}

function combo_fecha(qual){
	if(qual!=false){
		if(document.all){
			document.all["flash_" + qual].style.height = 20;
			document.all["flash_" + qual].setVariable("_root.executa_funcao", "combo_fecha");
		}
		else document.embeds["flash_" + qual].height = 20;
		combo = false;
	}
}

// ==========================================================================

function TrocaIdioma(novoidioma){

local = Mid(location.href,43,12);
if (local != "fornecedores")
{
		location = location.href.replace("/" + idioma + "/", "/" + novoidioma + "/");
}
else
{
	if (novoidioma == "eng"){location = '../../../eng/institucional/home/';}
	if (novoidioma == "esp"){location = '../../../esp/institucional/home/';}
}
	
	

}

function ingles()
{
	location = "../../../eng"; 
}

function espanhol()
{
	location = "../../../esp";
}

function Mid(str, start, len)
{
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}

// ===========================================================================


function flash(movie, flashvars, width, height, retorna){
	swfid = movie.split("/");
	swfid = swfid[swfid.length-1].replace(".swf", "");
	strSWF= '<object id="' + swfid + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + width + '" height="' + height + '">'+
			'<param name="movie" value="' + movie + '">'+
			'<param name="quality" value="high">'+
			'<param name="wmode" value="transparent">'+
			'<param name="FlashVars" value="' + flashvars + '">'+
			'<embed name="' + swfid + '" src="' + movie + '" width="' + width + '" height="' + height + '" flashvars="' + flashvars + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"></embed>'+
			'</object>';
	if(!retorna) this.document.write(strSWF);
	else return(strSWF);
}