//===========================================================================================================
function print_this()
{
  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
      disp_setting+="scrollbars=yes,width=700, height=500, left=50, top=25";
  var content_vlue = document.getElementById("print_content").innerHTML;
  var titulo_vlue = document.getElementById("titulo_content").innerHTML;
  var docprint=window.open("","",disp_setting);
   docprint.document.open();
   docprint.document.write('<html><head><title>');
   docprint.document.write(titulo_vlue);
   docprint.document.write('</title>');
   docprint.document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"print.css\" />');
   docprint.document.write('</head><body onLoad="self.print()">');
   docprint.document.write(content_vlue);
   docprint.document.write('<center>SistemasParaCartórios.com.br - <i>http://www.sistemasparacartorios.com.br</i>');
   docprint.document.write('</center><br></body></html>');
   docprint.document.close();
   docprint.focus();
}
//===========================================================================================================
function validaCPF(cpf) {
       if (cpf.length > 11) cpf = trimPreCPF(cpf);
       erro = new String;
       if (cpf.length < 11) erro += "São necessários 11 digitos para verificação do CPF! \n\n"; 
       var nonNumbers = /\D/;
       if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números! \n\n"; 
       if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
               erro += "Número de CPF inválido, Verifique !!"
     }
     var a = [];
     var b = new Number;
     var c = 11;
     for (i=0; i<11; i++){
             a[i] = cpf.charAt(i);
             if (i < 9) b += (a[i] * --c);
     }
     if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
     b = 0;
     c = 11;
     for (y=0; y<10; y++) b += (a[y] * c--); 
     if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
     if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
             erro +="Número de CPF Inválido, Verifique !!";
     }
     if (erro.length > 0){
             alert(erro);
             return false;
     }
     return true;
}

//===========================================================================================================

  function FormataCpf(campo,tammax,teclapres) {
    var tecla = teclapres.keyCode;
    // vr = document.form[campo].value;
    vr = event.srcElement.value;
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( ",", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( "-", "" );
    vr = vr.replace( "-", "" );
    vr = vr.replace( "-", "" );
    vr = vr.replace( "-", "" );
    vr = vr.replace( "-", "" );
    tam = vr.length;
    if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
    if (tecla == 8 ){ tam = tam - 1 ; }
    if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
    if ( tam <= 2 ){ 
    event.srcElement.value = vr ; }
    if ( (tam > 2) && (tam <= 5) ){
    event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 6) && (tam <= 8) ){
    event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 9) && (tam <= 11) ){
    event.srcElement.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 12) && (tam <= 14) ){
    event.srcElement.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 15) && (tam <= 17) ){
    event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ;}
    }  
    }

//===========================================================================================================

function trimPreCPF(preCPF)
{
 var tempTextVal = trimStr(preCPF);
 var len = tempTextVal.length;
 if (null==len || len<1)
  return "";
 
 var tempCPF = "";
 var charPosition = new Array();
 var count = 0;
 var pattern = /^\s|\/|\-|\.$/;
 
 for (var i=0; i<len; i++)//(tempTextVal.charAt(i)==".")
 {
  //nonspstr.test(textVal.charAt(i))
  if (pattern.test(tempTextVal.charAt(i))) 
  {
   charPosition[count]=i;
   count++;  
  }
    
 }
 
 if (charPosition.length > 0){
  for(i=0; i<charPosition.length; i++){
   if (i==0){
    if ( i==charPosition.length-1){
     tempCPF = tempCPF + tempTextVal.slice(0,charPosition[i]) + tempTextVal.slice(charPosition[i]+1);
    }else{
     tempCPF = tempCPF + tempTextVal.slice(0,charPosition[i]);
    }
   }else {
    if (i==charPosition.length-1) {
    tempCPF = tempCPF + tempTextVal.slice(charPosition[i-1]+1,charPosition[i]) + tempTextVal.slice(charPosition[i]+1);
   
    }else { 
    tempCPF = tempCPF + tempTextVal.slice(charPosition[i-1]+1,charPosition[i]);
    }
   }
  
  }
 return tempCPF ; 
 }
 else 
  return tempTextVal;
}

//===========================================================================================================

function trimStr(textVal)
{
 var len=textVal.length;
 if (null==len || len<1)
  return "";
 var trimleft = "";
 var nonspstr = /^\S$/;
 var spaceCount = 0;
 for (i=0; i<len; i++)
 {
  if ( nonspstr.test(textVal.charAt(i)) )
   break;
  else
   spaceCount++;
 }
 if (spaceCount==len)
  return "";
 else
  trimleft = textVal.slice(spaceCount);
 len = trimleft.length;
 spaceCount = 0;
 for (i=len-1; i > 0; i--)
 {
  if ( nonspstr.test(trimleft.charAt(i)) )
   break;
  else
   spaceCount++;
 }
 if (spaceCount==0)
  return trimleft;
 else
  return trimleft.slice(0, len-spaceCount);
}

