window.onload = initialize;

       ///////////////////////////////////////////////////////////////////////////
       // UnitConverter, Version 1.1, 2005/12       (approximately 700 units)   //
       // Copyright (C) 2005 by Hans Bauer, Schillerstr. 30, D-73072 Donzdorf   //
       //                       http://www.h-bauer.de                           //
       //                                                                       //
       // The converter has been modified by PaaPinden.dk                       //
       //                                                                       //
       // This program is free software; you can redistribute it and/or modify  //
       // it under the terms of the GNU General Public License as published by  //
       // the Free Software Foundation. No warranty is given for accuracy.      //
       ///////////////////////////////////////////////////////////////////////////
//          Basic websites (for imformations only):
//          - http://www.convert-me.com
//          - http://www.onlineconversion.com
//          - http://www.sengpielaudio.com/RechnerEinheitenumrechnung.htm
//          - http://en.wikipedia.org/wiki/Conversion_of_units
//          - http://de.wikipedia.org/wiki/Angloamerikanisches_Ma%C3%9Fsystem
//          - http://de.wikipedia.org/wiki/Druck_(Physik)

//          To open the UnitConverter in a popup-window:
//          -> Type 'popup' into the 'value'-field and press 'enter'.

       //  Changes Version 1.0 -> 1.1:
       //  - Conversion for 'millibar' in 'force per area' added
       //  - Erroneous conversions in 'volume flux' for m^3, dm^3, cm^2 rectified


function initialize() {                                                         //>Initialize
//indsætter tekst i skrivefeltet og markerer det
 fillLists();                                                                   // FillLists (base, target)
 document.getElementById("value").value = "indtast her";                        // Write 'popup' into value-field
 document.getElementById("value").focus();                                      // Set focus to value-field
 document.getElementById("value").select();                                     // Select 'popup' in value field
 calculate();  }                                                                // Calculate -> '- - - - -' in result

function newType()   { clearLists();                 fillLists();               //>NewType
                       calculate();                  setFocus();  }             //
function newValue()  { calculate();                  setFocus();  }             //>NewValue
function newBase()   { calculate();                  setFocus();  }             //>NewBase
function newTarget() { calculate();                  setFocus();  }             //>NewTarget
function setFocus()  { document.getElementById("value").focus();  }             //>SetFocus

function clearLists() {                                                         //>ClearLists (base, target)
 var base   = document.getElementById("base");                                  // Get Html-element of the base
 var target = document.getElementById("target");                                // Get Html-element of the target
 for (var i=base.length-1; i>=0; i--) {                                         // Loop: content of the lists
   base.options[i]   = null;                                                    //   Void base-item
   target.options[i] = null;  } }                                               //   Void target-item

//function enter() {                                                              //>Enter pressed
// var input = document.getElementById("value").value;                            // Value of the input-field
// if (input=="indtast her")                                                      // On 'popup'
//   var pop = window.open("strikkeomregner/enhedsomregner.html","",              // -> Open converter in a popup
//       "resizable=yes,scrollbars=no,width=650,height=180,left=50,top=8"); }     //    with several window options

function calculate() {                                                          //>Calculate
 var type        = document.getElementById("type");                             // Html-Element of type (category)
 var typeID      = type.options[type.selectedIndex].id;                         // ID of the choosen type
 var input       = document.getElementById("value").value;                      // Content (text) of input-value
 var white       = input.match(/\s*/);                                          // White spaces in input (for opera)
 var value       = parseFloat(input.replace(/,/,'.'));                          // Parsed string with ','->'.' or NaN
 var baseValue   = document.getElementById("base").value;                       // Base-value
 var targetValue = document.getElementById("target").value;                     // Target-value
 if (typeID=="numbersystem") {                                                  // On Numbersystem:
     calculateNumbersystem(input, baseValue, targetValue);    return; }         //    -> Calculate Numbersystem
 if (isNaN(value) || (white && input==white)) {                                 // No valid number or only whitespaces
     document.getElementById("result").value="- - - - - -";   return; }         //    -> Clear result value
 if (typeID=="temperature") {                                                   // On Temperature:
     calculateTemperature(value, baseValue, targetValue);     return; }         //    -> Calculate Temperature
 var result = value * baseValue / targetValue;                                  // Else: Convert value to result
 document.getElementById("result").value = result;  }                           //       Write result in text-field

