 function ccc(){									
                                        var cp = document.getElementById('NmReferencia').value.length;																		
                                        document.getElementById('cont').innerHTML = 100-cp;									 
                                    }

function Abre(url, width, height)
	{
	window.open(url,"_blank","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height)
	}

function limpa_campo(S){
var Digitos = "0123456789";
var temp = "";
var digito = "";
    for (var i=0; i<S.length; i++){
      digito = S.charAt(i);
      if (Digitos.indexOf(digito)>=0){temp=temp+digito}
    }
    return temp
}
                
function verifica_CPF(s)
{       
        var i;  
        s = limpa_campo(s);
        var c = s.substr(0,9);
        var dv = s.substr(9,2);
        var d1 = 0;
        for (i = 0; i < 9; i++)
        {
                d1 += c.charAt(i)*(10-i);
        }
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
        if (d1 > 9) d1 = 0;
        if (dv.charAt(0) != d1)
        {       
                return false;
        }
        
        d1 *= 2;
        for (i = 0; i < 9; i++)
        {
                d1 += c.charAt(i)*(11-i);
        }
        d1 = 11 - (d1 % 11);
        if (d1 > 9) d1 = 0; 
        if (dv.charAt(1) != d1)
        {
                return false;
        }
        return true;
}

function validEmail(email) {
	invalidChars = " /:,;"
	if (email == "") {						// cannot be empty
		return false
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					// and at least one "." after the "@"
		return false
	}
	if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
		return false
	}
	return true
}

function verifica_CNPJ(s)
{
	var i;
	s = limpa_campo(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
	{
			d1 += c.charAt(11-i)*(2+(i % 8));
	}
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1)
	{
			return false;
	}
	 
	d1 *= 2;
	for (i = 0; i < 12; i++)
	{
			d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(1) != d1)
	{
			return false;
	}
	return true;
}

function CheckInsc() {
	
var msg = "Atenção, verifique as seguintes pendências;<br><br>";
var erro = 1;
var ok = 1;

var form 				= document.getElementById('contact');
var tpcliente 			= document.getElementById('tppf');
var nmcliente 			= document.getElementById('NmUsuario');
var nmemail 			= document.getElementById('NmEmail');
var nucgccpf1 			= document.getElementById('NuCpfCnpj1');
var nucgccpf2 			= document.getElementById('NuCpfCnpj2');
var nutelefone1			= document.getElementById('NuTelefone');
var txlogin 			= document.getElementById('NmLogin');
var txsenha 			= document.getElementById('TxSenha');
var txsenhac 			= document.getElementById('TxSenhac');
var cdperfil 			= document.getElementById('CdPerfil');
var nmreferencia		= document.getElementById('NmReferencia');
var tpcli				= 0;

for (i=0;i<2;i++) {
		if (form[i].checked) {
			preencheu = true;
			tpcli = form[i].value;			
		}
	}
	
if(tpcli == 1){	
	
	if(nmcliente.value == ''){
		msg = msg + "O nome não pode ser vazio.<br>";
		erro = erro + 1;
		nmcliente.style.background = '#f2f2f2';
		}
	else{ok = ok + 1;nmcliente.style.background = '#ffffff';}
	
	if(nucgccpf1.value == ''){
		msg = msg + "O CPF não pode ser vazio.<br>";
		erro = erro + 1;
		nucgccpf1.style.background = '#f2f2f2';
		}
	else {
		if(!verifica_CPF(nucgccpf1.value)){
			msg = msg + "O CPF esta invalido.<br>";
			erro = erro + 1;
			nucgccpf1.style.background = '#f2f2f2';
			}
		else{ok = ok + 1;nucgccpf1.style.background = '#ffffff';}
		}	
	}
else {	
	if(nmcliente.value == ''){
		msg = msg + "O nome não pode ser vazio.<br>";
		erro = erro + 1;
		nmcliente.style.background = '#f2f2f2';
		}
	else{ok = ok + 1;nmcliente.style.background = '#ffffff';}	
	if(nucgccpf2.value == ''){
		msg = msg + "O CNPJ não pode ser vazio.<br>";
		erro = erro + 1;
		nucgccpf2.style.background = '#f2f2f2';
		}
	else {
		if(!verifica_CNPJ(nucgccpf2.value)){
			msg = msg + "O CNPJ esta invalido.<br>";
			erro = erro + 1;
			nucgccpf2.style.background = '#f2f2f2';
			}
		else{ok = ok + 1;nucgccpf2.style.background = '#ffffff';}				
	}
}

if(nmemail.value == ''){
	msg = msg + "O email tem que ser informado.<br>";
	erro = erro + 1;
	nmemail.style.background = '#f2f2f2';
	}
else{ok = ok + 1;nmemail.style.background = '#ffffff';}

if(!validEmail(nmemail.value)){
	msg = msg + "O email esta invalido.<br>";
	erro = erro + 1;
	nmemail.style.background = '#f2f2f2';
	}
else{ok = ok + 1;nmemail.style.background = '#ffffff';}

if(nutelefone1.value == ''){
	msg = msg + "O telefone tem que ser informado.<br>";
	erro = erro + 1;
	nutelefone1.style.background = '#f2f2f2';
	}
else{ok = ok + 1;nutelefone1.style.background = '#ffffff';}

if(txlogin.value == ''){
	msg = msg + "O login tem que ser informado.<br>";
	erro = erro + 1;
	txlogin.style.background = '#f2f2f2';
	}
else{ok = ok + 1;txsenha.style.background = '#ffffff';}

if(txsenha.value == ''){
	msg = msg + "A senha tem que ser informada.<br>";
	erro = erro + 1;
	txsenha.style.background = '#f2f2f2';
	}
else{ok = ok + 1;txsenha.style.background = '#ffffff';}

if(txsenha.value != txsenhac.value){
	msg = msg + "A confirmação da senha esta invalida.<br>";
	erro = erro + 1;
	txsenhac.style.background = '#f2f2f2';
	}
else{ok = ok + 1;txsenhac.style.background = '#ffffff';}

if(cdperfil.value == ''){
	msg = msg + "Um perfil tem que ser informado.<br>";
	erro = erro + 1;
	nmemail.style.background = '#f2f2f2';
	}
else{ok = ok + 1;nmemail.style.background = '#ffffff';}

if(nmreferencia.value == ''){
	msg = msg + "A referência do cadastro tem que ser informada.<br>";
	erro = erro + 1;
	nmreferencia.style.background = '#f2f2f2';
	}
else{ok = ok + 1;nmreferencia.style.background = '#ffffff';}

if(erro > 1){
	document.getElementById('idmensagem').innerHTML = msg;
	return false;
	}
	
}

