
var Locale = Class.create();
Locale.prototype = {
	initialize: function (signoDecimal, signoMiles) {
		this.signoDecimal = signoDecimal;
		this.signoMiles = signoMiles;
	}
};
var NumberField = Class.create();
NumberField.prototype = {
	initialize: function (idNumberField, locale) {
		this.numberField = $(idNumberField);
		Event.observe (this.numberField,"focus",this.onfocus.bindAsEventListener(this));
		Event.observe (this.numberField,"blur",this.onblur.bindAsEventListener(this));
		Event.observe (this.numberField,"change",this.onchange.bindAsEventListener(this));
		this.locale = locale;
		
		this.value = 0.0;
	},
	onfocus: function(ev) {
		this.setDisplayValue(this.formatNumber(this.value,2,'',this.locale.signoDecimal,'','','-',''));
		this.numberField.select();
		//$("log").innerHTML += "onfocus<br/>";
		
	},
	onblur: function(ev) {
		this.setDisplayValue(this.formatNumber(this.value,2,this.locale.signoMiles,this.locale.signoDecimal,'','','-',''));
		//$("log").innerHTML += "onblur<br/>";
	},
	onchange: function(ev) {
		try {
		if ((this.getDisplayValue().indexOf(this.locale.signoDecimal) >= 0) &&
			(this.getDisplayValue().indexOf(this.locale.signoMiles) >= 0)) {
			this.setDisplayValue(this.getDisplayValue().replace(this.locale.signoMiles,''));
		}
		this.value = parseFloat(this.getDisplayValue().replace(',','.'));
		this.setDisplayValue(this.formatNumber(this.value,2,this.locale.signoMiles,this.locale.signoDecimal,'','','-',''));
		} catch (ex) {
			aler(ex);
		}
//		this.value = this.getDisplayValue();
		//$("log").innerHTML += "onchange<br/>";
		return true;
	},
	formatNumber: function(num,dec,thou,pnt,curr1,curr2,n1,n2) {
		var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0');y.splice(z, 0, pnt); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;
	},
	setDisplayValue: function (number) {
		this.numberField.value = number;
	},
	getDisplayValue: function () {
		return this.numberField.value;
	},
	getValue: function () {
		return this.value;
	},
	setValue: function (number) {
		this.value = number;
		if (isNaN(this.value)) {
		 this.value =0.0;
		}
		this.setDisplayValue(this.formatNumber(this.value,2,this.locale.signoMiles,this.locale.signoDecimal,'','','-',''));
	}
	
}