//===========================================================================================================

function CriticaLogin()
    {
        if (document.login.nome == "")
        {
            alert("É necessário informar o nome do usuário !!!");
            return (false);
        }
        if (document.login.senha == "")
        {
            alert("É necessário digitar a senha do usuário !!!");
            return (false);
        }
    }

//===========================================================================================================

function CriticaCEP()
{
  if (document.Geral.CEP.value == "")
  {
    alert("Informe no mínimo os 5(cinco) primeiros dígitos do CEP. Ex. 70001");
    document.Geral.CEP.focus();
    return (false);
  }

  if (document.Geral.CEP.value.length <= 4)
  {
        alert("Informe no mínimo os 5(cinco) primeiros dígitos do CEP. Ex. 70001");
        document.Geral.CEP.focus();
        return (false);
  }  

  { 
   var Numeros = "0123456789";
   var Posic, Carac;
   var Temp = document.Geral.CEP.value.length;    
   var Cont = 0;
   for (var i=0; i < Temp; i++)   
   {  
   Carac =  document.Geral.CEP.value.charAt (i);
   Posic  = Numeros.indexOf (Carac);   
   if (Posic > -1)   
          Cont++;      
   }   
   if (Cont == 9)
   {
        alert("O CEP tem no máximo 8(oito) digitos numéricos. Ex. 70001-970");
        document.Geral.CEP.focus();
        return (false);
   } 
 }
  { 
   var Numeros = "0123456789-";
   var Posic, Carac;
   var Temp = document.Geral.CEP.value.length;    
   var Cont = 0;
   for (var i=0; i < Temp; i++)   
   {  
   Carac =  document.Geral.CEP.value.charAt (i);
   Posic  = Numeros.indexOf (Carac);   
   if (Posic == -1)   
      {   
        alert("Informe um CEP válido. Ex. 70001-970");
        document.Geral.CEP.focus();
        return (false);
      }
   }   
 }
}    

//===========================================================================================================

function MascaraCEP (formato, keypress, objeto)
 {
 campo = eval (objeto);
 if (formato=='CEP')
         {
         caracteres = '01234567890';
         separacoes = 1;
         separacao1 = '-';
         conjuntos = 2;
         conjunto1 = 5;
         conjunto2 = 3;
         if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
         (conjunto1 + conjunto2 + 1))
                 {
                 if (campo.value.length == conjunto1) 
                    campo.value = campo.value + separacao1;
                 }
         else 
                 event.returnValue = false;
         }
 }

//===========================================================================================================

function makeCRM(field)
 {
 if(field.value.substr(0,3)!='52-')
  field.value ='52-' + field.value;
 }

//===========================================================================================================

function IsNumeric(valor)
{
var log=valor.length; var sw="S";
for (x=0; x<log; x++)
{ v1=valor. substr(x,1);
v2 = parseInt(v1);
//Comprovo se é um valor numérico
if (isNaN(v2)) { sw= "N";}
}
if (sw=="S") {return true;} else {return false; }
}

var primeiroslap=false;
var segundoslap=false;
function formateadata(data)
{
var long = data.length;
var dia;
var mes;
var ano;

if ((long>=2) && (primeiroslap==false)) { dia=data.substr(0,2);
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { data=data.substr(0,2)+"/"+data.substr(3,7); primeiroslap=true; }
else { data=""; primeiroslap=false;}
}
else
{ dia=data.substr(0,1);
if (IsNumeric(dia)==false)
{data="";}
if ((long<=2) && (primeiroslap=true)) {data=data.substr(0,1); primeiroslap=false; }
}
if ((long>=5) && (segundoslap==false))
{ mes=data.substr(3,2);
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { data=data.substr(0,5)+"/"+data.substr(6,4); segundoslap=true; }
else { data=data.substr(0,3);; segundoslap=false;}
}
else { if ((long<=5) && (segundoslap=true)) { data=data.substr(0,4); segundoslap=false; } }
if (long>=7)
{ ano=data.substr(6,4);
if (IsNumeric(ano)==false) { data=data.substr(0,6); }
else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { data=data.substr(0,6); } } }
}

if (long>=10)
{
data=data.substr(0,10);
dia=data.substr(0,2);
mes=data.substr(3,2);
ano=data.substr(6,4);
// Ano nao bisexto e é fevereiro e o dia é maior a 28
if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { data=data.substr(0,2)+"/"; }
}
return (data);
} 
