﻿function confirmacao() {
        var msg = confirm('Deseja Salvar as Alterações');
        if (!msg){
            return false;
        }else
        return true;
}

function addImage()
{
    var newElement = document.createElement("link");
    newElement.setAttribute("rel", "icon");
    newElement.setAttribute("href", "images/favicon.png");
    document.getElementsByTagName("head")[0].appendChild(newElement);
}
addImage();

function BloqueiaLetras(evento)
{
var tecla;

if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = evento.which;
}

    if(tecla >= 48 && tecla <= 57 || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 47) return true;
   return false;
}

function numerosVirgulas(evento)
{
var tecla;

if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = evento.which;
}
    if(tecla >= 48 && tecla <= 57 || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 44) return true;
   return false;
}

function MascaraCNPJ(cnpj, e,campo){
if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = e.which;
}
    if ((tecla >= 48 && tecla <= 57) || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 13)
    {
        if(tecla != 13)
            return formataCampo(cnpj, '00.000.000/0000-00', e);
        else{
            
            document.getElementById(campo).focus();
            return false;
        }
        
    }
    
    else
        return false;
}

function MascaraCPFouCNPJ(cnpj, e, campo){

if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = e.which;
}



    if ((tecla >= 48 && tecla <= 57) || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 13)
    {
        if(tecla != 13){
            var exp = /\-|\.|\/|\(|\)|\:| /g
            var campoSoNumeros = cnpj.value.toString().replace( exp, "" );
            if(campoSoNumeros.length >= 11)
                return formataCampo(cnpj, '00.000.000/0000-00', e);
            else
                return formataCampo(cnpj, '000.000.000-00', e);
        }
        else
        {
            document.getElementById(campo).focus();
            return false;
        }
    }

    else
       return false;
}

//adiciona mascara de cep
function MascaraCep(cep, e,campo){
var tecla;
if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = e.which;
}
    
    if ((tecla >= 48 && tecla <= 57) || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 13){
        if(tecla != 13)
            return formataCampo(cep, '00.000-000', e);
        else{
                document.getElementById(campo).focus();
                return false;
            }
    }
    else        
            return false;
}

//adiciona mascara de data
function MascaraData(data){
        if(mascaraInteiro(data)==false){
                event.returnValue = false;
        }       
        return formataCampo(data, '00/00/0000', event);
}

function formataData(evento,id){
  var p= document.getElementById(id).value;
  
  var tecla;
  
    if(window.event) { // Internet Explorer
      tecla = event.keyCode;
    }
    else { // Firefox
      tecla = evento.which;
    }
    

  if(tecla > 8 || tecla < 8){
      if(p.length == 2){
        document.getElementById(id).value= p + "/";
      }
      
      if(p.length == 5){
        document.getElementById(id).value=p + "/";
      }
      
     if(evento.which == 13)
     {
        document.getElementById(proximo).focus();
        return false;
     }
  }
  
  
}

//adiciona mascara ao telefone
function MascaraTelefone(tel, e,campo){  
var tecla;
if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = e.which;
}
    
    if ((tecla >= 48 && tecla <= 57) || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 13){
        if(tecla != 13){
            return formataCampo(tel, '(00) 0000-0000', e);
        }
        else{
                document.getElementById(campo).focus();
                return false;
            }
    }
    else
        return false;
}

//adiciona mascara ao CPF
function MascaraCPF(cpf){
        if(mascaraInteiro(cpf)==false){
                event.returnValue = false;
        }       
        return formataCampo(cpf, '000.000.000-00', event);
}

//valida telefone
function ValidaTelefone(tel){
        exp = /\(\d{2}\)\ \d{4}\-\d{4}/
        if(!exp.test(tel.value))
                alert('Numero de Telefone Invalido!');
}

//valida CEP
function ValidaCep(cep){
        exp = /\d{2}\.\d{3}\-\d{3}/
        if(!exp.test(cep.value))
                alert('Numero de Cep Invalido!');               
}

//valida data
function ValidaData(data){
        exp = /\d{2}\/\d{2}\/\d{4}/
        if(!exp.test(data.value))
                alert('Data Invalida!');                        
}

