// JavaScript Document

//validar campos telefones
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function telefone1(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

/* modo de usar onkeyup="Data(this);" */
function Data(campo, e){
	
 	myVal = campo.value;
	if (myVal.length > 2 && !myVal.match(/\//)){
		myVal = '';
	} else {
		if (window.event){
			keycode = window.event.keyCode;
		} else if (e){
			keycode = e.which;
		}
					
		if (keycode < 48 || keycode > 57 && keycode < 96 || keycode > 105){
			myVal = myVal.substr(0, (myVal.length - 1));
		}
						
		if (myVal.length == 2){
			myVal += '/';
		}
						
		if (myVal.length == 5){
			myVal += '/';
		}
	}
	campo.value = myVal;
}


//-----> máscara cpf: 

function m_CPF(campo,tammax) {
    
    
    
    var vr = campo.value;
    
    vr = vr.replace( "-", "" );
    
    vr = vr.replace( ".", "" );
    
    vr = vr.replace( ".", "" );
    
    var tam = vr.length;
    
    
    
    if (tam < tammax) { tam = vr.length + 1; }
    
    
    
    tam = tam - 1;
    
    if ( (tam > 2) && (tam <= 11) ) {
        
        vr = vr.substr( 0, tam - 1 ) + '-' + vr.substr( tam - 1, tam ); }
    
    if ( (tam == 10) ) {
        
        vr = vr.substr( 0, tam - 7 ) + '.' + vr.substr( tam - 7, 3 ) + '.' + vr.substr( tam - 4, tam ); }
    
    
    
    campo.value = vr;
    
}


//####### formatar CEP ############
function MM_formtCep(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; }
}
}

//formatar CEP
function MM_formtCep(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; }
}
}


//validar campos telefones
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function telefone1(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}


//Formata número tipo moeda usando o evento onKeyDown 
function Formata(campo,tammax,teclapres,decimal) { 
    var tecla = teclapres.keyCode; 
    vr = Limpar(campo.value,"0123456789"); 
    tam = vr.length; 
    dec=decimal 
    
    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 <= dec ) 
                { campo.value = vr ; } 
            
            if ( (tam > dec) && (tam <= 5) ){ 
                campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; } 
            if ( (tam >= 6) && (tam <= 8) ){ 
                campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, 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 - dec, tam ) ; } 
            if ( (tam >= 15) && (tam <= 17) ){ 
                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 ) ;} 
        } 
        
    }

