function obterIndiceProximoElemento(campo) {
  var nObj, i;
  for (var i = 0; i < campo.form.elements.length; i++)
    if (campo == campo.form.elements[i]) {
      nObj = i;
    }
  for (var i = nObj + 1; i < campo.form.elements.length; i++)
    if (campo.form.elements[i].type != 'hidden' && !campo.form.elements[i].disabled && campo.form.elements[i].style.visibility != 'hidden') {
      return i;
    }
  return -1;
}

function editarCampo(evento, campo, tipo, mascara, prox, funcao) {
  var t, msk, virgula;
  if (campo.onblur == null)
    campo.onblur = campo.onfocus;
  if (campo.onkeyup == null)
    campo.onkeyup = campo.onfocus;
  if (campo.onkeypress == null)
    campo.onkeypress = campo.onfocus;
  if (evento.type == "focus") {
    campo.onfocus = null;
    return;
  }
  t = evento.keyCode;
  if (t == 9 || t == 16) {
    campo.focus();
    return;
  }
  if (funcao != null && evento.type == "keypress" && t == 13) {
    eval(funcao);
    return;
  }
  if (evento.type == "keypress") {
    return;
  }
  if (evento.type == "keypress" || evento.type == "keyup" || evento.type == "blur") {
    campo.value = eliminarCaracteresInvalidos(campo.value, tipo);
    campo.value = inserirMascara(campo.value, mascara);
    if (campo.value.length > campo.maxLength)
      campo.value = campo.value.substr(0, campo.maxLength);
    if (campo.value.length == campo.maxLength)
      if (prox != null){
      
        prox.focus();
      }  
      else {
        nOrd = obterIndiceProximoElemento(campo);
        if (nOrd > -1 && event.type != "blur") 
          campo.form.elements[nOrd].focus();
      }
  }
  if ((t>=16&&t<=20) || (t>=33&&t<=39) || (t>=44&&t<=45) || (t>=112&&t<=123) || (t>=144&&t<=145) || t==12)
    return true; 
  if ('ALNX'.indexOf(tipo, 0) != -1)
    campo.value = campo.value.toUpperCase();
  if (mascara != null && mascara != "" )
    if (campo.style.textAlign == "right") {
      msk = right(mascara, campo.value.length);
      campo.value = eliminarCaracteresInvalidos(campo.value, tipo);
      campo.value = eliminarZerosEsquerda(campo.value);
      mascara = inverter(mascara);
      virgula = mascara.indexOf(",")
      if (virgula > -1 && campo.value.length > 0)
        campo.value = preencheZeros(campo.value, virgula + 1);
      valor = inverter(campo.value);
      valor = inserirMascara(valor, mascara);
      campo.value = inverter(valor);
    }
    else {
      campo.value = eliminarCaracteresInvalidos(campo.value, tipo);
      campo.value = inserirMascara(campo.value, mascara);
      if (campo.value.length > campo.maxLength)
        campo.value = campo.value.substr(0, campo.maxLength);
    }
  return true;
}

function editarCampo2(evento, campo, tipo, mascara, prox, funcao) {
  var t, msk, virgula;
  if (campo.onblur == null)
    campo.onblur = campo.onfocus;
  if (campo.onkeyup == null)
    campo.onkeyup = campo.onfocus;
  if (campo.onkeypress == null)
    campo.onkeypress = campo.onfocus;
  if (evento.type == "focus") {
    campo.onfocus = null;
    return;
  }
  t = evento.keyCode;
  if (t == 9 || t == 16) {
    campo.focus();
    return;
  }
  if (funcao != null && evento.type == "keypress" && t == 13) {
    eval(funcao);
    return;
  }
  if (evento.type == "keypress") {
    return;
  }
  if (evento.type == "keypress" || evento.type == "keyup" || evento.type == "blur") {
    if (campo.value.length == campo.maxLength)
      if (prox != null){
      
        prox.focus();
      }  
      else {
        nOrd = obterIndiceProximoElemento(campo);
        if (nOrd > -1 && event.type != "blur") 
          campo.form.elements[nOrd].focus();
      }
  }
  if ((t>=16&&t<=20) || (t>=33&&t<=39) || (t>=44&&t<=45) || (t>=112&&t<=123) || (t>=144&&t<=145) || t==12)
    return true; 
  return true;
}



function leftTrim(cTexto) {
  var cNovoTexto = "";
  for (var i=0; i < cTexto.length; i++) {
    if (cTexto.substr(i, 1) != " ") { 
      cNovoTexto = cTexto.substr(i);
      break;
    }
  }
  return cNovoTexto;
}

