<!-- hide script from old browsers

// The Versatile Unit Converter, version 1.2
// Last change: 26 July 2000
// Written by C. Berthod, Christophe.Berthod@physics.unige.ch

/////////////////////////////////////////////////////////////////////////
///////////////////////////  DEFINITIONS  ///////////////////////////////
/////////////////////////////////////////////////////////////////////////
function define(){

//
//  Constants (expressed in the international MKSA system)
//  ******************************************************
//

var c    = 299792458        // speed of light in vacuum
var GN   = 6.67259E-11      // Newton constant of gravitation
var h    = 6.6260755E-34    // Planck constant
var q    = 1.60217733E-19   // elementary charge
var me   = 9.1093897E-31    // electron mass
var mmu  = 1.88353109E-28   // muon mass
var mn   = 1.6749286E-27    // neutron mass
var mp   = 1.6726231E-27    // proton mass
var u    = 1.6605402E-27    // atomic mass unit
var mue  = 928.47701E-26    // electron magnetic moment
var mumu = 4.49044813E-26   // muon magnetic moment
var mun  = 0.96623707E-26   // neutron magnetic moment
var mup  = 1.41060761E-26   // proton magnetic moment
var ge   =-2.0023193043737  // electron g-factor
var gmu  = 2.0023318320     // muon g-factor
var gn   =-3.82608545       // neutron g-factor
var gp   = 5.585694675      // proton g-factor
var R    = 8.314510         // molar gaz constant
var Na   = 6.0221367E+23    // Avogadro constant

pi       = Math.PI
alpha    = 4E-7*pi*c*Math.pow(q,2)/(2*h)  // fine structure constant
var Rinf = me*c*Math.pow(alpha,2)/(2*h)   // Rydberg constant

//
//  Definition of the units
//  ***********************
//
// (!! no symbol should contain "K", "oC", "oF" except temperatures)

function UnitDef(rad,m,kg,s,A,K,cd,mol,symbol,name,value)
{
  unit[0]+=1;unit[unit[0]]=new Array(symbol,rad,m,kg,s,A,K,cd,mol,name,value)
}
unit = new Array(0)

// Numbers ("one" must be the first being defined)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "one", "Number one", 1)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "%", "Percent", 1E-2)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "alpha", "Fine structure constant", alpha)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "bit", "Bit", 1)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "byte", "Byte", 8)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "pi", "Number pi", pi)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "ge", "Electron g-factor", ge)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "gmu", "Muon g-factor", gmu)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "gn", "Neutron g-factor", gn)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "gp", "Proton g-factor", gp)
UnitDef( 0, 0, 0, 0, 0, 0, 0, 0, "ppm", "Part per million", 1E-6)
// Angle
UnitDef( 1, 0, 0, 0, 0, 0, 0, 0, "arcmin", "Arc-minute", (2*pi)/360/60)
UnitDef( 1, 0, 0, 0, 0, 0, 0, 0, "arcsec", "Arc-second", (2*pi)/360/3600)
UnitDef( 1, 0, 0, 0, 0, 0, 0, 0, "deg", "Degree", (2*pi)/360)
UnitDef( 1, 0, 0, 0, 0, 0, 0, 0, "grad", "Grad", (2*pi)/400)
UnitDef( 1, 0, 0, 0, 0, 0, 0, 0, "rad", "Radian", 1)
// Length
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "AU", "Astronomical unit", 1.49597870E+11)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "Am", "Angstrom", 1E-10)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "Fm", "Fermi", 1E-15)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "a0", "Bohr radius", alpha/(4*pi*Rinf))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "ft", "Foot", 3.048E-1)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "fth", "Fathom", 1.8288)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "in", "Inch", 2.54E-2)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "lP", "Planck length", Math.pow(h/(2*pi)*GN/Math.pow(c,3),0.5))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "lambdaCe", "Electron Compton wavelength", h/(me*c))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "lambdaCmu", "Muon Compton wavelength", h/(mmu*c))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "lambdaCn", "Neutron Compton wavelength", h/(mn*c))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "lambdaCp", "Proton Compton wavelength", h/(mp*c))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "lr", "Length unit in the relativistic system", h/(2*pi)/(me*c))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "ly", "Light year", 9.4605E+15)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "m", "Meter", 1)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "mi", "Mile", 1.609344E+3)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "nmi", "Nautical mile", 1.852E+3)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "pc", "Parsec", 3.0857E+16)
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "re", "Classical electron radius", Math.pow(alpha,3)/(4*pi*Rinf))
UnitDef( 0, 1, 0, 0, 0, 0, 0, 0, "yd", "Yard", 9.144E-1)
// Mass
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "MA", "Mass unit in the Angstrom-eV system", Math.pow(h/(2*pi),2)/q*1E20)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "Sun", "Solar mass", 1.9891E30)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "ct", "Metrical carat", 2E-4)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "g", "Gram", 0.001)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "kg", "Kilogram", 1)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "lb", "Pound", 0.45359237)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "mP", "Planck mass", Math.pow(h/(2*pi)*c/GN,0.5))
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "me", "Electron mass", me)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "mmu", "Muon mass", mmu)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "mn", "Neutron mass", mn)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "mp", "Proton mass", mp)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "oz", "Ounce", 2.83495E-2)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "st", "Stone", 6.35029318)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "t", "Ton", 1000)
UnitDef( 0, 0, 1, 0, 0, 0, 0, 0, "u", "Atomic mass unit", u)
// Time
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "day", "Day", 24*3600)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "hr", "Hour", 3600)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "min", "Minute", 60)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "month", "Month", 365/12*7*24*3600)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "s", "Second", 1)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "s.year", "Sideral year", 365.25636*24*3600)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "t0", "Time unit in the atomic units system", Math.pow(h/(2*pi),3)/me/Math.pow(c*q,4)*1E+14)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "tA", "Time unit in the Angstrom-eV system", h/(2*pi)/q)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "tP", "Planck time", Math.pow(h/(2*pi)*GN/Math.pow(c,5),0.5))
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "tr", "Time unit in the relativistic system", h/(2*pi)/(me*Math.pow(c,2)))
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "week", "Week", 7*24*3600)
UnitDef( 0, 0, 0, 1, 0, 0, 0, 0, "year", "Year", 365*24*3600)
// Electric current
UnitDef( 0, 0, 0, 0, 1, 0, 0, 0, "A", "Ampere", 1)
UnitDef( 0, 0, 0, 0, 1, 0, 0, 0, "Bi", "Biot", 10)
UnitDef( 0, 0, 0, 0, 1, 0, 0, 0, "Gi", "Gilbert", 10/(4*pi))
UnitDef( 0, 0, 0, 0, 1, 0, 0, 0, "I0", "Current unit in the atomic units", me/Math.pow(h/(2*pi),3)*Math.pow(c*q,4)*q*1E-14)
UnitDef( 0, 0, 0, 0, 1, 0, 0, 0, "IA", "Current unit in the Angstrom-q system", Math.pow(q,2)*(2*pi)/h)
UnitDef( 0, 0, 0, 0, 1, 0, 0, 0, "Ir", "Current unit in the relativistic system", me*Math.pow(c,2)*q/(h/(2*pi)))
// Temperature
UnitDef( 0, 0, 0, 0, 0, 1, 0, 0, "K", "Kelvin", 1)
UnitDef( 0, 0, 0, 0, 0, 1, 0, 0, "TA", "Temperature unit in the Angstrom-q system", q/R*Na)
UnitDef( 0, 0, 0, 0, 0, 1, 0, 0, "oC", "Celsius or Centigrade", 1)
UnitDef( 0, 0, 0, 0, 0, 1, 0, 0, "oF", "Fahrenheit", 5/9)
// Luminous intensity
UnitDef( 0, 0, 0, 0, 0, 0, 1, 0, "cd", "Candela", 1)
UnitDef( 0, 0, 0, 0, 0, 0, 1, 0, "lm", "Lumen", 1)
// Quantity of matter
UnitDef( 0, 0, 0, 0, 0, 0, 0, 1, "mol", "Mole", 1)
// Solid angle
UnitDef( 2, 0, 0, 0, 0, 0, 0, 0, "sterad", "Steradian", 1)
// Inverse Length
UnitDef( 0,-1, 0, 0, 0, 0, 0, 0, "Rinf", "Rydberg constant", Rinf)
UnitDef( 0,-1, 0, 0, 0, 0, 0, 0, "d", "Diopter", 1)
// Area
UnitDef( 0, 2, 0, 0, 0, 0, 0, 0, "acre", "Acre", 4.04685642240E3)
UnitDef( 0, 2, 0, 0, 0, 0, 0, 0, "are", "Are", 100)
UnitDef( 0, 2, 0, 0, 0, 0, 0, 0, "b", "Barn", 1E-28)
UnitDef( 0, 2, 0, 0, 0, 0, 0, 0, "sigmae", "Electron Thomson cross section", 1/(6*pi)*Math.pow(alpha,6)/Math.pow(Rinf,2))
// Volume
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "UKgl", "UK Gallon or Imperial Gallon", 4.54609E-3)
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "USgl", "US Gallon", 3.78541E-3)
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "bbl", "US barrel", 0.159)
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "cup", "Cup", 2.36588E-4)
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "floz", "FluidOunce", 3.55163125E-5)
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "l", "Liter", 0.001)
UnitDef( 0, 3, 0, 0, 0, 0, 0, 0, "pt", "UK Pint", 0.568261E-3)
// Frequency
UnitDef( 0, 0, 0,-1, 0, 0, 0, 0, "Bq", "Bequerel", 1)
UnitDef( 0, 0, 0,-1, 0, 0, 0, 0, "Bd", "Baud", 1)
UnitDef( 0, 0, 0,-1, 0, 0, 0, 0, "Ci", "Curie", 3.7E+10)
UnitDef( 0, 0, 0,-1, 0, 0, 0, 0, "Hz", "Hertz", 1)
UnitDef( 0, 0, 0,-1, 0, 0, 0, 0, "Ru", "Rutherford", 1E+6)
UnitDef( 0, 0, 0,-1, 0, 0, 0, 0, "dpm", "Disintegration per minute", 1/60)
// Velocity
UnitDef( 0, 1, 0,-1, 0, 0, 0, 0, "M", "Mach", 340)
UnitDef( 0, 1, 0,-1, 0, 0, 0, 0, "c", "Velocity of light in vacuum", c)
UnitDef( 0, 1, 0,-1, 0, 0, 0, 0, "knot", "Nautical mile per hour", 1.852E+3/3600)
// Charge
UnitDef( 0, 0, 0, 1, 1, 0, 0, 0, "C", "Coulomb", 1)
UnitDef( 0, 0, 0, 1, 1, 0, 0, 0, "Fr", "Franklin", 1/c*1E-1)
UnitDef( 0, 0, 0, 1, 1, 0, 0, 0, "q", "Elementary charge", q)
// Luminance
UnitDef( 0,-2, 0, 0, 0, 0, 1, 0, "lx", "Lux", 1)
UnitDef( 0,-2, 0, 0, 0, 0, 1, 0, "ph", "Phot", 1E4)
// Force
UnitDef( 0, 1, 1,-2, 0, 0, 0, 0, "N", "Newton", 1)
UnitDef( 0, 1, 1,-2, 0, 0, 0, 0, "dyne", "Dyne", 1E-5)
UnitDef( 0, 1, 1,-2, 0, 0, 0, 0, "kgf", "Kilogram force", 9.80665)
UnitDef( 0, 1, 1,-2, 0, 0, 0, 0, "lbf", "Pound force", 0.45359237*9.80665)
// Energy
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "BTU", "British Thermal Unit", 1.05505585262E3)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "Hr", "Hartree", 4*pi*Rinf*Math.pow(c*q,2)/alpha*1E-7)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "J", "Joule", 1)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "Ry", "Rydberg", 2*pi*Rinf*Math.pow(c*q,2)/alpha*1E-7)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "cal", "Calorie", 4.1868)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "eV", "Electron volt", q)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "erg", "Erg", 1E-7)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "kWh", "Kilowatt hour", 3.6E+6)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "quad", "Quadrillion BTU", 1.05505585262E18)
UnitDef( 0, 2, 1,-2, 0, 0, 0, 0, "th", "Thermie", 4.185E+6)
// Power
UnitDef( 0, 2, 1,-3, 0, 0, 0, 0, "CV", "Horse", 7.3575E+2)
UnitDef( 0, 2, 1,-3, 0, 0, 0, 0, "W", "Watt", 1)
// Pressure
UnitDef( 0,-1, 1,-2, 0, 0, 0, 0, "Pa", "Pascal", 1)
UnitDef( 0,-1, 1,-2, 0, 0, 0, 0, "Torr", "Torr", 1.3322E+2)
UnitDef( 0,-1, 1,-2, 0, 0, 0, 0, "atm", "Atmosphere", 1.01325E+5)
UnitDef( 0,-1, 1,-2, 0, 0, 0, 0, "bar", "Bar", 1E+5)
UnitDef( 0,-1, 1,-2, 0, 0, 0, 0, "psi", "Pounds per Square Inch", 6.89475729317E+3)
// Viscosity
UnitDef( 0,-1, 1,-1, 0, 0, 0, 0, "P", "Poise", 0.1)
UnitDef( 0, 2, 0,-1, 0, 0, 0, 0, "St", "Stokes", 1E-4)
// Molar quantities
UnitDef( 0, 0, 0, 1, 1, 0, 0,-1, "Farad", "Faraday constant", Na*q)
UnitDef( 0, 0, 1, 0, 0, 0, 0,-1, "Mmol", "Molar mass", 0.001)
UnitDef( 0, 0, 0, 0, 0, 0, 0,-1, "Na", "Avogrado constant", Na)
UnitDef( 0, 3, 0, 0, 0, 0, 0,-1, "Vmol", "Molar volume", 0.0224141)
UnitDef( 0, 0, 0,-1, 0, 0, 0, 1, "kat", "Katal", 1)
// Physics
UnitDef( 0, 3,-1,-2, 0, 0, 0, 0, "GN", "Constant of gravitation", GN)
UnitDef( 0, 2, 1,-2, 0,-1, 0,-1, "R", "Molar gaz constant", R)
UnitDef( 0, 3, 1,-2, 0, 0, 0, 0, "e2", "q^2/(4 pi eps0)", Math.pow(c*q,2)*1E-7)
UnitDef( 0,-3,-1, 4, 2, 0, 0, 0, "eps0", "Vacuum permittivity", 1/(4*pi*Math.pow(c,2))*1E+7)
UnitDef( 0, 1, 0,-2, 0, 0, 0, 0, "gN", "Terrestrial acceleration", 9.80665)
UnitDef( 0, 2, 1,-1, 0, 0, 0, 0, "h", "Planck constant", h)
UnitDef( 0, 2, 1,-1, 0, 0, 0, 0, "hbar", "Dirac constant", h/(2*pi))
UnitDef( 0, 2, 1,-2, 0,-1, 0, 0, "k", "Boltzmann constant", R/Na)
UnitDef( 0, 1, 1,-2,-2, 0, 0, 0, "mu0", "Vacuum permeability", 4*pi*1E-7)
UnitDef( 0, 2, 0, 0, 1, 0, 0, 0, "muB", "Bohr magneton", q*h/(4*pi*me))
UnitDef( 0, 2, 0, 0, 1, 0, 0, 0, "muN", "Nuclear magneton", q*h/(4*pi*mp))
UnitDef( 0, 2, 0, 0, 1, 0, 0, 0, "mue", "Electron magnetic moment", mue)
UnitDef( 0, 2, 0, 0, 1, 0, 0, 0, "mumu", "Muon magnetic moment", mumu)
UnitDef( 0, 2, 0, 0, 1, 0, 0, 0, "mun", "Neutron magnetic moment", mun)
UnitDef( 0, 2, 0, 0, 1, 0, 0, 0, "mup", "Proton magnetic moment", mup)
UnitDef( 0,-3, 0, 0, 0, 0, 0, 0, "n0", "Loschmidt constant", Na/0.0224141)
UnitDef( 0, 2, 1,-2,-1, 0, 0, 0, "phi0", "Magnetic flux quantum", h/(2*q))
UnitDef( 0, 0, 1,-3, 0,-4, 0, 0, "sigma", "Stefan-Boltzmann constant", 2/15*pi*Math.pow(pi*R/Na/h,4)*h/c/c)
// Electricity
UnitDef( 0, 1, 0, 1, 1, 0, 0, 0, "D", "Debye", Math.pow(h/(2*pi*c),2)/(me*q)*1E+7)
UnitDef( 0,-2,-1, 4, 2, 0, 0, 0, "F", "Faraday", 1)
UnitDef( 0, 2, 1,-2,-2, 0, 0, 0, "H", "Henry", 1)
UnitDef( 0, 2, 1,-3,-2, 0, 0, 0, "Ohm", "Ohm", 1)
UnitDef( 0, 2, 1,-3,-2, 0, 0, 0, "RH", "Hall resistance", h/Math.pow(q,2))
UnitDef( 0,-2,-1, 3, 2, 0, 0, 0, "S", "Siemens", 1)
UnitDef( 0, 2, 1,-3,-1, 0, 0, 0, "V", "Volt", 1)
// Magnetism
UnitDef( 0, 0, 1,-2,-1, 0, 0, 0, "G", "Gauss", 1E-4)
UnitDef( 0, 2, 1,-2,-1, 0, 0, 0, "Mx", "Maxwell", 1E-8)
UnitDef( 0,-1, 0, 0, 1, 0, 0, 0, "Oe", "Oersted", 1/(4*pi)*1E+3)
UnitDef( 0, 0, 1,-2,-1, 0, 0, 0, "T", "Tesla", 1)
UnitDef( 0, 2, 1,-2,-1, 0, 0, 0, "Wb", "Weber", 1)
// Radioactivity
UnitDef( 0, 2, 0,-2, 0, 0, 0, 0, "Gy", "Gray", 1)
UnitDef( 0, 0,-1, 1, 1, 0, 0, 0, "Ro", "Rontgen", 2.58E-4)
UnitDef( 0, 2, 0,-2, 0, 0, 0, 0, "rd", "Rad", 1E-2)

