cont321 = 0;
function SelectAll() { 
   for (var i=0;i<document.FORMLISTA.elements.length;i++) {
     var x = document.FORMLISTA.elements[i];
     if (x.id == 'check_sel') { 
	   if (cont321 == 0)
         x.checked = true;
       else
         x.checked = false;	   
     } 
   }
   if (cont321 == 0)
   {    
     var elem = document.getElementById("sel_todos");
     elem.value = "Desmarcar todos";
     cont321 = 1;
   } 
   else 
   {
     var elem = document.getElementById("sel_todos");
     elem.value = "Selecionar todos";
     cont321 = 0;
   }  
}

// FUNCAO SELEIONA TODOS REGISTROS PRA EXCLUIR
function checkdeletetion(){
   if (!confirm("Você realmente deseja deletar este registro.") == false )
   FORMLISTA.submit();
}

function confirmaBloqueio(aURL) {
	if(confirm('Você tem certeza disso?')) {
	location.href = aURL;
	}
}

function FormatarCampo(src, mask) 
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida) 
  {
        src.value += texto.substring(0,1);
  }
}

function validate(FALE) {
	
	if(FALE.nome.value == ""){
	alert("Digite seu Nome");
	FALE.nome.focus();
	return (false);
	}

	if(FALE.cidade.value == ""){
	alert("Digite sua Cidade");
	FALE.cidade.focus();
	return (false);
	}
	
	if(FALE.telefone.value == ""){
	alert("Digite o Telefone");
	FALE.telefone.focus();
	return (false);
	}
			
	if(FALE.email.value == ""){
	alert("Digite o E-mail");
	FALE.email.focus();
	return (false);
	}
		
return (true);
}

function checkdeletetion(){
   if (!confirm("Você realmente deseja deletar este(s) registro(s)?") == false )
   FORMLISTA.submit();
}

function checa()
{
msg = "";

 //if (FORMULARIO.codigopromocao.value == "")
 // {
	if (FORMULARIO.cnpj.value == "") {msg=msg+"O preenchimento do campo ["+FORMULARIO.pessoa.value+"] é obrigatório.\n";}

	if (msg != "")
	 {alert(msg); return false;}
	else
	 {return true;}

 // }
 //else
 //   return true;
}

function TESTA(CNUMB,CTYPE)
{
	if (CNUMB=="") return true;
	
	  bok = false;
	 // if (CTYPE=="CNPJ"){
	//	bok = isCnpj(ParseNumb(CNUMB)));
	  //}else{
	///	bok = isCpf(ParseNumb(CNUMB)))
	  //}
	  bok = isCpfCnpj(ParseNumb(CNUMB));

	  if(!bok){
		   alert(CTYPE+" inválido!");
		   FORMULARIO.cnpj.value = "";
	  }
	  return bok;
	}

	function ClearStr(str,char)
	{
	  while((cx=str.indexOf(char))!=-1)
	  {
			str = str.substring(0,cx)+str.substring(cx+1);
	  }
	  return str;
}

function ParseNumb(c)
{
	  c=ClearStr(c,'-');
	  c=ClearStr(c,'/');
	  c=ClearStr(c,',');
	  c=ClearStr(c,'.');
	  c=ClearStr(c,'(');
	  c=ClearStr(c,')');
	  c=ClearStr(c,' ');
	  if((parseFloat(c) / c != 1))
	  {
			if(parseFloat(c) * c == 0)
			{
			  return(c);
			}
			else
			{
			  return(0);
			}
	  }
	  else
	  {
			return(c);
	  }
}

function Verify(CNUMB,CTYPE)
{
	  CNUMB=ParseNumb(CNUMB)
	  if(CNUMB == 0)
	  {
			return(false);
	  }
	  else
	  {
			g=CNUMB.length-2;
			if(TestDigit(CNUMB,CTYPE,g))
			{

			  g=CNUMB.length-1;
			  if(TestDigit(CNUMB,CTYPE,g))
			  {
					return(true);
			  }
			  else
			  {
					return(false);
			  }
			}
			else
			{
			  return(false);
			}
	  }
}

