// 2004 Oklahoma State University (Physical Plant)
// For info, contact webman@pp.okstate.edu

function isANumber(theString) {
// returns true if theString is a numeric string (no scientific)
  if (theString.length == 0)
    return false;
  for (var i=0; i < theString.length; i++)
    if ((theString.substring(i,i+1) < '0') || (theString.substring(i,i+1) > '9'))
      return false;    
  return true;
}

function isValidHex(theString) {
// determines if theString is a hexidecimal number
  var hexChars = '0123456789ABCDEFabcdef'; // define valid characters
  for (var i=0; i< theString.length; i++)
    if (hexChars.indexOf(theString.charAt(i)) == -1)
      return false; 
  return true;
}

function toUSCurrency (input) { 
// takes a string number and returns valid US currency as a string
// Courtesy Charlton Rose, April 16, 1997, from http://sharkysoft.com/tutorials/jsa/content/036.html
    // Make sure input is a string:
    input += "";

    // Keep original copy of input string:
    var original_input = input;

    // Strip leading dollar sign if necessary:
    if (input . charAt (0) == "$")
      input = input . substring(1, input . length)
    else 
      if(input . substring(0, 2) == "-$" || input . substring(0, 2) == "+$")
        input = input . charAt(0) + input . substring(2, input . length);

    // Get float value:
    var amount = parseFloat(input);

    // Return unmodified input if we weren't able to convert it:
    if (isNaN(amount))
      return original_input;

    // Express amount in pennies, rounded to the nearest penny:
    amount = Math . round(100 * amount);

    // Prepare to add a US currency prefix:
    var prefix = "$";
    if (amount < 0) {
      prefix = "-" + prefix;
      amount = - amount;
    }

    // Convert amount to string and pad with leading zeros if necessary:
    var string;
    if (amount < 10)
        string = "00" + amount
    else 
      if (amount < 100)
        string = "0" + amount
      else
        string = "" + amount;

    // Insert prefix:
    string = prefix + string;

    // Insert decimal point before last two digits:
    string = string . substring(0, string . length - 2) + "." + string . substring(string . length - 2, string . length);

    // Return formatted currency string:
    return string;
}


/*******************************************************
CONVERSIONS
By Ryan Parman
Distributed according to SkyGPL 2.1, http://www.skyzyx.com/license/
*******************************************************/

function decimal(dec)
{
    this.dec=dec;
    this.toBinary=function() { return this.dec.toString(2); }
    this.toHex=function() { return this.dec.toString(16).toUpperCase(); }
    this.toOctal=function() { return this.dec.toString(8); }
}

function binary(bin)
{
    this.bin=bin;
    this.toDecimal=function() { return parseInt(this.bin, 2); }
    this.toHex=function() { return this.toDecimal().toString(16).toUpperCase(); }
    this.toOctal=function() { return this.toDecimal().toString(8); }
}

function hex(hex)
{
    this.hex=hex;
    this.toDecimal=function() { return parseInt(this.hex, 16); }
    this.toBinary=function() { return this.toDecimal().toString(2); }
    this.toOctal=function() { return this.toDecimal().toString(8); }
}

function octal(oct)
{
    this.oct=oct;
    this.toDecimal=function() { return parseInt(this.oct, 8); }
    this.toBinary=function() { return this.toDecimal().toString(2); }
    this.toHex=function() { return this.toDecimal().toString(16).toUpperCase(); }
}
