
// JavaScript Document

function JumpField(fields) {
    if (fields.value.length == fields.maxLength) {
        for (var i = 0; i < fields.form.length; i++) {
            if (fields.form[i] == fields && fields.form[(i + 1)] && fields.form[(i + 1)].type != "hidden") {
                fields.form[(i + 1)].focus();
                //break;
            }
        }
    }
}

function proximo(obj){
    var elementos = document.getElementsByTagName('INPUT');
    for(var i=0;i<elementos.length;i++){
        if(elementos[i].name==obj.name){
            while(elementos[i+1]!=null){
                if(!elementos[i+1].disabled && elementos[i+1].tabIndex>=0){
                    elementos[i+1].focus();
                    return;
                }
                i++;
            }
        }
    }
}

function saltaCampo(obj){
    if(obj.value.length>=obj.maxLength){        
        proximo(obj);
        return false;
    }
    return true;
}

function data(data){
    if(data.value.length == 2){
        data.value = data.value + "/";
    }
    if(data.value.length == 5){
        data.value = data.value + "/";
    }
}

function Mascara (formato, keypress, objeto){
    campo = eval (objeto);

    // cpf
    if (formato=='cpf'){
        separador1 = '.'; 
        separador2 = '-'; 
        conjunto1 = 3;
        conjunto2 = 7;
        conjunto3 = 11;
        if (campo.value.length == conjunto1){
            campo.value = campo.value + separador1;
        }
        if (campo.value.length == conjunto2){
            campo.value = campo.value + separador1;
        }
        if (campo.value.length == conjunto3){
            campo.value = campo.value + separador2;
        }
    }
    
    // telefone
    if (formato=='telefone'){
        separador3 = '-';
        conjunto3 = 4;
        if (campo.value.length == conjunto3){
            campo.value = campo.value + separador3;
        }
    }

}

function excluirCadastro(url) { 
	if(confirm("Deseja realmente excluir este registro?")){
		window.location = url;
	}
}

function sair(url) { 
	if(confirm("Deseja sair do Sistema?")){
		window.location = url;
	}
}

function imprimir(URL){
	window.open(URL,"popup","resizable=no,menubar=yes,scrollbars=yes,location=no,top=20,left=20,width=800,height=500");
}

function abrirPopup(url){
    window.open( url , 'janela' , 'resize=0,width=545,height=630,top=10,left=10,addressbar=0,statusbar=0,menubar=0,scrollbars=0,toolbar=0') ; 
}

function abrirPopupImpressao(url){
    window.open( url , 'janela' , 'resize=0,width=700,height=500,top=30,left=30,addressbar=0,statusbar=0,menubar=0,scrollbars=1,toolbar=0') ; 
}

function upperCase(event) { 
   var keynum; 

   // IE 
   if (window.event) { 
   keynum = window.event.keyCode; 
} 
   // Netscape/Firefox/Opera 
   else if (event.which) { 
      keynum = event.which; 
   } 

   if ((keynum >= 97 && keynum <= 122) || (keynum >= 224 && keynum <= 255)) { 
      // converte de acordo com o valor decimal da tecla na tabela ascii    
      keynum = keynum - 32; 
       
      // IE 
      if (window.event) { 
         window.event.keyCode = keynum; 
      } 
      // firefox e outros que usam o Gecko 
      else if (event.which) { 
         var newEvent = document.createEvent("KeyEvents"); 
         newEvent.initKeyEvent("keypress", true, true, document.defaultView, 
                  event.ctrlKey, event.altKey, event.shiftKey, 
                  event.metaKey, 0, keynum); 
         event.preventDefault(); 
         event.target.dispatchEvent(newEvent); 
      } 
   } 
   return true; 
}

function popupPage() {
var page = "unidade-concessao.php";
windowprops = "location=no,"
+ "scrollbars=yes,menubars=no,toolbars=no,resizable=yes";

window.open(page, "Popup", windowprops);
}

