﻿// JScript File Diversas mascaras de TextBox
function formataMascara(campo, evt, formato) {
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;
    
    var result = "";
    var maskIdx = formato.length - 1;
    var error = false;
    var valor = campo.value;
    var posFinal = false;
    if (campo.setSelectionRange) {
        if (campo.selectionStart == valor.length)
            posFinal = true;
    }

    valor = valor.replace(/[^0123456789Xx]/g, '');
    for (var valIdx = valor.length - 1; valIdx >= 0 && maskIdx >= 0; --maskIdx) {
        var chr = valor.charAt(valIdx);
        var chrMask = formato.charAt(maskIdx);
        switch (chrMask) {
            case '#':
                if (!(/\d/.test(chr)))
                    error = true;
                result = chr + result;
                --valIdx;
                break;
            case '@':
                result = chr + result;
                --valIdx;
                break;
            default:
                result = chrMask + result;
        }
    }

    campo.value = result;
    campo.style.color = error ? 'red' : '';
    if (posFinal) {
        campo.selectionStart = result.length;
        campo.selectionEnd = result.length;
    }
    return result;
}

// Formata o campo valor monetário
function formataValor(campo,evt) {
    //1.000.000,00
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;



    vr = campo.value = filtraNumeros(filtraCampo(campo));
    if(vr.length > 0)
    {
        vr = parseFloat(vr.toString()).toString();
        tam = vr.length;



        if (tam == 1) 
            campo.value = "R$" + "0,0" + vr;
        if (tam == 2)
            campo.value = "R$" + "0," + vr;
        if ((tam > 2) && (tam <= 5)) {
            campo.value = "R$" + vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
        }
        if ((tam >= 6) && (tam <= 8)) {
            campo.value = "R$" + vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
        }
        if ((tam >= 9) && (tam <= 11)) {
            campo.value = "R$" + 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)) {
            campo.value = "R$" + 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 <= 18)) {
            campo.value = "R$" + 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);
        }
    }
}

// Formata data no padrão DD/MM/YYYY
function formataData(campo,evt) {
    xPos = PosicaoCursor(campo);
    //dd/MM/yyyy
    evt = getEvent(evt);
    
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;
    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;


    if (tam >= 2 && tam < 4)
    {
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2);
    }
    if (tam == 4)
    {
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/';
    }
    if (tam > 4)
    {
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4);
    }
    //if (tam >= 5 && tam <= 10)
    //    campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4, 4);
    if(xPos == 2 || xPos == 5)
        xPos = xPos +1;
    MovimentaCursor(campo,xPos);
}



function PosicaoCursor(textarea)
      {
          var range = document.selection.createRange();
                var i = 0;
                                
                for(i=textarea.value.length;i>0;i--)
                {
              if(range.moveStart('character',1) == 0)
                  break;
              }
                          
          return i;
      }
      
      function MovimentaCursor(textarea,pos)
      {
          var oRange = textarea.createTextRange();
          var LENGTH = 1;
                var STARTINDEX = pos;
          
          oRange.moveStart("character", -textarea.value.length);
          oRange.moveEnd("character", -textarea.value.length);
          oRange.moveStart("character", pos);
                //oRange.moveEnd("character", pos);
                oRange.select();
                textarea.focus();
      }



//Formata data e hora no padrão DD/MM/YYYY HH:MM
function formataDataeHora(campo,evt) {
    xPos = PosicaoCursor(campo);
    //dd/MM/yyyy
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;
    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;



    if (tam >= 2 && tam < 4)
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2);
    if (tam == 4)
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/';
    if (tam > 4)
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4);
    if (tam > 8 && tam < 11)
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4, 4) + ' ' + vr.substr(8,2);
    if (tam >= 11)
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4, 4) + ' ' + vr.substr(8,2) + ':' + vr.substr(10);
    
    campo.value = campo.value.substr(0,16);
    if(xPos == 2 || xPos == 5)
        xPos = xPos +1;
    if(xPos == 11 || xPos == 14)
        xPos = xPos +2;
    MovimentaCursor(campo,xPos);
}



