﻿// JScript File
//################################
//# VERIFICA SE CAMPO É NUMERICO #
//################################
//Ex: onKeyPress="return(FormataNumero(this,event))"
function FormataNumero(campo,e) {
	var strDigitos = "1234567890"; //Valores aceitos
	if (FormataDigitos(campo,e,strDigitos) != "OK")
		return FormataDigitos(campo,e,strDigitos);
}

function Count(text,long) 
{
	var maxlength = new Number(long); // Change number to your max length.
	if (text.value.length > maxlength){
		text.value = text.value.substring(0,maxlength);
	}
}

function Tecla(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
			{
				if (tecla != 8) // backspace
					event.keyCode = 0;
					//return false;
				else
					return true;
			}
}     


// JScript File

//-----------------------------------------------------
//Funcao: MascaraMoeda
//Sinopse: Mascara de preenchimento de moeda
//Parametro:
//   objTextBox : Objeto (TextBox)
//   SeparadorMilesimo : Caracter separador de milésimos
//   SeparadorDecimal : Caracter separador de decimais
//   e : Evento
//Retorno: Booleano
//Autor: Gabriel Fróes
//Data Criação: 15/02/2005
//-----------------------------------------------------
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    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) 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 focusNext_(campo,proximocampo,e) {
    //var sCampo;
    if (e.keyCode != 37 & e.keyCode != 38 & e.keyCode != 39 & e.keyCode != 40) {
	    if (e.keyCode == 13) {
		   proximocampo.focus();
		}   
		if (campo.value.length == (campo.maxLength)) 
		   proximocampo.focus();
	}	   
	//return false;
    }  
    
//################
//# FORMATA CNPJ #
//################
function FormataCNPJ(campo,e) {
	var strDigitos = "1234567890"; //Valores aceitos
	if (FormataDigitos(campo,e,strDigitos) != "OK")
		return FormataDigitos(campo,e,strDigitos);
	var vr = new String(campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1 ;
	if (e.keyCode == 13) {
	   return true;
	}   
	else if (e.keyCode != 9 && e.keyCode != 8) {
		if (tam >= 3 && tam < 6)
			campo.value = vr.substr(0,2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
	}
}

//###################
//# FORMATA DIGITOS #
//###################
function FormataDigitos(campo,e,digitos) {
	//Verificação dos digitos:
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13 || whichCode == 8 || whichCode == 0 || whichCode==9)
		return true;
	key = String.fromCharCode(whichCode);
	if (digitos.indexOf(key) == -1)
		return false;  // Chave inválida
	return "OK";
}

//###################################
//# Coloca mascara nos campos datas #
//###################################
function FormataData(campo,e){
   var strDigitos = "1234567890"; //Valores aceitos
   if (FormataDigitos(campo,e,strDigitos) != "OK")
	 return FormataDigitos(campo,e,strDigitos);
   var vr = new String(campo.value); //Guarda o valor do campo
   vr = vr.replace("/","");
   vr = vr.replace("/","");
   tam = vr.length + 1;
   if (e.keyCode != 9 && e.keyCode != 8){
     if (tam > 2 && tam < 5) {
	   campo.value = "";
	   campo.value = vr.substr(0,2) + '/' + vr.substr(2, tam);
	 }
	 if (tam >= 5 && tam < 8) {
	   campo.value = "";
	   campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,tam-4);
	 }
   }
}

// Abre o popup de cep
// Ricardo : 25.11.2007
// Returns Nothing
function ShowCepPopup(){
	window.open('http://www.correios.com.br/servicos/cep/cep_default.cfm','ShowCep','location=no,resize=no, width=730,height=400,left=18,top=18,maximized=0,scrollbars=1');
}

function FormataCpf(campo,tammax,teclapres) {
var tecla = teclapres.keyCode;

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 txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;


if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla > 47) && (nTecla < 58)); } 
      else { // qualquer caracter...
        return true;
      } 
    }
    else {
      return true;
    }
  }

