/*
	JS Extending Library
	Number functions.
	build 200806051950
*/

Number.prototype.leadingZeroes = function(zeroes) {
	if (this < 0) {
		var sign = '-';
		var res = this.substr(1);
	} else {
		var sign = '';
		var res = this.toString();
	}
	if (! isset(zeroes)) var zeroes = 2;
	zeroes -= res.length;

	for (var i = 0; i < zeroes; ++i) res = '0'+ res;

	return (sign + res);
}

Number.prototype.format = function(dec, decPoint, thousSep) {
	if (! isset(dec)) var dec = 2;
	if (! isset(decPoint)) var decPoint = '.';
	if (! isset(thousSep)) var thousSep = ' ';
	var res = this.toFixed(dec).toString().replace(',', '.').split('.');
	
	if (thousSep != '') {
		var tmp = '';
		var j = 0;
		for (var i = (res[0].length - 1); i > -1; --i) {
			if (j == 3) {
				tmp += thousSep;
				j = 0;
			}
			tmp += res[0].charAt(i);
			j++;
		}
		res[0] = tmp.reverse();
	}
	
	return res.join('.');
}

Number.prototype.base_convert = function(to, inUpperCase, zeroes) {
	var n = this;
	if (! isset(to)) to = 16;
	if (to < 2   ||   to > 32) return false;
	else to = parseInt(to);
	if (! isset(inUpperCase)) inUpperCase = true;
	if (! isset(zeroes)) zeroes = 0;
	else zeroes = parseInt(zeroes);

	var res = n.toString(to);

	if (inUpperCase) res = res.toUpperCase();
	if (zeroes > 0) res = res.leadingZeroes(zeroes);

	return res;
}

Number.prototype.round = function(dec){
	return this.toFixed(dec);
}

Number.prototype.decToHex = function(){
	return this.toString(16);
}

Number.prototype.hexTodec = function(){
	return parseInt(this, 16);
}

var Number = {
	rand: function(min, max) {
		return Math.floor(Math.random() * ((max + 1) - min)) + min;
	}
};

function random(min, max) {
	return Number.rand(min, max);
}