function TestDigit(CNUMB,CTYPE,g)
{
	  var dig=0;
	  var ind=2;
	  if (CTYPE!='CNPJ'){
			var a = true;
			for (t=0; t<CNUMB.length-1; t++){
				if (CNUMB.substring(t,t+1)!=CNUMB.substring(t+1,t+2)){
					a = false;
				}
				if (a) {
					return false;
				}
			}
	  }
	  for(f=g;f>0;f--)
	  {
			dig+=parseInt(CNUMB.charAt(f-1))*ind;
			if (CTYPE=='CNPJ')
			{ if(ind>8) {ind=2} else {ind++} }
			else
			{ ind++ }
	  }
	  dig%=11;
	  if(dig<2)
	  {
			dig=0;
	  }
	  else
	  {
			dig=11-dig;
	  }
	  if(dig!=parseInt(CNUMB.charAt(g)))
	  {
			return(false);
	  }
	  else
	  {
			return(true);

	  }
}

pj = '*Digite seu CNPJ:';
pf = '*Digite seu CPF:';

function escreveLayer(tipo)
{
   vbrowser=(document.getElementById)?0:((document.all)?0:1);
   if(vbrowser==0)
	 {MM_findObj('fgpto').innerHTML=tipo;}
   else
	 {
		MM_findObj('fgpto').document.open();
		MM_findObj('fgpto').document.write(tipo);
		MM_findObj('fgpto').document.close();
	 }
}

function formataCNPJ(cp,tipo)
{
	if ((event.keyCode<48) || (event.keyCode>57))
		return false;
	else{
		var v = cp.value;
		if (tipo=="CNPJ"){
			var maxlen = 18;
			if (v.length>=maxlen)
				return false;
			if (v.length==2 || v.length==6)
				cp.value = v +'.';
			else if (v.length==10)
				cp.value = v +'/';
			else if (v.length==15)
				cp.value = v +'-';
		}else{
			var maxlen = 14;
			if (v.length>=maxlen)
				return false;
			if (v.length==3 || v.length==7)
				cp.value = v +'.';
			else if (v.length==11)
				cp.value = v +'-';
		}
	}
}
		   
		   
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/**
 * @author Márcio d'Ávila
 * @version 1.01, 2004
 *
 * PROTÓTIPOS:
 * método String.lpad(int pSize, char pCharPad)
 * método String.trim()
 *
 * String unformatNumber(String pNum)
 * String formatCpfCnpj(String pCpfCnpj, boolean pUseSepar, boolean pIsCnpj)
 * String dvCpfCnpj(String pEfetivo, boolean pIsCnpj)
 * boolean isCpf(String pCpf)
 * boolean isCnpj(String pCnpj)
 * boolean isCpfCnpj(String pCpfCnpj)
 */


NUM_DIGITOS_CPF  = 11;
NUM_DIGITOS_CNPJ = 14;
NUM_DGT_CNPJ_BASE = 8;


/**
 * Adiciona método lpad() à classe String.
 * Preenche a String à esquerda com o caractere fornecido,
 * até que ela atinja o tamanho especificado.
 */
String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} //String.lpad


/**
 * Adiciona método trim() à classe String.
 * Elimina brancos no início e fim da String.
 */
String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
} //String.trim


/**
 * Elimina caracteres de formatação e zeros à esquerda da string
 * de número fornecida.
 * @param String pNum
 * 	String de número fornecida para ser desformatada.
 * @return String de número desformatada.
 */
function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
} //unformatNumber


/**
 * Formata a string fornecida como CNPJ ou CPF, adicionando zeros
 * à esquerda se necessário e caracteres separadores, conforme solicitado.
 * @param String pCpfCnpj
 * 	String fornecida para ser formatada.
 * @param boolean pUseSepar
 * 	Indica se devem ser usados caracteres separadores (. - /).
 * @param boolean pIsCnpj
 * 	Indica se a string fornecida é um CNPJ.
 * 	Caso contrário, é CPF. Default = false (CPF).
 * @return String de CPF ou CNPJ devidamente formatada.
 */