function mostrar(elem) {
	if (elem.style.display == 'block') {
		elem.style.display = 'none';
	} else {
		elem.style.display = 'block';
	}
}

function mostraFilho (elem) {
	irmao = elem.nextSibling;

	if(irmao.nodeName == 'DIV') {
		mostrar(irmao);
	}
	if (irmao.nextSibling)	mostraFilho(irmao);
}
	
function mudaImg (img,path,elemento) {
	var caminho = "_img/Eventos/" + img;
	document.getElementById(elemento).src= caminho;
}

function validaId() {
	if (parent.fMain.document.idForm.NuCpf.value == "") {
		alert('C.P.F. não Informado.');
		document.idForm.NuCpf.focus();
		return false;
	}
	var Cpf = parent.fMain.document.idForm.NuCpf.value;
	if (Cpf.length < 11) {
		alert('C.P.F. Incompleto.');
		parent.fMain.document.idForm.NuCpf.select();
		parent.fMain.document.idForm.NuCpf.focus();
		return false;
	}
	
	if (parent.fMain.document.idForm.TxSenhaInf.value == "") {
		alert('Senha não Informada.');
		parent.fMain.document.idForm.TxSenhaInf.focus();
		return false;			
	}
	
	var Sen = parent.fMain.document.idForm.TxSenhaInf.value;
	if ((Sen.length < 4) || (Sen.length > 10)){
		alert('Senha deve ter de 4 à 10 letras e/ou números.');
		parent.fMain.document.idForm.TxSenhaInf.select();
		parent.fMain.document.idForm.TxSenhaInf.focus();
		return false;
	}
    window.parent.fInvisivel.location.href="ValidaId.cfm?NuCpf="+Cpf+"&Senha="+Sen+"&"+(new Date()).getTime();
}

function confirma (pergunta) {
	var confirma = confirm(pergunta);
	if (confirma) return true;
	else return false;
}

function Abre(url, width, height) {
	window.open(url,"_blank","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=no,width=" + width + ",height=" + height)
}

function Cadastro(url, width, height) {
	window.open(url,"cadastro","resizable=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,width=" + width + ",height=" + height)
}

