<!--

var Language = "PT";

function FindObject(n, d) {
  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 = FindObject(n, d.layers[i].document);
  if ( !x && d.getElementById ) x = d.getElementById(n);
  return x;
}

function ValidateForm() {
  var k = 0, i, p, q, nm, test, val, num, vmin, vmax, errs = "", args = ValidateForm.arguments;
  for ( i = 0; i < ( args.length - 2 ); i += 3 ) {
    test = args[i+2]; val = FindObject(args[i]);
    if ( val ) {
      nm = ( args[i+1] != "" )?args[i+1]:val.name; nm = "- campo " + nm;
      if ( val.options && val.selectedIndex <= 0 ) errs += "" + nm + " deve ser selecionado\n";
      else if ( (val=val.value) != "" ) {
        if ( test.indexOf("isEmail") != -1 ) {
          p = val.indexOf("@"); if ( p < 1 || p == ( val.length - 1 ) ) errs += "" + nm + " deve conter um endereço de e-mail válido\n";
        } else if ( test != "R" ) {
          p = val.indexOf(","); if ( p != -1 ) val = val.substring(0, p) + "." + val.substring(p + 1);
          num = parseFloat(val); if ( val != ""+num && val.indexOf(".") == -1 ) errs += "" + nm + " deve conter um número\n";
          if ( test.indexOf("inRange") != -1 ) {
            p = test.indexOf(":"); vmin = parseFloat(test.substring(8, p)); vmax = parseFloat(test.substring(p + 1));
            if ( num < vmin || vmax < num ) errs += "" + nm + " deve conter um número entre " + vmin + " e " + vmax + "\n";
          }
        }
      } else if ( test.charAt(0) == "R" ) errs += "" + nm + " é requerido\n";
    }
  }
  return errs;
}

function CharsLeft(f, h, m) {
  val = FindObject(f);
  hlp = FindObject(h);
  if ( val && hlp ) {
    l = m - val.value.length;
    if ( l > 0 ) hlp.innerHTML = "Restam " + l + " caracteres até o limite!";
    else if ( l == 0 ) hlp.innerHTML = "Limite de caracteres esgotado!";
    else val.value = val.value.substring(0, m);
  }
}

function CheckAll() {
  for ( n = 0; n < document.forms.length; n++ ) {
    new_value = (!document.forms[n].elements[0].checked);
    for (var i = 0; i < document.forms[n].elements.length; i++) {
      document.forms[n].elements[i].checked = new_value;
    }
  }
}

function Redimension() {
  var args = Redimension.arguments;
  
  if ( args.length == 0 ) {
    x = self.document.body.scrollWidth + self.document.body.offsetWidth - self.document.body.clientWidth + 7;
    y = self.document.body.scrollHeight + self.document.body.offsetHeight - self.document.body.clientHeight + 15 + 58;
  } else if ( args.length >= 2 ) { x = args[0]; y = args[1]; }
  
  self.resizeTo(x, y);
  
  x = ( screen.availWidth - x ) / 2;
  y = ( screen.availHeight - y ) / 2;
  if ( x > 0 && y > 0 ) window.moveTo(x, y);
  
  self.focus();
}

function CloseDisplay() {
  var i, val, args = CloseDisplay.arguments;
  for ( i = 0; i < args.length; i++ ) {
    val = FindObject(args[i]);
	if ( val && val.tagName != "DIV" && typeof(val.style.display) != "undefined" ) val.style.display = "none";
    else if ( val && typeof(val.style.visibility) != "undefined" ) val.style.visibility = "hidden";
    else if ( val && typeof(val.style.display) != "undefined" ) val.style.display = "none";
  }
}

function OpenWindow(theURL, winName, features) {
  window.open(theURL, winName, features);
}

function CloseWindow() {
  window.opener.focus();
  window.close();
}

function DecodeURL(s) {
  var ds = unescape(s.toString());
  while ( ds.indexOf("+") != -1 ) ds = ds.replace("+", " ");
  return ds;
}

function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function FormatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if ( isNaN(num) ) num = "0";
  sign = ( num == ( num = Math.abs(num) ) );
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if ( cents < 10 ) cents = "0" + cents;
  for ( var i = 0; i < Math.floor((num.length-(1+i))/3); i++ ) num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
  return ( ((sign)?'':'-') + 'R$' + num + ',' + cents );
}

function CNumber(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if ( isNaN(num) ) num = "0";
  sign = ( num == ( num = Math.abs(num) ) );
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if ( cents < 10 ) cents = "0" + cents;
  return ( ((sign)?'':'-') + num + ',' + cents );
}

