Calculator = {
  fields: {
    '1a': null,
    '1b': null,
    '1c': null,
    '1d': null,
    '1e': null,
    '2a': null,
    '2b': null,
    '3a': null,
    '4a': null,
    '4b': null,
    '4c': null,
    '4d': null,
    '4e': null,
    '4f': null,
    '4g': null,
    '4h': null,
    '5a': null,
    '5b': null,
    '5c': null
  },
  totals: {
    'v1': null,
    'v2': null,
    'v3': null,
    'v4': null,
    'v5': null    
  },
  load_quiz: function(){
    Object.keys(Calculator.fields).each(function(field){
      new Field.Observer(field, 0.3, function(){ 
        Calculator.validate_field(field);
        Calculator.update_total(); 
      });
    });
  },
  validate_field: function(f){
    var v = $(f).value;    
    if (v == ''){
      $('q'+f).className = 'question'
      Calculator.fields[f] = null;
      return false
    }
    else{
      var n = parseFloat(v);
      if (isNaN(n)){
        $('q'+f).className = 'question qerr';
        Calculator.fields[f] = -1;
        return false
      }   
      else{
        $('q'+f).className = 'question qval';
        Calculator.fields[f] = n;
        return true
      } 
    }  
  },
  update_total: function(){
    f = Calculator.fields;
    c = Calculator.totals;
    
    v1f = ['1a','1b','1c','1d','1e'];
    this.sub_total(v1f,'v1');
            
    v2f = ['2a','2b'];
    this.sub_total(v2f,'v2');
        
    v3f = ['3a'];
    this.sub_total(v3f,'v3');

    v4f = ['4a','4b','4c','4d','4e','4f','4g','4h'];
    this.sub_total(v4f,'v4');

    v5f = ['5a','5b','5c'];
    this.sub_total(v5f,'v5');
    
    fk = Object.keys(f);
    if(fk.any(function(v){ return f[v] == -1 })){
      
    }
    else if(fk.any(function(v){ return f[v] == null })){
    }
    else{
      total_minutes = (f['5a'] * f['5c']) * f['3a'];
      hours = Math.floor(total_minutes/60);
      minutes = total_minutes%60;
      time = hours + " Hours and " + minutes + " Minutes";
      $('final_time').update(time);
      
      total_cost = c['v3'] + c['v4'] + c['v5'];
      $('final_total').update(total_cost.Currency('$'));   
      
    }    
    /* rough * days per weeks * 51 /60 */
    /* f1,f2,f3 * 51, 750 + 1500 */
  },
  sub_total: function(fields,va){
    if(fields.any(function(v){ return f[v] == -1 })){
      $(va).update("The fields in red doesn't contain valid numbers");
      $(va).className = 'sub_total verr';
    }  
    else if(fields.any(function(v){ return f[v] == null })){
      $(va).update("Fill in all the blank fields");
      $(va).className ='sub_total';
    }  
    else{
      if (va == 'v1'){
        total = f['1a'] + f['1b'] + f['1c'];
        total = total / f['1e'];
        total = total * (f['1d']);
        Calculator.totals['v1'] = total;
        $(va).update(total.Currency('$'));        
      }
      else if (va == 'v2'){
        total = f['2a'] * f['2b'];
        Calculator.totals['v2'] = total;
        $(va).update(total.Currency('$'));
      }
      else if (va == 'v3'){
        total = (Calculator.totals['v1'] + Calculator.totals['v2']) * f['3a']
        Calculator.totals['v3'] = total
        $(va).update(total.Currency('$') + ' &mdash; Costs of These Habits Per Year');
      }
      else if (va == 'v4'){
        first_flight  = f['4a'] * f['4b'];
        second_flight = f['4c'] * f['4d'];
        hotel_costs   = f['4e'] * f['4f'];
        meal_costs    = f['4g'] * f['4h'];
        total = first_flight + second_flight + hotel_costs + meal_costs
        Calculator.totals['v4'] = total
        $(va).update(total.Currency('$') + ' &mdash; Costs of These Habits Per Year');
      }
      else if (va == 'v5'){
        per = f['5a'] / 0.6;
        per = (per / 100);
        total = f['5b'] * per;
        total = f['5c'] * total;
        total = total * 51;
        Calculator.totals['v5'] = total;
        $(va).update(total.Currency('$') + ' &mdash; The Cost of Your Traveling Time');
      }      
      $(va).className = 'sub_total vval';
    }    
  }
}       

Number.prototype.Currency = function(uom){ //for values < 16 digits
  var intNum = Math.floor(this).toString();
  var decNum = (Math.round((this - Math.floor(this))*100)/100).toString();
  //integer
  if (intNum.length> 3){
    var start = intNum.length % 3;
    if (start) var intNumStr=intNum.substr(0,start)+",";
    else var intNumStr="";
    for (var i=start; i<intNum.length-3; i+=3) intNumStr += intNum.substr(i,3) + ",";
    intNumStr += intNum.substr(-3,3);
  }else intNumStr = intNum;
  //decimal
  while (decNum.length <4) decNum += "0";
  decNum = decNum.substr(-2,2)
  //full
  return uom + intNumStr +"."+ decNum
}