// Formata só números
function formataInteiro(campo, evt) {
    //1234567890
    xPos = PosicaoCursor(campo);
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;

    campo.value = filtraNumeros(filtraCampo(campo));
    MovimentaCursor(campo,xPos);
}



// Formata hora no padrao HH:MM
function formataHora(campo, evt) {
    //HH:mm
    xPos = PosicaoCursor(campo);
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;



    vr = campo.value = filtraNumeros(filtraCampo(campo));



    if (tam == 2)
        campo.value = vr.substr(0, 2) + ':';
    if (tam > 2 && tam < 5)
        campo.value = vr.substr(0, 2) + ':' + vr.substr(2);
    if(xPos == 2)
        xPos = xPos + 1;
    MovimentaCursor(campo,xPos);    
}



// limpa todos os caracteres especiais do campo solicitado
function filtraCampo(campo) {
    var s = "";
    var cp = "";
    vr = campo.value;
    tam = vr.length;
    for (i = 0; i < tam; i++) {
        if (vr.substring(i, i + 1) != "/"
            && vr.substring(i, i + 1) != "-"
            && vr.substring(i, i + 1) != "."
            && vr.substring(i, i + 1) != "("
            && vr.substring(i, i + 1) != ")"
            && vr.substring(i, i + 1) != ":" 
            && vr.substring(i, i + 1) != ",") {
            s = s + vr.substring(i, i + 1);
        }
    }
    return s;
    //return campo.value.replace("/", "").replace("-", "").replace(".", "").replace(",", "")
}



// limpa todos caracteres que não são números
function filtraNumeros(campo) {
    var s = "";
    var cp = "";
    vr = campo;
    tam = vr.length;
    for (i = 0; i < tam; i++) {
        if (vr.substring(i, i + 1) == "0" ||
            vr.substring(i, i + 1) == "1" ||
            vr.substring(i, i + 1) == "2" ||
            vr.substring(i, i + 1) == "3" ||
            vr.substring(i, i + 1) == "4" ||
            vr.substring(i, i + 1) == "5" ||
            vr.substring(i, i + 1) == "6" ||
            vr.substring(i, i + 1) == "7" ||
            vr.substring(i, i + 1) == "8" ||
            vr.substring(i, i + 1) == "9") {
            s = s + vr.substring(i, i + 1);
        }
    }
    return s;
    //return campo.value.replace("/", "").replace("-", "").replace(".", "").replace(",", "")
}



// limpa todos caracteres que não são letras
function filtraCaracteres(campo) {
    vr = campo;
    for (i = 0; i < tam; i++) {
        //Caracter
        if(vr.charCodeAt(i) != 32 && vr.charCodeAt(i) != 94 && (vr.charCodeAt(i) < 65 || 
        (vr.charCodeAt(i) > 90 && vr.charCodeAt(i) < 96) || 
            vr.charCodeAt(i) > 122) && vr.charCodeAt(i) < 192)
        {
            vr = vr.replace(vr.substr(i,1),"");
        }
    }
    return vr;
}



// limpa todos caracteres que não são números, menos a vírgula
function filtraNumerosComVirgula(campo) {
    var s = "";
    var cp = "";
    vr = campo;
    tam = vr.length;
    var complemento = 0;//flag paga contar o número de virgulas
    for (i = 0; i < tam; i++) {
        if ((vr.substring(i, i + 1) == "," && complemento == 0 && s != "") ||
            vr.substring(i, i + 1) == "0" ||
            vr.substring(i, i + 1) == "1" ||
            vr.substring(i, i + 1) == "2" ||
            vr.substring(i, i + 1) == "3" ||
            vr.substring(i, i + 1) == "4" ||
            vr.substring(i, i + 1) == "5" ||
            vr.substring(i, i + 1) == "6" ||
            vr.substring(i, i + 1) == "7" ||
            vr.substring(i, i + 1) == "8" ||
            vr.substring(i, i + 1) == "9") {
            if(vr.substring(i, i + 1) == ",")
                complemento = complemento + 1;
            s = s + vr.substring(i, i + 1);
        }
    }
    return s;
}



