// Funcao para gerar numero aleatorio
function rand(numero) {
	return Math.floor(Math.random()*numero);
}

function direcionar(arquivo) {
	document.location.href = arquivo;
}

function popup(arquivo, titulo, largura, altura, tipo) {
		
	if( typeof(tipo) == "undefined" ){
		novaJanela = window.open(arquivo, titulo, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+largura+',height='+altura+',top=0,left=0');
	} else if(tipo == "novo" || tipo == "nova") {
		novaJanela = window.open(arquivo, titulo, '');
	} else {
		novaJanela = window.open(arquivo, titulo, tipo);
	}

	// Verifica se abriu a janela
	if (!novaJanela) {

		elemento1 = 'document.getElementById("popupBloqueado")';
		elemento2 = 'document.getElementById("estrutura")';
		elemento3 = 'document.getElementById("bannerLateral")';

		// Verifica se existe o DIV
		if (eval(elemento1)) {
			eval(elemento1).style.display = 'block';
		}

		// Verifica se existe o DIV
		if (eval(elemento2)) {
			eval(elemento2).style.top = '42px';
		}

		// Verifica se existe o DIV
		if (eval(elemento3)) {
			eval(elemento3).style.top = '42px';
		}

	} else {
		novaJanela.focus();
	}
}

//Trocar a cor de fundo
function corFundo(el,cor) {
	el.style.backgroundColor=cor;
}

// Funcoes para mostrar ou ocultar objetos
function mostra(objeto) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	if(elemento) eval(elemento).style.display = 'inline';
}

function esconde(objeto) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	if(elemento) eval(elemento).style.display = 'none';
}

function escondeMostra(objeto) {
	var elemento = 'document.getElementById(\'' + objeto + '\')';
	if (eval(elemento).style.display == 'block' || eval(elemento).style.display == 'inline') {
		eval(elemento).style.display = 'none';
	} else {
		eval(elemento).style.display = 'block';
	}
}

// Função mostra e esconde na tabela.
function mostraEscondeAutomaticoBloco(objeto) {
	if(document.all) { // IE4+
		elemento = 'document.all[\'' + objeto + '\']';
	} else { // NS6+
		elemento = 'document.getElementById(\'' + objeto + '\')';
	}
	if (eval(elemento).style.display == 'block') {
		eval(elemento).style.display = 'none';
	} else {
		eval(elemento).style.display = 'block';
	}
}

//Funcao para marcar campo de erro
function campoErro(formulario, campo) {
	if (campo != undefined && formulario != undefined) {
		var campo = 'document.' + formulario + '.' + campo;
		eval(campo).focus();
		eval(campo).style.border = '1px solid #CC3300';
		eval(campo).style.background = '#FFFFE5';
	}
}

//Funcao para desmarcar campo de erro
function campoErroDesmarca(formulario, campo) {
	if (campo != undefined && formulario != undefined) {
		var campo = 'document.' + formulario + '.' + campo;
		eval(campo).style.border = '1px solid #d2d2d2';
		eval(campo).style.background = '#FFFFFF';
	}
}

function camposFile(nomeForm) {

	var campos = '';
	var separador = "/";
	var primeiro = 0;

	//Limpa marca de erro de todos os campos
	for(i=0; i<document.form1.length; i++) {
		if(document.form1[i].type == 'file') {
			if(primeiro == 0) {
				separador = "";
				primeiro = 1;
			} else {
				separador = "/";
			}
			campos += separador + document.form1[i].name;
		}
	}
	
	return campos;
}

function extensoesFile(nomeForm) {

	var campos = '';
	var separador = "/";
	var primeiro = 0;
	var valor = "";

	//Limpa marca de erro de todos os campos
	for(i=0; i<document.form1.length; i++) {
		if(document.form1[i].type == 'file') {
			valor = document.form1[i].value;
			verifica = valor.substring((valor.lastIndexOf(".")+1),valor.length);
			if(primeiro == 0) {
				separador = "";
				primeiro = 1;
			} else {
				separador = "/";
			}
			campos += separador + verifica;
		}
	}
	return campos;
}

function verificaTam(campo, tam, prox ,nomeForm){	
	str = "document." + nomeForm + "." + campo + ".value";
	obj = eval (str);
	if (obj.length == tam) {
	   str = "document."+ nomeForm + "." + prox + ".focus()";
	   eval (str);
	}
	return 1;    	    
}

function FormataData(Campo, teclapres) {
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 5)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
		if (tam >= 5 && tam <=10)
			Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
	}
}

