//(c) 2000,2001 Geoff Hobson
// These fuctions may be freely used, amended and added to,
// but please include aknowledgement of the original author
// Version 1.1 31 Oct 2001
// Corrected numericValue() and signedNumericValue() to return zero correctly

function isNumeric(str)
// returns true if str is numeric
// that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if ( ('0'<=ch && ch<='9') || ch == '.' )
      p++;
    else
      ok= false;
  }
  return ok;
}

function numericValue(str)
// returns the numeric value of str (which must be all digits)
{
  if (isNumeric(str))
  {
    while (str.charAt(0)=="0" && str.length>1) // if has leading zero (but not just "0")
      str= str.substring(1, str.length); // remove any leading zeros (otherwise assumes octal)
    return(parseInt(str));
  }
  else
  {
    alert("Attempt to take numericValue of non-Numeric string "+str);
    return "NaN";
  }
}

function isSignedNumeric(str)
// returns true if str is (optionally) signed numeric
// that is it contains only the digits 0-9 with an option leading sign (+ or -)
// returns false otherwise
// returns false if empty (or contains only a sign)
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var ch= str.charAt(0);
  if (ch=="+" || ch=="-")
    return isNumeric(str.substring(1,str.length));
  else
    return isNumeric(str);
}

function signedNumericValue(str)
// returns the numeric value of str (which must be a (optionally) signed number)
{
  if (isSignedNumeric(str))
  {
    var s=str.charAt(0);
    if (s=="+" || s=="-")
      str= str.substring(1, str.length);  // cut off sign
    while (str.charAt(0)=="0" && str.length>1) // if has leading zero (but not just "0")
      str= str.substring(1, str.length); // remove any leading zeros (otherwise assumes octal)
    if (s=="+" || s=="-")
      str= s+str;                        // restore sign
    return(parseInt(str));
  }
  else
  {
    alert("Attempt to take signedNumericValue of non-Numeric string "+str);
    return "NaN";
  }
}
function isAlphabetic(str)
// returns true if str is alphabetic
// that is only A-Z a-z or space
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if (  ('A'<=ch && ch<='Z')
        ||('a'<=ch && ch<='z')
        ||(ch==" ")
          )
      p++;
    else
      ok= false;
  }
  return ok;
}

function isAlphabeticPlus(str, plus)
// returns true if str is alphabetic with addition of characters in plus
// that is only A-Z a-z or space or any of the characters in the string plus
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if (  ('A'<=ch && ch<='Z')
        ||('a'<=ch && ch<='z')
        ||(ch==" ")
        ||(plus.indexOf(ch,0)>-1)
       )
      p++;
    else
      ok= false;
  }
  return ok;
}

function justify(str, width)
// adds spaces to front of str to make it width characters long
// does nothing if length of str is >= width
{
  alert("justify "+width+" "+str.length);
  while (str.length<width)
  {
    str= " "+str;
    alert("justify "+str.length);
  }

  return str;
}