//
//  Definition of the dimensions
//  ****************************
//

function DimDef(symbol,name)
{
  dimension[0]+=1;dimension[dimension[0]]=new Array(symbol,name)
}
dimension = new Array(0)

DimDef("","Number")
DimDef("rad","Angle")
DimDef("m","Length")
DimDef("kg","Mass")
DimDef("s","Time")
DimDef("A","Electric current")
DimDef("K","Temperature")
DimDef("cd","Luminous intensity")
DimDef("mol","Quantity of matter")
DimDef("rad^2","Solid angle")
DimDef("m^2","Area")
DimDef("m^3","Volume")
DimDef("m^-1","Inverse Length")
DimDef("m^-2","Fluence")
DimDef("m^-3","Volumic density")
DimDef("s^-1","Frequency")
DimDef("K^-1","Thermal Expansivity")
//
DimDef("m s^-1","Velocity")
DimDef("m s^-2","Acceleration")
DimDef("m^2 s^-1","Kinematic viscosity")
DimDef("m^2 s^-2","Specific energy")
DimDef("m^2 A","Magnetic moment")
DimDef("m^3 mol^-1","Molar volume")
DimDef("m^-1 A","Magnetic intensity")
DimDef("m^-2 s^-1","Fluence rate")
DimDef("m^-2 A","Electric current density")
DimDef("m^-2 cd","Luminance")
DimDef("m^-3 kg","Mass density")
DimDef("kg s^-1","Linear friction")
DimDef("kg mol^-1","Molar mass")
DimDef("s A","Electric charge")
DimDef("s^-1 mol","Catalytic activity")
//
DimDef("m kg s^-1","Momentum")
DimDef("m kg s^-2","Force")
DimDef("m kg^-1 s^2","Compressibility")
DimDef("m s A","Electric dipole")
DimDef("m^2 kg s^-1","Action")
DimDef("m^2 kg s^-2","Energy")
DimDef("m^2 kg s^-3","Power")
DimDef("m^2 s^-2 K^-1","Specific heat")
DimDef("m^3 kg^-1 s^-2","Gravific field")
DimDef("m^-1 kg s^-1","Absolute viscosity")
DimDef("m^-1 kg s^-2","Pressure")
DimDef("kg s^-2 A^-1","Magnetic field")
DimDef("kg^-1 s A","Specific charge")
DimDef("s A mol^-1","Molar charge")
//
DimDef("m kg s^-2 A^-1","Magnetic vector potential")
DimDef("m kg s^-2 A^-2","Permeability")
DimDef("m kg s^-3 A^-1","Electric field")
DimDef("m kg s^-3 K^-1","Thermal conductivity")
DimDef("m^2 kg s^-2 A^-1","Magnetic flux")
DimDef("m^2 kg s^-2 A^-2","Inductance")
DimDef("m^2 kg s^-2 K^-1","Entropy or Heat capacity")
DimDef("m^2 kg s^-3 A^-1","Electric potential")
DimDef("m^2 kg s^-3 A^-2","Electric resistance")
DimDef("m^3 kg s^-3 A^-2","Electric resistivity")
DimDef("m^-2 kg^-1 s^3 A^2","Electric conductance")
DimDef("m^-2 kg^-1 s^4 A^2","Electric capacitance")
DimDef("m^-3 kg^-1 s^3 A^2","Electric conductivity")
DimDef("m^-3 kg^-1 s^4 A^2","Permittivity")
//
DimDef("m^2 kg s^-3 A^-1 K^-1","Thermopower")

