//Função que só permite a digitação de números
function isNumberKey(evt) {
    var e;
    if (evt)
        e = evt;
    else
        e = window.event;

    var charCode = (e.which) ? e.which : e.keyCode

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

//Função que formata a mascara da data
function mascaraData(data) {
    var myData = '';
    myData = myData + data.value;
    if (myData.length == 2) {
        myData = myData + '/';
        data.value = myData;
    }
    if (myData.length == 5) {
        myData = myData + '/';
        data.value = myData;
    }
}

// Valida  data
function validaData(campo) {
    var aux = '';
    aux = aux + campo.value;

    if (aux.length < 10 && aux.length > 0) {
        splitString = aux.split('/');
        zero = new String("0")
        if ((splitString[0].length < 2) || (splitString[1].length < 2)) {
            if (splitString[0].length < 2) {
                splitString[0] = zero.concat(splitString[0]);
            }
            if (splitString[1].length < 2) {
                splitString[1] = zero.concat(splitString[1]);
            }
            campo.value = splitString[0] + '/' + splitString[1] + '/' + splitString[2];
        }
        else {
            alert('Data informada \351 inv\341lida!');
            campo.focus();
        }
    }
    else if (aux.length == 10) {
        reDate = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19[0-9]|20[0-6])?\d{1}$/;

        if (!reDate.test(aux)) {
            alert('Data informada \351 inv\341lida!');
            campo.value = "";
            campo.focus();
        }
    }
}

// Valida tecla
function validaTecla(campo,event)
{
	var BCK=8,ETR=13,TAB=9,key,tecla;CheckTAB=true;
	if(event.which)tecla=event.which;
	else tecla=event.keyCode;
	key=String.fromCharCode(tecla);
	if(tecla==ETR)return false;
	if(tecla==BCK || tecla==TAB)return true;
	return(/[0-9]/.test(key));
}

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;
    var whichCode = (window.addEventListener) ? e.which : 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 ((objTextBox.value.length >= objTextBox.maxLength) && (whichCode != 13) && (whichCode != 8)) whichCode = 1;
    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;
    if(len >= objTextBox.getAttribute('maxlength'))return false;

    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;
}


// Formata o campo valor
function formataValor(campo) {
 campo.value = filtraCampo(campo);
 vr = campo.value;
 tam = vr.length;

 if ( tam <= 2 ){
 campo.value = vr ; }
 if ( (tam > 2) && (tam <= 5) ){
 campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
 if ( (tam >= 6) && (tam <= 8) ){
 campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 if ( (tam >= 9) && (tam <= 11) ){
 campo.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) ){
 campo.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 <= 18) ){
 campo.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 ) ;}

}


// Formata o campo valor
function formataNumerico(campo) {

 campo.value = filtraCampo(campo);
 vr = campo.value;
 tam = vr.length;
}

// 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) != "," ){
 s = s + vr.substring(i,i + 1);}
 }
 campo.value = s;
 return cp = campo.value
}

// Valida um CPF utilizando diretamente um controle CustomValidator
function ValidarCpfArgs(source, args)
{
	args.IsValid = ValidarCpf(args.Value);
}

// Valida um CPF
function ValidarCpf(paramCpf)
{
	var valido = true, nonNumbers = /\D/, cpf = paramCpf;
	//Limpando o campo
	cpf = cpf.replace('.', '').replace('.', '').replace('-', '');
	//Validando...
	if (cpf.length < 11 || nonNumbers.test(cpf) || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
		valido = false;
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++) { a[i] = cpf.charAt(i); if (i < 9) b += (a[i] * --c); }
	if ((x = b % 11) < 2) { a[9] = 0; } else { a[9] = 11-x; }
	b = 0; c = 11;
	for (y=0; y<10; y++) b += (a[y] * c--);
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]))
		valido = false;
	return valido;
}

//validando se a tecla for ENTER, faz chamada para o evento do botão informado
function acaoDisparada(botao,event){
    var key = event.which || event.keyCode;
    //verificando se o botão pressionado for ENTER
    if(key == 13)
    {
      __doPostBack(botao,'');		
    }
  }
  
  
  function validaTeclaEnter(event){
    var key = event.which || event.keyCode;
    //verificando se o botão pressionado for ENTER
    if(key == 13)
    {
      return false;		
    }
    
    return true;
  }
