// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// xp/control.js
//
// copyright (c) 2006-2010 drow <drow@bin.sh>
// all rights reserved.

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// configuration

var form = 'xp_form';

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// initialize form

function init_form () {
  var form_elem = $(form).elements;
  var xp_prefs = get_cookie('xp') || {};

  if (value = xp_prefs.n_pc) setv(form_elem['n_pc'],value);
  if (value = xp_prefs.pc_level) setv(form_elem['pc_level'],value);

  calc_xp();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// set field

function set_field (field) {
  save_prefs('xp',field); calc_xp();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// calculate xp

function calc_xp () {
  var form_elem = $(form).elements;

  var pc_l; if (pc_l = parseInt(selv(form_elem['pc_level']))) {
    var n = parseInt(form_elem['enc_n'].value);
    var xp = 0;

    for (i = 0; i < n; i++) { j = i + 1;
      if (cr = selv(form_elem['cr.'+j])) {
        var n_x = parseInt(form_elem['n.'+j].value) || 1;
        var x = get_xp(pc_l,cr);

        xp += (n_x * x);
      }
    }
    form_elem['total_xp'].value = xp;

    var n_pc; if (n_pc = parseInt(selv(form_elem['n_pc']))) {
      var per_pc = Math.floor(xp / n_pc);
      form_elem['xp_per_pc'].value = per_pc;
    } else {
      form_elem['xp_per_pc'].value = '-';
    }
  } else {
    form_elem['total_xp'].value = '-';
    form_elem['xp_per_pc'].value = '-';
  }
  return;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// showtime

document.observe('dom:loaded',init_form);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

