    function fnBodyOnLoad() {
	    document.frm1.txt1.focus();
	    document.frm1.txt1.select();

	    // Clear text fields
	    document.frm1.txt1.value = "";
	    document.frm1.txt2.value = "";

	    // Ensure something is selected
	    document.frm1.slct1.options[0].selected = true;
	    document.frm1.slct2.options[0].selected = true;

        document.frm1.txt1.value = 1;
    }

    function fnSlctOnChange(nSlct) {

	    var nLastEditedTxt = fn_getLastEditedTxt();

	    if(1 == nLastEditedTxt) {
		    if( false == fnIsStrNum(document.frm1.txt1.value) ) {
			    fnDispInvalidNum();
			    document.frm1.txt2.value = "n/a";
			    document.frm1.txt1.focus();
			    document.frm1.txt1.select();
		    } else {
		    document.frm1.txt2.value =
		    fnConvert(document.frm1.slct1.selectedIndex,
		    document.frm1.txt1.value,
		    document.frm1.slct2.selectedIndex);
                        if(DHTML)
                            {
                            if(NS)
                                {
                                setCont("id","Erg1",null,"<span class=\"Erg1\">" + document.frm1.txt2.value + "<\/span>");
                                setCont("id","Erg2",null,"<span class=\"Erg2\">" + fnUnits_getName(document.frm1.slct2.selectedIndex) + "<\/span>");
                                setCont("id","Erg3",null,"<span class=\"Erg3\">" + fnUnits_getName(document.frm1.slct1.selectedIndex) + "<\/span>");
                                }
                            else{
                                setCont("id","Erg1",null,document.frm1.txt2.value);
                                setCont("id","Erg2",null,fnUnits_getName(document.frm1.slct2.selectedIndex));
                                setCont("id","Erg3",null,fnUnits_getName(document.frm1.slct1.selectedIndex));
                                }
                            }
                        else{
                            return;
                            }
	            }
	    } else
	    if(2 == nLastEditedTxt) {
		    if( false == fnIsStrNum(document.frm1.txt2.value) ) {
			    fnDispInvalidNum();
			    document.frm1.txt1.value = "n/a";
			    document.frm1.txt2.focus();
			    document.frm1.txt2.select();
		    } else {
		    document.frm1.txt1.value =
		    fnConvert(document.frm1.slct2.selectedIndex,
		    document.frm1.txt2.value,
		    document.frm1.slct1.selectedIndex);
	            }
	    }

    }

    function fnTxtOnChange(nTxt) {
	    if(1 == nTxt) {
		    // txt1 changed
		    fn_setLastEditedTxt(1);
		    fnSlctOnChange(1);
	    } else
	    if(2 == nTxt) {
		    // txt2 changed
		    fn_setLastEditedTxt(2);
		    fnSlctOnChange(2);
	    }
    }


    var garrUnits = new Array(
    // format: name, value, category
    // 84 entries
    "m (metre)", 1, "length",
    "cm (centimetre)", 0.01, "length",
    "mm (millimetre)", 0.001, "length",
    "um (micrometre)", 0.000001, "length",
    "km (kilometre)", 1000, "length",
    "n mile (nautical mile)", 1852, "length",

    "in (inch)", 0.0254, "length",
    "ft (foot)", 0.3048, "length",
    "yd (yard)", 0.9144, "length",
    "mile", 1609.3, "length",

    "kg (kilogram)", 1, "mass",
    "mg (milligram)", 0.000001, "mass",
    "g (gram)", 0.001, "mass",
    "t (tonne)", 1000, "mass",

    "oz (ounce)", 0.02835, "mass",
    "lb (pound)", 0.454, "mass",

    "s (second)", 1, "time",
    "min (minute)", 60, "time",
    "h (hour)", 3600, "time",
    "d (day)", 86400, "time",

    "m2 (square metre)", 1, "area",
    "mm2 (square millimetre)", 0.000001, "area",
    "cm2 (square centimetre)", 0.0001, "area",
    "ha (hectare)", 10000, "area",
    "km2 (square kilometre)", 1000000, "area",

    "in2 (square inch)", 0.000645, "area",
    "ft2 (square foot)", 0.0929, "area",
    "yd2 (square yard)", 0.836, "area",
    "ac (acre)", 4050, "area",
    "mile2 (square mile)", 2590000, "area",

    "m3 (cubic metre)", 1, "volume",
    "cm3 (cubic centimetre)", 0.000001, "volume",

    "in3 (cubic inch)", 0.0000164, "volume",
    "ft3 (cubic foot)", 0.0283, "volume",
    "yd3 (cubic yard)", 0.765, "volume",
    "bushel", 0.0364, "volume",

    "l (litre)", 0.001, "volume",
    "ml (millilitre)", 0.000001, "volume",
    "kl (kilolitre)", 1000 , "volume",

    "fl oz (fluid ounce)", 0.0000284, "volume",
    "pint", 0.000568, "volume",
    "gallon", 0.00455, "volume",

    "m/s (metre/second)", 1, "velocity",
    "km/h (kilometre/hour)", 0.27, "velocity",
    "kn (knot)", 0.514, "velocity",

    "mph (miles/hour)", 0.4347, "velocity",

    "N (newton)", 1, "force",
    "kN (kilonewton)", 1000, "force",
    "MN (meganewton)", 1000000, "force",

    "lbf (pound-force)", 4.45, "force",

    "J (joule)", 1, "energy",
    "kJ (kilojoule)", 1000, "energy",
    "MJ (megajoule)", 1000000, "energy",

    "Btu (British thermal unit)", 1060, "energy",

    "W (watt)", 1, "power",
    "kW (kilowatt)", 1000, "power",
    "MW (megawatt)", 1000000, "power",

    "hp (horsepower)", 746, "power",

    "kg/m3 (kilogram/cubic meter)", 1, "density",
    "t/m3 (tonne/cubic meter)", 1000, "density",
    "g/m3 (gram/cubic meter)", 0.001, "density",

    "kg/l (kilogram/litre)", 1000, "density",

    "Pa (pascal)", 1, "pressure",
    "kPa (kilopascal)", 1000, "pressure",
    "MPa (megapascal)", 1000000, "pressure",

    "mb (millibar)", 100, "pressure",

    "psi (pounds/square inch)", 6890, "pressure",

    "A (ampere)", 1, "current",
    "mA (milliampere)", 0.001, "current",

    "V (volt)", 1, "potential",
    "uV (microvolt)", 0.000001, "potential",
    "mV (millivolt)", 0.001, "potential",
    "kV (kilovolt)", 1000, "potential",
    "MV (megavolt)", 1000000, "potential",

    "ohm", 1, "resistance",
    "microhm", 0.000001, "resistance",
    "megohm", 1000000, "resistance",

    "Hz (hertz)", 1, "frequency",
    "kHz (kilohertz)", 1000, "frequency",
    "MHz (megahertz)", 1000000, "frequency",
    "GHz (gigahertz)", 1000000000, "frequency",

    "K (kelvin)", /*k=*/"x", "_temperature",
    "C (Celsius)", /*k=*/"x + 273.15", "_temperature",
    "F (Fahrenheit)", /*k=*/"( (5/9) * (x-32) )  + 273.15", "_temperature"
    // notice last ele has no ,
    );

    function fnUnits_InitNames() {
	    if(0 == garrstrNames) {
	     var nLenUnits = garrUnits.length;
	     var nNumNames = nLenUnits / 3;
	     garrstrNames = new Array(nNumNames);

	     for(var i = 0; i < nNumNames; i++) {
		     garrstrNames[i] = garrUnits[i*3];
	     }
            }
    }

    function fnUnits_getValue(nNameIndex) {
	    if( nNameIndex <= fnUnits_getNames().length
	    && nNameIndex >= 0 ) {
		    return garrUnits[3*nNameIndex+1];
	    } else {
		    return 0;
	    }
    }

    function fnUnits_getName(nNameIndex) {
	    if( nNameIndex <= fnUnits_getNames().length
	    && nNameIndex >= 0 ) {
		    return garrUnits[3*nNameIndex];
	    } else {
		    return 0;
	    }
    }

    function fnUnits_getCat(nNameIndex) {
	    if( nNameIndex <= fnUnits_getNames().length
	    && nNameIndex >= 0 ) {
		    return garrUnits[3*nNameIndex+2];
	    } else {
		    return "";
	    }
    }

    function fnUnits_IsCatSpec(nNameIndex) {
	    if( "_" == fnUnits_getCat(nNameIndex).charAt(0)  ) {
		    return true;
	    }
	    else {
		    return false;
	    }

    }

    function fnUnits_getNames() {
	    if(0 == garrstrNames) {
		    fnUnits_InitNames();
	    }
	    return garrstrNames;
    }

    function fn_getLastEditedTxt() {
	    return gnLastEditedTxt;
    }

    function fn_setLastEditedTxt(nTxt) {
	    if(1 == nTxt) {
		    gnLastEditedTxt = 1;
	    }

	    if(2 == nTxt) {
		    gnLastEditedTxt = 2;
	    }

    }

    function fnConvert(nNameIndexOrig, strTxtOrig, nNameIndexDest) {
	    // will return strTextDest
	    if( fnUnits_getCat(nNameIndexOrig)
	    != fnUnits_getCat(nNameIndexDest) ) {
		    // Orig & Dest categories don't match
		    return "n/a";
	    } else {
	    var nfOrig = parseFloat(strTxtOrig);

	    if(isNaN(nfOrig)) {
		    return "n/a";
	    }

	    var nTxtDest = 0;
            if(false == fnUnits_IsCatSpec(nNameIndexOrig) ) {
	    nTxtDest = nfOrig * fnUnits_getValue(nNameIndexOrig)
	    / fnUnits_getValue(nNameIndexDest);
	    } else {
	    // Special handling category
	    if( "_temperature" == fnUnits_getCat(nNameIndexOrig) ) {

		    var x = nfOrig;
		    var nKelvin = eval(fnUnits_getValue(nNameIndexOrig));

		    // Ensure nKelvin >= 0 for real life constraint
		    if(0 > nKelvin) {
			    return "n/a";
		    }

		    var strTempName = fnUnits_getName(nNameIndexDest);

		    if("K (kelvin)" == strTempName) {
			    nTxtDest = nKelvin;
		    } else
		    if("C (Celsius)" == strTempName) {
                      // k=x + 273.15
                      // k - 273.15 =x
			    nTxtDest = nKelvin - 273.15;
		    } else
		    if("F (Fahrenheit)" == strTempName) {
    		     // k=((5/9)*(x-32)) + 273.15
    		     // (k - 273.15)/(5/9)=(x-32)
    		     // ((k - 273.15)/(5/9))+32=x
			    nTxtDest = ((nKelvin - 273.15)/(5/9)) + 32;
		    }

	    }

            }

	    nTxtDest = fnRndToDecPlaces(nTxtDest);

	    if(nTxtDest <= Number.POSITIVE_INFINITY
	    && nTxtDest >= Number.NEGATIVE_INFINITY) {
		    return nTxtDest;
	    } else {
             return "n/a";
	    }

	    }


    }

    function fnIsCharNum(charNum) {
	    var chararrValidNum = new Array("+", "-",
	    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
	    ".", "E", "e");

	    var narrValidNumLen = chararrValidNum.length;

	    for(var i=0; i < narrValidNumLen; i++) {
		    if(charNum == chararrValidNum[i]) {
			    return true;
		    }
	    }
	    return false;
    }

    function fnIsStrNum(strNum) {
	    var nLen = strNum.length;
	    for(var i=0; i < nLen; i++) {
		    if(false == fnIsCharNum(strNum.charAt(i)) ) {
			    return false;
		    }
	    }
	    return true;
    }

    function fnDispInvalidNum() {
	    alert("Sorry, invalid number given");

    }

    /*
    Used to compensate for javascript math errors
    due to IEEE floating-point math standards
    hard coded for: 6 decimal places
    */
    function fnRndToDecPlaces(nNumber) {
    var nNumDecPlaces = 6;
    return Math.round(nNumber*Math.pow(10,nNumDecPlaces))
    / Math.pow(10,nNumDecPlaces);

    }

    function fnTestConversions() {
	    var arrstrNames = fnUnits_getNames();
	    var nNamesLen  = arrstrNames.length;

	    for(var i=0; i < nNamesLen; i++) {
		    for(var j=0; j < nNamesLen; j++) {

			    var nTextDest = fnConvert(i, "1", j);
			    if("n/a" != nTextDest) {
				    document.write("1 " + arrstrNames[i] +
				    " is equal to ");
				    document.write(fnRndToDecPlaces(nTextDest));
				    document.write(" " + arrstrNames[j]);
				    document.write("<br>");
			    }

			    nTextDest = null;

		    }
			    document.write("<br>");
	    }

    }

    // main()
    var gnLastEditedTxt = 0;
    var garrstrNames = 0;
    fnUnits_InitNames();
    // fnTestConversions();
    // alert("fnUnits_getNames().length=" + fnUnits_getNames().length);
