window.onload = init;

function init() 
{
	//#check methods supported
	if(!document.getElementById || !document.createElement || !document.createTextNode)
	{
		return;	
	}
	//#locate mailing list form, or quit if not found
	var formSubscribe = document.getElementById('formSubscribe');
	if(!formSubscribe)
	{
		return;	
	}
	//#attach validation to mailing list form
	formSubscribe.onsubmit = function () {
			var ok = validSubscribe(this);
			//#if mailing list form did not validate
			if(!ok)
			{
				//#check error message is not already there
				if(!document.getElementById('errSubscribe'))
				{
					//#create error message and add it to form
					error = document.createElement('p');
					error.id = 'errSubscribe';
					errText = document.createTextNode('Please enter a valid email address');
					error.appendChild(errText);	
					this.appendChild(error);
				}
			}
			return ok;
		}
}

function validSubscribe(form)
{
	return form.email.value!=null && form.email.value != '' && validEmail(form.email.value);
}

//#email validator based on w3schools.com example
function validEmail(value)
{
	var apos=value.indexOf('@');
	var dotpos=value.lastIndexOf('.');
	return apos>=1 && dotpos-apos>=2; 
}