// Check to be sure the first and last name fields are entered.
// Put up an alert box if they are not filled.
// Return true if fields are OK.  Otherwise, return false.
// As before, insert a value into the teacher field
//
function checkNames()	{

	df = document.forms["contactForm"];		// Use variable to provide shortcut
	df["teacher"].value = "Fitzgerald";		// set value of teacher

	if (df["firstName"].value == "")	{	// empty first name
		alert ("First name missing");
		df["firstName"].focus();
		return false;
	} else if (df["lastName"].value == "") {	// empty last name
		alert ("Last name missing");
		df["lastName"].focus();
		return false;
	} else
		return true;
}

// Call a series of validation or data manipulation functions.
function checkFields()	{
	checkNames();
	checkBoxes();
	checkRadioButtons();
	checkSelectBox();
	playWithStrings();
	return false;				// prevent fields from being cleared by returning false
}

//  Manipulate checkboxes.
//  Read a checkbox and displays its value in an alert box
//  Set a checkbox
function checkBoxes()	{
	if (document.forms["contactForm"]["assgn1"].checked)
		alert ( document.forms["contactForm"]["assgn1"].value );
	if (document.forms["contactForm"]["assgn2"].checked)
		alert ( document.forms["contactForm"]["assgn2"].value );	
	if (document.forms["contactForm"]["assgn3"].checked)
		alert ( document.forms["contactForm"]["assgn3"].value );

	document.forms["contactForm"]["assgn3"].checked = true;
}

// Only one radio button out of a group can be selected.
// Displays an alert box with the value of the chosen radio button
// or displays a message if no radio button has been chosen
function checkRadioButtons()	{
	radioGroup = document.forms["contactForm"]["grade"];	// the collection of radio buttons
	isChecked = false;
	
	for (i=0; i < radioGroup.length; i++)	{
		if ( radioGroup[i].checked )	{
			alert ( radioGroup[i].value );
			isChecked = true;
		}
	}
	
	if (!isChecked)
		alert ("No grade chosen");
		
	// Change the grade to an A
	radioGroup[0].checked = true;
}

// Checks to be sure user chose an option other than "none"
// Alert box shows value of option chosen
function checkSelectBox()	{
	if (document.forms["contactForm"]["course"].value == "none")	{
		alert ("Must choose a course");
		document.forms["contactForm"]["course"].selectedIndex = 1;		// set course to ICS 225
																		// indexing starts at 0
	}	else	
		alert ( document.forms["contactForm"]["course"].value );
}

function playWithStrings()	{
	// create local copy of comments
	comments = document.forms["contactForm"]["comments"].value;
	
	// don't process string if it is empty
	if (comments.length >0)	{
		// eliminate case sensitivity by converting to all lowercase
		comments = comments.toLowerCase();
		if (comments.indexOf('good')!=-1  ||
			comments.indexOf('excellent')!=-1 ||
			comments.indexOf('fun')!=-1 ||
			comments.indexOf('great')!=-1)
				alert ('Good course');
		else if (comments.indexOf('bad')!=-1)
				alert ('Bad course');
	}
}