function calculateNumbersystem(input, base, target) {                           //>CalculateNumbersystem
 var valid   = validateInput(input, base);                                      // Validate the input-field (value)
 var decimal = parseInt(valid, base);                                           // Convert input-value to decimal
 var result  = decimal.toString(target);                                        // Convert decimal-value to target syst.
 document.getElementById("result").value = result;  }                           // Write target-value to result field

function validateInput(input, base) {                                           //>ValidateInput (for number systems)
 var decimal = parseInt(input, base);                                           // Convert input-value->decimal-integer
 if (isNaN(decimal)) decimal = "";                                              // No decimal number  ->void input-field
 var valid = decimal.toString(base);                                            // Convert decimal back to base-system
 document.getElementById("value").value = valid;                                // Write validated value to input field
 return valid; }                                                                // Return validated value

function calculateTemperature(value, base, target) {                            //>CalculateTemperature
 var celsius = Number.NaN;                                                      // Convert base to celsius
 if      (base=="[F]")    celsius = (value-32)*5/9;                             //     [F] -> [C]
 else if (base=="[C]")    celsius =  value;                                     //     [C] -> [C]
 else if (base=="[K]")    celsius =  value-273.15;                              //     [K] -> [C]
 else if (base=="[R]")    celsius = (value*5/9)-273.15;                         //     [R] -> [C]
 var result = Number.NaN;                                                       // Convert celsius to target
 if      (target=="[F]")  result  = (celsius*9/5)+32                            //     [C] -> [F]
 else if (target=="[C]")  result  =  celsius;                                   //     [C] -> [C]
 else if (target=="[K]")  result  =  celsius+273.15;                            //     [C] -> [K]
 else if (target=="[R]")  result  = (celsius+273.15)*9/5;                       //     [C] -> [R]
 document.getElementById("result").value = result;  }                           // Write result in text-field

function enlist(text, value) {                                                  //>Enlist given items to base & target
 var base   = document.getElementById("base");                                  // Html-Element for Base-Unit
 var target = document.getElementById("target");                                //                  Target-Unit
 base.options[base.length]     = new Option(text, value);                       // Set Html-Properties to base
 target.options[target.length] = new Option(text, value); }                     //                        target

