function NumericUpDown(_name)
{

	var element;
	var name = _name;
	
	var value_before = null;
	var timer = null;
	var timerActive = false;
	
	var onchange;
	var onkeyup;
	
	this.min = -4294967296;
	this.max = 4294967296;
	this.decimals = 0;
	
	this.onUp = function() { return true };
	this.onDown = function() { return true };
	
	
	/** General **/
	
	this.assign = function(_id) {
		
		element = document.getElementById(_id);
		element.style.textAlign = "right";

		getById('nud_arrowContainer_' + _id).innerHTML = "<img id='img_" + name + "' src='" + iconPath + "/smallarrow_updown.png' usemap='#map_" + name + "' style='margin-left: 2px; border: none; position: relative; top: 2px' />"
			+ "<map name='map_" + name + "' style='display:none;'>"
			+ "<area href='#' onclick='return false' onmousedown='" + name + ".upMouseDown()' onmouseup='" + name + ".mouseUp()' onmouseleave='" + name + ".mouseUp()' coords='0,0,10,6' shape='rect' />"
			+ "<area href='#' onclick='return false' onmousedown='" + name + ".downMouseDown()' onmouseup='" + name + ".mouseUp()' onmouseleave='" + name + ".mouseUp()' coords='0,8,10,14' shape='rect' />"
			+ "</map>";
			
		// Set event handlers to check the value when changed
		onkeyup = element.onkeyup;
		element.onkeyup = new Function(name + ".keyUp()");
		onchange = element.onchange;
		element.onchange = new Function(name + ".change()");
		
		// Check current value
		this.checkLimits();
		this.cleanValue();
	
	}
	
	this.mouseUp = function() {
		timerActive = false;
		
		if(timer)
			window.clearTimeout(timer);
	}
	
	this.keyUp = function() {
		
		//this.cleanValue();
		
		if(onkeyup != null)
			onkeyup();
		
	}
	
	this.change = function() {
		
		this.checkLimits();
		this.cleanValue();
		
		if(onchange != null)
			onchange();
		
	}
	
	this.cleanValue = function() 
	{
		// Replace invalid chars and make int
		var value = element.value.replace(NumericUpDown.comma, ".");
		value = value.replace(/^[^0-9-\.]/g, "");
		value = value * 1; // trim leading zeros

		if(this.decimals == 0)
		{
			value = parseInt(value);
		}
		else 
		{
			if (parseFloat(value) == 0)
			{ 
				value = "0." + Array(this.decimals + 1).join("0");
			}
			else 
			{
				var multiplier = Math.pow(10, this.decimals);
				value = parseInt(value * multiplier).toString();
				var beforeComma = value.length - this.decimals;
				if(beforeComma > 0)
				{
					var beforeCommaString = value.substring(0, beforeComma);
					if(beforeCommaString.length == 0)
					{
						beforeCommaString = 0;
					}
					var afterCommaString = value.substring(beforeComma);
					
				}
				else
				{
					var beforeCommaString = '0';
					var afterCommaString = (value / multiplier).toString().substr(2);
				}
				while(afterCommaString.length < this.decimals)
				{
					afterCommaString += "0";
				}
				value = beforeCommaString + "." + afterCommaString;
			}
		}

		value = value.toString();
		
		// Set default value if no value set
		if(value == "NaN" || value == "")
			value = (this.min > 0 ? this.min : 0).toString();
		
		// Change separator
		value = value.replace('.', NumericUpDown.comma);
		
		// Set value
		element.value = value;
	}
	
	this.checkLimits = function() {
		
		this.cleanValue();
		
		if(parseFloat(element.value.replace(NumericUpDown.comma, '.')) > this.max)
			// Set to maximum if too high
			element.value = this.max.toString().replace('.', NumericUpDown.comma);
		else if(parseInt(element.value.replace(NumericUpDown.comma, '.')) < this.min)
			// Set to minimum if too small
			element.value = this.min.toString().replace('.', NumericUpDown.comma);
		else
			// Return true if everything's right
			return true;
		
		// Return false if value was too high or small
		return false;
		
	}
	
	this.setEnabled = function(_val) {
		
		element.disabled = !_val;
		
		if(_val) {
			getById("img_" + name).useMap = "#map_" + name;
		}
		else {
			getById("img_" + name).useMap = "";
		}
		
	}
	
	
	/** Up **/
	
	this.up = function() {
		this.cleanValue();
		var value = parseFloat(element.value.replace(NumericUpDown.comma, '.')) + 1;
		if(value <= this.max && element.disabled == false) {
			element.value = value.toString().replace('.', NumericUpDown.comma);
			this.cleanValue();
			this.onUp();
			if(onchange != null)
				onchange();
			return true;
		}
		else
			return false;
	}
	
	this.upTimer = function() {
		if(!timerActive)
			return;
		if(this.up())
			timer = window.setTimeout(name + ".upTimer()", 50);
	}
	
	this.upMouseDown = function() {
		this.up();
		timerActive = true;
		timer = window.setTimeout(name + ".upTimer()", 500);
	}
	
	
	/** Down **/
	
	this.down = function() {
		this.cleanValue();
		var value = parseFloat(element.value.replace(NumericUpDown.comma, '.')) - 1;
		if(value >= this.min && element.disabled == false) {
			element.value = value.toString().replace('.', NumericUpDown.comma);
			this.cleanValue();
			this.onDown();
			if(onchange != null)
				onchange();
			return true;
		}
		else
			return false;
	}
	
	this.downTimer = function() {
		if(!timerActive)
			return;
		if(this.down())
			timer = window.setTimeout(name + ".downTimer()", 50);
	}
	
	this.downMouseDown = function() {
		this.down();
		timerActive = true;
		timer = window.setTimeout(name + ".downTimer()", 500);
	}
	
}

NumericUpDown.comma = '.';
