﻿// JScript File

function MascaraData(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; 
		}
	}
}

function MascaraHora(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 != 4) { 
			return false; 
		} else { 
			return true; 
		}
	}
}

function MascaraNumero(tecla)
{
    if(typeof(tecla) == 'undefined')
    var tecla = window.event;
    var codigo = (tecla.which ? tecla.which : tecla.keyCode ? tecla.keyCode : tecla.charCode);
    // permite números, 8=backspace, 46=del e 9=tab
    if ( (codigo >= 48 && codigo <= 57) || (codigo >= 96 && codigo <= 105) || codigo == 8 || codigo == 46 || codigo == 9 )
    { return true; }
    else { 
    return false; 
    } 
}

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;
    var sinal = objTextBox.value.charAt(0);
    
    if(sinal == '-' || sinal == '+')
        objTextBox.value = objTextBox.value.substr(1, objTextBox.value.length);
    else
        sinal = '';

    if (whichCode == 13)
    {
        objTextBox.value = sinal + objTextBox.value;
        return true;
    }
    key = String.fromCharCode(whichCode); // Valor para o código da Chave

    if(key == '-')
    {
        objTextBox.value = '-' + objTextBox.value.substr(0, objTextBox.value.length);
        return false;
    }else if(key == '+'){
        objTextBox.value = '+' + objTextBox.value.substr(0, objTextBox.value.length);            
        return false;
    }

    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 = sinal + '0' + SeparadorDecimal + '0' + key;
    if (len == 2) objTextBox.value = sinal + '0'+ SeparadorDecimal + aux.substr(len - 2, len - 0);
    
    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);
        
        objTextBox.value = sinal + objTextBox.value.substr(0, objTextBox.value.length);
    }
    
    return false;
}


//Esta função valida valida se o tamanho do texto de um textbox foi
//excedido.
//Retorna "true" caso não tenha excedido o limite.
function ValidarTamanhoTexto(objTextBox, limiteTextBox)
{
	if(objTextBox.value.length > limiteTextBox)
	{
		alert("O limite deste campo (" + limiteTextBox + " caracteres) foi excedido em " + (objTextBox.value.length - limiteTextBox) + " caracter(es).");
		return false;
	}
	else
		return true;
}

//Verifica se uma data está no formato MM/YYYY e se é válida.
function ValidarDataFormatoMMYYYY(objTextBox)
{
    var mensagem = "O Prazo da Parcela Calculada está inválido.";
    var mes, ano;

    if(objTextBox.value.length == 0)
        return true;
        
    if(objTextBox.value.length != 7)
    {
        alert(mensagem);
        objTextBox.focus();
        objTextBox.select();
        return false;
    }
    
    mes = objTextBox.value.substr(0, 2);
    ano = objTextBox.value.substr(3, 4);
    
    if(isNaN(mes) || parseFloat(mes) < 1 || parseFloat(mes) > 12)
    {
        alert(mensagem);
        objTextBox.focus();
        objTextBox.select();
        return false;
    }
    
    if(isNaN(ano) || parseFloat(ano) < 1900 || parseFloat(ano) > 2199)
    {
        alert(mensagem);
        objTextBox.focus();
        objTextBox.select();
        return false;
    }
    
    return true;
}

function LimpaCnpj(stringCnpj)
{
	var strRetorno = '';
	for( iCont = 0; iCont < stringCnpj.length; iCont++)
	{
		if( stringCnpj.charAt( iCont ) != '.' && stringCnpj.charAt( iCont ) != '-' && stringCnpj.charAt( iCont ) != '/' )
		{
			strRetorno += stringCnpj.charAt( iCont );
		}
	}
	
	return strRetorno;
}

function MascaraCNPJ(objText, event)
{
	var valor = '';
	valor = LimpaCnpj(objText.value);
	var tecla = (window.event) ? event.keyCode : e.which;
	if(valor.length > 0  && ( ( tecla >= 48 && tecla <= 57 ) || ( tecla >= 96 && tecla <= 105)))
	{
		var mask = '';
		
		if( valor.length > 0)
		{
			mask = valor;
			if( valor.length >= 2)
				mask = valor.substring(0,2) + '.' + valor.substring(2,5);
			if( valor.length >= 5)
				mask = valor.substring(0,2) + '.' + valor.substring(2,5) + '.' + valor.substring(5,8);
			if( valor.length >= 8)
				mask = valor.substring(0,2) + '.' + valor.substring(2,5) + '.' + valor.substring(5,8) + '/' + valor.substring(8,12);
			if( valor.length >= 12)
				mask = valor.substring(0,2) + '.' + valor.substring(2,5) + '.' + valor.substring(5,8) + '/' + valor.substring(8,12) + '-' + valor.substring(12,15);
		}
		objText.value = mask;
	}
}

function MaxLengthTextArea(textAreaField, limit) {	
	if (textAreaField.value.length >= limit) {
		textAreaField.value = textAreaField.value.substring(0, limit-1);
	}
}




//Deixa digitar apenas números nos campos
function Numericos(evento, control)
{               
    var tecla = event.keyCode;
            
    // verifica se já existe a vírgula
    if(control.value.indexOf(',') > -1 && tecla == 44) event.keyCode = 0;
                                                
    if ((tecla > 47 && tecla < 58) || tecla == 44)
        { 
            return true;
        }
        else
        {
        if (tecla != 8)
        {
            // backspace
            event.keyCode = 0;
        }
        else
        {
            return true;
        }
    }
}

//Permiti digitar apenas números e virgula nos campos
function Numeros(evento)
{
  var tecla = event.keyCode;       
    
  if (tecla > 47 && tecla < 58)
  { 
    // numeros de 0 a 9
	return true;
  }
  else
  {
    if (tecla != 8)
    {
	   // backspace
	   event.keyCode = 0;
    }
    else
	{
	    return true;
    }
  }
}