function mask(o,f){
    v_obj=o;
    v_fun=f;
    setTimeout("execMask()",1);
}

function execMask(){
    v_obj.value=v_fun(v_obj.value);
}

function phone(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2");
    v=v.replace(/(\d{4})(\d)/,"$1-$2");
    v=v.replace(/(\d{4})(\d)/,"$1-$2");
    return v;
}

function date(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/(\d{2})(\d)/,"$1/$2");
    v=v.replace(/(\d{2})(\d)/,"$1/$2");
    v=v.replace(/^((0[1-9]|[12]\d)\-(0[1-9]|1[0-2])|30\-(0[13-9]|1[0-2])|31\-(0[13578]|1[02]))\-\d{4}$/);
    if(v.length>10) {
        v=v.toString().substring(0, 10);
    }
    return v;
}

function cpf(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/(\d{3})(\d)/,"$1.$2");
    v=v.replace(/(\d{3})(\d)/,"$1.$2");
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
    return v;
}

function cep(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/^(\d{5})(\d)/,"$1-$2");
    return v;
}

function cnpj(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/^(\d{2})(\d)/,"$1.$2");
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3");
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2");
    v=v.replace(/(\d{4})(\d)/,"$1-$2");
    return v;
}

function money(v) {
    v=v.replace(/\D/g,"");
    v=v.replace(/[0-9]{12}/,"");   //limita pra mÃ¡ximo 999.999.999,99
    v=v.replace(/(\d{1})(\d{8})$/,"$1.$2");
    v=v.replace(/(\d{1})(\d{5})$/,"$1.$2");
    v=v.replace(/(\d{1})(\d{1,2})$/,"$1,$2");
    return v;
}

function maskKeyPress (v) {
	var iKeyCode;
	
	if(window.event){
		iKeyCode = v.keyCode;
		if(iKeyCode >= 48 && iKeyCode <= 57)
			return true;
		else 
			return false;
	}
	else if(e.which){
		iKeyCode = v.which;
		if(iKeyCode >= 48 && iKeyCode <= 57)
			return true;
		else 
			return false;
	}
    
}

function formatInput(input, tammax, press, char){
	var tecla;
	if(press == null || press == "undefined"){
		tecla = -1;
	}else {
		tecla = press.keyCode; 
	}
	if(char == null || char == "undefined"){
		char = ".";
	}
	vr = input.value;
	if(char != ""){
		vr = changeSymbol(vr, char, "");
	}
	vr = changeSymbol(vr,"/","");
	vr = changeSymbol(vr,",","");
	vr = changeSymbol(vr,".","");
	
	tam = vr.length;
	if(tecla > 0){
		if(tam < tammax && tecla != 8){
			tam = vr.length + 1;
		}
		if(tecla == 8){
			tam = tam - 1;
		}
	}
	
	if(tecla == -1 || tecla == 8 || tecla >=48 || tecla <= 57 || tecla >= 96 && tecla <= 105){
		if(tam <= 2){
			input.value = vr;
		}
		if((tam > 2) && (tam<=4)){
			input.value = vr.substr(0, tam-2)+"."+vr.substr(tam-2, tam);
		}
	}
}

function changeSymbol(str, strexit, strstart){
	while(str.indexOf(strexit) > -1){
		str = str.replace(strexit, strstart);
	}
	return str;
}