//
//  Definition of the prefixes
//  **************************
//

function PrefixDef(symbol,value)
{
  prefix[0]+=1;prefix[prefix[0]] = new Array(symbol,value)
}
prefix = new Array(0)

PrefixDef("y", 1E-24)
PrefixDef("z", 1E-21)
PrefixDef("a", 1E-18)
PrefixDef("f", 1E-15)
PrefixDef("p", 1E-12)
PrefixDef("n", 1E-09)
PrefixDef("mu",1E-06)
PrefixDef("m", 1E-03)
PrefixDef("c", 1E-02)
PrefixDef("d", 1E-01)
PrefixDef("da",1E+01)
PrefixDef("h", 1E+02)
PrefixDef("k", 1E+03)
PrefixDef("M", 1E+06)
PrefixDef("G", 1E+09)
PrefixDef("T", 1E+12)
PrefixDef("P", 1E+15)
PrefixDef("E", 1E+18)
PrefixDef("Z", 1E+21)
PrefixDef("Y", 1E+24)

//
//  Definition of the systems of units
//  **********************************
//

//  International system
SI = [,"rad","m","kg","s","A","K","cd","mol"]

//  CGS system
CGS = [,"rad","cm","g","s","Gi","K","cd","mol"]

//  Atomic units (a0 = me = hbar = q = 1)
AU = [,"rad","a0","me","t0","I0","K","cd","mol"]

