//
//  Library containing the CSNum object
//
//  Author: Giles Mullen
//  Date:   08/27/2001
//
//  Requires: CS00_Lib_<Language> (i.e. CS00_Lib_English)
//            CS10_Lib_Shared
//            CS25_Lib_CSErr
//

//
//  CSNum - for formating numbers and currency.
//
CSNum.ERR_FAM             = 0x3000;

CSNum.ERR_OUTOFRANGE      = CSNum.ERR_FAM | 0x0004;
CSNum.ERR_INVLDFMT        = CSNum.ERR_FAM | 0x0008;

CSNum.ERR_OUTOFRANGE_B_STR   = CS_NUMBEROBJ_ERR_OUTOFRANGE_B_STR;
CSNum.ERR_OUTOFRANGE_G_STR   = CS_NUMBEROBJ_ERR_OUTOFRANGE_G_STR;
CSNum.ERR_OUTOFRANGE_L_STR   = CS_NUMBEROBJ_ERR_OUTOFRANGE_L_STR;
CSNum.ERR_OUTOFRANGE_E_STR   = CS_NUMBEROBJ_ERR_OUTOFRANGE_E_STR;
CSNum.ERR_FORMAT_INVALID_STR = CS_NUMBEROBJ_ERR_FORMAT_INVALID_STR;

function CSNum_getErrorStringStat( num, ex ) 
{
  var ret = CSErr.ERR_UNDEFINED_STR;
  if (num == CSNum.ERR_OUTOFRANGE) {
    if (ex.min != null && ex.max != null) {
      if (ex.min == ex.max) {
        ret = CS_InsertString( CSNum.ERR_OUTOFRANGE_E_STR, ex.min );
      } else {
        var tmp = CS_InsertString( CSNum.ERR_OUTOFRANGE_B_STR, ex.min )
        ret = CS_InsertString( tmp, ex.max );
      }
    } else if (ex.min != null && ex.max == null) {
      ret = CS_InsertString( CSNum.ERR_OUTOFRANGE_G_STR, ex.min );
    } else if (ex.min == null && ex.max != null) {
      ret = CS_InsertString( CSNum.ERR_OUTOFRANGE_L_STR, ex.max );
    } else {
      ret = CSNum.ERR_OUTOFRANGE_STR;
    }
  } else if (num == CSNum.ERR_INVLDFMT) {
    ret = CSNum.ERR_FORMAT_INVALID_STR;
  }
  return ret;
}

function CSNum_getErrorString() 
{
  return CSNum_getErrStrStat( this.err );
}

function CSNum_getErrStrStat( err )
{
  return CSNum_getErrorStringStat( err.no, err.ex );
}

function CSNum_insertPlaces( str, p )
{
  var i = 0
  var ret = ""
  if (p == null)
    p = ",";
  //
  //  strip leading zero's
  //
  str = str.replace( /^0*/, "" )
  var tmp = str.charAt( str.length - 1 )
  for (i = 2; i <= str.length; i++ ) {
    if (i % 3 == 1 && str.charAt( str.length - i ) != "") {
      tmp += p + str.charAt( str.length - i )
    } else {
      tmp += str.charAt( str.length - i )
    }
  }
  //
  //  reverse string
  //
  for (i = 0; i < tmp.length; i++) {
    ret += tmp.charAt( tmp.length - i - 1 )
  }
  if (ret == "") 
    ret = "0";
  return ret
}

function CSNum_evaluate( uval, cs, ds, ss, dp, app )
{
  var ret = null;
  this.err = new CSErr( CSErr.ERR_UNDEFINED );
  do {
    cs = (cs == null) ? "" : cs;
    dp = (dp == null) ? 0 : dp;
    if (ds == null || ds == "") {
      ds = ".";
      dp = 0;
    }
    var dsx = CS_EscapeRegExpLiterals( ds );
    var rgxcln = new RegExp( "[^\\d\\-" + dsx + "]", "g" );
    var cln = uval.replace( rgxcln, "" );
    rgxcln = new RegExp( dsx, "g" );
    cln = cln.replace( rgxcln, "." );
    var f = parseFloat( cln );
    if (isNaN( f )) {
      this.err = new CSErr( CSNum.ERR_INVLDFMT );
      break;
    }
    ret = f;
    this.num = f;
    this.err = new CSErr( CSErr.ERR_SUCCESS );
  } while (false);
  return ret;
} 

function CSNum_format( ival, cs, ds, ss, dp, app )
{
  if (ds == null || ds == "" || dp == 0) {
    ds = "";
    dp = 0;
  }
  ss = ss != null ? ss : "";
  if (ival == null) 
    n = 0;
  ival = CS_Round( ival, dp );
  var s;
  if (this.util) {
    // the following PDF-specific line can be removed once we are confident
    // that CS_Dec_Chop works the same.
    s = util.printf( "%." + dp + "f", ival );
  } else {
    s = CS_Dec_Chop( ival, dp );
    s = ival.toString();
  }
  var r = s.match(/(-?)(\d*)(\.?)(\d*)/);
  var w = CSNum_insertPlaces( r[2], ss ); 
  var fr = CS_NumberForceLengthAppend( r[4], dp );
  return (app ? r[1] + w + ds + fr + cs : r[1] + cs + w + ds + fr); 
}

function CSNum_validateRange( ival, mn, mx, cs, ds, ss, dp, app )
{
  var ret = true;
  if ((mn != null && ival < mn) || (mx != null && ival > mx)) {
    this.err = new CSErr( CSNum.ERR_OUTOFRANGE, new Object() );
    var n = new CSNum();
    this.err.ex.min = mn == null ? mn : n.format( mn, cs, ds, ss, dp, app );
    this.err.ex.max = mx == null ? mx : n.format( mx, cs, ds, ss, dp, app );
    ret = false;
  }
  return ret;   
}

function CSNum_valueOf()
{
  return this.num;
}

function CSNum_toString()
{
  return "num: " + this.num + "\rerror: " + this.err.no + "\rerror str: " + this.getErrorString() + "\rres: " + this.res;
}

function CSNum_reset( num )
{
  this.num = num == null ? 0 : num;
  this.err = new CSErr( CSErr.ERR_SUCCESS );
}

function CSNum( num )
{
  this.reset( num );
}

CSNum.prototype.reset          = CSNum_reset;
CSNum.prototype.valueOf        = CSNum_valueOf;
CSNum.prototype.toString       = CSNum_toString;
CSNum.prototype.getErrorString = CSNum_getErrorString;
CSNum.prototype.evaluate       = CSNum_evaluate;
CSNum.prototype.format         = CSNum_format;
CSNum.prototype.validateRange  = CSNum_validateRange;

CSNum.getErrorString           = CSNum_getErrStrStat;