//valida o CPF digitado
function ValidarCPF(Objcpf){
        var cpf = Objcpf.value;
        exp = /\.|\-/g
        cpf = cpf.toString().replace( exp, "" ); 
        var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
        var soma1=0, soma2=0;
        var vlr =11;
        
        for(i=0;i<9;i++){
                soma1+=eval(cpf.charAt(i)*(vlr-1));
                soma2+=eval(cpf.charAt(i)*vlr);
                vlr--;
        }       
        soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
        soma2=(((soma2+(2*soma1))*10)%11);
        
        var digitoGerado=(soma1*10)+soma2;
        if(digitoGerado!=digitoDigitado)        
                alert('CPF Invalido!');         
}

//valida numero inteiro com mascara
function mascaraInteiro(num, e,campo){

var tecla;
if(window.event) { // Internet Explorer
  tecla = event.keyCode;
}
else { // Firefox
  tecla = e.which;
}
    if ((tecla >= 48 && tecla <= 57) || tecla == 8 || tecla == 0 || tecla == 9 || tecla == 13)
    {
        if(tecla != 13)
            return true;
        else{
                document.getElementById(campo).focus();
                return false;
        }
            
    }
    else
    {
        return false;
    }
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj){
        var cnpj = ObjCnpj.value;
        var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
        var dig1= new Number;
        var dig2= new Number;
        
        exp = /\.|\-|\//g
        cnpj = cnpj.toString().replace( exp, "" ); 
        var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
                
        for(i = 0; i<valida.length; i++){
                dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);  
                dig2 += cnpj.charAt(i)*valida[i];       
        }
        dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
        dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
        
        if(((dig1*10)+dig2) != digito)  
                alert('CNPJ Invalido!');
                
}


function formataCampo(campo, Mascara, evento) {
    var boleanoMascara;
    //alert('formataCampo');
    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)|\:| /g
    campoSoNumeros = campo.value.toString().replace( exp, "" );
  
    var posicaoCampo = 0;    
    var NovoValorCampo="";
    var TamanhoMascara = campoSoNumeros.length;;
    
    if (Digitato != 8) { // backspace
        for(i=0; i<= TamanhoMascara; i++) {
            boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/") || (Mascara.charAt(i) == ":"))
            boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                  TamanhoMascara++;
            }else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
              }           
          }    
        campo.value = NovoValorCampo;
          return true;
    }else {
        return true;
    }
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e,campo){
    
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) {
        document.getElementById(campo).focus();
    }
    if (whichCode == 8) return true;
    if (whichCode == 0) return true;
    if (whichCode == 9) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function soNumeros(objTextBox, e){
    var strCheck = '0123456789';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    //alert(whichCode);
    if (whichCode == 13) return true;
    if(whichCode==8) return true; //Back-space
    if(whichCode==0) return true; //representa nulo, é igual delete, setas para cima,baixo etc.., tab etc....
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
}

//função que abre uma janela modal (endereco,largura,altura)
function openModal(pUrl, pWidth, pHeight) {
	
	if (window.showModalDialog) {
		return window.showModalDialog(pUrl, window,
		  "dialogWidth:" + pWidth + "px;dialogHeight:" + pHeight + "px");
	} else {
		try {
			netscape.security.PrivilegeManager.enablePrivilege(
			  "UniversalBrowserWrite");
			window.open(pUrl, "wndModal", "width=" + pWidth
			  + ",height=" + pHeight + ",top=50px,left=150px,resizable=no,modal=yes");
			return true;
		}
		catch (e) {
			alert("Script não confiável, não é possível abrir janela modal.");
			return false;
		}
	}
}

function enterasTab(evento,campo){
            var tecla;
           
            if(window.event) { // Internet Explorer
                tecla = event.keyCode;
            }
            else { // Firefox
                tecla = evento.which;
            }   
            
           if(tecla == 13){
                if(campo != ""){
                    document.getElementById(campo).focus();
                    return false;
                }
                else return true;
        
         }
   }
   
   
