﻿Type.registerNamespace("dotEditor.EmailMarketing.NewsletterSignup");
dotEditor.EmailMarketing.NewsletterSignup = $.Class.extend({
    init: function () {
        this.TextBox = arguments[0];
        this.Submit = arguments[1];
        this.NewsletterAddressBook = arguments[2];
        this.FormPlaceholder = arguments[3];
        this.ThankyouPlaceholder = arguments[4];

        this.Submit.bind('click', this.ValidateData.bind(this));
    },
    ValidateData: function () {
        var error = '';
        var emailAddress = '';
        var PostedAddresses = new Array();
        emailAddress = this.TextBox.val()
        if (!emailAddress.match(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)) {
            error += '\'' + emailAddress + '\' is not a valid email address\n';
        } else {
            PostedAddresses.push(emailAddress);
        }

        if (error == '') {
            dotEditor.EmailMarketing.Service.Competition.AddAddressesToDotMailer(this.NewsletterAddressBook, [emailAddress]);
            this.Thankyou();
        } else {
            alert(error);
        }
        return false;
    },
    Thankyou: function () {
        this.FormPlaceholder.hide();
        this.ThankyouPlaceholder.show();

        window.setTimeout(this.Reset.bind(this), 5000);
    },
    Reset: function () {
        this.ThankyouPlaceholder.hide();
        this.FormPlaceholder.show();
    }
});
