// DHTML determina se o browser conhece javascript
var DHTML = (document.getElementById || document.all || document.layers);


// Le objetos da pagina indepentendemente do browser
function le_objeto(Nome) {
	if (!DHTML) return;
	if      (document.getElementById) Saida = document.getElementById(Nome);
	else if (document.all)            Saida = document.all[Nome];
	else if (document.layers)         Saida = document.layers[Nome];
	return Saida;
}


//Retorna o conteudo de um objeto da pagina 
function conteudo(Nome) {
	if (!DHTML) return;
  var Objeto = le_objeto(Nome);
  try      { Saida = Objeto.value; }
  catch(e) {
    try      { Saida = Objeto.options[Objeto.selectedIndex].text; }
    catch(e) {
      try      { Saida = Objeto.innerHTML; }
      catch(e) { return false; }
    }
  }
  return Saida;
}


//Retorna o tamanho (em caracteres) de um conteudo de um objeto da pagina
function tamanho(Nome) {
	if (!DHTML) return;
  var Campo = conteudo(Nome);
  return Campo.length;
}


//Encontra em uma lista um valor
function encontra(Valor,Lista) {
	if (!DHTML) return;
  var Tam = Lista.length;
  var i = 0;
  var Saida = false;
  while ((i < Tam) && !Saida){
    if (Valor==Lista[i]) Saida = true;
    else i++;
  }
  if (Saida) return i+1;
  return Saida;
}


//Permite somente a digitacao de numeros (48-57)
//de DEL (8) e Menos (9)
function somente_numero(Teclado) {
	if (!DHTML) return;
  var TeclasN = Array(8,9,48,49,50,51,52,53,54,55,56,57);
  if (Teclado.keyCode) codTecla = Teclado.keyCode;
  else codTecla = Teclado.which;
  var Aux = encontra(codTecla,TeclasN);
  if (Aux>0) return true;
  return false;
}


//Permite somente a digitacao de numeros (48-57)
//de DEL (8), Menos (9) Espaco (32)
//parentesis (40-41) e barra (45)
function somente_numero_telefone(Teclado) {
	if (!DHTML) return;
  var TeclasN = Array(8,9,32,40,41,45,48,49,50,51,52,53,54,55,56,57);
  if (Teclado.keyCode) codTecla = Teclado.keyCode;
  else codTecla = Teclado.which;
  var Aux = encontra(codTecla,TeclasN);
  if (Aux>0) return true;
  return false;
}


//Escolhe o tipo de formatacao numerica
function escolhe_mascara(Campo,Tipo) {
	if (!DHTML) return;
	Escolha = conteudo(Tipo);
	if (Escolha=='I') mascara_numero_inteiro(Campo);
	else mascara_numero(Campo);
  return;
}


//Formata a saida para numeros com separacao de milhar com duas casas decimais
function mascara_numero(Campo) {
	if (!DHTML) return;
  var Milhar = '.';
  var Decima = ',';
  Numero = conteudo(Campo);
  while (Numero.lastIndexOf(Milhar)>-1) Numero = Numero.replace(Milhar, "");
  Numero = Numero.replace(Decima, "");
  Numero = Numero.replace(/^[0]+/g,"");
  Tamanh = Numero.length;
  NovoNu = '';
  Contad = 0;
  for (i=Tamanh-2; i>0; i--){
    if (Contad==3) {
       NovoNu = Milhar + NovoNu;
       Contad = 0;
    }
    NovoNu = Numero.charAt(i-1) + NovoNu;
    Contad++;
  }
  if (Tamanh==0) Decima = '';
  if (Tamanh==1) Decima = '0,0';
  if (Tamanh==2) Decima = '0,';
  NovoNu = NovoNu + Decima + Numero.charAt(Tamanh-2) + Numero.charAt(Tamanh-1);
  Objeto = le_objeto(Campo);
  Objeto.value = NovoNu;
  return;
}


//Formata a saida para numeros com separacao de milhar sem casas decimais
function mascara_numero_inteiro(Campo) {
	if (!DHTML) return;
  var Milhar = '.';
  Numero = conteudo(Campo);
  while (Numero.lastIndexOf(Milhar)>-1) Numero = Numero.replace(Milhar, "");
  Tamanh = Numero.length;
  NovoNu = '';
  Contad = 0;
  for (i=Tamanh; i>0; i--){
    if (Contad==3) {
       NovoNu = Milhar + NovoNu;
       Contad = 0;
    }
    NovoNu = Numero.charAt(i-1) + NovoNu;
    Contad++;
  }
  Objeto = le_objeto(Campo);
  Objeto.value = NovoNu;
  return;
}


