
var EmailSignupForm = Class.create(
{
	element: null,
	
	dom: {},
	
	defaultValue: "",
	
	initialize: function( element )
	{
		this.element = $( element );
		
		this.initDOM();
		this.initOther();
		this.initEvents();
	},
	
	initDOM: function()
	{
		this.dom = {};
		this.dom.emailInput = $( 'email-signup' );
		this.dom.submitButton = $( 'email-signup-submit' );
	},
	
	initOther: function()
	{
		this.defaultValue = this.dom.emailInput.getAttribute( 'value' );
	},
	
	initEvents: function()
	{
		this.element.observe( "submit", this.handleSubmit.bind( this ) );
		this.dom.submitButton.observe( "click", this.handleClickSubmit.bind( this ) );
	},
	
	handleSubmit: function( e )
	{
		Event.stop( e );
		
		var emailAddress = $F( this.dom.emailInput );
		
		if( emailAddress == "" || emailAddress == this.defaultValue )
		{
			this.dom.emailInput.focus();
			alert( "Please enter an e-mail address" );
			return;
		}
		
		var emailFilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		
		if( !emailFilter.test( emailAddress ) )
		{
			this.dom.emailInput.focus();
			alert( "Please enter a VALID e-mail address\n`" + emailAddress + "` is not valid" );
			return;
		}
			
		this.element.submit();
	},
	
	handleClickSubmit: function( e )
	{
		this.handleSubmit( e );
	}
});

document.observe( "dom:loaded", function(){ new EmailSignupForm( "email-signup-form" ) } );