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

names = new Array('cr', 'in', 'mu', 'pe', 're',
	'an', 'aq', 'au', 'co', 'he', 'ig', 'im', 'me', 'te', 'vi');

function askValue(message)
{
	if(v = prompt(message, "120")) {
		var t = parseInt(v);
		if((t.toString() == v) && (t > 0)) {
			return(v);
		}
	}
	return(false);
}


function costsForValue(v)
{
	var costs = 0;
	for(var i = Math.abs(parseInt(v)); i > 0; i--) {
		costs = costs + i;
	}
	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);
		base = getValue('document.forms.stats.elements.' + names[i] + '_base');
		costs = costs - costsForValue(base);
		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 = document.forms.stats.elements.' + names[i] + '_base.value');
		eval('document.forms.stats.elements.' + names[i] + '_costs.value = 0');
		eval('document.forms.stats.elements.' + names[i] + '_total.value = parseInt(document.forms.stats.elements.' + names[i] + '_base.value) + parseInt(document.forms.stats.elements.' + names[i] + '_bonus.value)');
	}
}


function currentToBase()
{
	document.forms.stats.elements.remain.value = getValue('document.forms.stats.elements.initial');
	document.forms.stats.elements.spent.value = 0;
	for(var i = 0; i < names.length; i++) {
		eval('document.forms.stats.elements.' + names[i] + '_base.value = document.forms.stats.elements.' + names[i] + '_score.value');
		eval('document.forms.stats.elements.' + names[i] + '_costs.value = 0');
	}
	document.forms.stats.elements.ticker.value = 'New development period with ' + document.forms.stats.elements.remain.value + ' points to spend';
}


function decreaseValue(item)
{
	var v = getValue('document.forms.stats.elements.' + item + '_score');
	if(v > 0) {
		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();
}
