/*********************************************************************************
** The contents of this file has been downloaded from http://www.merlyn.demon.co.uk/js-rndg3.htm
* The Original Code is: (C) J R Stockton, ? 2006-05-19
* The Initial Developer of the Original Code is (C) J R Stockton, ? 2006-05-19.
* All Rights Reserved.
********************************************************************************/

//function SetGlobals() {
	var undefined;
	U = undefined;
	n = null;
	P = + Infinity;
	M = - Infinity;
	N = NaN;
	CaseNo =5;
//}

function PrfxTo(S, L, C) {
	S += "";
	if (C.length > 0) {
		while (S.length < L) {
			S = C + S;
		}
	}
	return S;
}

function SpcsTo(S, L) {
	S += "";
	while (S.length < L) {
		S = " " + S;
	}
	return S;
}

function Sign(X) {
	return X > 0 ? "+" : X < 0 ? "-" : " ";
}

function NearHalf(frc) {
	return Math.abs(frc % 1 - 0.5) < 0.000001;
}

function NearHole(frc) {
	return NearHalf(frc + 0.5);
}

function IntFrc(frc) {
	with (Math) {
		switch (CaseNo) {
			case 0:
				return floor(frc);
			case 1:
				return round(frc);
			case 2:
				if (frc % 1 >= 0.5) {
					frc++;
				}
				return frc | 0;
			case 3:
				return frc % 1 != 0.5 ? round(frc) : 2 * round(frc / 2);
			case 4:
				return NearHalf(frc) ? 2 * round(frc / 2) : round(frc);
			case 5:
				if (NearHalf(frc)) {
					frc = round(2 * frc) / 2;
				}
				return round(frc);
			case 6:
				return ceil(frc);
			case 7:
				return frc + random() | 0;
			default:
				return " TypeError";
			}
		}
	}

function CORE(X, N) {
	var int = Math.floor(X), frc = (X - int + 1) * Math.pow(10, N);
	frc = String(IntFrc(frc));
	return int + + (frc.charAt(0) == "2") + "." + frc.substring(1);
	}

function NOGO(str)
{
	//return( /[a-z-]/.test(str));
	reg = new RegExp(/[a-z-]/);
	return reg.test(str) ;
}

function STRU(X, M, N) {
	var str = CORE(X, N);
	if (NOGO(str)) {
		return SpcsTo(X, M + 1 + N);
	}
	return PrfxTo(str, M + 1 + N, "0");
	}

function STRT(X, M, N) {
	var str = CORE(X, N);
	if (NOGO(str)) {
		str = String(X);
	}
	return SpcsTo(str, M + 1 + N);
	}

function STRS(X, M, N) {
	return Sign(X) + STRU(Math.abs(X), M - 1, N);
	}

function STRW(X, M, N) {
	return SpcsTo(STRS(X, 1, N), M + N + 1);
	}

function jsRoundTest () {
	var start=10;
	var offs=0.04;
	var decimals=2;
	for (i=0;i<=20;i++) {
		num=start+i;
		document.write(num+offs);
		document.write(' = ');
		document.write(CORE(num+offs,decimals));
		document.write('<br>');
	}
	}

//jsRoundTest();
