var MF = MF || {};
/**
 * @class Newsletter acts upon the DOM objects of the code generated by the
 *  GetNewsLetterSignup Function.
 */
MF.Newsletter = Class.create(
/** @scope MF.Newsletter.prototype */
{
    /**
     * Default options
     * @type Object
     */
    defaultOptions : {
        container : "markupfactory-getnewslettersignup",
        form : "markupfactory-getnewslettersignup-signup",
        input : "markupfactory-getnewslettersignup-email",
        submit : "markupfactory-getnewslettersignup-submit",
        inputText : "email address"
    },

    /**
     * Initialize the object
     */
    initialize : function (options) {
        options = options || {};
        this.container = options.container || this.defaultOptions.container;
        this.input = options.input || this.defaultOptions.input;
        this.submit = options.submit || this.defaultOptions.submit;
        this.inputText = options.inputText || this.defaultOptions.inputText;
        this.form = options.form || this.defaultOptions.form;

        // Check for existence of elements
        if ($(this.container) && $(this.input) && $(this.submit) && 
            $(this.form)) { 
            // Set input text
            $(this.input).value = this.inputText;
            // Disable the submit button
            $(this.submit).disabled = true;
            // Observe input field
            $(this.input).observe("keyup", 
                this.validateInput.bindAsEventListener(this));
            $(this.input).observe("click", 
                this.clearInputContents.bindAsEventListener(this));
            $(this.input).observe("blur", 
                this.setInputContents.bindAsEventListener(this));
            $(this.form).observe("submit", this.submitForm.bind(this));
        }
    },

    /**
     * Keep the submit button disabled unless a valid email address is in the
     * input.
     */
    validateInput : function () {
        var validEmailPattern = 
            /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  
        if ($F(this.input).match(validEmailPattern)) {
            $(this.submit).disabled = false;
        } else {
            $(this.submit).disabled = true;
        }
    },

    /** 
     * Clear out the input contents if they match what was there initially.
     */
    clearInputContents : function () {
        if ($F(this.input) === this.inputText) {
            $(this.input).value = "";
        }
    },

    /** 
     * Reset the input contents if empty
     */
    setInputContents : function () {
        if ($F(this.input).empty()) { 
            $(this.input).value = this.inputText; 
        }
    },

    /**
     * Submit the form w/ Ajax
     */
    submitForm : function (evt) {
      evt.stop();
      $(this.form).request({
        onSuccess : function (transport) {
          alert(transport.responseText);
          //$(this.container).update(transport.responseText);
        }.bind(this) 
      });

      //new Ajax.Updater(this.container, "/newsletter/signup.asp", {
        //parameters : {}       
      //});
    }
});

new MF.Newsletter();