function Encarte(url, width, height) {
	window.open(url,"encarte","resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=no,width=" + width + ",height=" + height)
}

function Receitas(url, width, height) {
	window.open(url,"receitas","resizable=yes,toolbar=no,status=yes,menubar=no,scrollbars=yes,width=" + width + ",height=" + height)
}

function popupImageWindow(url) {
	document.close();
	window.open(url,'popupImageWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

function isNum( caractere ) {
	var strValidos = "0123456789"
	if ( strValidos.indexOf( caractere ) == -1 )
		return false;
	return true;
}

function validaTecla(campo, event) {
	var BACKSPACE= 8;
	var key;
	var tecla;
	CheckTAB=true;
 	if(navigator.appName.indexOf("Netscape")!= -1)
		tecla= event.which;
	else
		tecla= event.keyCode;
	key = String.fromCharCode(tecla);
	//alert( 'key: ' + tecla + ' -> campo: ' + campo.value);
	//if ( tecla == 13 ) 
	//return false; 
	if ((tecla == 8) || (tecla == 0))
		return true;
	return (isNum(key));
}

function verify_fCep(pCepBusca) {
	if (pCepBusca == '') {
		alert("Informe o CEP.");
		parent.fMain.document.fCep.CEP.focus();
		return false;
	}
	var url = "Busca7.cfm?pCepBusca=" + pCepBusca;
	parent.fInvisivel.location.href=url;
}

function RegVoto()
{
	var wNum = -1;
	if (document.fEnquete.elements['OpcaoEnquete'].type != "hidden") {
		for (i=0; i < document.fEnquete.OpcaoEnquete.length; i++) 
		{
			if (document.fEnquete.OpcaoEnquete[i].checked) { wNum = i }
		}
		if (wNum == -1) {
			alert('Selecione uma resposta para a enquete');
			document.fEnquete.OpcaoEnquete[0].focus();
			return false;
		}
		var url = 'registra_voto.cfm?OpcaoEnquete='+document.fEnquete.OpcaoEnquete[wNum].value;
		/*Abre(url,450,350);*/
		parent.location = url;
		document.fEnquete.OpcaoEnquete[wNum].checked = false;
	}
	return false;
}

function checkEnter(event)
{ 	
	NS4 = (document.layers) ? true : false;
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13) {
		psqPalavra(document.fPesq.PalavraPsq.value);
	}
}

function psqPalavra() {
	var val = document.fPesq.PalavraPsq.value;
	if ((val == "") || (val == "Palavra-chave")) {
		alert('Palavra a ser pesquisada não informada.');
		document.fPesq.PalavraPsq.focus();
		document.fPesq.PalavraPsq.select();
		return false;
	}
	var nome = val;
	if (nome.length < 3) {
		alert('Palavra a ser pesquisada deve ter no mínimo 3 letras.');
		document.fPesq.PalavraPsq.focus();
		document.fPesq.PalavraPsq.select();
		return false;		
	}
	parent.fMain.location.href = "PsqProdutos.cfm?Palavra="+val+"&IniSec=1&"+(new Date()).getTime();
	//document.fPesq.PalavraPsq.value = "Palavra-chave";
}

function popupImageWindow(url) {
	document.close();
	window.open(url,'popupImageWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

function AtualizaForm(Url) {
	location.href=Url;
}

function InicializarIndices() {
	if (document.CargaInicial==null) {
		document.CargaInicial=false; // Seta para só fazer uma vez por documento
		var ctrlAnterior=null;
		var IndAnt=0;
		for ( var i=0; i<document.forms[0].elements.length;i++)	{
			var e=document.forms[0].elements[i];
			if ( e.type!="hidden" && e.type!="image" ) {
				if (ctrlAnterior != null) ctrlAnterior.IndicePosterior=i;
				ctrlAnterior=e;
				e.Indice=i;
				e.IndiceAnterior=IndAnt;
			}
		}
	}
}

// Colocar o foco em determinado campo
function SetarFoco(ind) {
	InicializarIndices();
	if ( isNaN(ind) && document.forms[0].elements[ind].type!="hidden" )
		document.forms[0].elements[ind].focus();
	else
		for (;ind<document.forms[0].elements.length;ind++)
			if (document.forms[0].elements[ind].type!="hidden")
				break;
		if (ind<=document.forms[0].elements.length)
			document.forms[0].elements[ind].focus();
}

// Limpar o conteúdo do(s) campo(s)
function LimparCampo(ind) {	// Para -1, limpa todos os elementos
	if (isNaN(ind)) // Limpa pelo nome
		document.forms[0].elements[ind].value="";
	else if (ind != -1) // Limpa o elemento "ind" ( só considera "text" e "password" )
		for (var i=ind; i < document.forms[0].elements.length;i++ )
			if(document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password") { // Só limpa campo "text"
				document.forms[0].elements[i].value="";
				break;
			}
	else // Limpa todos os elementos "text" e "password"
		for ( var i=0; i < document.forms[0].elements.length; i++)
			if ( document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password")
				document.forms[0].elements[i].value="";
}

// Verificar qual navegador
function QualNavegador() {
	var s = navigator.appName
	if(s == "Microsoft Internet Explorer") return "IE";
	else if ( s == "Netscape" ) return "NE";
	else return "";
}

// Verificar qual a versão do navegador
function QualVersao() {
	var s = navigator.appVersion;
	if ( QualNavegador() == "IE" ) {
		var i = s.search("MSIE");
		s=s.substring(i+5);
		i=s.search(".");
		return parseInt(s.substring(0,i+1));
	}
	else if ( QualNavegador() == "NE" )	return parseInt(s.substring(0,1));
	else return 0;
}

// Setar o evento
function SetarEvento(ctrl, Tam, Tipo, AutoSkip) { // Filtra navegadores conhecidos
	var s = QualNavegador();
	if (s.length==0) return;
	if (s=="IE" && QualVersao()>6) return;
	if (s=="NE" && QualVersao()>4) return;
	if (ctrl.onkeypress==null) {
		if (AutoSkip==null) AutoSkip=true;
		if (Tipo!=null)	Tipo.toUpperCase();
		ctrl.Tam=Tam;
		ctrl.Tipo=Tipo;
		ctrl.AutoSkip=true;
		ctrl.Saltar=false;
		InicializarIndices();
		ctrl.onkeypress=ValidarTecla;
		if (QualNavegador()=="IE" && QualVersao()>=5) ctrl.onkeyup=SaltarCampo;
	}
}

function SaltarCampo(ctrl) {
	if (ctrl==null)	ctrl=this;
	if ( ctrl.AutoSkip && ctrl.Saltar)
		if (ctrl.Saltar) {
			ctrl.Saltar=false;
			if ( ctrl.IndicePosterior != null ) SetarFoco(ctrl.IndicePosterior);
		}
}

// Fazer o salto de campo
function ValidarTecla(evnt) {
	var tk;
    var c;
	// Recebe a tela pressionada
	tk = ( (QualNavegador()=="IE") ? event.keyCode : evnt.which);
    c=String.fromCharCode(tk);
	c=c.toUpperCase();
	// Só aceita teclas alfanuméricas. Não aceita teclas de controle
	//if (tk == 13) enviaFormLogin(document.login.PROD_I[document.login.PROD_I.selectedIndex], document.login.USERID, document.login.AGENCIA, document.login.CONTA);
    if(tk<32) return true;
	if (tk>127)	return false;

	switch (this.Tipo) {
	case "D":
		if (c<"0" || c>"9") {
            alert("Este campo só aceita números inteiros");
       		return false;
       	}
		break;
	case "N":
		//if ((c<"0" || c>"9") && (c!="." && c!=","))
		pont = ".";
		if ((c<"0" || c>"9") && (c!=pont))
			return false;
		//if ((c==",") && ((this.value.search(",")>-1) || (this.value.length==0)))
		if ((c==pont) && ((this.value.indexOf(pont)>-1) || (this.value.length==0)))
			return false;
		if ((c==pont) && (this.value.length==0))
			return false;			
		break;
	case "C":
		if ( c<"A" || c>"Z" ) return false;
		break;
	default:
		break;
	}
	this.Saltar=(this.value.length==this.Tam-1);
	if(((QualNavegador()=="IE") && QualVersao()<5) || (QualNavegador()!="IE")) SaltarCampo(this);
	return true;
}

function reloadPage(init) {
  c = navigator.appName
  if (init==true) with (navigator) {
    if (c=="Netscape") { document.pgW=innerWidth; document.pgH=innerHeight; onresize=reloadPage; }
  } else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}
reloadPage(true);

function trocar(hide, show) {
	document.getElementById(hide).style.display = 'none';
	document.getElementById(show).style.display = 'block';
}