//  Relativistic units (c = me = hbar = q = 1)
REL = [,"rad","lr","me","tr","Ir","K","cd","mol"]

//  Angstrom-eV system (Am = eV = q = hbar = k = 1)
AeV = [,"rad","Am","MA","tA","IA","TA","cd","mol"]

//
//  Table used by the Adapt function
//  ********************************
//

AdTab=[[,"m","kg","s","A","K","rad","cd","mol"],
       [,  1,  -2,  0,  0,  0,    0,   0,  0  ],
       [,  1,	0,  1, -1,  0,    0,   0,  0  ],
       [,  0,	0,  0,  0, -1,    0,   0,  0  ],
       [,  0,	0,  0,  1,  0,    0,   0,  0  ],
       [, -1,	1, -1,  1,  1,    0,   0,  0  ],
       [,  0,	0,  0,  0,  0,    1,   0,  0  ],
       [,  0,	0,  0,  0,  0,    0,   1,  0  ],
       [,  0,	0,  0,  0,  0,    0,   0,  1  ],
       [,"c", "h","k","q","E","rad","cd","mol"],
       [,  0,	0,  0,  0,  0,    0,   0,  0  ]]


}
/////////////////////////////////////////////////////////////////////////
/////////////////////  INIT AND UPDATE ROUTINES  ////////////////////////
/////////////////////////////////////////////////////////////////////////

//
//  Initializations
//  ***************
//

function init(sys,s,n)
{
  uI=new Array(0);uO=new Array(0)
  VI="NaN";VO="NaN"
  system=sys;notation=s;digits=n
  Tshift=false;dummy = 0
  for(var i=1; i<=unit[0]; i++)
  {
    if(unit[i][0]=="Vmol"){unit[i][10]=0.0224141}
    if(unit[i][0]=="Mmol"){unit[i][10]=0.001}
  }
}

//
//  Updating the options
//  ********************
//

function UpdateSys(form)
{
  if(form.Systems.selectedIndex==0){system=SI;message(0)}
  if(form.Systems.selectedIndex==1){system=SI;message(0)}
  if(form.Systems.selectedIndex==2){system=CGS;message(0)}
  if(form.Systems.selectedIndex==3){system=AU;message(25)}
  if(form.Systems.selectedIndex==4){system=REL;message(26)}
  if(form.Systems.selectedIndex==5){system=AeV;message(27)}
}

function UpdateNotation(form)
{
  if(form.Notation.selectedIndex==0){notation="scientific"}
  if(form.Notation.selectedIndex==1){notation="decimal"}
}

function UpdateDigits(form){digits=form.Digits.selectedIndex+1}

function UpdateMol(form)
{
  for(var i=1; i<=unit[0]; i++)
  {
    if(unit[i][0]=="Vmol"){unit[i][10]=Math.abs(Number(form.Vmol.value))/1000;form.Vmol.value=unit[i][10]*1000}
    if(unit[i][0]=="Mmol"){unit[i][10]=Math.abs(Number(form.Mmol.value))/1000;form.Mmol.value=unit[i][10]*1000}
  }
}

//
//  Formatting numbers
//  ******************
//

function format(a,scidec,dig)
//Formats the number a for printout, according to the options
//scidec ("scientific" or "decimal") and dig (number of digits)
{
  if(a==0){return "0"};var s=""
  var loga=Math.log(Math.abs(a))/Math.log(10)
  if(Math.abs(loga)<1E-15){loga=0}
  var sig=loga/Math.abs(loga);if(loga==0){sig=0}
  var x=0;var n=0;var i=0
  while(loga>=1||loga<0){i++,loga=loga-sig};n=sig*i
  x=Math.pow(10,Math.abs(loga))*a/Math.abs(a)
  x=Math.round(Math.pow(10,dig-1)*x)/Math.pow(10,dig-1)
  if(x==10){x=1;n+=1}
  var s1=" "+x;if(s1.indexOf(".")>0){s1+="000000000000"}else{s1+=".000000000000"}
  if((scidec=="scientific" && n!=0)||n>12||n<-12)
  {
    s=s1.substring(1,dig+2);if(x<0){s+=s1.substring(dig+2,dig+3)}
    s+=" * 10^"+n;return s
  }
  if(n==0){s=s1.substring(1,dig+2);if(x<0){s+=s1.substring(dig+2,dig+3)};return s}
  if(scidec=="decimal")
  {
    if(x>0){s="";s1=s1.substring(1,s1.length)}else{s="-";s1=s1.substring(2,s1.length)}
    s1=s1.substring(0,1)+s1.substring(2,s1.length)
    if(n<0){s+="0.";for(var i=1; i<=-n-1; i++){s+="0"};s+=s1.substring(0,dig)}
    if(n>0)
    {
      s+=s1.substring(0,1);
      for(var i=1; i<=n; i++){s+=s1.substring(i,i+1)};s+="."
      if(n<dig-1){s+=s1.substring(n+1,dig)}
    }
    return s
  }
}


/////////////////////////////////////////////////////////////////////////
//////////////////////////  INPUT CHECKING  /////////////////////////////
/////////////////////////////////////////////////////////////////////////