function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} //formatCpfCnpj


/**
 * Calcula os 2 dígitos verificadores para o número-efetivo pEfetivo de
 * CNPJ (12 dígitos) ou CPF (9 dígitos) fornecido. pIsCnpj é booleano e
 * informa se o número-efetivo fornecido é CNPJ (default = false).
 * @param String pEfetivo
 * 	String do número-efetivo (SEM dígitos verificadores) de CNPJ ou CPF.
 * @param boolean pIsCnpj
 * 	Indica se a string fornecida é de um CNPJ.
 * 	Caso contrário, é CPF. Default = false (CPF).
 * @return String com os dois dígitos verificadores.
 */
function dvCpfCnpj(pEfetivo, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for (j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
} //dvCpfCnpj


/**
 * Testa se a String pCpf fornecida é um CPF válido.
 * Qualquer formatação que não seja algarismos é desconsiderada.
 * @param String pCpf
 * 	String fornecida para ser testada.
 * @return <code>true</code> se a String fornecida for um CPF válido.
 */
function isCpf(pCpf)
{
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;

	// Valida dígitos verificadores
	if (numero != base + digitos) return false;

	/* Não serão considerados válidos os seguintes CPF:
	 * 000.000.000-00, 111.111.111-11, 222.222.222-22, 333.333.333-33, 444.444.444-44,
	 * 555.555.555-55, 666.666.666-66, 777.777.777-77, 888.888.888-88, 999.999.999-99.
	 */
	algUnico = true;
	for (i=1; i<NUM_DIGITOS_CPF; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} //isCpf


/**
 * Testa se a String pCnpj fornecida é um CNPJ válido.
 * Qualquer formatação que não seja algarismos é desconsiderada.
 * @param String pCnpj
 * 	String fornecida para ser testada.
 * @return <code>true</code> se a String fornecida for um CNPJ válido.
 */
function isCnpj(pCnpj)
{
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;

	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) return false;

	/* Não serão considerados válidos os CNPJ com os seguintes números BÁSICOS:
	 * 11.111.111, 22.222.222, 33.333.333, 44.444.444, 55.555.555,
	 * 66.666.666, 77.777.777, 88.888.888, 99.999.999.
	 */
	algUnico = numero.charAt(0) != '0';
	for (i=1; i<NUM_DGT_CNPJ_BASE; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	/* Não será considerado válido CNPJ com número de ORDEM igual a 0000.
	 * Não será considerado válido CNPJ com número de ORDEM maior do que 0300
	 * e com as três primeiras posições do número BÁSICO com 000 (zeros).
	 * Esta crítica não será feita quando o no BÁSICO do CNPJ for igual a 00.000.000.
	 */
	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
} //isCnpj


/**
 * Testa se a String pCpfCnpj fornecida é um CPF ou CNPJ válido.
 * Se a String tiver uma quantidade de dígitos igual ou inferior
 * a 11, valida como CPF. Se for maior que 11, valida como CNPJ.
 * Qualquer formatação que não seja algarismos é desconsiderada.
 * @param String pCpfCnpj
 * 	String fornecida para ser testada.
 * @return <code>true</code> se a String fornecida for um CPF ou CNPJ válido.
 */
function isCpfCnpj(pCpfCnpj)
{
	var numero = pCpfCnpj.replace(/\D/g, "");
	if (numero.length > NUM_DIGITOS_CPF)
		return isCnpj(pCpfCnpj)
	else
		return isCpf(pCpfCnpj);
} //isCpfCnpj

function validate(FORMULARIO) {
	
	if(FORMULARIO.nome.value == ""){
	alert("Digite seu Nome");
	FORMULARIO.nome.focus();
	return (false);
	}
return (true);
}