var iOffer = iOffer ? iOffer : {}
iOffer.Email = iOffer.Email ? iOffer.Email : {}

iOffer.Email.PASS_IMAGE_SRC = '/images/icons/required_pass.gif';
iOffer.Email.FAIL_IMAGE_SRC = '/images/icons/required_fail.gif';

iOffer.Email.EmailValidator = Class.create({
    initialize : function(id, asterisk, options) {
        if (!options || options == undefined)
            options = {};

        this.input    = $(id);
        this.asterisk = $(asterisk);

        this.check_initial =
            ( options.checkInitial == undefined ? true : options.checkInitial );

        if (this.check_initial)
            this.validate();
        
        this.register_events();
    },

    register_events : function() {
        this.input.observe('keyup', this.validate.bind(this));
    },

    validate : function() {
        if (this.valid()) {
            this.asterisk.src = iOffer.Email.PASS_IMAGE_SRC;
        } else {
            this.asterisk.src = iOffer.Email.FAIL_IMAGE_SRC;
        }
    },
    
    valid : function() {
        var value = this.input.getValue();
        return (
            value.length > 0 &&
            value.match(/(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/i) );
    }
});