//
//  String manipulation
//  *******************
//

function clean(s)
//Replaces "*" by spaces in the string s
//Removes "1^1" and "1^-1" factors
//Replaces multiple spaces by single spaces
//Replaces " /" by "/"
//Removes spaces at the beginning and end of s
//Removes spaces in front of the "^" operators
{
  while(s.indexOf("*")>=0){s=s.replace("*"," ")}
  while(s.indexOf(" 1^1 ")>=0){s=s.substring(0,s.indexOf(" 1^1 "))+s.substring(s.indexOf(" 1^1 ")+4,s.length)}
  while(s.indexOf(" 1^-1 ")>=0){s=s.substring(0,s.indexOf(" 1^-1 "))+s.substring(s.indexOf(" 1^-1 ")+5,s.length)}
  while(s.indexOf("  ")>=0){s=s.replace("  "," ")}
  while(s.indexOf(" /")>=0){s=s.replace(" /","/")}
  if(s.indexOf(" ")==0){s=s.substring(1,s.length)}
  if(s.substring(s.length-1,s.length)==" "){s=s.substring(0,s.length-1)}
  while(s.indexOf(" ^")>=0){s=s.substring(0,s.indexOf(" ^"))+s.substring(s.indexOf(" ^")+1,s.length)}
  return s
}

function nextop(s)
//Returns the substring in front of the first "/" or "(" character
{
  var nf=s.indexOf("/");var np=s.indexOf("(")
  if(nf>=0 && np>=0){s=s.substring(0,Math.min(nf,np))}
  if(nf>=0 && np<0){s=s.substring(0,nf)}
  if(nf<0 && np>=0){s=s.substring(0,np)}
  return s
}

function closing(s)
//Finds the closing parenthesis for a string beginning with "("
//Returns "error" if an error occurs
{
  if(s.indexOf(")")==1){fatal(4,s);return "error"}
  var p=1
  for(var i=1; i<=s.length; i++)
  {
    if(s.substring(i,i+1)=="("){p+=1}
    if(s.substring(i,i+1)==")"){p-=1}
    if(p==0){return s.substring(1,i)}
  }
  fatal(3,s);return "error"
}

function preformat(s)
//Converts a string containing neither "/" nor "(" into the format "a1^n1 a2^n2 ..."
//Returns "error" if an error occurs
{
  s=clean(s);if(s.length==0||s=="error"){return s}
  var s1=s+" ";var s2=s1;s=""
  if(s1.indexOf("^")==0){fatal(1,s1);return "error"}
  while(s1.length>0)
  {
    s2=s1.substring(0,s1.indexOf(" "));s1=s1.substring(s2.length+1,s1.length);s+=s2
    if(s2.indexOf("^")>0)
    {
      var exp=s2.substring(s2.indexOf("^")+1,s2.length)
      if(exp.length==0){fatal(10,s2);return "error"}
      if(isNaN(Number(exp))||Number(exp)==0){fatal(2,exp);return "error"}
      s+=" "
    } else {s+="^1 "}
  }
  return s.substring(0,s.length-1)
}

function power(s,n)
//Gives the nth power of an expression of the type "a^i b^j ...",
//i.e., "a^i*n b^j*n ..."
{
  if(s=="error"){return s}
  var s1=s+" ";s=""
  while(s1.indexOf("^")>0){
    s=s+s1.substring(0,s1.indexOf("^")+1)+eval(s1.substring(s1.indexOf("^")+1,s1.indexOf(" "))+"*"+n)+" "
    s1=s1.substring(s1.indexOf(" ")+1,s1.length)}
  return s.substring(0,s.length-1)
}

//
//  Unit decoding
//  *************
//

function expand(s)
//Expands an expression with "/" and "(" operators as a product "a1^n1 a2^n2 ..."
//Returns "error" if an error occurs
//The function calls itself recursively
{
  var s1=nextop(s);var s2="";var f=1
  if(s1==s){return clean(preformat(s))}
  var st=preformat(s1);if(st=="error"){return st}else{st+=" "}
  s1=clean(s.substring(s1.length,s.length))
  if(s1.substring(0,1)=="/"){s1=s1.substring(1,s1.length);f=-1}
  if(s1.substring(0,1)=="(")
  {
    s2=closing(s1);if(s2=="error"){return "error"}
    s1=s1.substring(s2.length+2,s1.length)
    if(s1.substring(0,1)=="^")
    {
      var exp=s1.substring(1,Math.min((s1+" ").indexOf(" "),nextop(s1).length))
      if(isNaN(Number(exp))||exp.length==0||Number(exp)==0)
      {
        if(isNaN(Number(exp))||Number(exp)==0){fatal(2,exp)}
        if(exp.length==0){fatal(10,s2+"^")}
        var n="error"
      } else {var n=parseInt(exp)}
      s1=s1.substring(exp.length+1,s1.length)
    } else {var n=1}
  }
  else
  {
    s1=clean(s1);s2=s1.substring(0,Math.min((s1+" ").indexOf(" "),nextop(s1).length))
    s1=s1.substring(s2.length,s1.length)
    if(s2.length==0){fatal(11,"");var n="error"}else{var n=1}
  }
  if(n=="error"){return "error"}
  st=st+power(expand(s2),n*f)+" "+expand(s1)
  if(st.indexOf("error")!=-1){st="error"}
  return clean(st)
}

function CheckUnit(s)
//Takes any user input and returns an array
//The array structure is as follows:
//u[0]    = number of array elements (=-1 if an error is found in the input)
//u[i][0] = value of the prefix for the ith unit
//u[i][1] = index of the ith unit in the array unit[]
//u[i][2] = value of the exponent for the ith unit
//u[u[0]+1] = "dimension"
{
  s=expand(s);var u=new Array(0);if(s.length==0){u[1]="Number";return u}
  var n=0;var i=0;var j=0
  var s1=s;if(s1=="error"){u[0]=-1;return u};var s2=s1;var ok=false
  while(s1.length>0)
  {
    s2=s1.substring(0,(s1+" ").indexOf(" "))
    s1=s1.substring(s2.length+1,s1.length)
    n+=1;u[0]+=1;u[n]=new Array(1,0,1)
    if(s2.indexOf("^")>0)
    {
      u[n][2]=Number(s2.substring(s2.indexOf("^")+1,s2.length))
      s2=s2.substring(0,s2.indexOf("^"))
    }
    if(s2.substring(0,1)=="-"){u[n][0]=-1;s2=s2.substring(1,s2.length)}
    ok=false
    i=0;while(ok==false && i<unit[0]){i++;if(s2==unit[i][0]){u[n][1]=i;ok=true}}
    if(ok==false)
    {
      i=0;while(ok==false && i<unit[0])
      {
        i++
        if(s2.substring(1,s2.length)==unit[i][0])
        {
          u[n][1]=i
          for(j=1; j<=prefix[0]; j++)
            {if(s2.substring(0,1)==prefix[j][0]){u[n][0]=u[n][0]*prefix[j][1];ok=true}}
          if(ok==false){u[0]=-1;fatal(5,s2.substring(0,1));return u}
        }
      }
    }
    if(ok==false)
    {
      i=0;while(ok==false && i<unit[0])
      {
        i++
        if(s2.substring(2,s2.length)==unit[i][0])
        {
          u[n][1]=i
          for(j=1; j<=prefix[0]; j++)
            {if(s2.substring(0,2)==prefix[j][0]){u[n][0]=u[n][0]*prefix[j][1];ok=true}}
          if(ok==false){u[0]=-1;fatal(5,s2.substring(0,2));return u}
        }
      }
    }
    if(ok==false)
    {
      if(isNaN(Number(s2))){u[0]=-1;fatal(6,s2);return u}
      if(Number(s2)==0){u[0]=-1;fatal(14,"");return u}
      u[n][0]=u[n][0]*Number(s2);u[n][1]=1
    }
  }
  Tshift=TemperatureShift(s,u)
  CheckDim(u)
  return u
}