function imprimir(URL){
    window.open(URL,"popup","resizable=no,menubar=yes,scrollbars=yes,location=no,top=20,left=20,width=700,height=450");
}

var valorAntigo = "";

function semNumero(e) {

    //alert("Entrou!");

    teclaPressionada = -1;

    try {
        var teclaPressionada = e.which;
        //alert("Entrou Gecko!");
        //alert("teclaPressionada: " + teclaPressionada);
    } catch(er) {
    }

    try {
        var teclaPressionada = event.keyCode;
        //alert("Entrou IE!");
    } catch(er) {
    }

    processar = false;

    if ((teclaPressionada != 8) &&
        (teclaPressionada != 16) &&
        (teclaPressionada != 17) &&
        (teclaPressionada != 18) &&
        (teclaPressionada != 20) &&
        (teclaPressionada != 33) &&
        (teclaPressionada != 34) &&
        (teclaPressionada != 35) &&
        (teclaPressionada != 36) &&
        (teclaPressionada != 37) &&
        (teclaPressionada != 38) &&
        (teclaPressionada != 39) &&
        (teclaPressionada != 40) &&
        (teclaPressionada != 45) &&
        (teclaPressionada != 46)){
        processar = true;
    }

    if (processar) {

        avancarCursor = false;

        valorOriginal = this.value;

        //alert("valorOriginal: " + valorOriginal);

        tamanhoValorOriginal = valorOriginal.length;

        //alert("tamanhoValorOriginal: " + tamanhoValorOriginal);

        posicaoCaracter = -1;

        caracteresValor = valorOriginal.split("");

        novosCaracteresValor = Array();

        ind = 0;

        naoExisteVirgula = true;

        for (i = 0; i < tamanhoValorOriginal; i++) {
            if (isNaN(caracteresValor[i])) {
                if (naoExisteVirgula) {
                    if (caracteresValor[i] == ":") {
                        naoExisteVirgula = false;
                        novosCaracteresValor[ind] = caracteresValor[i];
                        ind++;
                    } else {
                        posicaoCaracter = ind;
                    }
                }
            } else {
                novosCaracteresValor[ind] = caracteresValor[i];
                ind++;
            }
        }

        novoValor = novosCaracteresValor.join("");

        //alert("novoValor antes: " + novoValor);

        novoTamanhoValor = novoValor.length;

        

        //alert("novoValor depois: " + novoValor);

        novoTamanhoValor = novoValor.length;

        posicaoVirgula = novoValor.indexOf(",");



        //alert("novoValor final: " + novoValor);

        this.value = novoValor;


        /*

        var trSelection;

        if (isIE) {
            trSelection = this.createTextRange();
        } else {
            trSelection = document.selection.createRange();
        }

        if (posicaoCaracter == -1) {
            posicaoCaracter = getSelectionStart(this);
        }

        if (avancarCursor) {
            posicaoCaracter++;
        }

        trSelection.collapse(true);
        trSelection.moveStart("character", posicaoCaracter);
        trSelection.select();

        valorAntigo = novoValor;

        */

    }
}

function mostrarForm(obj){
    
    if(obj.value=="pj"){
        document.getElementById("divcnpj").style.display="block";
        document.getElementById("divcpf").style.display="none";
        document.getElementById("divcnpj1").style.display="block";
        document.getElementById("divcpf1").style.display="none";
        document.getElementById("divcnpj_nome").style.display="block";
        document.getElementById("divcpf_nome").style.display="none";
    }else if(obj.value=="pf"){
        document.getElementById("divcnpj").style.display="none";
        document.getElementById("divcpf").style.display="block";
        document.getElementById("divcnpj1").style.display="none";
        document.getElementById("divcpf1").style.display="block";
        document.getElementById("divcnpj_nome").style.display="none";
        document.getElementById("divcpf_nome").style.display="block";
    }
}

