var decimais = 0;
var sinal = false;

function keypressNumero(obj,e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	if (key ==0 || key ==8)return true;

	if (keychar==',' && obj.value.match(/,/g))return false;
	if (keychar=='-' && obj.value.match(/\-/g))return false;

	var reg = /[\d]/;

	if (sinal){
		if (decimais>0) reg = /[\-\d,]/
		else reg = /[\-\d]/
	}else{
		if (decimais>0) reg = /[\d,]/
		else reg = /[\d]/
	}

	return reg.test(keychar);
}

function blurNumero(obj){
	if (obj.value.indexOf('-')>=0)
		obj.value = "-"+obj.value.replace(/[^\d,]/g,'');
	else
		obj.value = obj.value.replace(/[^\d,\-]/g,'');
	if(isNaN(obj.value.replace(',','.'))){
		alert("O valor informado não um número válido.");
		obj.value = "";
		obj.focus();
		return;
	}

	if(obj.value.length==0)
		return;

	var indVirgula = obj.value.indexOf(',');
	if (indVirgula >=0){
		if(decimais<=0)
			obj.value = obj.value.substring(0,obj.value.length-indVirgula);
		else{
			obj.value = obj.value.substring(0,indVirgula+decimais+1);
		}
		var len = obj.value.length;
		for(i=len; i<=indVirgula+decimais;i++)
			obj.value = obj.value+"0";
	}else{
		if(decimais>0){
			var len = obj.value.length;
			obj.value = obj.value+",";
			for(i=0; i < decimais;i++)
				obj.value = obj.value + "0";
		}
	}
	if (indVirgula == 0)
		obj.value = "0"+obj.value;
}

function onfocusInNumero(element,e){
	if (!e) var e = window.event;
}

function onbeforepasteInNumero(element,e){
	if (!e) var e = window.event;
	e.returnValue = false;
}

function ondragenterInNumero(element,e){
	if (!e) var e = window.event;
	e.returnValue = false;
}

function onblurInNumero(element,e){
	if (!e) var e = window.event;
	blurNumero(element,e);
}

function onkeypressInNumero(element,e){
	if (!e) var e = window.event;
	return keypressNumero(element,e);
}

function onkeyupInNumero(element,e){
	if (!e) var e = window.event;
	proximoCampo(element, e);
}

function onpasteInNumero(element,e){
	if (!e) var e = window.event;
	e.returnValue = false;
}


