/*
 * $Id:$
 *
 * This is a javascript file used for holding onto helper functionality
 * for this site.
 * 
 * For example, we make a Range(..) object so we can deal with the weight
 * ranges.
 */

/*
 * Just like prototype's  F$(...) function (http://www.prototypejs.org/api/utility#method-$f)
 * but works with radio buttons
 */
function $R(nodeSet) {
  for(var i = 0; i < nodeSet.length; i++) {
    if(nodeSet[i].checked) {
      return nodeSet[i].value;
    }
  }
  return false;
}

/*
 * This range object is a helper object to make reasoning about ranges
 * easier.
 *
 * A range is just constructed as a low/high value.
 */
function Range(low, high) {
  this.low = Math.min(low, high);
  this.high = Math.max(low, high);
}

function r(low, high) {
  return new Range(low, high);
}

/*
 * Provide a way of mapping a range object to a nice lookign string.
 * Because this is a local object, we can hard code it to deal in pounds.
 */
Range.prototype.toString = function() {
  return this.low + " to " + this.high + " lb";
};

/*
 * provide an easy way to create a new range by subtracting 
 * a constant from it
 */
Range.prototype.sub = function(val) {
  return new Range(val - this.high, val - this.low);
};

/*
 * Return true if the range is within the bounds. Useful for making 
 * decisions about where to send the user
 */
Range.prototype.between = function(lower, upper) {
  return this.low >= lower && this.low <= upper;
};

/*
 * Return true if the range represents a negative loss
 */
Range.prototype.isNeg = function() {
  return this.low < 0;
};

Range.prototype.crossesZero = function() {
  return this.low <= 0 && this.high >= 0;
};

/*
 * take the absolute value of the range, deals with negative numbers.
 */
Range.prototype.abs = function() {
  return new Range(Math.abs(this.low), Math.abs(this.high));
};
  

