//********************************************************************************
//********************************************************************************
// Version 01.00 - 12/02/2007
//
// This file contains some general numeric handling routines
//********************************************************************************
// FormatNumeric : Validates and formats a numeric value
// IsInteger     : Is value an integer      
// IsNumeric     : Is value a numeric value
// RoundNumber   : Round a number to n decimal places
//********************************************************************************
//********************************************************************************


//********************************************************************************
//    Author: Steve Betts
//      Date: Feb 2007
//      Desc: FormatNumeric
//            Validates and formats a numeric value
//
// Amendment:
//********************************************************************************
function FormatNumeric(p_sValue, p_nDigits, p_nDP, p_sCurrency) {

  var sValue  = "";
  var sAlert  = "";
  var sParts  = "";
  var sPart1  = "";
  var sPart2  = "";
  var sResult = "";


  sValue = Trim(p_sValue + "");
  if ( sValue.length == 0 ) return sResult;

  // ---------------------------
  // Validate the numeric string
  // ---------------------------
  sParts = sValue.split(".");
  if (sParts.length > 2) {
     sAlert = sValue + " is not a valid numeric value";
  }
  else {
     for (i=0; i<sParts[0].length; i++) {
       if ( !(i == 0 && sParts[0].charAt(i) == p_sCurrency) ) {
          if ( sParts[0].charAt(i) >= "0" && sParts[0].charAt(i) <= "9" ) {
             sPart1 = sPart1 + sParts[0].charAt(i);
          }
          else {
             sAlert = sValue + " is not a valid numeric value";
             i = sParts[0].length;
          };
       };
     }; 

     if (sAlert == "" && sParts.length == 2) {
        for (i=0; i<sParts[1].length; i++) {
          if ( sParts[1].charAt(i) >= "0" && sParts[1].charAt(i) <= "9" ) {
             sPart2 = sPart2 + sParts[1].charAt(i);
          }
          else {
             sAlert = sValue + " is not a valid numeric value";
             i = sParts[1].length;
          };
        };
     }; 

     if (sAlert == "" && sPart1.length == 0 && sPart2.length !=0) sPart1 = "0";
     if (sAlert == "" && sPart1.length != 0 && sPart2.length ==0) sPart2 = "0";

  };

  // -----------------------
  // Round value and resplit
  // -----------------------
  if (sAlert == "") {
     sParts = RoundNumber(parseFloat(sPart1 + "." + sPart2),p_nDP).toString(10).split(".");
  };

  // ------------------------------------------------
  // Format the rounded value for returning to caller
  // ------------------------------------------------
  if (sAlert == "") {
     sResult = sParts[0];
     if (p_nDP != 0)  {
        if (sParts.length == 2) {
           sResult = sResult + '.' + PadR(sParts[1],p_nDP,"0");
        }
        else {
           sResult = sResult + "." + PadR("0",p_nDP,"0");
        };
     };
  
     sResult = p_sCurrency + sResult;
  };

  // ---------------------------------
  // Display alert if value is invalid
  // ---------------------------------
  if (sAlert != "") {
     alert(sAlert);
  };

  return sResult;

};


//********************************************************************************
//    Author: Steve Betts
//      Date: Oct 2005
//      Desc: IsInteger
//            Returns true if string value is an integer
//
// Amendment:
//********************************************************************************
function IsInteger(p_sValue){
 var nPtr;
 var sChar;

 for (nPtr = 0; nPtr < p_sValue.length; nPtr++){   
   sChar = p_sValue.charAt(nPtr);
   if (((sChar < "0") || (sChar > "9"))) return false;
 };
 return true;
};


//********************************************************************************
//    Author: Steve Betts
//      Date: Apr 2007
//      Desc: IsNumeric
//            Checks that the value is a number
//
// Amendment:
//********************************************************************************
function IsNumeric(p_sValue) {

  var sValue  = "";
  var bOK     = true;
  var sParts  = "";

  sValue = Trim(p_sValue);
  if ( sValue.length != 0 ) {
     sParts = sValue.split(".");
     if (sParts.length > 2) {
        bOK = false;
     }
     else {
        for (i=0; i<sParts[0].length; i++) {
          if ( ! (sParts[0].charAt(i) >= "0" && sParts[0].charAt(i) <= "9") ) {
             bOK = false;
             i = sParts[0].length;
          };
        }; 
     }; 

     if (bOK && sParts.length == 2) {
        for (i=0; i<sParts[1].length; i++) {
          if ( ! (sParts[1].charAt(i) >= "0" && sParts[1].charAt(i) <= "9") ) {
             bOK = false;
             i = sParts[1].length;
          };
        };
     }; 
  };

  return bOK;

};


//********************************************************************************
//    Author: Steve Betts
//      Date: Feb 2007
//      Desc: RoundNumber
//            Round number to n decimal places
//
//      Note: JavaScript has bug where it can't reliably round numbers between 
//            8192 and 10484, hence +/- 5000 workaround.

// Amendment:
//********************************************************************************
function RoundNumber(p_nValue, p_nDP) {

  var nRounded;

  if (p_nValue > 8191 && p_nValue < 10485) {
     p_nValue = p_nValue-5000;
     nRounded = Math.round(p_nValue*Math.pow(10,p_nDP))/Math.pow(10,p_nDP);
     nRounded = nRounded+5000;
  } else {
     nRounded = Math.round(p_nValue*Math.pow(10,p_nDP))/Math.pow(10,p_nDP);
  };

  return nRounded;
};