function CheckDim(u)
//Determines the dimension of the suite of units in the array u[]
//Adds an element to the array: u[u[0]+1] = "dimension"
{
  if(u[0]==-1){return}
  var s=ToSystem(u,SI);u[u[0]+1]=" ?";var i=0
  while(i<dimension[0]){i++;if(s==dimension[i][0]){u[u[0]+1]=dimension[i][1];return}}
  return
}

function CheckNumber(s){VI=eval("with(Math) "+s);return}

function Validate(form)
//Checks the user inputs and prepares the unit conversion
{
  if(isNaN(VI)){fatal(7,"");return false}
  if(uI[0]==-1||uO[0]==-1){return false}
  if(form.Systems.selectedIndex==0 && uI[0]!=0 && uI[uI[0]+1]!="Number" && uO[0]==0)
    {fatal(8,"");return false}
  return true
}


/////////////////////////////////////////////////////////////////////////
////////////////////////  CONVERSION ROUTINES  //////////////////////////
/////////////////////////////////////////////////////////////////////////

//
//  Unit conversion
//  ***************
//

function Convert(v,u,k)
//Converts the number v from unit u to MKSA (k>0) or from MKSA to unit u (k<0)
{
  for(var i=1; i<=u[0]; i++)
  {
    if(u[i][0]*unit[u[i][1]][10]<0){v=Math.pow(-1,u[i][2])*v}
    if(k>0){v=v * Math.exp(Math.log(Math.abs(u[i][0]*unit[u[i][1]][10]))*u[i][2])}
    if(k<0){v=v / Math.exp(Math.log(Math.abs(u[i][0]*unit[u[i][1]][10]))*u[i][2])}
  }
  return v
}

//
//  Converting to a system of units
//  *******************************
//

function ToSystem(u,system)
//Returns the string expression of the unit u[] in a given system of units
{
  var s="";var k=0
  for(var i=1; i<=8; i++)
  {
    k=0;for(var j=1; j<=u[0]; j++){k+=u[j][2]*unit[u[j][1]][i]}
    if(k!=0){s+=" "+system[i];if(k>1||k<0){s+="^"+k}}
  }
  s=s.substring(1,s.length)
  return s
}

//
//  Temperature related functions
//  *****************************
//

function TemperatureShift(s,u)
//Controls whether the Celsius and Fahrenheit temperature scales are
//combined to other units.
{
  if(s.indexOf("oF")!=-1)
  {
    for(var i=1; i<=u[0]; i++)
    {
      if((unit[u[i][1]][0]!="one"&&unit[u[i][1]][0]!="oF")||(unit[u[i][1]][0]=="oF"&&u[i][2]!=1))
      {return false}
    }
  }
  if(s.indexOf("oC")!=-1)
  {
    for(var i=1; i<=u[0]; i++)
    {
      if((unit[u[i][1]][0]!="one"&&unit[u[i][1]][0]!="oC")||(unit[u[i][1]][0]=="oC"&&u[i][2]!=1))
      {return false}
    }
  }
  return true
}

function TExtract(s)
//Determines which temperature scale is used in an expression s
{
  s=expand(s)
  if(s.indexOf("K")>=0){s="K";return s}
  if(s.indexOf("oC")>=0){s="oC";return s}
  if(s.indexOf("oF")>=0){s="oF";return s}
  s="K";return s
}

function TOffset(T,s1,s2)
//Adds the appropriate temperature offset for temperature conversions
{
  if(s1=="K"){if(s2=="oC"){T=T-273.15};if(s2=="oF"){T=T-459.67};return T}
  if(s1=="oC"){if(s2=="K"){T=T+273.15};if(s2=="oF"){T=T+32};return T}
  if(s1=="oF"){if(s2=="K"){T=T+255.38};if(s2=="oC"){T=T-17.77};return T}
}

function Compute(form)
//Performs the unit conversion
{
  if(Validate(form)==false)
  {
    dummy+=1
    if(dummy>1){Event(form,1);Event(form,2);Event(form,7);dummy=0}
    message(0);document.main.pic.src=sad.src;return
  }
  var sI=ToSystem(uI,system)
  var sO=ToSystem(uO,system)
  if(sO!=sI)
  {
    if(uI[uI[0]+1]==" ?"&&uO[uO[0]+1]==" ?")
    {fatal(9,"");message(0);document.main.pic.src=sad.src;return}
    else{fatal(9,form.UnitIn.value+"  :  "+uI[uI[0]+1]+"\n"+form.UnitOut.value+"  :  "+uO[uO[0]+1]);message(0);document.main.pic.src=sad.src;return}
  }
  var VSI=Convert(VI,uI,1)
  VO=Convert(VSI,uO,-1)
  message(28)
  if(uI[uI[0]+1]=="Temperature"&&Tshift)
  {
    var TI=TExtract(form.UnitIn.value);var TO=TExtract(form.UnitOut.value)
    if(TI!=TO){VO=TOffset(VO,TI,TO);message(29)}
  }
  form.ValueOut.value = format(VO,notation,digits)
}

