 function keyDown(e) {
 	key_code = e.which;
 }
 
 function mascara(Campo, Mascara, evtKeyPress)
 {
	var i, j, Valor, TamanhoCampo, TamanhoMascara, MascaraOk, Codigo, Tecla;
	Tecla = KeyCode(evtKeyPress);
	
	if (Tecla==8) return true;
	
 	Valor = Campo.value; 
 	Valor = Valor.toString().replace( ':', '' );
 	Valor = Valor.toString().replace( '-', '' ); 
 	Valor = Valor.toString().replace( '-', '' ); 
 	Valor = Valor.toString().replace( '.', '' ); 
 	Valor = Valor.toString().replace( '.', '' ); 
 	Valor = Valor.toString().replace( '/', '' ); 
 	Valor = Valor.toString().replace( '/', '' ); 
 	Valor = Valor.toString().replace( '(', '' ); 
 	Valor = Valor.toString().replace( '(', '' ); 
 	Valor = Valor.toString().replace( ')', '' ); 
 	Valor = Valor.toString().replace( ')', '' ); 
 	Valor = Valor.toString().replace( ' ', '' ); 
 	Valor = Valor.toString().replace( ' ', '' ); 
 	TamanhoCampo = Valor.length; 
 	TamanhoMascara = Mascara.length; 
 	
 	i = 0; j = 0; Codigo = ''; 
 	TamanhoMascara = TamanhoCampo; 
 	
 	while ( i <= TamanhoMascara ){
 	
 		MascaraOk = ((Mascara.charAt(i) == '-') || 
 		             (Mascara.charAt(i) == '.') || 
 		             (Mascara.charAt(i) == '/') || 
 		             (Mascara.charAt(i) == '(') || 
 		             (Mascara.charAt(i) == ')') || 
 		             (Mascara.charAt(i) == ':') ||
 		             (Mascara.charAt(i) == ' ')
 		            );
 		            
 		if (MascaraOk){
 			Codigo += Mascara.charAt(i);          
 			TamanhoMascara++;
 		} 
 		else {
 			Codigo += Valor.charAt(j);
 			j++;
 		}   
 		i++;
 	}
 	
 	Campo.value = Codigo;

 	if (Tecla != 8){
 		if (Mascara.charAt(i-1) == "9" && Mascara.length > Valor.length){
 			return ((Tecla > 47) && (Tecla < 58));
 		}else if (Mascara.charAt(i-1) == "X" && Mascara.length > Valor.length){
 			return ((Tecla >= 65) && (Tecla <= 90));
 		}else if (Mascara.charAt(i-1) == "x" && Mascara.length > Valor.length){
 			return ((Tecla >= 97) && (Tecla <= 122));
 		}else{
 			return true;
 		}
 	}else{
		return true;
 	}
 }

 function KeyCode(e)
 {
 	if(document.all){
	    return e.keyCode;
	}
    else if(navigator.appName == 'Netscape'){
    	return e.which;
    }
 }
 
 function numerico(event){
 	Tecla = KeyCode(event);
 	if ((Tecla > 47) && (Tecla < 58)) 
 		return true;
 	else
 	 	return false;
 }

function masc(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
} 
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}
function soNumeros(v){
    return v.replace(/\D/g,"")
}

function mascaraCEP(Campo, t)
{
	var Len = Campo.value.length;
 	var Tecla = KeyCode(t);
 	if (Tecla == 8)
	{
 		return true;
	}
 	else
	{
	 	switch (Len)
		{
	 		case 5:
	 			Campo.value = Campo.value + "-";
	 		break;
	 	}
	}
 }
 
function mascaraData(Campo, e)
{
 	var Len = Campo.value.length;
 	var Tecla = KeyCode(e);
 	if (Tecla == 8) 
 		return true;
 	else
	 	if(Len == 2 || Len == 5) 
	 		Campo.value = Campo.value + "/";  
}

function mascaraCPF(Campo, e){
	Len = Campo.value.length;
 	Tecla = KeyCode(e);
 	if (Tecla == 8)
 		return true;
 	else
	{
	 	switch (Len)
		{
	 		case 3:
	 			Campo.value = Campo.value + ".";
	 		break;
	 		case 7:
	 			Campo.value = Campo.value + ".";
	 		break;		
	 		case 11:
	 			Campo.value = Campo.value + "-";
	 		break
	 	}
	}
 }
 
function formataValor(form, campo,tammax,teclapres) {
	
	var tecla = teclapres.keyCode;
    var strCheck = '0123456789';
	
	vr = form[campo].value;
	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 (strCheck.indexOf(tecla) == -1) return false; // Chave inválida
	
	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		form[campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		form[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) ){
	 		form[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 <= 17) ){
	 		form[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 ) ;}
	}

}

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 validaMatricula(id)
{
	var valor = document.getElementById(id).value;
	if( /^[a-zA-Z]\d{6}$/.test(valor) )
	{
	
	}
	else
	{
		alert('Matrícula inválida. A matrícula deve ser uma letra seguida de seis números');
	}
}