//Valida um CNPJ dado
function valida_cnpj(cnpj){
	if (!DHTML) return;
  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
  digitos_iguais = 1;
  if (cnpj.length < 14 && cnpj.length < 15) return false;
  for (i = 0; i < cnpj.length - 1; i++) {
    if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
      digitos_iguais = 0;
      break;
    }
  }
  if (!digitos_iguais) {
    tamanho = cnpj.length - 2
    numeros = cnpj.substring(0,tamanho);
    digitos = cnpj.substring(tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2) pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0)) return false;
    tamanho = tamanho + 1;
    numeros = cnpj.substring(0,tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2) pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1)) return false;
    return true;
  }
  else return false;
}


//Faz um objeto visibility=visible aparecer
function visivel(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.style.visibility;
  if (Mostra=='visible') return false;
  Objeto.style.visibility = 'visible';
  return true;
}


//Faz um objeto visibility=hidden desaparecer
function invisivel(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.style.visibility;
  if (Mostra=='hidden') return false;
  Objeto.style.visibility = 'hidden';
  return true;
}


//Faz um objeto aparecer/desaparecer dependendo do seu estado atual
function visivel_invisivel(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.style.visibility;
  if (Mostra=='visible') invisivel(Identif);
  else visivel(Identif);
  return true;
}


//Faz um objeto display=none aparecer
function aparece(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.style.display;
  if (Mostra=='') return false;
  Objeto.style.display = '';
  return true;
}


//Faz um objeto display=none desaparecer
function esconde(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.style.display;
  if (Mostra=='none') return false;
  Objeto.style.display = 'none';
  return true;
}


//Faz um objeto aparecer/desaparecer dependendo do seu estado atual
function aparece_esconde(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.style.display;
  if (Mostra=='') esconde(Identif);
  else aparece(Identif);
  return true;
}


//Faz um objeto disable=false
function habilita(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  Objeto.disabled=false;
  return;
}


//Faz um objeto disable=true
function desabilita(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  Objeto.disabled=true;
  return;
}


//Faz um objeto disable=false/true dependendo do seu estado atual
function habilita_desabilita(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
  var Mostra = Objeto.disabled;
  if (Mostra) habilita(Identif);
  else desabilita(Identif);
  return true;
}


//Tira o check de um objeto
function desmarca(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
	try {Objeto.checked=false;}
	catch(e) {}
  try {Objeto.value="";}
  catch(e) {}  
  return;
}


//Tira o select de um objeto
function desselect(Identif) {
	if (!DHTML) return;
	var Objeto = new le_objeto(Identif);
	Objeto.selectedIndex=0;
  return;
}


//Valida um e-mail quanto aa sua grafia
function valida_email(Texto){
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(Texto)) return true;
  else return false;
}


//Preenche com zeros um numero inteiro
function preenche_comzeros(Numero,Tamanho){
  if (!DHTML) return;
  Zero = "";
  Tamanho = parseInt(Tamanho);
  for (i = 1; i <= Tamanho; i++) Zero += "0";
  Numero = String(parseInt(Numero));
  Numero = Zero + Numero.replace(" ","");
  TamAtu = parseInt(Numero.length);
  Numero = Numero.substr(TamAtu-Tamanho,Tamanho);
  return Numero;
}


//Localiza o selectedindex de um select a partir do value
function localiza_index(Campo,Valor){
  if (!DHTML) return;
  Obj   = le_objeto(Campo);
  Tam   = Obj.length;
  Valor = parseInt(Valor);
  Saida = -1;
  i=0;
  while (Saida<0){
    k = parseInt(Obj.options[i].value);
    if (k==Valor) Saida = i;
    i++;
    if ((Saida<0) && (i>Tam)) Saida = i;
  }
  return Saida;
}


//Adiciona um option em um select
function adiciona_option(Campo,Texto,Valor){
  if (!DHTML) return;
  Obj       = le_objeto(Campo);
  Tam       = Obj.length;
  Aux       = document.createElement('option');
  Aux.text  = Valor+"-"+Texto;
  Aux.value = Valor;
  Valor     = parseInt(Valor);
  Saida     = -1;
  i         = 0;
  k         = parseInt(Obj.options[i].value);
  while (k<Valor){
    i++;
    k = parseInt(Obj.options[i].value);
  }
  Pos = Obj.options[i];
  try {
    Obj.add(Aux,Pos);
    Obj.selectedIndex = Pos;
  } catch(e){
    Obj.add(Aux);
    Obj.selectedIndex = Obj.length - 1;
  }
}


//Cria a funcao que pesquisa um elemento, ou grupo de
//elementos pela classe
function getElementsByClass( searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl)!=-1) el[j++] = tags[i];
	}
	return el;
}