function formataMesAno(campo, evt) {
    //MM/yyyy
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;



    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;



    if (tam >= 2)
        campo.value = vr.substr(0, 2) + '/' + vr.substr(2);
}



function formataCNPJ(campo,evt) {
    //99.999.999/9999-99
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;



    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;



    if (tam >=2 && tam < 5)
        campo.value = vr.substr(0, 2) + '.' + vr.substr(2);
    else if (tam >= 5 && tam < 8)
        campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5);
    else if (tam >= 8 && tam < 12)
        campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3) + '/' + vr.substr(8);
    else if (tam >= 12)
        campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3) + '/' + vr.substr(8, 4) + '-' + vr.substr(12);
}



function formataCPF(campo, evt) {



    //999.999.999-99
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);   
    if (!teclaValida(tecla))
        return;



    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;
    if (tam >= 3 && tam < 6)
        campo.value = vr.substr(0, 3) + '.' + vr.substr(3);
    else if (tam >= 6 && tam < 9)
        campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6);
    else if (tam >= 9)
        campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '-' + vr.substr(9);
}



function formataDouble(campo, evt) {
    //18,53012
    xPos = PosicaoCursor(campo);
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;
    
    campo.value = filtraNumerosComVirgula(campo.value);
    MovimentaCursor(campo,xPos);
}



function formataTelefone(campo, evt) {
    // 0000-0000
    xPos = PosicaoCursor(campo);
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;
        
    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;


    if (tam < 4)
        campo.value = vr;
    //else if (tam == 4)
        //campo.value = vr + '-';
    else if (tam > 4) 
        campo.value = vr.substr(0, 4) + '-' + vr.substr(4);

    //if (tam = 4)
        //campo.value = vr.substr(0, 4) + '-' + vr.substr(4,4);        
    
    //(
    if(xPos == 1 || xPos == 3 || xPos == 5 || xPos == 9)
        xPos = xPos +1
    MovimentaCursor(campo,xPos);    
}



function formataTexto(campo, evt, sMascara){
     //Nome com Inicial Maiuscula.
    evt = getEvent(evt);
    xPos = PosicaoCursor(campo);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;
    vr = campo.value = filtraCaracteres(filtraCampo(campo));
    tam = vr.length;
    
    if (sMascara == "Aa" || sMascara == "Xx")
 {
  var valor = campo.value.toLowerCase();
  var count = campo.value.split(" ").length - 1;
  var i;
  var pos = 0;
  var valorIni;
  var valorMei;
  var valorFim;
  valor = valor.substring(0,1).toUpperCase() + valor.substring(1,valor.length);
  for (i=0;i<count;i++)
  {
   pos = valor.indexOf(" ",pos+1);
   valorIni = valor.substring(0,valor.indexOf(" ",pos-1))+" ";
   valorMei = valor.substring(valor.indexOf(" ",pos)+1,valor.indexOf(" ",pos)+2).toUpperCase();
   valorFim = valor.substring(valor.indexOf(" ",pos)+2,valor.length);
   valor = valorIni + valorMei + valorFim;
  }
  campo.value = valor;
 }
 if (sMascara == "Aaa" || sMascara == "Xxx")
 {
  var valor = campo.value.toLowerCase();
  var count = campo.value.split(" ").length - 1;
  var i;
  var pos = 0;
  var valorIni;
  var valorMei;
  var valorFim;
  var ligacao = false;
  var chrLigacao = Array("de","da","do","para","e")
  valor = valor.substring(0,1).toUpperCase() + valor.substring(1,valor.length);
  for (i=0;i<count;i++)
  {
   ligacao = false;
   pos = valor.indexOf(" ",pos+1);
   valorIni = valor.substring(0,valor.indexOf(" ",pos-1))+" ";
   for (var a=0;a<chrLigacao.length;a++)
   {
    if (valor.substring(valorIni.length,valor.indexOf(" ",valorIni.length)).toLowerCase() == chrLigacao[a].toLowerCase())
    {
     ligacao = true;
     break;
    }
    else if (ligacao == false && valor.indexOf(" ",valorIni.length) == -1)
    {
     if (valor.substring(valorIni.length,valor.length).toLowerCase() == chrLigacao[a].toLowerCase())
     {
      ligacao = true;
      break;
     }
    }
   }
   if (ligacao == true)
   {
    valorMei = valor.substring(valor.indexOf(" ",pos)+1,valor.indexOf(" ",pos)+2).toLowerCase();
   }
   else
   {
    valorMei = valor.substring(valor.indexOf(" ",pos)+1,valor.indexOf(" ",pos)+2).toUpperCase();
   }
   valorFim = valor.substring(valor.indexOf(" ",pos)+2,valor.length);
   valor = valorIni + valorMei + valorFim;
  }
  
  campo.value = valor;
 }
 MovimentaCursor(campo,xPos);
 return true;
}



