
// is_number() - returns true if inval is number, false otherwise
function is_number(inval)
{
   if (inval-0 == 0) return true;
   return (inval/inval == 1);
}

// split_it() - splits a string into pieces based on a delimiter; returns result as array.
function split_it(theString,delim)
{
   var parts = new Array(1);
   var piece;
   var arrayIndex=0; 
   var indexA = 0;
   var indexB = theString.indexOf(delim);
   while (indexB >= 0) {
      piece = theString.substring(indexA,indexB);
      if (piece != " " && piece != "") { parts[arrayIndex++] = piece; }
      indexA = ++indexB;   //search after delimiter found
      indexB = theString.indexOf(delim,indexA);
   }
   piece = theString.substring(indexA,theString.length);
   if (piece != " " && piece != "") { parts[arrayIndex] = piece }   
   return parts;
}

// stripSpaces() - strips leading spaces from a string
function strip_spaces(theString) {
   while (theString.substring(0,1) == ' ') theString = theString.substring(1);
   return theString;
}


// eval_num() - parses a fractional or whole number and returns it's decimal equivalent
//              Example: in = 10 1/2,  out = 10.5
function eval_num(inval)
{
   var numer, denom, whole_num;
   var parts = split_it(inval,"/");
   if (parts.length == 1) {     // not fraction
      if (is_number(parts[0])) { return inval; } else { return null };
   } 
   else {
      if (parts.length == 2) {   // fraction
         denom = parts[1];
         if (denom == "0") return null; // no division by zero please
         var sub_parts = split_it(parts[0]," ");  
         if (sub_parts.length > 2) { return null; }
         if (sub_parts.length == 1) {
            whole_num = 0;
            numer = sub_parts[0];
         }
         else { // 2 pieces (whole number and numerator of fraction)
            whole_num = sub_parts[0];
            numer = sub_parts[1];
         }
         if ( is_number(numer) && is_number(denom) && is_number(whole_num) ) { 
            return eval(whole_num) + eval(numer/denom);  }
      }
   }
   return null;  // if we made it here, must be bad number
}

// decimalize() - rounds a number to either 1 or 2 decimal places
function decimalize(amount,num_places) {
   if (num_places == 1) {
       amount = Math.round(amount*10)/10;
       return (amount == Math.floor(amount)) ? amount + '.0' : amount;
   } else {
       amount = Math.round(amount*100)/100;
       return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
   } 
   return amount; // none of the above
}


// check_number() - checks the value of a numeric field and returns error message if problem found
function check_number(field_name, field_val, min, max, opt_req_type)
{
   var msg = "";
   var myval;
   if (field_val == "") {
      if (opt_req_type == "REQ") {
         msg = "A value is required for " + field_name + ".\n";
      }
   } else {
      myval = eval_num(field_val);
      if ( myval == null ){
         msg = "Invalid value for " + field_name + ". Valid range is " + min + " to " + max + "\n";
      } else {
         if (eval(myval) < min || eval(myval) > max) {
            msg = "Invalid value for " + field_name + ". Valid range is " + min + " to " + max + "\n";
         }
      }
   }
   return msg;
}

