<!--

function stringMatches( aString, validChars )
{
	var matches = true;
	for ( i = 0; i < aString.length; i++)
	{
		if ( ! charMatches( aString.charAt(i), validChars ) )
		{
			matches = false;
			break;
		}
	}
	return matches;
}

function charMatches( character, validChars )
{
	var matches2 = false;

	for ( j = 0; j < validChars.length; j++ )
	{
		if (character == validChars.charAt(j))
		{
			matches2 = true;
			break;
		}
	}
	return matches2;
}

function convertBase( form )
{
	var base;
	var input = form.textInput.value;
	var output = -1;
	var validInput = true;

	if (form.selectBase.selectedIndex==0)
		base=2;
	else if (form.selectBase.selectedIndex==1)
		base=8;
	else if (form.selectBase.selectedIndex==2)
		base=10;
	else
		base=16;

	// begin validation.
	// I would just use the parseInt function to validate, but
	// It does not return NaN when I pass in 1234 and base 2.
	// Because of the bad implementation of parseInt,
	// I have to parse it myself.
	var inputString = new String(input);

	if ( inputString == "" )
	{
		alert("Please enter a number in base " + base + ".");
		validInput = false;
	}
	else
	{
		if (base == 2 && ! stringMatches( inputString, "01" ) )
		{
			alert("Invalid Base 2 Number.\nYou must enter only 1\'s and 0\'s.");
			validInput = false;
		}
		if (base == 8 && ! stringMatches( inputString, "01234567" ) )
		{
			alert("Invalid Base 8 Number.\nYou must enter digits between 0 and 7.");
			validInput = false;
		}
		if (base == 10 && ! stringMatches( inputString, "0123456789" ) )
		{
			alert("Invalid Base 8 Number.\nYou must enter digits between 0 and 9.");
			validInput = false;
		}
		if (base == 16 && ! stringMatches( inputString, "0123456789ABCDEFabcdef" ) )
		{
			alert("Invalid Base 8 Number.\nYou must enter digits between 0 and 9 and A through B.");
			validInput = false;
		}
	}
	// end validation.

	if ( validInput )
	{
		form.textOutput.value = parseInt( input, base );
	}
	else
	{
		form.textInput.value = "";
		form.textOutput.value = "";
	}

}
// -->