﻿function Sites_a_Common_Modules_NewsletterBoxHorizontal(id)
{
    this.Inherits(Reflective.Web.UI.UserControl, id, "Sites_a_Common_Modules_NewsletterBoxHorizontal");
    if (id)
		window.newsletterBox = this;    
}

Core.setInheritance(Sites_a_Common_Modules_NewsletterBoxHorizontal, 
    Reflective.Web.UI.UserControl);

Core.extend(Sites_a_Common_Modules_NewsletterBoxHorizontal,
    {
        OnInit : function()
        {
            if (this.NewsletterForm)
            {
                this.NewsletterForm.BindEvent("onvalidate", this.id, "Validate");
                this.NewsletterForm.BindEvent("onsubmit", this.id, "DoSubmit");
                this.NewsletterForm.GetInput("AllTopics").BindEvent("checkedchanged", this.id, "AllTopicsCheckedChanged");
            }
        },
        AllTopicsCheckedChanged : function(sender, checked)
        {
            this.NewsletterForm.SetPanelVisible("TopicsPanel", !checked);
        },
        Validate : function(sender, args)
        {
			//submit url is http://app.icontact.com/icp/signup.php
			
            if (this.NewsletterForm.GetInput("FirstName").GetValue() == ""
                || this.NewsletterForm.GetInput("LastName").GetValue() == ""
                || this.NewsletterForm.GetInput("EmailAddress").GetValue() == "")
            {
                alert("Please fill out all fields on the form so that we can process your request.");
                args.IsValid = false;
                return;
            }
            else
            {
                var email = this.NewsletterForm.GetInput("EmailAddress").GetValue();
                
                if (email.indexOf("@") == -1 || email.indexOf(".") == -1)
                {
                    alert("It appears you have entered an invalid email address. Please check your entry and try again.");
                    args.IsValid = false;
                    return;
                }
            }
        },
        DoSubmit : function(sender)
        {
			$(document.body).append("<form id=\"subForm\" method=\"post\" action=\"http://app.icontact.com/icp/signup.php\"></form>");
			var form = $("#subForm");
			var url = Environment.UrlPath;
			if (url.indexOf("?") > -1)
				url += "&iContact=";
			else
				url += "?iContact=";
				
			$("input[name='redirect']").attr("value", url + "success");
			$("input[name='errorredirect']").attr("value", url + "error");
			$("input[name='fields_email']").attr("value", this.NewsletterForm.GetInput("EmailAddress").GetValue());
			$("input[name='fields_fname']").attr("value", this.NewsletterForm.GetInput("FirstName").GetValue());
			$("input[name='fields_lname']").attr("value", this.NewsletterForm.GetInput("LastName").GetValue());
			$("input[name='fields_groups']").attr("value", this.GetGroups());
			
			//dump the icontact values into the form...
			$("#IContactFields > input").each(
				function(item)
				{
					form.append(this);
				}
            );
            
            $g("subForm").submit();
        },
        GetGroups : function()
        {
			var checkBoxes = this.NewsletterForm.GetInputsOfType("checkbox");
			var groupstring = "Newsletter";
			
			checkBoxes.foreach(
				function(item)
				{
					if (item.GetChecked() && item._attr("IsGroup"))
					{
						groupstring += "," + item._attr("ValueIfChecked");
					}
				}
			);
			
			return groupstring;
        }
    }
);

