<!--
// Formata o campo de acordo com a máscara informada.
/* 
A função é simples usada, basicamente, para inserir máscaras em campos que recebem:
- Datas,
- CPF,
- CEP,
- CNPJ,
- Telefone,
- entre outros.

A função recebe o objeto do formulário, normalmente um textbox, e a formatação da máscara como o programador quiser estipular, por exemplo:

- Data: '*-/*-/****', '##/##/####'.
- CEP: '**.***-***', '##.###-###'.
*/
function FormatarCampo(objCampo, strMascara) 
{
	var intDigito = event.keyCode;

	// Expressão regular para validação de caractere dígitado.
	// São aceitos apenas números entre "0-9", são feitos dois testes pois existem "dois teclados numéricos" e seus caracteres ASCII são diferentes.
	var objER = /^(4[8-9]|5[0-7]|9[6-9]|10[0-5])$/;

	if(objER.test(intDigito))
		{
			var intTamanho   = objCampo.value.length;
			var strCaractere = strMascara.charAt(0);
			var strMascara   = strMascara.substring(intTamanho)

			if (strMascara.charAt(0) != strCaractere)
				objCampo.value += strMascara.charAt(0);
		}
}

function formataMoeda(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) {
    	whichCode = (e.keyCode);
    }

    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	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 formatarInteiro(objTextBox, e){
    var key = '';
    var strCheck = '0123456789';
    var whichCode = (window.Event) ? e.which : e.keyCode;    

    if(!whichCode) {
    	whichCode = (e.keyCode);
    }

    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	return true;
		
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    
    if (strCheck.indexOf(key) == -1) 
    	return false; // Chave inválida
	
	return true
}


function showSheetOne(sheet1) {	
	if (document.getElementById(sheet1).style.display == 'none'){
		document.getElementById(sheet1).style.display = 'block';
	} else {
		document.getElementById(sheet1).style.display = 'none';
	}		
}

function formatarHora(evt,campo)
{
    var retorno = false;

    if(evt.keyCode)
    {
        if (((evt.keyCode) > 47) && ((evt.keyCode) < 58))
        {
            NumDig = campo.value;
            TamDig = NumDig.length;
            if (TamDig == 2)
                campo.value = NumDig.substr(0,2) + ":";

            retorno = true;
        //} else if( evt.keyCode == 8 )
        } else if( evt.keyCode == 8 || evt.keyCode == 9 || evt.keyCode == 37 || evt.keyCode == 39 )
        {
            retorno = true;
        }
    }

    if (evt.charCode)
    {
        if (((evt.charCode) > 47) && ((evt.charCode) < 58))
        {
            NumDig = campo.value;
            TamDig = NumDig.length;
            if (TamDig == 2)
                campo.value = NumDig.substr(0,2) + ":";

            retorno = true;
        } else if( evt.keyCode == 47 )
        {
            retorno = true;
        }
    }

    if( retorno == true ) return true;
	else return false;
}

function formatarData(campoData){
	  var data = campoData.value;
	  if (data.length == 2){
		  data = data + '/';
		  campoData.value = data;
		return true;              
	  }
	  if (data.length == 5){
		  data = data + '/';
		  campoData.value = data;
		  return true;
	  }
}

function formatarCEP(e, src, mask) {
        if(window.event) { _TXT = e.keyCode; } 
        else if(e.which) { _TXT = e.which; }
        if(_TXT > 47 && _TXT < 58) { 
  var i = src.value.length; var saida = mask.substring(0,1); var texto = mask.substring(i)
  if (texto.substring(0,1) != saida) { src.value += texto.substring(0,1); } 
     return true; } else { if (_TXT != 8) { return false; } 
  else { return true; }
        }
}

/*
Descrição.: formata um campo do formulário de
acordo com a máscara informada...
Parâmetros: - objForm (o Objeto Form)
- strField (string contendo o nome do textbox)

* - sMask (mascara que define o
* formato que o dado será apresentado,
* usando o algarismo "9" para
* definir números e o símbolo "!" para
* qualquer caracter...
* - evtKeyPress (evento)
* Uso.......: <input type="textbox"
* name="xxx".....
* onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);">
* Observação: As máscaras podem ser representadas como os exemplos abaixo:
* CEP -> 99.999-999
* CPF -> 999.999.999-99
* CNPJ -> 99.999.999/9999-99
* Data -> 99/99/9999
* Tel Resid -> (99) 999-9999
* Tel Cel -> (99) 9999-9999
* Processo -> 99.999999999/999-99
* C/C -> 999999-!
* E por aí vai...
***/

function txtBoxFormat(objForm, strField, 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 = objForm[strField].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( " ", "" );
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++;
}

objForm[strField].value = sCod;
if (nTecla != 8) { // backspace
    if (sMask.charAt(i-1) == "9") { // apenas números...
    return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
else { // qualquer caracter...
    return true;
}
} else {
    return true;
}
}