var anStats = new Array();
anStats[0] = 8;
anStats[1] = 9;
anStats[2] = 10;
anStats[3] = 11;
anStats[4] = 12;
anStats[5] = 13;
anStats[6] = 14;
anStats[7] = 15;
anStats[8] = 16;
anStats[9] = 17;
anStats[10] = 18;

var astrBonuses = new Array();
astrBonuses[0] = "-1";
astrBonuses[1] = "-1";
astrBonuses[2] = "0";
astrBonuses[3] = "0";
astrBonuses[4] = "+1";
astrBonuses[5] = "+1";
astrBonuses[6] = "+2";
astrBonuses[7] = "+2";
astrBonuses[8] = "+3";
astrBonuses[9] = "+3";
astrBonuses[10] = "+4";


function SetOptions()
{
	var nMax = document.getElementById("inputMax").value - 0;
	var nTotal = 0;

	// Calculate current point total
	var aSelects = document.getElementsByTagName("select");
	for (i = 0; i < aSelects.length; i++)
	{
		var elSel = aSelects[i];
		var selIndex = elSel.selectedIndex;
		if ( selIndex < 0 ) selIndex = 2; // Default:10
		var nCurrPoints = anPointCosts[selIndex];
		nTotal += nCurrPoints;
	}

	var nPointsAvailable = nMax - nTotal;

	// Add options to selects
	for (i = 0; i < aSelects.length; i++)
	{
		var elSel = aSelects[i];
		var selIndex = elSel.selectedIndex;
		nOptIndex = selIndex+1;
		if ( selIndex < 0 )
		{
			selIndex = 2; // Default:10
		}

		// Remove all options above selection
		while (elSel.length > selIndex+1)
		{
			elSel.remove(elSel.length - 1);
		}

		// Re-add options
		var nCurrPoints = anPointCosts[selIndex];
		for (; nOptIndex < 11; nOptIndex++)
		{
			if ( anPointCosts[nOptIndex] <= (nCurrPoints + nPointsAvailable) )
			{
				AddOption(elSel, nOptIndex);
			}
			else
			{
				break;
			}
		}

		// Set current bonus
		elSel.parentNode.parentNode.getElementsByTagName("td")[2].innerHTML = astrBonuses[selIndex];

		// Set current points
		elSel.parentNode.parentNode.getElementsByTagName("td")[3].innerHTML = nCurrPoints;

		// Reset current selection
		elSel.selectedIndex = selIndex;
	}

	document.getElementById("spanTotal").firstChild.nodeValue = nTotal;
}


function AddOption(elSel, nIndex)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = anStats[nIndex];

	try
	{
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex)
	{
		elSel.add(elOptNew); // IE only
	}
}