// Ars Magica Characteristics Calculator
// (c)2006 Pitt Murmann

names = new Array('strength', 'stamina', 'dexterity', 'quickness',
	'intelligence', 'perception', 'presence', 'communication');

function costsForValue(v)
{
	var costs = 0;
	for(var i = Math.abs(parseInt(v)); i > 0; i--) {
		costs = costs + i;
	}
	if(v < 0) {
		return(0 - costs);
	}
	else {
		return(costs);
	}
}


function getValue(item)
{
	var v = eval(item + '.value');
	var t = parseInt(v);
	if(t.toString() != v) {
		alert('Invalid value: ' + v + '!');
		return(0);
	}
	return(v);
}


function updateValues()
{
	remain = getValue('document.forms.stats.elements.initial');
	spent = 0;
	for(var i = 0; i < names.length; i++) {
		score = getValue('document.forms.stats.elements.' + names[i] + '_score');
		costs = costsForValue(score);
		eval('document.forms.stats.elements.' + names[i] + '_costs.value = costs');
		eval('document.forms.stats.elements.' + names[i] + '_total.value = parseInt(document.forms.stats.elements.' + names[i] + '_score.value) + parseInt(document.forms.stats.elements.' + names[i] + '_bonus.value)');
		spent = spent + costs;
	}
	document.forms.stats.elements.spent.value = spent;
	document.forms.stats.elements.remain.value = remain - spent;
}


function startOver()
{
	var i = getValue('document.forms.stats.elements.initial');
	document.forms.stats.elements.remain.value = i;
	document.forms.stats.elements.spent.value = 0;
	for(var i = 0; i < names.length; i++) {
		eval('document.forms.stats.elements.' + names[i] + '_score.value = 0');
		eval('document.forms.stats.elements.' + names[i] + '_costs.value = 0');
		eval('document.forms.stats.elements.' + names[i] + '_bonus.value = 0');
		eval('document.forms.stats.elements.' + names[i] + '_total.value = 0');
	}
}


function decreaseValue(item)
{
	var v = getValue('document.forms.stats.elements.' + item + '_score');
	eval('document.forms.stats.elements.' + item + '_score.value = parseInt(v) - 1');
	updateValues();
}


function increaseValue(item)
{
	var v = getValue('document.forms.stats.elements.' + item + '_score');
	eval('document.forms.stats.elements.' + item + '_score.value = parseInt(v) + 1');
	updateValues();
}