function rightTrim(cTexto) {
  var cNovoTexto = "";
  for (var i = (cTexto.length - 1); i > -1 ; i--) {
    if (cTexto.substr(i, 1) != " ") { 
      cNovoTexto = cTexto.substr(0, i + 1);
      break;
    }
  }
  return cNovoTexto;
}

function allTrim(cTexto) {
  return leftTrim(rightTrim(cTexto));
}
function preencheZeros(Numero, Tamanho) {

  var Diferenca

  Numero = allTrim(Numero)
  Diferenca = Tamanho - Numero.length
  for (var i=0; i < Diferenca; i++)
    {
    Numero = "0" + Numero
    }

  return Numero

}
function inverter(s) {
  var i, ret;
  ret = "";
  i = s.length - 1;
  while (i >= 0) {
    ret += s.substr(i, 1);
    i--;
  }
  return ret;
}
function eliminarZerosEsquerda(valor) {
  var ret = "";
  var status = 0;
  for (var i = 0; i < valor.length; i++) {
    if (valor.substr(i, 1) != "0")
      status = 1;
    if (status == 1)
      ret += valor.substr(i, 1);
  }
  return ret;
}
function right(s, n) {
  return s.substr(s.length - n, n);
}
function inserirMascara(valor, mascara) {
  var fimValor, fimMascara, posV, posM, ret;
  ret = "";
  posV = 0;
  posM = 0;
  fimValor = false;
  fimMascara = (mascara.length == 0) ? true : false;
  while (posV < valor.length) {
    if (!fimMascara && mascara.substr(posM, 1) != "X") {
      ret += mascara.substr(posM, 1);
      posM++;
      if (posM == mascara.length)
        fimMascara = true;
      }
    else {
      ret += valor.substr(posV, 1);
      posV++;
      posM++;
    }
  }
  return ret;
}
function eliminarCaracteresInvalidos(valor, tipo) {
  var i, ret;
  if (tipo == "9")
    caracteres = "0123456789";
  else if (tipo == "A" || tipo == "a" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  else if (tipo == "L" || tipo == "l" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  else if (tipo == "N" || tipo == "n" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  else if (tipo == "T" || tipo == "t" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ÁáÉéÍíÓóÚúÀàÇçÃãÕõ-=*&#+\/,.;:";
  ret = "";
  for (i = 0; i < valor.length; i++)
    if (tipo == 'x' || tipo == 'X')
      ret += valor.substr(i, 1);
    else
      if (caracteres.indexOf(valor.substr(i, 1), 0) != -1)
        ret += valor.substr(i, 1);
  return ret; 
}
function identBrowser() {
  if (document.all)
	return "ie";
  if (document.layers)
	return "ns4";
  if (document.getElementById)
	return "ns6";
}
function procurarObjetoPorTipo(nome, tipo) {
  var  xx, ii, obj;
  obj = null;
  if (identBrowser() == "ns6") {
    var vetor = document.getElementsByTagName(tipo);
    for (ii = 0; vetor.item(ii) != null; ii++) {
      if (nome == vetor.item(ii).getAttribute("name"))
        return vetor.item(ii);
    }
  }
  else {
    xx = (!document.all);
    if (!xx) 
      for (ii = 0; ii < document.all.length; ii++)
        if (document.all[ii].nodeName == tipo)
            if (document.all[ii].name == nome || document.all[ii].id == nome)
              return document.all[ii];
  }
  return obj;
}

function tiraPonto(str,numDecimais) 
//Tira ponto e define o número de casas decimais
{
  for (i = 0; i < str.length; i++) 
  {
    if (str.charAt(i) == ".")
    {
		return str.substring(0,i)+str.substring(i+1,i+numDecimais+1);
	}
  }
  return str;
}


function calculaTotal(campo){

  var a = 0;
  
  if (campo.name == "dependente")
	mascara = "XX";
  else
    mascara = "X.XXX.XXX.XXX.XXX.XXX.XXX.XXX.XXX.XXX,XX";
  msk = right(mascara, campo.value.length);
  campo.value = eliminarCaracteresInvalidos(campo.value, '9');
  campo.value = eliminarZerosEsquerda(campo.value);
  mascara = inverter(mascara);
  virgula = mascara.indexOf(",")
  if (virgula > -1 && campo.value.length > 0)
    campo.value = preencheZeros(campo.value, virgula + 1);
  valor = inverter(campo.value);
  valor = inserirMascara(valor, mascara);
  campo.value = inverter(valor);
  if (campo.value == "")
    {
	campo.value = "";
	}

  a = parseFloat(eliminarCaracteresInvalidos(document.form.valor.value, '9'));
  if ((document.form.valor.value == "") || (document.form.valor.value == null))
	document.form.valor.value= "";
}