function Round(number, decimalPlaces)
{
	return Math.round(number * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
}

function FormatNumber(input, decimalPlaces)
{
	decimalPlaces = Coalesce(decimalPlaces, 0);
	var value = input.value;
	
	if (value && value.charAt(value.length - 1) != ".")
	{
		var number = parseFloat(value);
		
		if (isNaN(number))
		{
			input.value = "0";
		}
		else
		{
			input.value = Round(number, decimalPlaces);
		}
	}
}

var myrules = {
	".integer" : function(input)
	{
		input.onkeyup = function()
		{
			FormatNumber(this);
		};
		input.onchange = function()
		{
			FormatNumber(this);
		};
		input.onfocus = function()
		{
			if (this.value == "0")
			{
				this.select();
			}
		};
	},
	".decimal" : function(input)
	{
		input.onkeyup = function()
		{
			FormatNumber(this, Coalesce(this.places, 2));
		};
		input.onchange = function()
		{
			FormatNumber(this, Coalesce(this.places, 2));
		};
		input.onfocus = function()
		{
			if (this.value == "0")
			{
				this.select();
			}
		};
	}
};

Behaviour.register(myrules);
