﻿function RegistrationControl(mainContainerId) {
    var control = {
        id: CreateUUID(),
        containerId: mainContainerId,
        isSyncInProgress: false,
        ucCorporateAccountSelectionControlEnableButtons: null,
        ucCorporateAccountSelectionControlDisableButtons: null,
        dataReferences: {
            email: "",
            confirmEmailId: "",
            passwordId: "",
            confirmPasswordId: "",
            firstNameId: "",
            lastNameId: "",
            phoneNumberId: "",
            syncWithServerBtnId: "",
            emailSyncWithServerBtnId: "",
            btnRegisterId: "",
            btnCancelId: ""
        },
        watchers: {
            emailWatcher: null,
            confirmEmailWatcher: null,
            passwordWatcher: null,
            confirmPasswordWatcher: null,
            firstNameWatcher: null,
            lastNameWatcher: null,
            phoneNumberWatcher: null
        }
    };

    control.SetSyncInProgress = function (isInProgress) {
        this.isSyncInProgress = isInProgress;
    };

    control.SyncWithServer = function () {
        if (!this.isSyncInProgress) {
            this.SetSyncInProgress(true);
            ClickButton(this.dataReferences.syncWithServerBtnId);
        }
    };

    control.EmailSyncWithServer = function () {
        ClickButton(this.dataReferences.emailSyncWithServerBtnId);
    };

    control.Initialize = function () {
        this.watchers.emailWatcher = new InputWatcherPrototype(this.dataReferences.emailId, "");
        this.watchers.confirmEmailWatcher = new InputWatcherPrototype(this.dataReferences.confirmEmailId, "");
        this.watchers.passwordWatcher = new InputWatcherPrototype(this.dataReferences.passwordId, "");
        this.watchers.confirmPasswordWatcher = new InputWatcherPrototype(this.dataReferences.confirmPasswordId, "");
        this.watchers.firstNameWatcher = new InputWatcherPrototype(this.dataReferences.firstNameId, "");
        this.watchers.lastNameWatcher = new InputWatcherPrototype(this.dataReferences.lastNameId, "");
        this.watchers.phoneNumberWatcher = new InputWatcherPrototype(this.dataReferences.phoneNumberId, "");

        this.watchers.emailWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.emailId); },
            function () { control.EmailSyncWithServer(); });
        this.watchers.confirmEmailWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.confirmEmailId); },
            function () { control.SyncWithServer(); });
        
        this.watchers.passwordWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.passwordId); },
            function () { control.SyncWithServer(); });
        this.watchers.confirmPasswordWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.confirmPasswordId); },
            function () { control.SyncWithServer(); });
        this.watchers.firstNameWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.firstNameId); },
            function () { control.SyncWithServer(); });
        this.watchers.lastNameWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.lastNameId); },
            function () { control.SyncWithServer(); });
        this.watchers.phoneNumberWatcher.Initialize(
            function () { return GetControlValue(control.dataReferences.phoneNumberId); },
            function () { control.SyncWithServer(); });
    };

    control.ActivateWatchers = function() {
        this.watchers.emailWatcher.Activate(true);
        this.watchers.confirmEmailWatcher.Activate(true);
        this.watchers.passwordWatcher.Activate(true);
        this.watchers.confirmPasswordWatcher.Activate(true);
        this.watchers.firstNameWatcher.Activate(true);
        this.watchers.lastNameWatcher.Activate(true);
        this.watchers.phoneNumberWatcher.Activate(true);
    };

    control.EnableButtons = function () {
        EnableControl(control.dataReferences.btnRegisterId);
        EnableControl(control.dataReferences.btnCancelId);
        EnableControl(control.dataReferences.syncWithServerBtnId);
        EnableControl(control.dataReferences.emailSyncWithServerBtnId);

        if (control.ucCorporateAccountSelectionControlEnableButtons)
            control.ucCorporateAccountSelectionControlEnableButtons();
    };

    control.DisableButtons = function () {
        DisableControl(control.dataReferences.btnRegisterId);
        DisableControl(control.dataReferences.btnCancelId);
        DisableControl(control.dataReferences.syncWithServerBtnId);
        DisableControl(control.dataReferences.emailSyncWithServerBtnId);

        if (control.ucCorporateAccountSelectionControlDisableButtons)
            control.ucCorporateAccountSelectionControlDisableButtons();
    };

    return control;
}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
