/**
 * Class mooTotalValueCalculator
 * Calculates the total value in a form with user-filled amounts
 * 
 * version: 0.3
 * copyright 2008 06 16 - Huug Helmink, Ace Group bv
 *
 * requires mootools 1.2 core
 * - Core(Core, Browser)
 * - Native(Array, Function, Number, String, Hash, Event)
 * - Class(Class, Class.Extras)
 * - Element(Element, Element.Event)
 * - Utilities(Selectors,DomReady)
 */
var mooTotalValueCalculator = new Class({
  Implements: Options,
  options: {
    totalString: 'Total',
    unitSign: '&euro;',
    unitBeforeTotal: true
  },

  /**
   * Initialize a mooTotalValueCalculator a new object
   * 
   * @param itemTable           A single element or array of elements which contains all items
   * @param totalValueContainer An element to display the total sum of all items
   * @param options             An object with optional attributes
   */
  initialize: function(itemTable,totalValueContainer,options) {
    this.setOptions(options);
    
    this.TotalValue = 0;
    this.Amount = '';
    this.valueToCalc = '';
    this.itemsTable = Array.from(itemTable);
    this.valueToCalcContainer = totalValueContainer;
  },
  
  /**
   * Calculate the total sum of all items by amount
   *
   * @param t An element which value has the amount
   */
  CalcTotal: function(t) {
    var newContent;
    var am = t.get('value');
    am = this.GetOnlyInts(am);
    t.set('value',am);
    
    this.TotalValue = 0;
    this.itemsTable.each(function(tbl) {
      this.GetTotal(tbl);
    },this);
    
    if(this.options.unitBeforeTotal == true) {
      newContent = '<strong>' + this.options.totalString + '</strong> ' + this.options.unitSign + ' <span id="mooTotalValue">' + this.TotalValue.toFixed(2).toString().replace(/\./, ',') + '</span>';
    } else {
      newContent = '<strong>' + this.options.totalString + '</strong> <span id="mooTotalValue">' + this.TotalValue.toFixed(2).toString().replace(/\./, ',') + '</span> ' + this.options.unitSign;
    }
    this.valueToCalcContainer.set('html',newContent);
  },
  
  /**
   * Get the total value by this amount
   * 
   * @param element An element to calculate
   */
  GetTotal: function(element) {
    element.getElements('tr').each(function(child) {
      this.Amount = '';
      this.valueToCalc = '';

      if(child.getElement('[title=valueToCalc]') && child.getElement('[title=amount]')) {
        this.valueToCalc = child.getElement('[title=valueToCalc]').get('html');
        this.Amount = child.getElement('[title=amount]').get('value');

        this.TotalValue += this.GetMultiplicity(this.Amount,this.valueToCalc);
      }
    },this);
  },
  
  /**
   * Only allow numeric values.
   * When a non-numeric character is entered, it will be deleted
   *
   * @param amount  The amount entered by the user
   * @return        The new amount without non-numeric characters
   */
  GetOnlyInts: function(amount) {
    var numbers = new RegExp('[0-9]');
    var newAmount = '';

    if (amount.test(numbers) != false) newAmount = amount.toInt();
    return newAmount;
  },

  /**
   * Get the multiplicity of a value by an amount
   *
   * @param amount The amount entered by the user
   * @param value  The value on wich the amount must be multyplied
   * @return       The multyplicity of the value by the amount
   */
  GetMultiplicity: function(amount,value) {
    var mp = 0.00;
    
    if ((amount != '') && (value != '')) {
      var myAmount = amount.toInt();
      value = value.replace(',','.');
      var myValue = value.toFloat();
      mp = myValue * myAmount;
    }
    
    return mp;
  }
});
