//<SCRIPT> // <- dummy script tag used to enable InterDev syntax coloring
//------------------------------------------------------------------------
// File:    imkDataConv.js
// Author:  Al Yanchak
// Date:    2/13/98
//
// I-Mark Data Conversion Library functions
//
// Copyright© 1998, I-Mark Inc. All Rights Reserved
//
// WARNING: The information in this file is protected by copyright law
// and international treaty provisions. Unauthorized reproduction or
// distribution of this file, or any portion of it, may result in severe
// criminal and civil penalties, and will be prosecuted to the maximum
// extent possible under the law.  Further, you may not reverse engineer,
// decompile, or disassemble the file.
//------------------------------------------------------------------------

//------------------------------------------------------------------------
//  Numeric conversion routines, each of the following routines provides
//  numeric onversion with validation, replace XXX with 'Int' or 'Float'
//
//   imkToXXX
//     Converts the text value a numeric, blank values ("") are converted
//     to zero (0), non numerics (NaN) produces an error message
//
//   imkToXXXNonZero
//     Converts the text value a numeric, NaN, blanks, or 0, produces an 
//     error message
//
//   imkToXXXPos
//     Converts the text value a numeric, NaN and values < 0 produces an
//     error message, blank values ("") are converted to zero (0)
//
//   imkToXXXPosNonZero
//     Converts the text value a numeric, NaN, blanks, and values <= 0
//     produces an error message
//
//   imkToXXXAbs
//     Converts the text value to the absolute value numeric, blank
//     values ("") are converted to zero (0), non numerics (NaN) produces
//     an error message
//
// Each function takes the form of
//
//   ToXXX(txt, [varName])
//
//     Var         Type        Desc
//     ------------------------------------------------------------------
//     txt         String      text input to be converted to numeric
//
//     varName     String      optional parameter, if present adds the
//                             following to the error message generated
//                             in a conversion failure - " for: " + varName
//
// returns: a numeric value of the appropriate type or NaN on conversion
//          failure
//
//
// Modified: 1/15/2003, changed base conversion (imkToInt and imkToFloat)
// to verify that the numeric number has the same text representation as
// the input value, this ensures that the inputs with trailing text 
// characters (e.g.; '100fabc') are not accepted. Such characters are
// ignored by the Javascript parseInt and parseFloat methods, but cause
// problems with server side VB Script - Al Yanchak
//------------------------------------------------------------------------

//------------------------------------------------------------------------
// Integer
//------------------------------------------------------------------------
function imkToInt(txt, varName)
{
  if(txt == "")
    txt = "0";
    
  var val = parseInt(txt, 10);
  if(isNaN(val) || !/^\s*\d+\s*$/.test(txt))
  {
    var msg = "Please provide a numeric input";
    if(varName)
      msg += " for: " + varName;
    alert(msg);
    val = NaN;
  } 
  return val;
}

function imkToIntNonZero(txt, varName)
{
  var val = imkToInt(txt, varName);
  if(!isNaN(val))
  {
    if(val == 0)
    {
      var msg = "Please provide a non-zero numeric input";
      if(varName)
        msg += " for: " + varName;     
      alert(msg);
      val = NaN;
    }
  }
  return val;
}

function imkToIntPos(txt, varName)
{
  var val = imkToInt(txt, varName);
  if(!isNaN(val))
  {
    if(val < 0)
    {
      var msg = "Please provide a positive numeric input";
      if(varName)
        msg += " for: " + varName;     
      alert(msg);
      val = NaN;
    }
  }
  return val;
}

function imkToIntPosNonZero(txt, varName)
{
  var val = imkToInt(txt, varName);
  if(!isNaN(val))
  {
    if(val <= 0)
    {
      var msg = "Please provide a positive non-zero numeric input";
      if(varName)
        msg += " for: " + varName;     
      alert(msg);
      val = NaN;
    }
  }
  return val;
}

function imkToIntAbs(txt, varName)
{
  return Math.abs(imkToInt(txt, varName));
}

//------------------------------------------------------------------------
// Float
//------------------------------------------------------------------------
function imkToFloat(txt, varName)
{
  if(txt == "")
    txt = "0";
    
  var val = parseFloat(txt);
  if(isNaN(val) || !/^\s*(\d*\.)?\d+\s*$/.test(txt))
  {
    var msg = "Please provide a numeric input";
    if(varName)
      msg += " for: " + varName;
    alert(msg);
    val = NaN;
  } 
  return val;
}

function imkToFloatNonZero(txt, varName)
{
  var val = imkToFloat(txt, varName);
  if(!isNaN(val))
  {
    if(val == 0)
    {
      var msg = "Please provide a non-zero numeric input";
      if(varName)
        msg += " for: " + varName;     
      alert(msg);
      val = NaN;
    }
  }
  return val;
}

function imkToFloatPos(txt, varName)
{
  var val = imkToFloat(txt, varName);
  if(!isNaN(val))
  {
    if(val < 0)
    {
      var msg = "Please provide a positive numeric input";
      if(varName)
        msg += " for: " + varName;     
      alert(msg);
      val = NaN;
    }
  }
  return val;
}

function imkToFloatPosNonZero(txt, varName)
{
  var val = imkToFloat(txt, varName);
  if(!isNaN(val))
  {
    if(val <= 0)
    {
      var msg = "Please provide a positive non-zero numeric input";
      if(varName)
        msg += " for: " + varName;     
      alert(msg);
      val = NaN;
    }
  }
  return val;
}

function imkToFloatAbs(txt, varName)
{
  return Math.abs(imkToFloat(txt, varName));
}

function imkRound(val, places)
{
  if(isNaN(places) != true)
  {
    places = Math.abs(places);
    var den = Math.pow(10, places);
    var x = Math.round(val * den);
    return x/den;
  }
  else
  {
    return NaN;
  }
}

function imkFormatDec(val, places)
{
  places = Math.abs(places);
  if(isNaN(val) != true)
  {
    var aftDec;
    var exp;
    var pos;
    
    var str = val.toString();
        
    str.toLowerCase();
    pos = str.indexOf("e")
    if(pos > 0)
    {
      exp = str.substr(pos);
      str = str.substr(0, pos);
    }
    else
    {
      exp = "";
    }

    var n = imkRound(parseFloat(str), places);    
    str = n.toString();
    
    pos = str.indexOf(".");
    if(pos > 0)
    {
      aftDec = str.length - pos - 1;
    }
    else
    {
      aftDec = 0;
      if(places > 0)
        str += ".";
    }
    while(aftDec < places)
    {
      str += "0";
      aftDec++;
    }
    
    str += exp;
  }
  else
  {
    str = "NaN";
  }
  
  return str;
}

function imkFormatScientific(val, places)
{
  var count = 0;
  var n = val;
  var strRes;

  if(isNaN(n) != true)
  { 
    places = Math.abs(places);
    
    strRes = imkFormatDec(0, places);
    strRes += "E0"

    if(Math.abs(n) >= 1)
    {
      while(Math.floor(Math.abs(n/10)) > 0)
      {
        n /= 10;
        count++;
      }
      
      strRes = imkFormatDec(n, places);
      strRes = strRes + "E" + count;
    }
    else if (Math.abs(n) > 0)
    {
      do
      { 
        n *= 10;
        count++;
      }
      while(Math.floor(Math.abs(n)) == 0);

      strRes = imkFormatDec(n, places);
      strRes = strRes + "E-" + count;
    }
  }
  else
  {
    strRes = "NaN";
  }
  
  return strRes;
}
