Object.merge(DOM, {

  // get the value of a form element
  getValue : function ( elm )
  {
    elm = DOM.$(elm);
    if (elm && elm.type)
    {
      switch (elm.type)
      {
        case 'radio':
          return DOM.getRadioValue( elm );
          break;
        case 'checkbox':
          return (elm.checked) ? elm.value : '';
          break;
        case 'select-one':
          return DOM.getOptionValue( elm.options[elm.selectedIndex] );
          break;
        // case 'select-multiple':
          // break;
        default:
          return elm.value;
      }
    }
  },


  getRadioValue : function ( elm )
  {
    elm = DOM.$( elm );
    var form = DOM.parent('form'),
        name = elm.name,
        rdos = DOM.get('input[type=radio]', form);
    for (var i=0,l=rdos.length; i<l; i++)
    {
      if (rdos[i].name == name  &&  rdos[i].checked)
      {
        return rdos[i].value;
      }
    }
  }



});



/* global */ isafoldCalculatorPrototype = {


  _numFormat: function (num, places, decimal, comma) {

    decimal = decimal || '.';
    comma   = comma   || ',';

    // Type checking
    if(num == null  ||  (typeof num != "number")  &&  num.constructor != Number) {
      return null;
    }

    var _neg = (num < 0) ? '-' : '',
        _number = Math.abs( num.toFixed(0) ).toString();

    if (!places) {
      // make something reasonable up
      places = (num.toPrecision().split('.')[1] || '').length;
      places = (places > 8) ? 8 : places; // max 8
    }
    var _fraction = (num.toFixed(places).split('.')[1] || ''),
        _s = [];

    for (i=_number.length; i>0; i-=3) { _s.push(_number.substring(i-3, i)); }

    return  _neg + _s.reverse().join(decimal) + (_fraction ? comma+_fraction : '');
  },



  // enter key allowed one : button|textarea|submit
  _defangEnterkey: function (e) {
    if (e.keyCode == 13) {
      var n = e.target;
      if (n && /^(text|radio|checkbox|password|file|hidden|select\-(one|multiple))$/.test(n.type || '')) {
        return false;
      }
    }
  },





  _handleNavButtonClick: function (e)
  {
    var m = '';
    if (DOM.hasClass(this, 'back') || DOM.hasClass(this.parentNode, 'back')) {
      m = 'elmBefore';
    }
    else if (DOM.hasClass(this, 'forward') || DOM.hasClass(this.parentNode, 'forward')) {
      m = 'elmAfter';
    }
    // find next/prev fieldset
    var fs = DOM.parent(this, 'fieldset');
    if (m && fs) {
      var tf = DOM[m](fs, 'fieldset');
      // prev fieldset is a tabpane?... let's switch to that
      if (tf && DOM.hasClass(fs, 'tabpane') && tf.id) {
        tabSwitcher.switchTo(tf.id);
      }
    }
    return false;
  },





  _switchCurrency: function ( e ) {
    var target = e.target;
    if (target && target.tagName == 'A')
    {
      if (target != this.currentCurrencyElm)
      {
        DOM.removeClass(this.currentCurrencyElm, 'current');
        DOM.addClass(target, 'current');
        this.currentCurrencyElm = target;
        this.currentCurrency = target.id.substr(3);
        this.currentCurrencySymb = target.innerHTML;
        this.calculate();
      }
    }
    return false;
  },




  _displayTotalPrice: function(price, extras)
  {
    var fullPrice = '-',
        est          = '-',
        estShown     = '-',
        extraCost  = 0,
        rentalCost  = 0;
    if (!isNaN(price))
    {
      var lang = DOM.getLang() || 'en',
          demille  = (lang == 'en') ? ',' : '.',
          point    = (lang == 'en') ? '.' : ',',
          currencies = this.dataset.currency;

      rentalCost = Math.floor( price / (currencies[this.currentCurrency] || currencies.eur) ); // to selected currency
      extraCost = Math.floor( extras / (currencies[this.currentCurrency] || currencies.eur) ); // to selected currency
      
      fullPrice = rentalCost + extraCost;
      
      rentalCost = this._numFormat( rentalCost, 0, demille, point );
      extraCost = this._numFormat( extraCost, 0, demille, point );
      fullPrice = this._numFormat( fullPrice, 0, demille, point );

      est  = Math.floor( price ) + ' isk';
      estShown = fullPrice + ' ' + this.currentCurrency;

    }
    DOM.$('tc_total').innerHTML      = fullPrice;
    DOM.$('ex_total').innerHTML      = extraCost;
    DOM.$('re_total').innerHTML      = rentalCost;
    DOM.$('costestimate').value      = est;
    DOM.$('costestimateshown').value = estShown;
  },




  _defaultPreInit: function ()
  {
    // defang enter-submit
    EEvent.add( this._theForm, "keypress", this._defangEnterkey );

    // currency selector
    var currc = this.priceElm = DOM.$('currency');
    if (currc)
    {
      var selC = DOM.get('a.current', currc)[0];
      this.currentCurrencyElm = selC;
      this.currentCurrency = ( selC &&  selC.id.substr(3) );
      EEvent.add( currc, 'click', this._switchCurrency, this );
    }

    // hook paging buttons
    var _navButtons = DOM.get('div.calculator input[type=button]'),
        i = _navButtons.length;
    while (i--) 
    {
      EEvent.add(_navButtons[i], 'click', this._handleNavButtonClick);
    }

  },


  _defaultPostInit: function ()
  {
    // calculate something to render the estimate block
    var _this = this;
    setTimeout(function(){ _this.calculate(); }, 10);

    // display the calculator
    var c = DOM.get( 'div.calculator div.boxbody' )[0];
    if (c) {  c.style.display = '';  }

  }





};



