//Objetivo:	Esta função tem a finalidade de ir colocando o valor digitado no campo em formato monetário.
//Parâmetros:	Recebe como parâmetro o nome completo do campo onde está sendo digitado o valor,
//		por exemplo ''''''''''''''''document.frmInclusao.valor.value''''''''''''''''.
/*
function Mascara_Valor(campo, event)
{
	Tecla = KeyCode(event);
	
	if (Tecla != 8){
		if(Tecla == 46){
			if(campo.value.indexOf(".")==-1)
				return true;
			else
				return false;
		}
// 		if((Tecla > 47) && (Tecla < 58) && campo.value.charAt(campo.value.length-3)!='.'){
 		if((Tecla > 47) && (Tecla < 58)){
			return true;
		}else{
			return false;
		}
 	}else{
		return true;
 	}
}
*/
 function keyDown(e) {
 	key_code = e.which;
 }
 
 function mascara(Formulario, Campo, Mascara, evtKeyPress){
	var i, j, Valor, TamanhoCampo, TamanhoMascara, MascaraOk, Codigo, Tecla;
	 
	Tecla = KeyCode(evtKeyPress);
	
	if (Tecla==8) return true;
	
 	Valor = Formulario[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( ' ', '' ); 
 	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++;
 	}
 	
 	Formulario[Campo].value = Codigo;

 	if (Tecla != 8){
 		if ((Mascara.charAt(i-1) == "9" || Mascara.charAt(i-1) == "") && Mascara.length > Valor.length){
			return ((Tecla > 47) && (Tecla < 58));
 		}
 		if (Mascara.charAt(i-1) == "A" && Mascara.length > Valor.length){
 			return ((Tecla > 64) && (Tecla < 91) || (Tecla > 96) && (Tecla < 123));
 		}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 formataValor(form, campo,tammax,teclapres) {
	
	var tecla = teclapres.keyCode;
	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 (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 FormataCNPJ(Campo, teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1 ;
	if(tecla != 9 && tecla !=8)
	{
		if(tam > 2 && tam < 6)
		Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if(tam >= 6 && tam < 9)
		Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if(tam >= 9 && tam < 13)
		Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if(tam >= 13 && tam < 15)
		Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
	}
}
