// JavaScript Document

var NumFormatter=new Class({
	Implements:[Options],
	options: {
		currencySymbol: "£",
		decimalSymbol: ".",
		decimalPlaces:2,
		separator:",",
		separatePlaces:3,
		currencyPos:"L"
	},
	initialize: function(options) {
		this.setOptions(options);
	},
	format: function(num) {
		var str="";
		var neg=num<0;
		var epsilon=0.00000000001;
		num=Math.abs(num);
		var maxPlace=Math.floor(Math.log(num)/Math.LN10);
		if (maxPlace<0) maxPlace=0;
		var minPlace=-this.options.decimalPlaces;
		var place=Math.pow(10,maxPlace);
		for (var p=maxPlace;p>=minPlace;p--) {
		
			var digit;
			if (p>minPlace) {
				digit=Math.floor(num/place+epsilon);
			}else {
				digit=Math.round(num/place);
			}
			str+=""+digit;
			num-=digit*place;
			
			if (p>0 && (p % this.options.separatePlaces==0)) {
				str+=this.options.separator;
			}

			if (p==0 && p>minPlace) {
				str+=this.options.decimalSymbol;
			}
			place/=10;
		}
		if (neg) {
			str="-"+str;
		}
		if (this.options.currencyPos=="L") {
			str=this.options.currencySymbol+str;
		} else if (this.options.currencyPos=="L") {
			str=str+this.options.currencySymbol;
		}
		return str;
	}
		
});