/////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                             //
// ENGROUND FUNCTION                                                                           //
// This function is called by all Calculation Results pages of the Convection Wizard, and      //
// the Units Conversion Tool.                                                                  //
//                                                                                             //
// It rounds numbers to 4 significant figures and returns the number in the format x.xxxe+-y.  //
// It will only perform this operation on numbers in the range, 1.0e-36 < x < 1.0e36.          //
//                                                                                             //
// Written by Jan Radtke, Sept 30, 1999                                                        //
// Copyright 1999 (c) MAYA                                                                     //
//                                                                                             //
/////////////////////////////////////////////////////////////////////////////////////////////////

function engRound(valROUND){

  ///////////////////////////////////////////////////////////////////////////////////////////////
  //                                                                                           //
  // Declare all variables used in this function. No variables added later.                    //
  // Change only the sigFig values here.                                                       //
  //                                                                                           //
  ///////////////////////////////////////////////////////////////////////////////////////////////

  valROUND = "" + valROUND;                 // Input value
  var ExtendLowerRange = "No";              // This is import. for IE for numbers < 1.0e-6
  var ExtendLowestRange = "No";             // This is import. for IE for numbers < 1.0e-19
  var ExtendUpperRange = "No";              // This is import. for IE for numbers > 1.0e-21
  var Exception = "No";                     // Numbers between 1.0 > input >= 1.0e-3
  var Sign = 1;                             // Positive(1) / negative(0) input value
  var Exponent = 0;                         // Initial exponent for calculations
  var Exp = 0;                              // Final exponent values also holds info.
  var Str = "";                             // The main string for number manipulation
  var PeriodPos = -1;                       // Position of the decimal in input
  var PeriodLeftMost = "No";                // Decimal the first character in string or not
  var NumLen = 0;                           // The number of digits in number
  var HitFirstNonZero = "No";               // First non-zero find flag when abs. input<1.0
  var LeadingNonZeroPos = 1;                // First non-zero position when abs. input<1.0
  var round = 0.0;                          // Variable used in the rounding module
  var c = 0;                                // Used to fill out the number with c additional zeros
  var Result = 0;                           // Result when rounding completes
  var Rounded = "No";                       // Input was rounded up due to 9's (extra digit added)
  var Periodinsert = 0;                     // The position where the decimal will be inserted
  var Left = "";                            // The Number to the left of the decimal
  var Right = "";                           // The number to the right of the decimal
  var Zeros = 0;                            // For indicating zeros to be added when 1.0 > input >= 1.0e-3
  var LastPos = "";                         // The last position of a digit in the number
  var LastNum = 0;                          // The last digit in the number
  var Truncate = "";                        // True or False when zeros must be truncated
  var Final = 0.0;                          // Final Answer that will be output

  var sigFig = 6;                           // Number of significant figures

  ///////////////////////////////////////////////////////////////////////////////////////////////
  //                                                                                           //
  // NOTE:                                                                                     //
  // Please note that if this file's range of potential input values is to be extended,        //
  // another variable added above (similar to the "ExtendedLowerRange" and then checked        //
  // below by another if statement. The corresponding value must be added to the exponent      //
  // here as well. No other changes are necessary. The exponent's modification must be a       //
  // multiple of three, otherwise the engineering notation will not work.                      //
  //                                                                                           //
  ///////////////////////////////////////////////////////////////////////////////////////////////

  if ((Math.abs(valROUND) < 1.0e36) && (Math.abs(valROUND) > 1.0e-36)) {

    if (valROUND < 0.0) {
      valROUND = valROUND.substring(1,valROUND.length);
      Sign = 0;
    }
    if ((valROUND < 1.0) && (valROUND >= 1.0e-3)) {
      Exception = "Yes";                     // No modifications, will undergo at very end of function.
    }
    if (valROUND > 1.0e21) {
      valROUND = valROUND / 1.0e15;
      ExtendedUpperRange = "Yes";
      Exp = 15;
    }
    if (valROUND < 1.0e-19) {
      valROUND = valROUND * 1.0e30;
      ExtendLowestRange = "Yes";
      Exp = -30;
    }
    if ((valROUND < 1.0e-6) && (ExtendLowestRange == "No")){
      valROUND = valROUND * 1.0e15;
      ExtendLowerRange = "Yes";
      Exp = -15;
    }
   }
  else {
    if (valROUND != 0.0) {
      if (valROUND < 1.0e-36) {
       return "approx. 0";
      }
      else {
       return "above 1.0e36";
      }
    }
  }


  ///////////////////////////////////////////////////////////////////////////////////////////////
  //                                                                                           //
  // ROUNDING MODULE                                                                           //
  // This Module rounds to the amount of significant figures specified by the                  //
  // sigFig variable. It then inserts the decimal at the position given by the                 //
  // notation module.                                                                          //
  //                                                                                           //
  ///////////////////////////////////////////////////////////////////////////////////////////////

  // Find the position of the decimal

  if (valROUND != 0.0) {
    Str = "" + valROUND;                  // Have a string of the valROUND.
      if (Str.indexOf(".") != -1) {
        PeriodPos = Str.indexOf(".");
        Str = Str.substring(0,PeriodPos) + Str.substring(PeriodPos + 1,Str.length);
        Str = Str + "000000000000000";     //ensure that # of zeros > SigFig
        if (PeriodPos == 0) {
           PeriodLeftMost = "Yes";
        }
      }  
      else {
        PeriodPos = Str.length;
      }

    NumLen = Str.length;                 // holds the number of digits in the number

    if (Math.abs(valROUND) >= 1.0) {     // If Input >= 1.0
      Exponent = PeriodPos;
      round = (valROUND / (Math.pow(10.0, (PeriodPos-(sigFig+1)))));  // rounds number to sigFig digits.
    }
    else {
      if (Math.abs(valROUND) < 1.0) {      // Finds position where to start calc for abs. (1.0>x>0.0)
        for (var i = 0; i <= NumLen; i++) {
           if ((HitFirstNonZero == "No") && (Str.charAt(i) != 0)) {
              HitFirstNonZero = "Yes";
              LeadingNonZeroPos = i;
           }
        }
      }
      Exponent = (LeadingNonZeroPos-1);   // Set the "new" Exponent according to old decimal places.
      End = LeadingNonZeroPos+sigFig+1;   // End of string that will be rounded
      round = Str.substring(LeadingNonZeroPos, End);   //make string that will be rounded.
    }
    round = parseFloat(round) / 10.0;        // get ready for rounding
    round = Math.round(round);               // round number
    Result = "" + round;                     // turn into a character string
    if (Result.length > sigFig) {            // number was rounded up and has an extra zero at the end
      Result = Result.substring(0, sigFig)   // remove the zero
      Rounded = "Yes";
    }
  }
  else {           // valROUND = 0.0
    Result = "0";
  }

  if (Result.length < (sigFig)) {
     c = ((sigFig) - Result.length);    // The number of zeros that must be added to the end of the string.
     for (var i = 0; i <= c; i++) {
         Result += "0";
     }
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////
  // END OF ROUNDING MODULE                                                                    //
  ///////////////////////////////////////////////////////////////////////////////////////////////


  ///////////////////////////////////////////////////////////////////////////////////////////////
  //                                                                                           //
  // ENGINEERING NOTATION MODULE.                                                              //
  // This section finds the position where the decimal has to be inserted                      //
  // when engineering notation is to be used.                                                  //
  //                                                                                           //
  ///////////////////////////////////////////////////////////////////////////////////////////////

  if (valROUND != 0.0) {
    if (Math.abs(valROUND) >= 1.0) {            // to set the exponents
      if (Rounded == "Yes") {
        Exponent = Exponent+1;
      }
      if (Exponent % 3 == 0) {                 // is exponent in engineering notation?
        Exp = Exp + (Exponent-3);
        PeriodInsert = 3;
      }
      else {
        Exponent = (Exponent - 1)
        if (Exponent % 3 == 0) {
          Exp = Exp + Exponent;
          PeriodInsert = 1;
        }
        else {
          Exponent = (Exponent - 1)
          if (Exponent % 3 == 0) {
            Exp = Exp + Exponent;
            PeriodInsert = 2;
          }
          else {
            return " ";          // Error occured!
          }
        }
      }
    }
    else {
      if (Math.abs(valROUND) < 1.0) {       // Absolte value of input is < 1.0
        if (Rounded == "Yes") {
          Exponent = Exponent-1;
        }
        if (Exponent % 3 == 0) {
          Exp = Exp - Exponent - 3;
          PeriodInsert = 3;
        }
        else {
          Exponent = Exponent - 1;
          if (Exponent % 3 == 0) {
          Exp = Exp - Exponent - 3;
          PeriodInsert = 2;
          }
          else {
            Exponent = Exponent - 1;
            if (Exponent % 3 == 0) {
              Exp = Exp - Exponent - 3;
              PeriodInsert = 1;
            }
            else {
              return " ";         // Error occured!
            }
          }
        }
      }
    }
  }
  else {
    if (valROUND == 0.0) {
      Exp = 0;
    }
  }

  if (Exp > 0.0) {
     Exp = "+" + Exp;
  }

  if (PeriodLeftMost == "Yes") {
     PeriodInsert = PeriodInsert - 1;
     Left = "0";
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////
  // END OF ENGINEERING NOTATION MODULE                                                        //
  ///////////////////////////////////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////////////////////////////
  //                                                                                           //
  // NUMBER FORMATTING AND OUTPUT MODULE                                                       //
  // Rounding and correct number of significant figures has been achieved. The Exponent has    //
  // been set. Now, insert the decimal and append the exponent to the string if needed.        //
  // Finally, the module will specify the output of the function.                              //
  //                                                                                           //
  ///////////////////////////////////////////////////////////////////////////////////////////////

  if ((valROUND != 0.0) && (Exp != "-3")) {
    Left = Left + Result.substring(0,(PeriodInsert)); // Split the string into left and right parts
    Right = Result.substring(PeriodInsert,sigFig);    // for decimal insertion.
  }
  else {
    if (Exp == "-3") {                                // If exponent = -3, it will be removed
      Left = "0";
      if (Rounded == "No") {
        Zeros = (LeadingNonZeroPos-2);                // Number of zeros after decimal
      }
      else {                                          // Rounded = "Yes"
        Zeros = (LeadingNonZeroPos-3);
      }
      for (var i = 0; i <= Zeros; i++) {
        Right += "0";
      }
      Right = Right + Result.substring(0,sigFig)
      Exp = 0;
    }
    else {
      if (valROUND == 0.0) {
        Left = "0"                                     // Split the string when input was 0.0
        Right = "00"
      }
    }
  }
  Result = Left + "." + Right;                           // Insert decimal.

  LastPos = (Result.length);                             // Find the length of the string
  LastNum = Result.charAt(LastPos-1);
  if (LastNum == "0") {                                  // The least digit is zero, number will be
    Truncate = "True";                                   // truncated.
    do {
       LastPos = LastPos-1;
       if ( Result.charAt(LastPos - 2) == "." ) {
         Truncate = "False";
       }
       else {
         if ( Result.charAt(LastPos - 1) != "0" ) {
           Truncate = "False";
         }
       }
    } while (Truncate=="True")
  }
  Result = Result.substring(0,LastPos);                  // Trailing zeros removed

  if (Sign == 0) {                                       // Was the number negative?
    Result = "-" + Result;                               // Add negaitive sign if it was.
  }  

  if (Exp != 0.0) {
    Final = Result + "E" + Exp;
  }
  else {
    Final = "" + Result;
  }

  return Final;               // "Final" string is the Output string.

  ///////////////////////////////////////////////////////////////////////////////////////////////
  // END OF NUMBER FORMATTING AND OUTPUT MODULE                                                //
  ///////////////////////////////////////////////////////////////////////////////////////////////

}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
// END OF ENGROUND FUNCTION                                                                    //
/////////////////////////////////////////////////////////////////////////////////////////////////
