//form.js

window.onload = function()
{
	var commentform = document.getElementById( "commentform" );
	commentform.onsubmit = function()
	{
		//console.log( this );

		var nameField = document.getElementById( "commentauthor" );
		var emailField = document.getElementById( "commentemail" );
		var commentField = document.getElementById( "comment" );

		//console.log( "name: " + nameField + "\n\t" + nameField.value );
		//console.log( "email: " + emailField + "\n\t" + emailField.value );
		//console.log( "comment: " + commentField + "\n\t" + commentField.value );

		var valid = true;
		var invalidColor = "#FF0000";
		var validColor = "#2E3940";
		var alertMessage = "The following required fields are invalid:";
		
		if( "" == nameField.value )
		{
			alertMessage += "\n\tName";
			invalidateField( nameField, invalidColor, validColor );
			valid = false;
		};

		if( "" == emailField.value || -1 == emailField.value.indexOf( "@" ) || -1 == emailField.value.indexOf( "." ) )
		{
			alertMessage += "\n\tEmail";
			invalidateField( emailField, invalidColor, validColor );
			valid = false;
		}
		
		if( "" == commentField.value )
		{
			alertMessage += "\n\tComment";
			invalidateField( commentField, invalidColor, validColor );
			valid = false;
		}
		

		if( ! valid ) alert( alertMessage );

		return valid;
	}
 }

function invalidateField( elField, invalidColor, normalColor )
{
	//console.log( "invalidateField: " + "\n\t" + elField + "\n\t" + invalidColor + "\n\t" + normalColor );
	elField.style.borderColor = invalidColor;
	elField.onfocus = function()
	{
		//console.log( "focus: " + this );
		this.style.borderColor = normalColor;
		this.onfocus = null;
	}
 }