function fillLists() {                                                          //>FillLists
 var type   = document.getElementById("type");                                  // Get Html-Element of the 'type'
 var typeID = type.options[type.selectedIndex].id;                              // Get the ID of the selected type
 if      (typeID=="length") {                                                   // Lengths:
   enlist("[m] Meter",                              1.0);                       //   1 [m]  is base
//   enlist("[km] Kilometer",                         1000.0);                    //   1 [km]   = 1000             [m]
   enlist("[dm] Decimeter",                         0.1);                       //   1 [dm]   = 0.1              [m]
   enlist("[cm] Centimeter",                        0.01);                      //   1 [cm]   = 0.01             [m]
   enlist("[mm] Millimeter",                        0.001);                     //   1 [mm]   = 0.001            [m]
//   enlist("[um] Micron | Micrometer",               0.000001);                  //   1 [um]   = 0.000001         [m]
//   enlist("[nm] Nanometer",                         1e-9);                      //   1 [nm]   = 1e-9             [m]
//   enlist("[A] Angström",                           1e-10);                     //   1 [A]    = 1e-10            [m]
//   enlist("[pm] Picometer| Pikometer",              1e-12);                     //   1 [pm]   = 1e-12            [m]
//   enlist("[fm] Fermi | Femtometer",                1e-15);                     //   1 [fm]   = 1e-15            [m]
   enlist("[in] Inch (britisk/international tomme) = 2,54 cm",    
					                                0.0254);                    //   1 [in]   = 0.0254           [m]
//   enlist("[Mil, thou] Mil | Milli-Inch",           0.0000254);                 //   1 [Mil]  = 0.0000254        [m]
   enlist("[ft] Feet (britisk/international fod) = 12 inches",
					                                0.3048);                    //   1 [ft]   = 0.3048           [m]
   enlist("[yd] Yard (britisk/international) = 3 feet",
					                                0.9144);                    //   1 [yd]   = 0.9144           [m]
//   enlist("[ha] Hands (britisk/international) = 4 inches",        
//					                                0.1016);                    //   1 [ha]   = 0.1016           [m]
//   enlist("[sp] Span (britisk) = 9 inches",         0.2286);                    //   1 [sp]   = 0.2286           [m]
//   enlist("[mi] Mile (Br.) | Meile",                1609.344);                  //   1 [mi]   = 1609.344         [m]
//   enlist("[mi-u] Mile (U.S. Survey)",              1609.347219);               //   1 [mi-u] = 1609.347219      [m]
//   enlist("[NM, n mi] Nautical mile | Seemeile",    1852.0);                    //   1 [n mi] = 1852             [m]
//   enlist("[NM-a] Naut. mile = 6080ft (admirality)",1853.184);                  //   1 [nm-a] = 1853.184         [m]
//   enlist("[ch] Chain (Br.) | Ketten",              20.1168);                   //   1 [ch]   = 20.1168          [m]
//   enlist("[li] Links (Br.) | Kettenglieder",       0.201168);                  //   1 [li]   = 0.201168         [m]
   enlist("[BC] Barleycorn (britisk) = 1/3 inch",   0.008467);                  //   1 [BC]   = 0.008467         [m]
//   enlist("[rd] Rod, pole, perch | Rute",           5.0292);                    //   1 [rd]   = 5.0292           [m]
//   enlist("[fur] Furlong (Br.) | Achtelmeile",      201.168);                   //   1 [fur]  = 201.168          [m]
//   enlist("[fth] Fathom (Br.) | Faden",             1.8288);                    //   1 [fth]  = 1.8288           [m]
//   enlist("[ftu] Fathom (Hungary | Ungarn)",        1.8964838);                 //   1 [ftu]  = 1.8964838        [m]
//   enlist("[pi] Pica | Cicero = 12pt",              0.004217);                  //   1 [pi]   = 0.004217         [m]
//   enlist("[pt-a] Point ATA | Punkt",               0.0003514598);              //   1 [pt-a] = 0.0003514598     [m]
//   enlist("[pt-p] Point postscript = 1/72 in",      0.000352778);               //   1 [pt-p] = 0.000352778      [m]
//   enlist("[pt-e] Point  (Didot; European)",        0.000376065);               //   1 [pt-e] = 0.000376065      [m]
//   enlist("[pt-m] Point metric",                    0.000375);                  //   1 [pt-m] = 0.000375         [m]
//   enlist("[AU] Astronomical unit (Sun->Earth)",    149597870691);              //   1 [AU]   = 149597870691     [m]
//   enlist("[pc] Parsec | Parallaxensekunde",        30856775813057300);         //   1 [pc]   = 30856775813057300[m]
//   enlist("[LY] Light year",                        9460730472580800);          //   1 [LY]   = 9460730472580800 [m]
//   enlist("[Bu] Bu (China)",                        1.666667);                  //   1 [Bu]   = 1.666667         [m]
//   enlist("[Ch] Chi (China)",                       0.3333333);                 //   1 [Ch]   = 0.3333333        [m]
//   enlist("[Cho] Chou, cho (Japan)",                109.09);                    //   1 [Cho]  = 109.09           [m]
//   enlist("[Chr] Chr (Taiwan)",                     0.30303);                   //   1 [Chr]  = 0.30303          [m]
//   enlist("[Cun] Cun (China)",                      0.03333333);                //   1 [Cun]  = 0.03333333       [m]
//   enlist("[fe] Fen (China)",                       0.003333333);               //   1 [fe]   = 0.003333333      [m]
//   enlist("[Ha] Hao (China)",                       0.00003333333);             //   1 [Ha]   = 0.00003333333    [m]
//   enlist("[hu] Hu (China)",                        3.333333e-7);               //   1 [hu]   = 3.333333e-7      [m]
//   enlist("[Gan] Gan (Korea)",                      1.818);                     //   1 [Gan]  = 1.818            [m]
//   enlist("[Hu] Hunh (Thailand)",                   0.002604167);               //   1 [Hu]   = 0.002604167      [m]
//   enlist("[Ja] Ja (Korea)",                        0.303);                     //   1 [Ja]   = 0.303            [m]
//   enlist("[Je] Jeong (Korea)",                     109.091);                   //   1 [Je]   = 109.091          [m]
//   enlist("[jy] Jyo (Japan)",                       3.030324);                  //   1 [jy]   = 3.030324         [m]
//   enlist("[ka] Kabiet (Thailand)",                 0.005208333);               //   1 [ka]   = 0.005208333      [m]
//   enlist("[Ka] Kairi (Japan)",                     1852.0);                    //   1 [Ka]   = 1852             [m]
//   enlist("[Ke] Ken (Japan)",                       1.81818);                   //   1 [Ke]   = 1.81818          [m]
//   enlist("[Kb] Keub (Thailand)",                   0.25);                      //   1 [Kb]   = 0.25             [m]
//   enlist("[La] Lar (Thailand)",                    0.91432);                   //   1 [La]   = 0.91432          [m]
//   enlist("[Li] Li (China)",                        500.0);                     //   1 [Li]   = 500.0            [m]
//   enlist("[Li-sm] Li-small (China)",               0.0003333333);              //   1 [Li-sm]= 0.0003333333     [m]
//   enlist("[mo] Mon (Japan)",                       0.024);                     //   1 [mo]   = 0.024            [m]
//   enlist("[Ni] Nieu (Thailand",                    0.0208333);                 //   1 [Ni]   = 0.0208333        [m]
//   enlist("[Ri] Ri (Japan)",                        3927.27);                   //   1 [Ri]   = 3927.27          [m]
//   enlist("[Rk] Ri (Korea)",                        392.727);                   //   1 [Rk]   = 392.727          [m]
//   enlist("[Sa] Sawk (Thailand)",                   0.5);                       //   1 [Sa]   = 0.5              [m]
//   enlist("[Sh] Shaku, shyaku (Japan)",             0.30303);                   //   1 [Sh]   = 0.30303          [m]
//   enlist("[Se] Sen (Thailand)",                    40.0);                      //   1 [Se]   = 40               [m]
//   enlist("[si] Si (China)",                        0.000003333333);            //   1 [si]   = 0.000003333333   [m]
//   enlist("[Su] Sun (Japan)",                       0.030303);                  //   1 [Su]   = 0.030303         [m]
//   enlist("[Ts] Tsuen (Taiwan)",                    0.030303);                  //   1 [Ts]   = 0.030303         [m]
//   enlist("[Va] Va (Thailand)",                     2.0);                       //   1 [Va]   = 2                [m]
//   enlist("[Yi] Yin (China)",                       33.33333);                  //   1 [Vi]   = 33.33333         [m]
//   enlist("[Yo] Yote (Thailand)",                   16000.0);                   //   1 [Yo]   = 16000            [m]
//   enlist("[Zh] Zhang (China)",                     3.33333);                   //   1 [Zh]   = 3.33333          [m]
}
 else if (typeID=="mass") {                                                     // Mass:
   enlist("[kg] Kilogram",                          1.0);                       //   1 [kg] is base
   enlist("[g] Gram",                               0.001);                     //   1 [g]     = 0.001          [kg]
//   enlist("[t] Ton | Tonne",                        1000.0);                    //   1 [t]     = 1000           [kg]
//   enlist("[tn] Long ton (Br.)",                    1016.0469088);              //   1 [tn]    = 1016.0469088   [kg]
//   enlist("[sh tn] Short ton (Am.)",                907.18474);                 //   1 [sh tn] = 907.18474      [kg]
   enlist("[oz] Ounce (britisk/international) = [lb]/16",     
													0.028349523125);            //   1 [oz]    = 0.028349523125 [kg]
   enlist("[lb] Pound (britiske/internationale pund) = 7000 grain",     
                                                    0.45359237);                //   1 [lb]    = 0.45359237     [kg]
   enlist("[lb-m] Pound (metriske pund) = 500 gram",0.500);                     //   1 [lb-m]  = 0.500          [kg]
//   enlist("[oz t] Ounce (troy) = 480 grain",        0.0311034768);              //   1 [oz t]  = 0.0311034768   [kg]
//   enlist("[lb-t] Pound (troy pund) = 5760 grain",  0.3732417216);              //   1 [lb-t]  = 0.3732417216   [kg]
//   enlist("[st] Stone | Stein",                     6.35029318);                //   1 [st]    = 6.35029318     [kg]
//   enlist("[qt] Quarter = 2 stone",                 12.70058636);               //   1 [qt]    = 12.70058636    [kg]
   enlist("[gr] Grain (britisk/international)",
					                                0.00006479891);             //   1 [gr]    = 0.00006479891  [kg]
//   enlist("[sl] Slug, geepound",                    14.593903);                 //   1 [sl]    = 14.593903      [kg]
//   enlist("[q] Quintal (metric)",                   100.0);                     //   1 [q]     = 100            [kg]
//   enlist("[kt] Carat (metric) | Karat",            0.0002);                    //   1 [kt]    = 0.0002         [kg]
//   enlist("[ba] Baht (Thailand)",                   0.015);                     //   1 [ba]    = 0.015          [kg]
//   enlist("[ch] Chung (Thailand)",                  1.2);                       //   1 [ch]    = 1.2            [kg]
//   enlist("[do] Don (Korea)",                       0.00375);                   //   1 [do]    = 0.00375        [kg]
//   enlist("[ge] Geun (Korea)",                      0.600);                     //   1 [ge]    = 0.600          [kg]
//   enlist("[gw] Gwan (Korea)",                      3.75);                      //   1 [gw]    = 3.75           [kg]
//   enlist("[ha] Harb (Thailand)",                   60.0);                      //   1 [ha]    = 60             [kg]
//   enlist("[jn] Jin (China)",                       0.500);                     //   1 [jn]    = 0.500          [kg]
//   enlist("[Jn] Jin (Taiwan)",                      0.600);                     //   1 [Jn]    = 0.600          [kg]
//   enlist("[ka] Kan (Japan)",                       3.75);                      //   1 [ka]    = 3.75           [kg]
//   enlist("[ki] Kin, kinn (Japan)",                 0.600);                     //   1 [ki]    = 0.600          [kg]
//   enlist("[li] Liang (China)",                     0.0500);                    //   1 [li]    = 0.0500         [kg]
//   enlist("[Li] Liang (Taiwan)",                    0.0375);                    //   1 [Li]    = 0.0375         [kg]
//   enlist("[mo] Monme (Japan)",                     0.00375);                   //   1 [mo]    = 0.00375        [kg]
//   enlist("[sa] Saloung (Thailand)",                0.00375);                   //   1 [sa]    = 0.00375        [kg]
//   enlist("[ta] Tamlung (Thailand)",                0.06);                      //   1 [ta]    = 0.06           [kg]

} }