function VerCPF(cpf) {
  //--- CPF (Cadastro de Pessoa Física)
  //--- O CPF é composto por 9 dígitos, mais 2 DVs, resultando em um número com 11 casas decimais.
  //--- Os 8 primeiros são chamados de número-base, o 9º define a Região Fiscal, o 10º é o DV módulo 11 dos 9 anteriores, e o último é o DV módulo 11 dos 10 anteriores.
  var OK = false;
  if ( cpf.length == 11 && cpf.substring(0, 9) != "000000000" ) {
    var RegiaoFiscal = "";
    switch ( cpf.substring(8, 9) ) {
      case "1": RegiaoFiscal = "DF, GO, MS, MT, TO"; break;
      case "2": RegiaoFiscal = "AC, AM, AP, PA, RO, RR"; break;
      case "3": RegiaoFiscal = "CE, MA, PI"; break;
      case "4": RegiaoFiscal = "AL, PB, PE, RN"; break;
      case "5": RegiaoFiscal = "BA, SE"; break;
      case "6": RegiaoFiscal = "MG"; break;
      case "7": RegiaoFiscal = "ES, RJ"; break;
      case "8": RegiaoFiscal = "SP"; break;
      case "9": RegiaoFiscal = "PR, SC"; break;
      case "0": RegiaoFiscal = "RS"; break;
    }
    var NumBase = cpf.substring(0, 9), Peso, Soma;
    Peso = 9; Soma = 0;
    for ( d = NumBase.length; d > 0; d-- ) {
      Soma += eval(NumBase.charAt(d - 1)) * Peso;
	  Peso--; if ( Peso < 0 ) Peso = 9;
    }
	var DV1 = Soma % 11; if ( DV1 == 10 ) DV1 = 0;
	NumBase += DV1;
    Peso = 9; Soma = 0;
    for ( d = NumBase.length; d > 0; d-- ) {
      Soma += eval(NumBase.charAt(d - 1)) * Peso;
	  Peso--; if ( Peso < 0 ) Peso = 9;
    }
	var DV2 = Soma % 11; if ( DV2 == 10 ) DV2 = 0;
	NumBase += DV2;
    OK = ( cpf == NumBase );
  }
  return OK;
}

function VerCNPJ(cnpj) {
  //--- CNPJ (Cadastro Nacional de Pessoa Jurídica)
  //--- O CNPJ é composto por 8 dígitos de número base, seguidos de 4 dígitos de ordem das filiais da empresa, e 2 DVs - totalizando 14 casas decimais.
  //--- A base para sequência de somatória e multiplicação inicia em 9, e termina em 2.
  //--- Também deve-se validar o 8º dígito pela somatória da múltiplicação dos 7 primeiros dígitos, usando como base a sequência 2 1 2 1 2 1 2 somando resultados noves fora e módulo 10.
  var OK = false;
  if ( cnpj.length == 14 && cnpj.substring(0, 8) != "00000000" && cnpj.substring(8, 12) != "0000" ) {
    var NumBase = cnpj.substring(0, 12), Peso, Soma;
    Peso = 9; Soma = 0;
    for ( d = NumBase.length; d > 0; d-- ) {
      Soma += eval(NumBase.charAt(d - 1)) * Peso;
      Peso--; if ( Peso < 2 ) Peso = 9;
    }
	var DV1 = Soma % 11; if ( DV1 == 10 ) DV1 = 0;
	NumBase += DV1;
    Peso = 9; Soma = 0;
    for ( d = NumBase.length; d > 0; d-- ) {
      Soma += eval(NumBase.charAt(d - 1)) * Peso;
      Peso--; if ( Peso < 2 ) Peso = 9;
    }
	var DV2 = Soma % 11; if ( DV2 == 10 ) DV2 = 0;
	NumBase += DV2;
    OK = ( cnpj == NumBase );
    if ( OK && cnpj.charAt(0) > "0" ) {
      var Resultado, NovesFora;
      NumBase = cnpj.substring(0, 7);
      Peso = 2; Soma = 0;
      for ( d = 1; d <= NumBase.length; d++ ) {
        Resultado = eval(NumBase.charAt(d - 1)) * Peso;
        while ( Resultado > 9 ) {
          NovesFora = String(Resultado);
          Resultado = eval(NovesFora.charAt(0)) + eval(NovesFora.charAt(1));
        }
        Soma += Resultado;
        Peso--; if ( Peso < 1 ) Peso = 2;
      }
	  var D8 = ( ( 10 - ( Soma % 10 ) ) % 10 );
      //OK = ( cnpj.substring(7, 8) == D8 );
    }
  }
  return OK;
}

function VerCPFCNPJ(nrf) {
  return ( VerCPF(nrf) || VerCNPJ(nrf) );
}

function NumbersOnly(myfield, e) {
  if ( myfield.length == 0 ) myfield.value = 0;
  var key;
  var keychar;
  if ( window.event ) key = window.event.keyCode;
  else if ( e ) key = e.which;
  else return true;
  keychar = String.fromCharCode(key);
  if ((key == null) || (key == 0) || (key == 8) || (key == 9)|| (key == 13)|| (key == 27)) return true;
  else if ( (("0123456789").indexOf(keychar) > -1) ) return true;
  else return false;
}

//-->