function mostrarLicitacao(obj){
    
    if(obj.value == "3"){
        document.getElementById("label_licitacao").style.display="block";
        document.getElementById("input_licitacao").style.display="block";
        
    }else {
        document.getElementById("label_licitacao").style.display="none";
        document.getElementById("input_licitacao").style.display="none";
    }
}



function ContaCaracteres(){
    num_carac=document.getElementById('missao_retirado').value.length;
    restam=150-num_carac;
    d=document.getElementById('caracteres');
    d2=document.getElementById('missao_retirado');    
    d.value=restam;
    if(d2.value.length > 150){
        aux=d2.value;
        d2.value=aux.substr(0, 150);
        d.value=0;        
    }
}

function ContaCaracteres2(){
    num_carac=document.getElementById('missao_retirado2').value.length;
    restam=150-num_carac;
    d=document.getElementById('caracteres2');
    d2=document.getElementById('missao_retirado2');    
    d.value=restam;
    if(d2.value.length > 150){
        aux=d2.value;
        d2.value=aux.substr(0, 150);
        d.value=0;        
    }
}

function ContaCaracteres3(){
    num_carac=document.getElementById('visao_retirado').value.length;
    restam=150-num_carac;
    d=document.getElementById('caracteres3');
    d2=document.getElementById('visao_retirado');    
    d.value=restam;
    if(d2.value.length > 150){
        aux=d2.value;
        d2.value=aux.substr(0, 150);
        d.value=0;        
    }
}


function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
	 
	function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
	 
	function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
	ele.className=ele.className.replace(reg,' ');
	}
}


function mostrarGere(obj){	
    
    if(obj.value=="1"){
        document.getElementById("gere1").style.display="block";
        document.getElementById("gere2").style.display="block";
       // removeClass(document.getElementById("gere2"),"invisi");
       // addClass(document.getElementById("gere2"),"visi");
        document.getElementById("gere3").style.display="none";
        document.getElementById("mes").style.display="block";
        document.getElementById("ano").style.display="block";
        document.getElementById("btn").style.display="block";
        document.getElementById("regi1").style.display="none";
    }else if(obj.value=="2"){
        document.getElementById("gere1").style.display="none";
        document.getElementById("gere2").style.display="none";
      //  removeClass(document.getElementById("gere2"),"visi");
      //  addClass(document.getElementById("gere2"),"invisi");
        document.getElementById("gere3").style.display="block";
        document.getElementById("mes").style.display="none";
        document.getElementById("ano").style.display="none";
        document.getElementById("btn").style.display="block"; 
        document.getElementById("regi1").style.display="none";
    }else if(obj.value=="3"){
    	document.getElementById("gere1").style.display="none";
        document.getElementById("gere2").style.display="block";
    //  removeClass(document.getElementById("gere2"),"invisi");
    //  addClass(document.getElementById("gere2"),"visi");        
        document.getElementById("gere3").style.display="none";        
        document.getElementById("regi1").style.display="block";
        document.getElementById("mes").style.display="block";
        document.getElementById("ano").style.display="block";
        document.getElementById("btn").style.display="block";
    
    }else{
        document.getElementById("gere1").style.display="none";
        document.getElementById("gere2").style.display="none";
       // removeClass(document.getElementById("gere2"),"visi");
       // addClass(document.getElementById("gere2"),"invisi");
        document.getElementById("gere3").style.display="none";
        document.getElementById("mes").style.display="none";
        document.getElementById("ano").style.display="none";
        document.getElementById("btn").style.display="none";
        document.getElementById("regi1").style.display="none";
    }
}



