		var fmt = new MathContext(15, MathContext.prototype.SCIENTIFIC, MathContext.prototype.DEFAULT_LOSTDIGITS, MathContext.prototype.DEFAULT_ROUNDINGMODE);
		var fmt25 = new MathContext(25, MathContext.prototype.SCIENTIFIC, MathContext.prototype.DEFAULT_LOSTDIGITS, MathContext.prototype.DEFAULT_ROUNDINGMODE);

		var bd3m = new BigDecimal('1e-3');
		var bd2m = new BigDecimal('1e-2');
		var bd2 = new BigDecimal('1e2');
		var bd3 = new BigDecimal('1e3');
		var bdPatm = new BigDecimal('101325');
		var bdPmmhg = bdPatm.divide(new BigDecimal('760'), fmt25);
		var bdPpsi = new BigDecimal('6894.75729');
		var bdPat = new BigDecimal('98066.5');
		var bdPgcm = new BigDecimal('98.0665');
		var bdPbar = new BigDecimal('100000');
		var bdPmbar = new BigDecimal('100');
		var bdPdcm = new BigDecimal('0.1');

		var bdin2m = new BigDecimal('0.0254');
		var bdft2m = new BigDecimal('0.3048');
		var bdyd2m = new BigDecimal('0.9144');
		var bdmi2m = new BigDecimal('1609.344');

		var bdWlb = new BigDecimal('0.45359237');
		var bdWstn = new BigDecimal('907.18474');
		var bdWltn = new BigDecimal('1016.0469088');

		var bdVin = new BigDecimal('16.387064E-6');
		var bdVigal = new BigDecimal('4.54609E-3');
		var bdVigalt = new BigDecimal('4.545964591E-3');
		var bdVugal = new BigDecimal('3.785411784E-3');
		var bdVft = new BigDecimal('0.028316846592');
		var bdVbbl = new BigDecimal('0.158987294928');

		function addCommas(nStr)
		{
			nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return x1 + x2;
		}

		bd2string = function(l) {
			return addCommas( l.finish(fmt, true).toString());
		};

		conv =
		[
			{ 'source': 'g1f1',  'target': 'pressure', 'f': function(l) { return l; } },
			{ 'source': 'g1f2',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPatm, fmt25); } },
			{ 'source': 'g1f3',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPmmhg, fmt25); } },
			{ 'source': 'g1f4',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPpsi, fmt25); } },
			{ 'source': 'g1f5',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPat, fmt25); } },
			{ 'source': 'g1f6',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPgcm, fmt25); } },
			{ 'source': 'g1f7',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPbar, fmt25); } },
			{ 'source': 'g1f8',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPmbar, fmt25); } },
			{ 'source': 'g1f9',  'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bdPdcm, fmt25); } },
			{ 'source': 'g1f10', 'target': 'pressure', 'f': function(l) { return (l === null) ? null : l.multiply(bd2, fmt25); } },
			// conversion to K
			{ 'source': 't1', 'target': 'temp', 'f': function(l) { return (l < 0) ? Number.NaN : l; } },
			{ 'source': 't2', 'target': 'temp', 'f': function(t) { var k = parseFloat(t) + 273.15; return (k < 0) ? Number.NaN : k; } },
			{ 'source': 't3', 'target': 'temp', 'f': function(t) { var k = ((parseFloat(t )- 32) / 1.8) + 273.15; return (k < 0) ? Number.NaN : k; } },

			// conversion to meter
			{ 'source': 'l1', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bd3m, fmt); } },
			{ 'source': 'l2', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bd2m, fmt); } },
			{ 'source': 'l3', 'target': 'length', 'f': function(l) { return l; } },
			{ 'source': 'l4', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bd3, fmt); } },
			{ 'source': 'l5', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bdin2m, fmt); } },
			{ 'source': 'l6', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bdft2m, fmt); } },
			{ 'source': 'l7', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bdyd2m, fmt); } },
			{ 'source': 'l8', 'target': 'length', 'f': function(l) { return (l === null) ? null : l.multiply(bdmi2m, fmt); } },
			// conversion to kg
			{ 'source': 'w1', 'target': 'weight', 'f': function(l) { return l; } },
			{ 'source': 'w2', 'target': 'weight', 'f': function(l) { return (l === null) ? null : l.multiply(bd3, fmt25); } },
			{ 'source': 'w3', 'target': 'weight', 'f': function(l) { return (l === null) ? null : l.multiply(bdWlb, fmt25); } },
			{ 'source': 'w4', 'target': 'weight', 'f': function(l) { return (l === null) ? null : l.multiply(bdWstn, fmt25); } },
			{ 'source': 'w5', 'target': 'weight', 'f': function(l) { return (l === null) ? null : l.multiply(bdWltn, fmt25); } },
			// conversion to cm3
			{ 'source': 'v1', 'target': 'volume', 'f': function(l) { return l; } },
			{ 'source': 'v2', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bd3m, fmt25); } },
			{ 'source': 'v7', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bdVin, fmt25); } },
			{ 'source': 'v3', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bdVigal, fmt25); } },
			{ 'source': 'v5', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bdVugal, fmt25); } },
			{ 'source': 'v8', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bdVft, fmt25); } },
			{ 'source': 'v6', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bdVbbl, fmt25); } },
			{ 'source': 'v4', 'target': 'volume', 'f': function(l) { return (l === null) ? null : l.multiply(bdVigalt, fmt25); } }

		];

		dataOut =
		[
			{ 'source': 'pressure', 'target': 'g1f1',  'toString': function(l) { return bd2string(l); } },
			{ 'source': 'pressure', 'target': 'g1f2',  'toString': function(l) { return bd2string(l.divide(bdPatm, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f3',  'toString': function(l) { return bd2string(l.divide(bdPmmhg, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f4',  'toString': function(l) { return bd2string(l.divide(bdPpsi, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f5',  'toString': function(l) { return bd2string(l.divide(bdPat, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f6',  'toString': function(l) { return bd2string(l.divide(bdPgcm, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f7',  'toString': function(l) { return bd2string(l.divide(bdPbar, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f8',  'toString': function(l) { return bd2string(l.divide(bdPmbar, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f9',  'toString': function(l) { return bd2string(l.divide(bdPdcm, fmt25)); } },
			{ 'source': 'pressure', 'target': 'g1f10', 'toString': function(l) { return bd2string(l.divide(bd2, fmt25)); } },

			{ 'source': 'temp', 'target': 't1', 'toString': function(l) { var t = parseFloat(l); return parseFloat(t.toFixed(7)); } },
			{ 'source': 'temp', 'target': 't2', 'toString': function(l) { var t =l - 273.15; return parseFloat(t.toFixed(7)); } },
			{ 'source': 'temp', 'target': 't3', 'toString': function(l) { var t = (1.8 * (l - 273.15)) + 32; return parseFloat(t.toFixed(7)); } },
			
			
			{ 'source': 'length', 'target': 'l1', 'toString': function(l) { return bd2string(l.multiply(bd3, fmt)); } },
			{ 'source': 'length', 'target': 'l2', 'toString': function(l) { return bd2string(l.multiply(bd2, fmt)); } },
			{ 'source': 'length', 'target': 'l3', 'toString': function(l) { return bd2string(l); } },
			{ 'source': 'length', 'target': 'l4', 'toString': function(l) { return bd2string(l.multiply(bd3m, fmt)); } },
			{ 'source': 'length', 'target': 'l5', 'toString': function(l) { return bd2string(l.divide(bdin2m, fmt)); } },
			{ 'source': 'length', 'target': 'l6', 'toString': function(l) { return bd2string(l.divide(bdft2m, fmt)); } },
			{ 'source': 'length', 'target': 'l7', 'toString': function(l) { return bd2string(l.divide(bdyd2m, fmt)); } },
			{ 'source': 'length', 'target': 'l8', 'toString': function(l) { return bd2string(l.divide(bdmi2m, fmt)); } },
			
			{ 'source': 'weight', 'target': 'w1', 'toString': function(l) { return bd2string(l); } },
			{ 'source': 'weight', 'target': 'w2', 'toString': function(l) { return bd2string(l.divide(bd3, fmt25)); } },
			{ 'source': 'weight', 'target': 'w3', 'toString': function(l) { return bd2string(l.divide(bdWlb, fmt25)); } },
			{ 'source': 'weight', 'target': 'w4', 'toString': function(l) { return bd2string(l.divide(bdWstn, fmt25)); } },
			{ 'source': 'weight', 'target': 'w5', 'toString': function(l) { return bd2string(l.divide(bdWltn, fmt25)); } },

			{ 'source': 'volume', 'target': 'v1', 'toString': function(l) { return bd2string(l); } },
			{ 'source': 'volume', 'target': 'v2', 'toString': function(l) { return bd2string(l.divide(bd3m, fmt25)); } },
			{ 'source': 'volume', 'target': 'v7', 'toString': function(l) { return bd2string(l.divide(bdVin, fmt25)); } },
			{ 'source': 'volume', 'target': 'v3', 'toString': function(l) { return bd2string(l.divide(bdVigal, fmt25)); } },
			{ 'source': 'volume', 'target': 'v5', 'toString': function(l) { return bd2string(l.divide(bdVugal, fmt25)); } },
			{ 'source': 'volume', 'target': 'v8', 'toString': function(l) { return bd2string(l.divide(bdVft, fmt25)); } },
			{ 'source': 'volume', 'target': 'v6', 'toString': function(l) { return bd2string(l.divide(bdVbbl, fmt25)); } },
			{ 'source': 'volume', 'target': 'v4', 'toString': function(l) { return bd2string(l.divide(bdVigalt, fmt25)); } }
		];


		function clearout( obj) {
			var objName = obj.id;
			var i,j, t;
			
			for (j in conv) {
				// find the field we have been using
				if (conv[j].source == objName) {
					// set the type
					t = conv[j].target;
					break;
				}
			}
			
			for (i in dataOut) {
				if (dataOut[i].source == t) {
					if (dataOut[i].target != objName) {
						$('#'+dataOut[i].target).val('');
					}
				}
			}		
		}
		
		function recalc( input, obj) {
			var v, v1, t, i, j;
			//currentValue = input;
			var objName = obj.id;

			if (input === '') {
				clearout(obj);
				return;
			}
			else {
				v1 = new BigDecimal(input);
			}
			
			
			for (j in conv) {
				// find the field we have been using
				if (conv[j].source == objName) {
					// set the type
					t = conv[j].target;
					// and convert to base value
					try	{
						v = conv[j].f(v1);
					}
					catch (e) {
						return;
					}
				}
			}
			
			for (i in dataOut) {
				if (dataOut[i].source == t) {
					if (dataOut[i].target != objName) {
						$('#'+dataOut[i].target).val(dataOut[i].toString(v));
					}
				}
			}
		}
		
		function process(event) {
			//var numpattern = /^(([0-9]+)[,\.]?([0-9]*)|[,\.]([0-9]+))$/;
			// below is only with 2 decimals
			//var numpattern= /^(-?)(((\d{1,3})(,\d{3})*)|(\d+))(\.\d{1,2})?$/;
			var numpattern= /((\b[0-9]+)?\.)?[0-9]+\b/;
			var input = this.value.toString();
			// make sure we remove all the comma's
			input = input.replace(/[,]/g, '');
			// check if input is numeric
			if (!numpattern.test(input)) {
				// not a number.. so empty fields
				clearout(this);
				return;
			}
			recalc( input, this);
		}