function Adapt(form)
//Makes different units compatible using physical constants.
{
  if(Validate(form)==false)
  {
    dummy+=1
    if(dummy>1){Event(form,1);Event(form,2);Event(form,7);dummy=0}
    message(0);document.main.pic.src=sad.src;return
  }
  var sI=ToSystem(uI,SI)
  var sO=ToSystem(uO,SI)
  if(sI==sO){Compute(form);message(30);return}

  var ok=false
  var s=preformat(sO)+" "+power(preformat(sI),-1)+" ";var s1="";var s2=""
  AdTab[10]=[,0,0,0,0,0,0,0,0];var n=0;var i=0;var j=0
  while(s.length>0)
  {
    s1=s.substring(0,s.indexOf("^"));s2=s.substring(s.indexOf("^")+1,s.indexOf(" "))
    n=Number(s2);s=s.substring(s.indexOf(" ")+1,s.length)
    for(i=1; i<=8; i++){if(s1==AdTab[0][i]){for(j=1; j<=8; j++){AdTab[10][j]+=n*AdTab[j][i]}}}
  }
  if(AdTab[10][5]==0&&AdTab[10][6]==0&&AdTab[10][7]==0&&AdTab[10][8]==0)
  {
    s="  * "
    for(i=1; i<=4; i++){if(AdTab[10][i]!=0){s+=" "+AdTab[9][i];if(AdTab[10][i]!=1){s+="^"+AdTab[10][i]}}}
    form.UnitIn.value+=s;Event(form,2);Compute(form);message(31);return
  }
  s=preformat(sO)+" "+preformat(sI)+" ";s1="";s2=""
  AdTab[10]=[,0,0,0,0,0,0,0,0]
  while(s.length>0)
  {
    s1=s.substring(0,s.indexOf("^"));s2=s.substring(s.indexOf("^")+1,s.indexOf(" "))
    n=Number(s2);s=s.substring(s.indexOf(" ")+1,s.length)
    for(i=1; i<=8; i++){if(s1==AdTab[0][i]){for(j=1; j<=8; j++){AdTab[10][j]+=n*AdTab[j][i]}}}
  }
  if(AdTab[10][5]==0&&AdTab[10][6]==0&&AdTab[10][7]==0&&AdTab[10][8]==0)
  {
    s=""
    for(i=1; i<=4; i++){if(AdTab[10][i]!=0){s+=" "+AdTab[9][i];if(AdTab[10][i]!=1){s+="^"+AdTab[10][i]}}}
    form.UnitIn.value=clean(s)+" /(  "+form.UnitIn.value+"  )"
    form.ValueIn.value="1/(  "+form.ValueIn.value+"  )"
    Event(form,1);Event(form,2);Compute(form);message(31);return
  }
  fatal(12,"");message(32)
}


/////////////////////////////////////////////////////////////////////////
/////////////////  EVENTS, MESSAGES, ERRORS, AND HELP  //////////////////
/////////////////////////////////////////////////////////////////////////

//
//  Events handler
//  **************
//

function Event(form,i)
{
  if(i==0)  // Loading
  {
    form.ValueIn.value="";form.UnitIn.value="";form.DimIn.value=""
    form.ValueOut.value="";form.UnitOut.value="";form.DimOut.value=""
    form.Notation.options[0].selected=true
    form.Digits.options[4].selected=true
    form.Systems.options[0].selected=true
    form.Vmol.value="22.4141";form.Mmol.value="1"
    define();init(SI,"scientific",5);message(1)
  }
  if(i==1)  // Change in ValueIn
  {
    form.ValueOut.value="";VO="NaN"
    message(11)
    VI="NaN";CheckNumber(form.ValueIn.value)
    message(12)
  }
  if(i==2)  // Change in UnitIn
  {
    form.ValueOut.value="";VO="NaN"
    uI=CheckUnit(form.UnitIn.value)
    if(uI[0]>0)
    {
      form.DimIn.value=uI[uI[0]+1]
      if(form.Systems.selectedIndex!=0)
      {
        form.UnitOut.value=ToSystem(uI,system)
        uO=CheckUnit(form.UnitOut.value)
        form.DimOut.value=uO[uO[0]+1]
      }
      if(uI[uI[0]+1]!=" ?"){message(13)}else{message(24)}
    }
    else
    {
      form.DimIn.value="";message(0)
      if(form.Systems.selectedIndex!=0)
      {
        form.UnitOut.value="";form.DimOut.value="";uO[0]=uI[0]
      }
    }
    if(uI[0]<0){message(14)}
  }
  if(i==3)  // Change in DimIn
  {
    if(uI[0]>0){form.DimIn.value=uI[uI[0]+1]}else{form.DimIn.value=""}
    message(15)
  }
  if(i==4)  // Change in ValueOut
  {
    if(isNaN(VO)){form.ValueOut.value=""}else{form.ValueOut.value=format(VO,notation,digits)}
    message(16)
  }
  if(i==5)  // Change in Notation
  {
    UpdateNotation(form)
    if(isNaN(VO)){form.ValueOut.value=""}else{form.ValueOut.value=format(VO,notation,digits)}
  }
  if(i==6)  // Change in Digits
  {
    UpdateDigits(form)
    if(isNaN(VO)){form.ValueOut.value=""}else{form.ValueOut.value=format(VO,notation,digits)}
  }
  if(i==7)  // Change in UnitOut
  {
    form.ValueOut.value="";VO="NaN"
    form.Systems.options[0].selected=true;system=SI
    uO=CheckUnit(form.UnitOut.value)
    if(uO[0]>0)
    {
      form.DimOut.value=uO[uO[0]+1]
      if(uO[uO[0]+1]!=" ?"){message(17)}else{message(24)}
    }
    else{form.DimOut.value="";message(0)}
    if(uO[0]<0){message(18)}
  }
  if(i==8)  // Change in Systems
  {
    UpdateSys(form)
    if(uI[0]>0 && form.Systems.selectedIndex!=0)
    {
      form.ValueOut.value="";VO="NaN"
      form.UnitOut.value=ToSystem(uI,system)
      uO=CheckUnit(form.UnitOut.value)
      form.DimOut.value=uO[uO[0]+1]
    }
    if(uI[0]==0 && form.Systems.selectedIndex!=0)
    {form.UnitOut.value="";form.DimOut.value="";uO[0]=0}
    if(uI[0]==-1 && form.Systems.selectedIndex!=0)
    {form.UnitOut.value="";form.DimOut.value="";uO[0]=0;fatal(13,"")}
  }
  if(i==9)  // Change in DimOut
  {
    if(uO[0]>0){form.DimOut.value=uO[uO[0]+1]}else{form.DimOut.value=""}
    message(15)
  }
  if(i==10)  // Clear button
  {
    message(0)
    form.ValueIn.value="";form.UnitIn.value="";form.DimIn.value=""
    form.ValueOut.value="";form.UnitOut.value="";form.DimOut.value=""
    init(system,notation,digits)
    form.Vmol.value="22.4141";form.Mmol.value="1"
  }
  if(i==11)  // Adapt button
  {
    Adapt(form)
  }
  if(i==12)  // Convert button
  {
    Compute(form)
  }
  if(i==13)  // Change in Vmol
  {
    form.ValueOut.value="";VO="NaN"
    message(19)
    if(isNaN(Number(form.Vmol.value))){fatal(15,"Molar volume");return}
    message(20)
    UpdateMol(form)
  }
  if(i==14)  // Change in Mmol
  {
    form.ValueOut.value="";VO="NaN"
    message(21)
    if(isNaN(Number(form.Mmol.value))){fatal(15,"Molar mass");return}
    UpdateMol(form)
    message(22)
  }
  if(i==15)  // Change in Mess
  {
    message(23)
  }
}

//
//  Messages
//  ********
//

