var SignupForm = new Class({
    
    container:null,
    opt_in_checkbox:null, 
    email_textbox:null,
    submit_button:null,
    
    succeeded:false,
    
    initialize:function()
    {
        var self = this; 

        this.container = $$("#form_container");
        if(!$chk(this.container)){ return; }
        
        this.opt_in_checkbox = $("opt_in_checkbox");
        if(!$chk(this.opt_in_checkbox)){ return; }
        
        this.email_textbox = $("email_textbox");
        if(!$chk(this.email_textbox)){ return; }
        
        this.submit_button = $("submit_button");
        if(!$chk(this.submit_button)){ return; }
        
        this.submit_button.addEvent("click", function(e){ e.stop(); self.process_click(); });
    },
    
    process_click:function()
    {
        if(!this.opt_in_checkbox.checked)
        {
            this.display_message("You must check the checkbox to continue.");
            return;
        }
        
        if(!this.validate_email(this.email_textbox.value))
        {
            this.display_message("You must enter a valid email to continue.");
            return;
        }
        
        this.display_message("Signing up...");
        this.submit_request(this.email_textbox.value);
    },
    
    display_message:function(message, cls)
    {
        var message_container = $("subscribe_message");
        
        if(cls == undefined || cls == null){ cls = "error"; }
        
        if(!$chk(message_container))
        {
            if($chk(this.container))
            {
                var p = new Element("p", {"id":"subscribe_message", "class": cls});
                p.set("html", message);
                this.container.grab(p, "top");
            }
        }
        else
        {
            message_container.set("html", message);
        }
    },
    
    display_thanks:function()
    {
        this.container.set("html", "");
        this.display_message("Thank you for signing up!", "thanks");
    },
    
    validate_email:function(email)
    {
        var email_regex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
        return email_regex.test(email);
    },
    
    submit_request:function(email)
    {
        var self = this; 
        
        if(!$chk(email) || email == "")
        {
            this.display_message("You must provide an email");
            return;
        }
        
        var request = new Request({
            method: "post",
            
            url: "/_subscribe.php",
            
            data: { "q":"put", "email":email },
            
            onRequest: function()
            {
            },
            
            onException:function()
            {
                self.display_message("An exception occurred during signup.");
            },
            
            onFailure: function(xhr)
            {
                self.display_message("An error occurred during signup.");
            },
            
            onComplete: function(response)
            {
                if(response == "-2")
                {
                    self.display_message("This email has already been registered.");
                }
                else if(response == "1")
                {
                    self.display_thanks();
                }
                else
                {
                    self.display_message("An error occurred during signup.");
                }
            }
        });
        
        request.send();
    }
});

/*loader.add(function(){ 
    new SignupForm();
    });*/