function EmailSignup(settings) {

    this.signupWrapper = settings.signupWrapper;
    this.firstNameInput = settings.firstNameInput;
    this.emailInput = settings.emailInput;
    this.zipcodeInput = settings.zipcodeInput;
    this.countrySelect = settings.countrySelect;
    this.submitButton = settings.submitButton;
    this.coverElement = settings.coverElement;
    this.coverTextElement = settings.coverTextElement;
    this.signupMethodCode = settings.signupMethodCode;
    this.signupType = (settings.signupType === undefined) ? 'weeklyNewsletter' : settings.signupType;
    this.weeklyNewsletterCheckbox = settings.weeklyNewsletterCheckbox; //this is only relevant when signupType ISN'T 'weeklyNewsletter'
    this.eventAlertPID = settings.eventAlertPID;
    this.eventAlertName = settings.eventAlertName;
    this.prefillFormItems = settings.prefillFormItems;

    this.emailCovered = false;

    this.Initialize = function () {
        //store email wrapper height (will need for closing the cover)
        this.signupWrapper.attr('naturalheight', this.signupWrapper.height() + "px");

        this.signupWrapper.find('input').each(function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr('initialTxt'));
                $(this).css("color", "gray");
            }
        });
        this.signupWrapper.find('input').focus(function () {
            if ($(this).val() == $(this).attr('initialTxt')) {
                $(this).css("color", "black");
                $(this).val('');
            }
        });
        this.signupWrapper.find('input').blur(function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr('initialTxt'));
                $(this).css("color", "gray");
            }
            else {
                $(this).css("color", "black");
            }
        });
        this.signupWrapper.find('input').keypress({ eso: this },
            function (e) {
                if (e.which == 13) {
                    e.data.eso.submitButton.click();
                }
            }
        );

        this.submitButton.click({ eso: this },
            function (event) {
                var eso = event.data.eso;
                //first, unerror all errored fields
                var erroredClass = 'errored';
                $('.' + erroredClass).removeClass(erroredClass);

                var firstNameInputValue = eso.firstNameInput.val();
                var emailInputValue = eso.emailInput.val();
                var zipInputValue = eso.zipcodeInput.val();
                var selectCountry = eso.countrySelect.find(':selected').text();
                var includeWeeklyNewsletterSignup = eso.signupType == 'weeklyNewsletter' || (eso.weeklyNewsletterCheckbox != undefined && eso.weeklyNewsletterCheckbox.is(':checked'));
                var includeEventAlertSignup = eso.signupType == 'eventAlert';

                //validate inputs
                var alertMessage = '';
                if (firstNameInputValue == eso.firstNameInput.attr('initialtxt') || firstNameInputValue == '') {
                    eso.firstNameInput.addClass(erroredClass);
                    alertMessage += '\nPlease enter your name.';
                }
                if (emailInputValue == eso.emailInput.attr('initialtxt') || emailInputValue == '' || emailInputValue.search("^[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9}$")) {
                    eso.emailInput.addClass(erroredClass); ;
                    alertMessage += '\nPlease enter a valid email address.';
                }
                if (zipInputValue == eso.zipcodeInput.attr('initialtxt') || zipInputValue == '' || zipInputValue.search("^[-\\w\\s\\d]*$")) {
                    eso.zipcodeInput.addClass(erroredClass);
                    alertMessage += '\nPlease enter your zipcode (dashes, spaces, numbers, and letters only).';
                }
                if (selectCountry == 'Country' || selectCountry == '') {
                    eso.countrySelect.addClass(erroredClass);
                    alertMessage += '\nPlease select a country.';
                }
                if (alertMessage.length > 0) {
                    alert(alertMessage);
                    return;
                }

                //if valid
                event.stopPropagation();
                var outerElement = $(this).parent();
                //outerElement.animate({ height: "105px" }, 500);
                eso.coverTextElement.html('Loading...');
                //eso.coverElement.animate({ height: "110px" }, 500, "linear",
                eso.coverElement.animate({ height: eso.coverElement.parent(':first').outerHeight() }, 500, "linear",
                function () {
                });
                this.emailCovered = true;

                $.get('/ajax/email-signup.aspx?em=' + urlEncode(emailInputValue)
                + '&zp=' + urlEncode(zipInputValue)
                + '&fn=' + urlEncode(firstNameInputValue)
                + '&cy=' + urlEncode(selectCountry)
                + '&cd=' + urlEncode(eso.countrySelect.val())
                + '&md=' + urlEncode(eso.signupMethodCode)
                + '&pid=' + urlEncode(eso.eventAlertPID)
                + (!includeWeeklyNewsletterSignup ? '&weeklySignup=false' : '')
                + (includeEventAlertSignup ? '&eventAlertSignup=true' : ''),
                function (data) {
                    var result;
                    eval(data);
                    var displayText = '';
                    if (includeWeeklyNewsletterSignup) {
                        switch (result.weeklyNewsletter) {
                            case "new":
                                displayText += 'Thank you for subscribing to the TicketNetwork Newsletter.';
                                break;
                            case "update":
                                //displayText += 'It appears that you are already signed up for the weekly newsletter! ';
                                displayText += 'Thank you for subscribing to the TicketNetwork Newsletter.';
                                break;
                            default:
                                displayText += 'There was a problem processing your request to sign up for the weekly newsletter. Please try again later.';
                        }
                    }
                    if (includeEventAlertSignup) {
                        if (includeWeeklyNewsletterSignup)
                            displayText += displayText.length > 0 ? '<br /><br />' : '';
                        switch (result.eventAlert) {
                            case "new":
                                displayText += 'Thank you for subscribing to ' + eso.eventAlertName + ' alerts.';
                                break;
                            default:
                                displayText += 'There was a problem processing your request to sign up for alerts for ' + eso.eventAlertName + '. Please try again later.';
                        }
                    }
                    eso.coverTextElement.html(displayText);
                });

                //add cookie for mbox segmentation
                if (includeWeeklyNewsletterSignup) {
                    TNR4CreateCrossDomainCookie('tnSegSessionEmailSubscriber', 'true');
                }
            }
        );


        //Prefill all specified form items
        if (this.prefillFormItems != undefined) {
            //put the values from the querystring in, or initialTxt
            var queryString = GetQueryStringObj();

            if (queryString) {
                if (this.prefillFormItems == true) {
                    //first name
                    if (queryString['fname'])
                        this.FillFirstName(urlDecode(queryString['fname']));

                    //email address
                    if (queryString['email'])
                        this.FillEmail(urlDecode(queryString['email']));

                    //zip code
                    if (queryString['zip'])
                        this.FillZipCode(urlDecode(queryString['zip']));

                    //country select
                    if (queryString['cid'])
                        this.SelectCountry(queryString['cid']);
                }
            }
        }
    }

    this.FillFirstName = function (value) {
        this.firstNameInput.focus();
        this.firstNameInput.val(value);
        this.firstNameInput.blur();
    }

    this.FillEmail = function (value) {
        if (!value.search("^[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9}$")) {
            this.emailInput.focus();
            this.emailInput.val(value);
            this.emailInput.blur();
        }
    }

    this.FillZipCode = function (value) {
        if (!value.search("^[-\\w\\s\\d]*$")) {
            this.zipcodeInput.focus();
            this.zipcodeInput.val(value);
            this.zipcodeInput.blur();
        }
    }

    this.SelectCountry = function (value) {
        if (!value.search("^\\d+$")) {
            if (this.countrySelect.find('option[value="' + value + '"]').length > 0)
                this.countrySelect.val(value);
        }
    }

    this.Initialize();
}

