/*
 * The Javascript file for create-account.jspx.
 */
/**
 * Register the component's namespace.
 */
YAHOO.namespace('forexpert.createAccount');  // YAHOO.forexpert.createAccount

(function(){
    //define a few shortcuts
    var Event = YAHOO.util.Event,
        Dom = YAHOO.util.Dom,
        Selector = YAHOO.util.Selector,
        ur = YAHOO.forexpert.createAccount;

    // singleton for the form object
    ur.form = null;

    // Handle show/hide the "Other" hearFrom source
    ur.pageInit = function()
    {
        var formContainerEl = document.getElementById("account-form-panel");
        ur.form = new ur.RegisterForm(formContainerEl);
    };

    ur.pageDestroy = function()
    {
        if(ur.form)
            ur.form.destroy();
    };

    Event.onDOMReady(ur.pageInit);
    Event.addListener(window, "unload", ur.pageDestroy);

    ur.RegisterForm = function(containerEl)
    {
        this.init(containerEl);
    };

    ur.RegisterForm.prototype =
    {
        containerEl: null,

        init: function(containerEl)
        {
            this.containerEl = containerEl;
            this.hearFromSelectEl = Selector.query("select.hearFrom", this.containerEl, true);
            this.otherEl = Selector.query(".other-hearfrom-source", this.containerEl, true);
            this.otherErrorEl = Selector.query(".other-hearfrom-source-error", this.containerEl, true);

            this.addListeners();
        },

        destroy: function()
        {
            this.removeListeners();

            this.containerEl = null;
        },

/*
        // Copied from create-account.js, but needs work since ExtJS should not be included on this page.
        addValidation: function()
        {
            var firstName = 'firstName';
            var lastName = 'lastName';
            var password = 'password';
            var password2 = 'password2';
            var email = 'email';
            var email2 = 'email2';
            var hearFrom = 'hearFrom';
            var acceptDisclaimer = 'acceptDisclaimer';

            new forexpert.FormValidator("account-form", account.username, account.username + "-error", account.contextPath + "/validations/username-format/validate");
            new forexpert.FormValidator("account-form", account.password, account.password + "-error", account.contextPath + "/validations/password-length/validate");
            new forexpert.FormValidator("account-form", account.password2, account.password2 + "-error", account.contextPath + "/validations/password-confirm/validate");
            new forexpert.FormValidator("account-form", account.email22, account.email22 + "-error", account.contextPath + "/validations/email-format/validate");
            new forexpert.FormValidator("account-form", account.acceptDisclaimer, account.acceptDisclaimer +"-error", account.contextPath + "/validations/accept-disclaimer/validate");
            new forexpert.FormValidator("account-form", account.postal, account.postal + "-error", account.contextPath + "/validations/postal-format/validate");
        },
*/

        addListeners: function()
        {
            Event.addListener(this.hearFromSelectEl, "change", this.handleChangeHearFrom, this, true);
        },

        removeListeners: function()
        {
            Event.removeListener(this.hearFromSelectEl, "change", this.handleClickHearFrom);
        },

        handleChangeHearFrom: function(event)
        {
            var target = Event.getTarget(event);
            var index = target.selectedIndex;
            var option = target.options[index].value;

            // If the selected option is "Other", show the other field
            if(option == "OTHER")
            {
                this.showOtherField();
            }
            else
            {
                // else hide the other field
                this.hideOtherField();
            }
        },

        showOtherField: function()
        {
            Dom.removeClass(this.otherEl, "hidden");
            Dom.removeClass(this.otherErrorEl, "hidden");
        },

        hideOtherField: function()
        {
            Dom.addClass(this.otherEl, "hidden");
            Dom.addClass(this.otherErrorEl, "hidden");
        }
    };

})();