function contacampo(campo, tamtxt) {
    document.forms.aapf[tamtxt].value = 1540 - document.forms.aapf[campo].value.length;
}



// Formata o campo CEP
function formataCEP(campo, evt) {
    //312555-650
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;



    vr = campo.value = filtraNumeros(filtraCampo(campo));
    tam = vr.length;



    if (tam < 5)
        campo.value = vr;
    else if (tam == 5)
        campo.value = vr + '-';
    else if (tam > 5) 
        campo.value = vr.substr(0, 5) + '-' + vr.substr(5);
}



function formataCartaoCredito(campo, evt) {
    //0000.0000.0000.0000
    evt = getEvent(evt);
    var tecla = getKeyCode(evt);
    if (!teclaValida(tecla))
        return;



    var vr = campo.value = filtraNumeros(filtraCampo(campo));
    var tammax = 16;
    var tam = vr.length;



    if (tam < tammax && tecla != 8)
    { tam = vr.length + 1; }



    if (tam < 5)
    { campo.value = vr; }
    if ((tam > 4) && (tam < 9))
    { campo.value = vr.substr(0, 4) + '.' + vr.substr(4, tam - 4); }
    if ((tam > 8) && (tam < 13))
    { campo.value = vr.substr(0, 4) + '.' + vr.substr(4, 4) + '.' + vr.substr(8, tam - 4); }
    if (tam > 12)
    { campo.value = vr.substr(0, 4) + '.' + vr.substr(4, 4) + '.' + vr.substr(8, 4) + '.' + vr.substr(12, tam - 4); }
}




//recupera tecla



//evita criar mascara quando as teclas são pressionadas
function teclaValida(tecla) {
    if (tecla == 8 //backspace
        //Esta evitando o post, quando são pressionadas estas teclas.
        //Foi comentado pois, se for utilizado o evento texchange, é necessario o post.
        || tecla == 9 //TAB
        || tecla == 27 //ESC
        || tecla == 16 //Shif TAB 
        || tecla == 45 //insert
        || tecla == 46 //delete
        || tecla == 35 //home
        || tecla == 36 //end
        || tecla == 37 //esquerda
        || tecla == 38 //cima
        || tecla == 39 //direita
        || tecla == 40)//baixo
        return false;
    else
        return true;
}



// recupera o evento do form
function getEvent(evt) {
    if (!evt) evt = window.event; //IE
    return evt;
}
//Recupera o código da tecla que foi pressionado
function getKeyCode(evt) {
    var code;
    if (typeof (evt.keyCode) == 'number')
        code = evt.keyCode;
    else if (typeof (evt.which) == 'number')
        code = evt.which;
    else if (typeof (evt.charCode) == 'number')
        code = evt.charCode;
    else
        return 0;



    return code;
}

//Funcao que dispara para campo telefone quando DDD é preenchido
function fncDDD()
	{
		var txtDDD;
		var txtTelefone;
		txtDDD = document.getElementById('ctl00_Principal_txtDDD');
		txtTelefone = document.getElementById('ctl00_Principal_txtTelefone');
		
		if ((txtDDD.value.length + 1) > 2)
		{
			txtTelefone.focus();	
			return false;
		}

		if ((txtDDD.value.length + 1) = 2)
		{
			txtTelefone.focus();
			return true;
		}
		 
	}