/*

// Exemplo de invocação.

necessitaResposta = true; // ou false para receber a resposta.
tipoDadosResposta = "texto"; // ou "xml".

Parâmetros opcionais em caso de modoResposta = "alert".

    limparInput = modoResposta[1];
    focarInput = modoResposta[2];
    avisoResposta = modoResposta[3];
    idDestinoAvisoResposta = modoResposta[4];

modoResposta = ["innerHTML"];   // ou ["alert" [,

     true ou false para limpar o input [,
     true ou false para focar o input [,
     true ou false para dar aviso de resposta, "idDestinoAvisoResposta" id do objeto html ] ] ] ]

idDestinoResposta = "idDestinoResposta"; // id do objeto html.

exibirCarregando = false; // ou true para exibir o carregando.
modoRespostaCarregando = ["innerHTML"]; // ou ["alert"].
idDestinoCarregando = "idDestinoCarregando"; // id do objeto html.
conteudoCarregando = "Conteúdo Carregando..."; // Conteúdo com ou sem HTML.

tratarErro = false; // ou true para tratar erro.
modoRespostaErro = ["innerHTML"]; // ou ["alert"].
idDestinoMensagemErro = "idDestinoMensagemErro"; // id do objeto html.
conteudoMensagemErro = "Conteúdo da Mensagem de Erro."; // Conteúdo com ou sem HTML.

metodoRequisicao = "GET"; // ou "POST". Utilizar apenas GET.
urlRequisicao = "pagina.php?parametros"; // Para GET.
estadoAssincrono = true; // Utilizar apenas true.
dadosEnviar = null; // Utilizar apenas null.

*/

function conferirMatricula(selectOrig, selectDest, param){

    matricula = selectOrig.value;
    param = param;
    
    //alert(selectDest);

    necessitaResposta = true;
    tipoDadosResposta = "texto";
    modoResposta = ["innerHTML"];
    idDestinoResposta = selectDest;

    exibirCarregando = false;
    modoRespostaCarregando = "";
    idDestinoCarregando = "";
    conteudoCarregando = "";

    tratarErro = false;
    modoRespostaErro = "";
    idDestinoMensagemErro = "";
    conteudoMensagemErro = "";

    metodoRequisicao = "GET";
    urlRequisicao = "./conferir_matricula.php?mat=" + matricula + "&param=" + param;
    estadoAssincrono = true;
    dadosEnviar = null;

    retorneResposta2(
        
        necessitaResposta,
        tipoDadosResposta,
        modoResposta,
        idDestinoResposta,

        exibirCarregando,
        modoRespostaCarregando,
        idDestinoCarregando,
        conteudoCarregando,

        tratarErro,
        modoRespostaErro,
        idDestinoMensagemErro,
        conteudoMensagemErro,

        metodoRequisicao,
        urlRequisicao,
        estadoAssincrono,
        dadosEnviar);

}

function conferirMatricula1(campo_matricula, painel_resposta ) {

    matricula = campo_matricula.value;

    if (matricula != "") {

        //origem = "../sistema/comum/";

        necessitaResposta = true;
        tipoDadosResposta = "texto";
        modoResposta = ["alert",true,true];
        idDestinoResposta = painel_resposta;
        exibirCarregando = false;
        idDestinoCarregando = "";
        conteudoCarregando = "";
        tratarErro = false;
        idDestinoMensagemErro = "";
        metodoRequisicao = "GET";
        urlRequisicao = "./conferir_matricula.php?mat=" + matricula;
        //if (isGecko) {
            estadoAssincrono = true;
        //} else {
          //  estadoAssincrono = false;
        //}
        dadosEnviar = null;

        retorneResposta(necessitaResposta, tipoDadosResposta, modoResposta, idDestinoResposta, exibirCarregando, idDestinoCarregando, conteudoCarregando, tratarErro, idDestinoMensagemErro, metodoRequisicao, urlRequisicao, estadoAssincrono, dadosEnviar);

    }
}

function submitenter(myfield,e){
    var keycode;
    if (window.event){
        keycode = window.event.keyCode;
    }else if (e) {
        keycode = e.which;
    }else{
        return true;
    }

    if (keycode == 13){
        //myfield.form.submit();
        document.getElementById("btn1").onclick();
        return false;
    }else{
        return true;
    }
}