function message(i)
//Prints the messages
{
  if(i== 0){document.main.Mess.value="";document.main.pic.src=inert.src}
  if(i== 1){document.main.Mess.value=" Welcome to the Versatile Unit Converter !";document.main.pic.src=happy.src}
  if(i== 2){document.main.Mess.value=" A short version history";document.main.pic.src=inert.src}
  if(i== 3){document.main.Mess.value=" The Entry page: general infos.";document.main.pic.src=inert.src}
  if(i== 4){document.main.Mess.value=" Read the Help; then I'll hold no more secrets for you.";document.main.pic.src=inert.src}
  if(i== 5){document.main.Mess.value=" A few examples might be better than any Help file...";document.main.pic.src=inert.src}
  if(i== 6){document.main.Mess.value=" See how the systems of units are defined.";document.main.pic.src=inert.src}
  if(i== 7){document.main.Mess.value=" A list of the prefixes you can use.";document.main.pic.src=inert.src}
  if(i== 8){document.main.Mess.value=" The JavaScript mathematical functions.";document.main.pic.src=inert.src}
  if(i== 9){document.main.Mess.value=" My links to units related sites.";document.main.pic.src=inert.src}
  if(i==10){document.main.Mess.value=" Any question, comment, or good idea? Send me an email !";document.main.pic.src=happy.src}
  if(i==11){document.main.Mess.value=" I can't understand the number you typed !";document.main.pic.src=sad.src}
  if(i==12){document.main.Mess.value=" The number you typed is OK.";document.main.pic.src=happy.src}
  if(i==13){document.main.Mess.value=" The source unit you typed is OK.";document.main.pic.src=happy.src}
  if(i==14){document.main.Mess.value=" Problem with the source unit.";document.main.pic.src=sad.src}
  if(i==15){document.main.Mess.value=" I determine the dimension from the unit you type. You cannot edit it.";document.main.pic.src=inert.src}
  if(i==16){document.main.Mess.value=" It's my job to compute the converted number...";document.main.pic.src=inert.src}
  if(i==17){document.main.Mess.value=" The target unit you typed is OK.";document.main.pic.src=happy.src}
  if(i==18){document.main.Mess.value=" Problem with the target unit.";document.main.pic.src=sad.src}
  if(i==19){document.main.Mess.value=" Please give me a valid number for 'Vmol' (in liters/mole).";document.main.pic.src=sad.src}
  if(i==20){document.main.Mess.value=" OK. I have updated the value of 'Vmol'.";document.main.pic.src=happy.src}
  if(i==21){document.main.Mess.value=" Please give me a valid number for 'Mmol' (in grams/mole).";document.main.pic.src=sad.src}
  if(i==22){document.main.Mess.value=" OK. I have updated the value of 'Mmol'.";document.main.pic.src=happy.src}
  if(i==23){document.main.Mess.value=" This line is reserved for me to talk...";document.main.pic.src=inert.src}
  if(i==24){document.main.Mess.value=" The unit is OK, but I don't know the dimension. If you know it, please email me !";document.main.pic.src=inert.src}
  if(i==25){document.main.Mess.value=" These are the atomic Hartree units, with a0 = me = hbar = q = 1.";document.main.pic.src=inert.src}
  if(i==26){document.main.Mess.value=" These units are such that c = me = hbar = q = 1.";document.main.pic.src=inert.src}
  if(i==27){document.main.Mess.value=" These units are such that Am = eV = q = hbar = k = 1.";document.main.pic.src=inert.src}
  if(i==28){document.main.Mess.value=" The conversion was successful !";document.main.pic.src=happy.src}
  if(i==29){document.main.Mess.value=" The appropriate temperature shift has been added.";document.main.pic.src=inert.src}
  if(i==30){document.main.Mess.value=" The source and target units are compatible; no need to adapt them.";document.main.pic.src=inert.src}
  if(i==31){document.main.Mess.value=" The adaptation was successful !";document.main.pic.src=happy.src}
  if(i==32){document.main.Mess.value=" Sorry, the 'Adapt' button cannot work miracles.";document.main.pic.src=sad.src}
  if(i==33){document.main.Mess.value=" Examples:  3.14159,  -1.2345e-6,  3*14/159,  1-2+3/(4-5),  3*pow(2,1/3),  1/sqrt(2).";document.main.pic.src=inert.src}
  if(i==34){document.main.Mess.value=" Examples:  m,  ms,  a0,  mue,  km/hr,  kcal/mol,  hbar^2/(2 me),  MeV/c^2,  m^2 kg/s.";document.main.pic.src=inert.src}
  if(i==35){document.main.Mess.value=" Type a unit here or select a system of units in the menu.";document.main.pic.src=inert.src}
  if(i==36){document.main.Mess.value=" For infos on the units and symbols, check the menus or the 'By Name' and 'By Symbol' buttons.";document.main.pic.src=inert.src}
}

//
//  Errors
//  ******
//

function fatal(i,s)
//Handles the error messages
{
  if(i!=9&&i!=15){s='<<   '+s+'   >>'}
  if(i== 1){alert('Missing a symbol in front of "^":\n'+s)}
  if(i== 2){alert('Illegal exponent:\n'+s+'\nExponents must be non zero integers.')}
  if(i== 3){alert('Unclosed parenthesis:\n'+s)}
  if(i== 4){alert('Empty parenthesis:\n'+s)}
  if(i== 5){alert('Illegal prefix:\n'+s)}
  if(i== 6){alert('Unknown unit:\n'+s)}
  if(i== 7){alert('Please enter a number in the field\n"Number to convert".')}
  if(i== 8){alert('Please enter a unit in the field\n"Unit to convert to",\nor select a system of units in the menu.')}
  if(i== 9){alert('Incompatible units.\n'+s+"\n\nTry the Adapt button.")}
  if(i==10){alert('Missing exponent:\n'+s)}
  if(i==11){alert('Missing denominator.')}
  if(i==12){alert('Unable to adapt the units.')}
  if(i==13){alert('Type a valid unit in the field\n"Unit to convert from".')}
  if(i==14){alert('"0" (zero) is not allowed in this field.')}
  if(i==15){alert('Please enter a valid number in the '+s+' field')}
  dummy=0
}

//
//  Help
//  ****
//

function Help(select)
//Displays the info on the symbols and units
{
  var i=select.selectedIndex;var s=select.options[i].text;var n=0
  select.options[0].selected = true
  for(var j=1; j<=unit[0]; j++){if(unit[j][0]==s){n=j}}
  if (n==0){return}
  var u=CheckUnit(s)
  var s1=ToSystem(u,SI);var v=format(unit[n][10],"scientific",5)
//  document.main.UnitOut.value+=" "+s
//  alert(s+" = "+unit[n][9]+"  ("+u[u[0]+1]+")\n1 "+s+" = "+v+" "+s1)
//  window.status=s+" = "+unit[n][9]+"  ("+u[u[0]+1]+")    1 "+s+" = "+v+" "+s1
  document.main.Mess.value= " '"+s+"' is the '"+unit[n][9]+"' ("+clean(u[u[0]+1])+").  "+s+" = "+v+" "+s1
  document.main.pic.src=inert.src
}


/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

function winopen(s1,s2,x1,x2,d1,d2,s3)
{
  var s4="screenX="+x1+",screenY="+x2+",left="+x1+",top="+x2+",width="+d1+",height="+d2+",scrollbars=yes,resizable=yes"+s3
  window.open(s1,s2,s4)
}

happy = new Image(28,28);happy.src="gifs/happy.gif"
sad = new Image(28,28);sad.src="gifs/sad.gif"
inert = new Image(28,28);inert.src="gifs/inert.gif"

// end script hiding -->

