//This is the Javascript file for checking the form replies.
//If you (ill-advisedly) mess around with it too much and blow it up,
//you can contact its creator, Alex Zaslavsky, at azaslavsky@berkeley.edu
//However, it would probably be much wiser to leave the code un-blown up to begin with.
//Be careful.

//This first function shows/hides the div for the catalog mailing form when users click the button.
function toggle(id)
{
	div = document.getElementById(id);
	if (div.style.display == "none")
	{
		div.style.display = "block";
	}
	else if (div.style.display == "block")
	{
		div.style.display = "none";
	}
}

//Now, for the goodies: the functions that test the fields for errors!

/*Begin Error Checker*/
//This first function is merely an archive of all the other error-checking functions.
//If you write a new error-checking function, make sure to add it to this list, or it won't run!
function testFields()
{
	//This is the array of error-checking functions.
	//Again, if you make a new function, PUT IT IN THIS ARRAY!
	var error = new Array(5);
		error[0] = checkField("lastName", "Last Name");
		error[1] = checkField("address1", "Address (line 1)");
		error[2] = checkField("city", "City");
		error[3] = checkState();
		error[4] = checkZip();
		error[5] = checkEmail();

	//Answer is a variable that records whether or not there are errors.
	//If an error is recored at any point, answer will change to 1, and the form won't submit.
	var answer = 0;

	//This is a foreach loop.
	//It runs through every value in the "error" array, and, if it fins that an error has occurred, does three things:
	//1) It sets this functions return value to false, so that the form does not submit,
	//2) It sets the answer value to 1, so that the function does not return true, and
	//3) It ends the foreach loop.
		for (k in error)
		{
			if (error[k] != 1)
			{
				alert (error[k]);
				return false;
				answer = 1;
				break;
			}
		}
	
	//If, at this point, we have not found an errors (aka, the answer variable is still 0)
	//we can return true, allowing the form to submit.
		if (answer == 0)
		{
			return true;
		}
}

//This function checks if our text inputs were done as required.
function checkField(id, field)
{
	//Pretty simple.  If the value's length is more than zero, we're cool.
	if (document.getElementById(id).value.length > 0)
	{
		return 1;
	}

	//If not, let's see that error!
	else
	{
		return "You have not filled in the " + field + " field!";
	}
}

//This function checks whether or not a state was selected.
function checkState()
{
	//If the value isn't zero, we're alright.
	if (document.getElementById("state").value != 0)
	{
		return 1;
	}

	//If not, let's see that error!
	else
	{
		return "You have not selected a State!";
	}
}

//This function tests if we have a proper zip code.
function checkZip()
{
	zip = document.getElementById("zip");
	//"If the zip code's has 5 characters,
	//or has ten characters and a dash is the sixth character, we are okay."
	if ( zip.value.length == 5 || ( zip.value.length == 10 && zip.value.charat(6) == "-" ) )
	{
		return 1;
	}

	//If not, error!
	else
	{
		return "You have not entered a valid Zip Code!";
	}
}

//This function checks if a valid email has been entered in the email field.
function checkEmail()
{
	email = document.getElementById("email");
	//All emails have at least 6 characters, so the string must be at least that long.
	//Also, all emails have the @ symbol and a period, so those must exist as well.
	//First, if they ask us a question or leave a comment, we need an email.
	if ( document.getElementById("comments").value.length > 0)
	{
		if (email.value.length > 5 && email.value.IndexOf("@") != -1 && email.value.IndexOf(".") != -1 )
		{
			return 1;
		}
		else
		{
			return "If you would like a response to your questions/comments, you must enter a valid email!";
		}
	}

	//But what if they merely left an email with no comments?  Let's check to make sure its a valid one.
	else if ( document.getElementById("comments").value.length == 0 && email.value.length > 0)
	{
		if (email.value.length > 5 && email.value.IndexOf("@") != -1 && email.value.IndexOf(".") != -1 )
		{
			return 1;
		}
		else
		{
			return "The Email you entered is not valid!";
		}
	}
	
	//No email AND no comments?  That's cool.
	else
	{
		return 1;
	}
}

/*End Error Checker*/


/*THIS IS A BROKEN FUNCTION - FIX AT YOUR OWN RISK.

This function checks if at least one of the grade level options has been selected.
function checkGrade()
{
	//Let's create a variable that will determine whether or not one of the grade levels was selected.
	//If "checked" is still 0 by the end of the function, then the answer if no.
	var checked = 0;

	//Here, we do a for loop to test whether or not each grade level has been checked.
	//Once we run into one where that has indeed been checked we can end the for loop
	//and set "checked" to 1.
	for (i=1; i=3; i++)
	{
		id = "gradeLevel_" + i;
		alert(id);
		if (document.getElementById(id).checked)
		{
			checked = 1;
			return 1;
			break;
		}
	}

	//If checked is still zero, no grade level has been checked, and we return an error.
	if (checked == 0)
	{
		alert ("You have not checked a Grade Level!");
		return 0;
	}
}
*/