function alteraFormulario(idCampo1, idCampo2){
    
    if(document.getElementById(idCampo1).checked){
        document.getElementById('divSindicato').style.display='block';
        document.getElementById('divContribuinte').style.display='none';
    }
    
    if(document.getElementById(idCampo2).checked){
       
        document.getElementById('divSindicato').style.display='none';
        document.getElementById('divContribuinte').style.display='block';
    }
}

function mascaraHorario(evento,idCampoFormatar, idProximoCampo)
{ 
      var p= document.getElementById(idCampoFormatar).value;
      
      if(evento.which > 8 || evento.which < 8){
          if(p.length == 2){
            document.getElementById(idCampoFormatar).value= p + ":";
          }
                  
         if(evento.which == 13)
         {
            document.getElementById(idProximoCampo).focus();
            return false;
         }
       }
}


function formataTelefone(evento,idCampoFormatar){
  var p= document.getElementById(idCampoFormatar).value;
  //alert(evento.which);
  
    var tecla;
           
    if(window.event) { // Internet Explorer
        tecla = event.keyCode;
    }
    else { // Firefox
        tecla = evento.which;
    }  
  
   
  if(tecla > 8 || tecla < 8){
          if(p.length == 0 && evento.which != 0){
            document.getElementById(idCampoFormatar).value= p + "(";
          }
          
          if(p.length == 3){
            document.getElementById(idCampoFormatar).value=p + ")";
          }
          
          if(p.length == 8){
             document.getElementById(idCampoFormatar).value=p + "-";
          }
      }
      
  }
  
  
  
  function formataCep(evento, id){
  var p= document.getElementById(id).value;
  
  var tecla;
           
    if(window.event) { // Internet Explorer
        tecla = event.keyCode;
    }
    else { // Firefox
        tecla = evento.which;
    }  
    
      if(tecla > 8 || tecla < 8){
          
          if(p.length == 5){
            document.getElementById(id).value= p + "-";
        }
      }
  
  }
  
  function exibeSubMenu(id){
        document.getElementById(id).style.display="block";
  }
  
  function escondeSubMenu(id){
        document.getElementById(id).style.display="none";
  }
  
  function selecionaMenu(id, classe){
    //alert(id);
    document.getElementById(id).className=classe;
  }


function desmarcaMenu(id,classe){
    document.getElementById(id).className = classe;
}


function imprimir(){
    window.print();
}


function exibeDiv(id,controle){

        document.getElementById(id).style.display='block';
 
}

function escondeDiv(id,divSecundario){

    document.getElementById(id).style.display='none';
    if(divSecundario != ""){
        document.getElementById(divSecundario).style='none';
    }
    
}

function exibeAbas(id,top){
    

    document.getElementById('aba1').style.display='none';
    document.getElementById('aba2').style.display='none';
    //document.getElementById('aba3').style.display='none';
    document.getElementById('aba4').style.display='none';
    document.getElementById(id).style.display='block';
    
    document.getElementById('top1').className='header-tab';
    document.getElementById('top2').className='header-tab';
    //document.getElementById('top3').className='header-tab';
    document.getElementById('top4').className='header-tab';
    document.getElementById(top).className = 'header-tab-sel';
}




function formataCPF(event, id){
    var tecla;
    var p;
    
    p= document.getElementById(id).value;
    
    if(window.event) { // Internet Explorer
      tecla = event.keyCode;
    }
    else { // Firefox
      tecla = e.which;
    }
        
          if(tecla > 8 || tecla < 8)
         {
            if(p.length == 2 && evento.which != 0)
            {
              document.getElementById(id).value= p + ".";
            }
          
           if(p.length == 6)
           {
             document.getElementById(idCampoFormatar).value=p + ".";
           }
          
           if(p.length == 9)
           {
             document.getElementById(idCampoFormatar).value=p + "-";
           }
         }
}


function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e,campo){
    
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) {
        document.getElementById(campo).focus();
    }
    if (whichCode == 8) return true;
    if (whichCode == 0) return true;
    if (whichCode == 9) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}






