// mascara de números
// uso: onkeypress="return(mascaraNumeros(this,event));"
function mascaraNumeros(campo,e) {
	var teclaValida = true;
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
} 


// mascara de CEP "01234-100"
// uso: onkeypress="return (mascaraCEP(this,event));" maxlength="9"
function mascaraCEP(campo,e) {

	var teclaValida = true;
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}
	
	if (campo.value.length == 5 && whichCode != '8' && whichCode != '0') 
		campo.value = campo.value + '-';

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
} 


// mascara de data e hora "01/2000"
// uso: onkeypress="return(mascaraMesAno(this,event));"
function mascaraMesAno(vdateValue,e) {
	var strSeparator='/';
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	var mesCheck ='01,02,03,04,05,06,07,08,09,10,11,12';
	if (whichCode == 13) return true;
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		//alert('Voce pressionou a tecla invalida');
		//vdateName.value=vdateName.value.substr(0,(vdateValue.length-1));
		return false;
	}
	
	if (vdateValue.value.length == 2) {
	 	vdateValue.value=vdateValue.value+strSeparator;
	}    
	
	return true;
}

// uso: onkeypress="return mascaraRG(this,event);" maxlength="12"
function mascaraRG(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
		
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 6) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + '-';

	return true;
}



// uso: onkeypress="return mascaraCPF(this,event);" maxlength="14"
function mascaraCPF(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	if (vdateValue.value.length == 3) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 7) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 11) vdateValue.value=vdateValue.value + '-';

	return true;
}


 
// uso: onkeypress="return mascaraCNPJ(this,event);" maxlength="18"
function mascaraCNPJ(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	//e.preventDefault();
	//var tecla = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 6) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 15) vdateValue.value=vdateValue.value + '-';;
		
	
	return true;
}

// uso: onkeypress="return mascaraIE(this,event);" maxlength="15"
function mascaraIE(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	//e.preventDefault();
	//var tecla = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 3) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 7) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 11) vdateValue.value=vdateValue.value + '.'
	
	
	return true;
}


// uso: onkeypress="return mascaraData(this,event);" maxlength="10"
function mascaraData(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';

	
	return true;
}

// uso: onkeypress="return mascaraHora(this,event);" maxlength="5"
function mascaraHora(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + ':';
	
	return true;
}

// uso: onkeypress="return mascaraDataHora(this,event);" maxlength="16"
function mascaraDataHora(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + ' ';
	if (vdateValue.value.length == 13) vdateValue.value=vdateValue.value + ':';
	
	
	return true;
}

// uso: onkeypress="return mascaraNumeros(this,event);"
function mascaraNumeros(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	return true;
}


// Mascara para campos de valores fincanceiros ex: "1.200,00"
// uso: onKeyPress="return mascaraDinheiro(this,'.',',',event);"
function mascaraDinheiro(fld, milSep, decSep, 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;

	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 13 -> Enter
	*  -> Barra de Espaço
	*/

    if (whichCode == 13) return true; // Enter
    if (whichCode == 0) return true;  // Del
    if (whichCode == 8) return true;  // BackSpace
    if (whichCode == 32) return true;  // Barra de Espaço

    key = String.fromCharCode(whichCode);  // Get key value from key code
 
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
    len = fld.value.length;
    for(i = 0; i < len; i++)
    if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
    if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
    if (j == 3) {
    aux2 += milSep;
    j = 0;
    }
    aux2 += aux.charAt(i);
    j++;
    }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}
