/**
 * Takes care of the newsletter validation and of showing the erros on a LightBox panel
 * @author José Fernández Alameda
 * @date 2010-11-30
 */

var NewsletterValidation = Class.create({
	
	/**
	 * Contains a reference to the form
	 */
	form: null,
	
	/**
	 * Class constructor
	 * @param formId The form Identificator 
	 */
	initialize: function(formId,text) {
		this.form = $(formId);
		this.form.observe("submit",this.performValidation.bind(this));
		this.text = text;
	},
	
	/**
	 * Performs the form validation
	 */
	performValidation: function(event) {
		var mail = this.form.down("input").getValue();
		if(this.validateEmail(mail)) {
			this.formSubmit();
		}
		else {
			event.stop();
			this.displayValidationError();
		}
	},
	
	/**
	 * Validates a mail address
	 * @param elementValue
	 */
	validateEmail: function(elementValue){  
       var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
       return emailPattern.test(elementValue);  
    },

	/**
	 * Submits the form
	 */
	formSubmit: function() {
		this.form.submit();
	},
	
	/**
	 * Displays the form validation error
	 */
	displayValidationError: function() {
		var element = this.text;
		LightBoxInstance.startText(element,400);
	}
});
