
try{ window.attachEvent( "onload", setValidation );} catch(e){}

try{ window.addEventListener( "load", setValidation, false )} catch(e){}


function setValidation()
{
	try
	{
		$("howAreWeDoing").addEventListener( "submit", checkForm, false );	
	}
	catch(e){}
	
	try
	{
		$("howAreWeDoing").attachEvent( "onsubmit", checkForm );
	}
	catch(e){}
	
}	// end setValidation function

function checkForm(e)
{
	
	e = e || window.event;
	var formUsed;
	
	try{ formUsed = e.srcElement.getAttribute('id'); } catch(e){}
	try{ formUsed = this.getAttribute('id'); } catch(e){}
	
	var validationInfo = formUsed + 'ValidationInfo';
	var submitButton = formUsed + 'Submit';
	
	var nameSubmitted = $(formUsed).firstName.value;
	var emailSubmitted = $(formUsed).email.value;
	var commSubmitted = $(formUsed).community.value;
	
	$(validationInfo).innerHTML = "";
	
	var nameCheck = /^\D/; // regExp for date
	var emailCheck = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;

	if( ( nameSubmitted == '' ) )
	{
		stopFormSubmission(e);
		$(formUsed).firstName.style.backgroundColor = "yellow";
		validationText = "Please enter a First Name";
		setValidationText(validationInfo, validationText);
	}
	if( nameSubmitted != '')
	{
		if( nameCheck.test( nameSubmitted ) != true )
		{
			stopFormSubmission(e);
			$(formUsed).firstName.style.backgroundColor = "yellow";
			validationText = "Names may only contain letters.";	
			setValidationText(validationInfo, validationText);
		}
	}
	if( emailSubmitted == '')
	{
		$(formUsed).email.style.backgroundColor = "";
	}
	if( emailSubmitted != '' )
	{		
		if( emailCheck.test(emailSubmitted) != true )
		{
			stopFormSubmission(e);
			$(formUsed).email.style.backgroundColor = "yellow";
			validationText = "Please enter a valid e-mail address (e.g. sample@sample.com).";	
			setValidationText(validationInfo, validationText);
		}
	}
	if( commSubmitted == "noneSelected" )
	{
		stopFormSubmission(e);
		$(formUsed).community.style.backgroundColor = "yellow";
		validationText = "Please select the community nearest to you.";
		setValidationText(validationInfo, validationText);
	}
}
function stopFormSubmission(e)
{
	try{ event.returnValue = false; } catch(error){}
	try{ e.preventDefault(); } catch(error){}
}
function setValidationText( element, text )
{
	$(element).style.display = "block";
	$(element).innerHTML += "<br />" + text;	
}