function MascaraCPF(Campo, teclapres) {
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;
	if (tecla != 9 && tecla != 8) {
	 	if(tam > 3 && tam < 5){
		Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
	 	}
	 if(tam > 6 && tam < 8) {
	  		Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, tam);
	 	}
	 if(tam > 9 && tam < 11) {
	  		Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '-' + vr.substr(9, 11);
	 	} 
	}
}

function limiteCampo(campo, limite) {
	if (campo.value.length > limite)
		campo.value = campo.value.substring(0, limite);
}


//jump menu

function jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//Funcoes para redimensionar objetos flash
function setFlashWidth(id,width) {
	document.getElementById(id).style.width = width;
}

function setFlashHeight(id,height) {
	document.getElementById(id).style.height = height;
}
					  
		  
carregarDetalhes = function(acao, tipo, codigo) {
	var arquivo = ""
	var divExterno = ""
	var divInterno = ""
	
	if(navigator.platform == "MacIntel"){
		if(tipo == 1) {
			arquivo = "_produtosDetalhes.cfm?produto="+codigo
		} else if(tipo == 2) {
			arquivo = "noticiasDetalhes.cfm?codigo="+codigo
		} else if(tipo == 3) {
			arquivo = "contato.cfm"
		} else if(tipo == 4) {
			arquivo = "filiais.cfm"
		} else if(tipo == 5) {
			arquivo = "lancamentosDetalhes.cfm?codigo="+codigo
		} else if(tipo == 6) {
			arquivo = "politica.cfm"
		}
		
		if(tipo == 3) {
			largura = 382
			altura = 619
		} else {
			largura = 537
			altura = 447
		}
				
		popup(arquivo, "PERDIG&Atilde;O. &Eacute; DO CORA&Ccedil;&Atilde;O DO BRASIL.", largura, altura)
		
	} else {
	
		//Carregar detalhes ou limpar formulario
		if(acao) {
	
			//Produtos, Noticias, Contato, Filiais de Venda
			if(tipo == 1) {
				arquivo = "_produtosDetalhes.cfm?produto="+codigo
			} else if(tipo == 2) {
				arquivo = "noticiasDetalhes.cfm?codigo="+codigo
			} else if(tipo == 3) {
				arquivo = "contato.cfm"
			} else if(tipo == 4) {
				arquivo = "filiais.cfm"
			} else if(tipo == 5) {
				arquivo = "lancamentosDetalhes.cfm?codigo="+codigo
			} else if(tipo == 6) {
				arquivo = "politica.cfm"
			}
	
			if(tipo == 3) {
				divExterno = "estruturaContato"
				divInterno = "contato"
				frameLargura = 382
				frameAltura = 619
				fecharTop  = 13
				fecharLeft = 330
				moverTop = 13
				moverLeft = 253
				detalhesTop = 180
				detalhesLeft = 270
				consertoLargura = 488
				consertoAltura = 719
				consertoTop = 180
				consertoLeft = 270
			} else {
				divExterno = "estruturaCategorias"
				divInterno = "categorias"
				frameLargura = 537
				frameAltura = 447
				fecharTop  = 29
				fecharLeft = 512
				moverTop = 29
				moverLeft = 435
				detalhesTop = 180
				detalhesLeft = 500
				consertoLargura = 670
				consertoAltura = 566
				consertoTop = 180
				consertoLeft = 160
			}
			
			document.getElementById("estruturaDetalhes").className = divExterno
			document.getElementById("estruturaDetalhes").style.top = detalhesTop
			document.getElementById("estruturaDetalhes").style.left = detalhesLeft
			document.getElementById("detalhesDiv").className = divInterno
			document.getElementById("detalhesFrame").src = arquivo
			
			document.getElementById("detalhesFrame").width = frameLargura
			document.getElementById("detalhesFrame").height = frameAltura
			document.getElementById("idMover").style.left = moverLeft
			document.getElementById("idFechar").style.left = fecharLeft
			
	
			if(Browser.isIE()){
				document.getElementById("idMover").style.top = moverTop
				document.getElementById("idFechar").style.top = fecharTop
			} else {
				document.getElementById("idMover").style.top = moverTop+2
				document.getElementById("idFechar").style.top = fecharTop+2
			}
			//alert("carregou");
			mostra("estruturaDetalhes")
		} else {
			esconde("estruturaDetalhes");
			//alert("descarregou");
			document.getElementById("detalhesFrame").src = "_blank.cfm"
		}
	}
} 

carregarNoticias = function(acao, codigo) {
	if(acao) {
		mostra("estruturaCategorias");
		document.getElementById("detalhes").src = "noticia"+codigo+".cfm"
	} else {
		esconde("estruturaCategorias");
		document.getElementById("detalhes").src = "_blank.cfm"
	}
}