// D20 basic ability score generator
// (c)2001-2003 Pitt Murmann

costs = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0,
	1, 2, 3, 4, 5, 6, 8, 10, 13, 16);
names = new Array('str', 'dex', 'con', 'iq', 'wis', 'cha');

function badchoice(which)
{
	which.value = "";
	alert("You cannot change this field!\n" +
	"It is automatically updated when changing the score.");
}

function startOver()
{
	v = document.forms.abilities.elements.initial.value;
	n = parseInt(v);
	if(n.toString() != v) {
		alert('Invalid value for initial amount of points: ' + v);
		which.focus();
		return(0);
	}
	document.forms.abilities.elements.remain.value = document.forms.abilities.elements.initial.value;
	document.forms.abilities.elements.spent.value = 0;
	for(n = 0; n < 6; n++) {
		eval('document.forms.abilities.elements.' + names[n] + '.value = 8');
		eval('document.forms.abilities.elements.' + names[n] + '_mod.value = -1');
		eval('document.forms.abilities.elements.' + names[n] + '_cp.value = 0');
	}
}

function setScore(which)
{
	v = which.value;
	n = parseInt(v);
	if(n.toString() != v) {
		alert('Invalid value: ' + v);
		which.focus();
		return(0);
	}
	if((v < 8) || (v > 18)) {
		alert('The entered value must be within the range of 8 and 18!');
		which.value = "";
		which.focus();
		return(0);
	}
	n = which.name;
	eval('document.forms.abilities.elements.' + n + '_mod.value = ' + Math.floor((v - 10) / 2));
	eval('document.forms.abilities.elements.' + n + '_cp.value = ' + costs[v]);
	document.forms.abilities.elements.spent.value =
		costs[document.forms.abilities.elements.str.value] +
		costs[document.forms.abilities.elements.dex.value] +
		costs[document.forms.abilities.elements.con.value] +
		costs[document.forms.abilities.elements.iq.value] +
		costs[document.forms.abilities.elements.wis.value] +
		costs[document.forms.abilities.elements.cha.value];
	document.forms.abilities.elements.remain.value =
		document.forms.abilities.elements.initial.value -
		document.forms.abilities.elements.spent.value;
}
