/*
	HOW TO USER
	var df = new numberFormat();
	df.setMaximumFractionDigits();
	
	df.format(); (format string must look like this: 11231321.456)
*/

numberFormat = function() {
	this.MaximumFractionDigits = 0;
	
	this.setMaximumFractionDigits = function(val) {this.MaximumFractionDigits = val;}

	this.format = function(val) {
		var N = '' + this.numberformat(val, this.MaximumFractionDigits);

		if(N.length>3 ) {
			var output = '';
			var tmpVal = N.split(',');
			var tmpStr = '';
			for(var i = tmpVal[0].length, j = 0; i >= 0; i--, j++) {
				tmpStr += tmpVal[0].charAt(i);
				if(j == 3 && i > 0) {
					tmpStr += '.';
					j = 0;
				}
			}
			for(var i = tmpStr.length; i >= 0; i--) {
				output += tmpStr.charAt(i);
			}
			output += ',' + tmpVal[1];
			output = this.checkOutput(output);
			return output;
		}
		return N;
	}
	
	this.numberformat = function(num,dec) {
		var mul = Math.pow(10,dec);
		var num = (num * mul);
		num = Math.round(num);
		num = (num / mul);
		var numstr = String(num);
		if(numstr.indexOf(".") == -1) {numstr = numstr + ".";for(nfi=0;nfi<dec;nfi++) {numstr = numstr + "0"};}
		
		var decpl = numstr.length - numstr.indexOf(".");
		decpl = decpl - 1;
		if (decpl < dec) {for(nfi=decpl;nfi<dec;nfi++) {numstr = numstr + "0";}} 
		return (numstr.replace('.', ','));
	}
	this.checkOutput = function(val) {
		var newVal = val;
		if(newVal.indexOf('.,') > -1) {
			newVal = newVal.substr(0, newVal.indexOf('.,')) + newVal.substr((newVal.indexOf('.,') + 1), newVal.length)
		}
		return newVal;
	}
}

function stripDots(val) {
	var newVal = '';
	if(val.length > 0) {
		for(var i = 0; i < val.length; i++) {
			if(val.charAt(i) != '.') {
				newVal += val.charAt(i);
			}
		}
	}
	return newVal;
}