/* * * * * * * * * *     CONVERT SCRIPT    * * * * * * * * * *\
*                                                             *
*    Converts between U.S./Imperial and SI (Metric) units.    *
*                                                             *
*        --  Written by Bo Johansson  1998-11-10  --          *
*        --      Area measures added  1999-02-21  --          *
*        --  Pressure measures added  2000-05-17  --          *
*        --        Opera 4 bug fixes  2000-10-14  --          *
*        --  Other pressure measures  2002-03-21  --          *
*        --  Pressure measures moved                          *
*            to script convert2.js    2002-05-23  --          *
*        --  Only common functions                            *
*            in this script file      2002-06-14  --          *
*        --  Bug fixes                2002-06-14  --          *
*                                                             *
*   --   http://w1.545.telia.com/~u54504162/index_e.htm   --  *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



function checkNum(str)
{
  for (var i=0; i<str.length; i++)
  {
    var ch = str.substring(i, i + 1)
    
    if (ch!="." && ch!="+" && ch!="-" && ch!="e" && ch!="E" && (ch < "0" || ch > "9"))
    {
      alert("Vänligen, sätt in en korrekt siffra!");
      return false;
    }
  }
  return true

}  // * * * * * * * END function checkNum() * * * * * * * * *


function normalize(what,digits)     // *** Changed 2002-06-14
{
  theInput = String(what);

  outputDigits = parseInt(digits);

  var theOutput;

  var theShift;

  var outputExp = '';

  var outputSign = '';

  if( theInput.indexOf("e") != -1)
  {
    outputExp = theInput.substring(theInput.indexOf("e"), theInput.length);
    theInput = theInput.substring(0, theInput.indexOf("e"));
  }

  else
  {
    if ( theInput.indexOf("E") != -1)
    {
      outputExp = theInput.substring(theInput.indexOf("E"), theInput.length);
      theInput = theInput.substring(0, theInput.indexOf("e"));
    }

    if(theInput.substring(0, 1) == '-')
    {
      outputSign = '-';
      theInput = theInput.substring(1, theInput.length);
    }
  }

  if(parseFloat(theInput) >= 1.0)
  {
    var pointPos = theInput.indexOf(".");

    if(pointPos == -1)
    {
      pointPos = theInput.length;
    }

    theShift = pointPos - outputDigits;
  }

  else
  {
    var notNull = String(theInput).lastIndexOf('.') + 1;

    if(notNull == 1 )
    {
      notNull += 1;
    }

    while(String(theInput).charAt(notNull) == '0')
    {
      notNull++;
    }

    notNull -= 2;
    theShift = -(outputDigits + notNull);
  }

  theOutput = Math.round(theInput/Math.pow(10, theShift));

  if(theShift >= 0)
  {
    for(var i = 1; i <= theShift; i++)
    {
      theOutput += '0';
    }
  }

  else
  {
    theOutput += '';

    if(theOutput.length+theShift > 0)
    {
      theOutput = theOutput.substring(0, theOutput.length+theShift) + '.' + theOutput.substring((theOutput.length + theShift), theOutput.length);
    }

    else
    {
      var theOutput1 = '0.';

      for(var i =-1; i >= theOutput.length + theShift; i--)
      {
        theOutput1 += '0';
      }

      theOutput = theOutput1 + theOutput;
    }

    while(theOutput.charAt(theOutput.length - 1) == '0')
    {
      theOutput = theOutput.substring(0, theOutput.length - 1);
    }

    if(theOutput.charAt(theOutput.length-1) == '.')
    {
      theOutput = theOutput.substring ( 0, theOutput.length-1 );
    }
  }

//  if((theOutput <= 0) && (theOutput >= 0))
//    outputExp = '';

  theOutput = outputSign + theOutput + outputExp;

  return theOutput;

}  // * * * * * * * END function normalize() * * * * * * * * *



function compute(obj,val,data)
{
  if (obj[val].value)
  {
    var uval=0;
    
    uval = obj[val].value*data[val];
    
    if( (uval >= 0) && (String(obj[val].value).charAt(0) == '-') )
    {
      uval = -uval;    // *** Hack for Opera 4.0  2000-10-14
    }                  // *** Corrected (bugfix)  2002-06-14

    for (var i in data)
      obj[i].value=normalize(uval/data[i],8);
  }

}  // * * * * * * * END function compute() * * * * * * * * *


function resetValues(form,data)    // *** Hack for Opera 4.0  2000-10-14
{
    for (var i in data)
      form[i].value="";

}  // * * * * * * * END function resetValues() * * * * * * * * *


//* * * * * * * *   CONVERT SCRIPT - END -  * * * * * * * * *

