Element.extend({
		duration: 400,
		fadeIn: function (obj) {
			var currentOpacity = this.getStyle('opacity');
			this.setStyles({visibility: 'visible', display: 'block'});
			//if (obj) this.injectInside(obj);
			var myEffects = new fx.Style(this, 'opacity', {duration: this.duration, transition: Fx.Transitions.quintIn});
			myEffects.custom(Number(currentOpacity), 1);
		},
		fadeOut: function (duration) {
			var myEffects = new fx.Style(this, 'opacity', {onComplete: function (el) {el.remove();}, duration: this.duration, transition: Fx.Transitions.quintOut});
			myEffects.custom(1, 0);
		}
	});

var Form = new Class({
	url: '',
	initialize: function(form){

		if (!form) return;
		this.form = form;
		this.form.addEvent('submit', this.submitRequest.bind(this));
		$('email_txt').addEvent('focus', function () { this.value = ''; });
		$('email_txt').addEvent('keyup', this.clearError );
		
		this.spinner = new Element('img');
		this.spinner.setProperty('src', 'images/indicator_arrows.gif');
    },
	validate: function () {
		var validData = new Object();
		var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,2}$/;
		return emailRegxp.test(this.form.email_txt.value);
	},
	submitRequest: function (e) {
		Window.stopEvent(e);
		if ($('error')) return;
		if (this.validate()) {
			var url = this.url + 'scripts/php/email.ajax.php';
			new Ajax(url, {postBody: 'email_txt=' + this.form.email_txt.value, onComplete: this.response.bind(this)}).request();
			$('emailFormContainer').fadeOut();
		} else {
			this.showError();
		}
	},
	clearError: function () {
		if ($('error')) {
			$('error').remove();
			var p = new Element('p');
			p.setProperty('id','replace');
			p.setHTML('Please submit your email address and recieve special updates.');
			p.injectBefore($('emailForm'));
		}
	},
	showError: function (error) {
		
		var p = new Element('p');
		p.setProperty('id', 'error');
		p.setHTML('Please enter a valid email address.')
		p.setStyle('opacity', 0);
		p.injectBefore($('emailForm'));
		
		$('replace').remove();
		
		p.fadeIn(null);
				
		var tempValue = $('email_txt').value;
		$('email_txt').focus();
		$('email_txt').value = tempValue;
		$('email_txt').select();
	},
	response: function (obj) {
		var p = new Element('p');
		p.setHTML('Thank you!<br />');
		p.setStyle('opacity', 0);
		p.injectBefore($('disclaimer'));
		p.fadeIn(null);
	}
});