// -------------------------------------------------------------------------
// Formata numero

function fNumero(campo) {
	if(!document.all) return true;
	
	var teclas = new String("0123456789.");
	
	if(window.event) {
		if(teclas.search(String.fromCharCode(event.keyCode)) == -1) {
			event.returnValue = false;
		}
	}
}

// Formata numero inteiro

function fNumeroInt(campo, e) {
	try {
		if(window.event) var tecla = e.keyCode;
		else tecla = e.which;
		
		if((tecla > 47 && tecla < 58) || (tecla == 0) || (tecla == 8) || (tecla == 13)) return true;
		else return false;
	}
	catch(err) { return true; }
}

// -------------------------------------------------------------------------
// Formata numero ponto flutuante

function fNumeroFloat(campo, e) {
	try {
		if(window.event) var tecla = e.keyCode;
		else tecla = e.which;
		
		if((tecla > 47 && tecla < 58) || (tecla == 0) || (tecla == 8) || (tecla == 13) || (tecla == 46)) return true;
		else return false;
	}
	catch(err) { return true; }
}

// -------------------------------------------------------------------------
// Formata CPF

function fCpf(campo, e) {
	var vnum = fNumeroInt(campo, e);
	if(vnum == false) return false;
	
	if(window.event) var tecla = e.keyCode;
	else tecla = e.which;
	
	if(tecla != 8 && tecla != 13) {
		var str = campo.value;
		
		var str = new String(campo.value);
		str = str.replace(/(\||\_|\.|\(|\)|\/|\-| )+/g, '');
	
		var tam = str.length + 1;
		
		if(tam > 3 && tam < 7) campo.value = str.substr(0,3) + '.' + str.substr(3, tam);
    	if(tam >= 7 && tam < 10) campo.value = str.substr(0,3) + '.' + str.substr(3,3) + '.' + str.substr(6, tam-6);
    	if(tam >= 10 && tam < 12) campo.value = str.substr(0,3) + '.' + str.substr(3,3) + '.' + str.substr(6,3) + '-' + str.substr(9,tam-9);
	}
	
	return true;
}

// -------------------------------------------------------------------------
// Formata CNPJ.

function fCnpj(campo) {
	if(!document.all) return true;
	
	var v_atual = campo.value;
	var teclas = new String("0123456789");
	
	if(window.event) {
		if(teclas.search(String.fromCharCode(event.keyCode)) == -1) {
			event.returnValue = false;
		}
	}
	
	v_atual = v_atual.replace(".", "");
	v_atual = v_atual.replace(".", "");
	v_atual = v_atual.replace("/", "");
	v_atual = v_atual.replace("-", "");
   
	if(v_atual.length >= 2) {
		campo.value = v_atual.substr(0,2)+"."+v_atual.substr(2);
	}
	
	if(v_atual.length >= 5) {
		campo.value = v_atual.substr(0,2)+"."+v_atual.substr(2,3)+"."+v_atual.substr(5);
	}
	
	if(v_atual.length >= 8) {
		campo.value = v_atual.substr(0,2)+"."+v_atual.substr(2,3)+"."+v_atual.substr(5,3)+"/"+v_atual.substr(8);
	}
	
	if(v_atual.length >= 12) {
		campo.value = v_atual.substr(0,2)+"."+v_atual.substr(2,3)+"."+v_atual.substr(5,3)+"/"+v_atual.substr(8,4)+"-"+v_atual.substr(12,2);
	}
	
	if(v_atual.length >= 14) {
		if(window.event) event.returnValue = false;
	}
}

// -------------------------------------------------------------------------
// Formata data

function fData(campo) {
	if(!document.all) return true;
	
	var v_atual = campo.value;
	var teclas = new String("0123456789");
	
	if(window.event) {
		if(teclas.search(String.fromCharCode(event.keyCode)) == -1) {
			event.returnValue = false;
		}
	}
	
	v_atual = v_atual.replace(".", "");
	v_atual = v_atual.replace(",", "");
	v_atual = v_atual.replace("/", "");
	v_atual = v_atual.replace("-", "");
   
	if(v_atual.length >= 2) {
		campo.value = v_atual.substr(0,2)+"/"+v_atual.substr(2);
	}
	
	if(v_atual.length >= 4) {
		campo.value = v_atual.substr(0,2)+"/"+v_atual.substr(2,2)+"/"+v_atual.substr(5);
	}
	
	if(v_atual.length >= 9) {
		if(window.event) event.returnValue = false;
	}
}

// -------------------------------------------------------------------------
// Formata cep

function fCep(campo) {
	if(!document.all) return true;
	
	var v_atual = campo.value;
	var teclas = new String("0123456789-");
	
	if(window.event) {
		if(teclas.search(String.fromCharCode(event.keyCode)) == -1) {
			event.returnValue = false;
		}
	}
	
	v_atual = v_atual.replace(".", "");
	v_atual = v_atual.replace(",", "");
	v_atual = v_atual.replace("/", "");
	v_atual = v_atual.replace("-", "");
   
	if(v_atual.length >= 5) {
		campo.value = v_atual.substr(0,5)+"-"+v_atual.substr(5);
	}
	
	if(v_atual.length >= 9) {
		if(window.event) event.returnValue = false;
	}
}
