// Find all the <input> tags we need to register an event handler on
function initFilter(tag) {
	if (tag.type != "text")
		return; // We only want text fields

	if (tag.addEventListener)
		tag.addEventListener("keypress", filterNumber, false);
	else {
		// We don't use attachEvent because it does not invoke the
		// handler function with the correct value of the this keyword.
		tag.onkeypress = filterNumber;
	}
}

// This is the keypress handler that filters the user's input
function filterNumber(event) {
	// Get the event object and character code in a portable way
	var e = event || window.event; // Key event object
	var code = e.charCode || e.keyCode; // What key was pressed

	// If this keystroke is a function key of any kind, do not filter it
	if (e.charCode == 0)
		return true; // Function key (Firefox only)
	if (e.ctrlKey || e.altKey)
		return true; // Ctrl or Alt held down
	if (code < 32)
		return true; // ASCII control character

	// Now look up information we need from this input element
	var allowed = "0123456789"; // Legal chars

	// Convert the character code to a character
	var c = String.fromCharCode(code);

	// See if the character is in the set of allowed characters
	if (allowed.indexOf(c) != -1) {
		return true; // And accept the character
	} else {
		// And reject this keypress event
		if (e.preventDefault)
			e.preventDefault();
		if (e.returnValue)
			e.returnValue = false;
		return false;
	}
}
