﻿// © Copyright Strategy Builders Inc. 2000-2009. All rights reserved.
//   
// The entire contents of this file is protected by International and
// U.S.Copyright Laws. Unauthorized reproduction, distribution, 
// redistribution and reverse engineering of any or all portion of the code 
// contained in this file is strictly prohibited and may
// result in severe civil and criminal penalties and will be
// prosecuted to the maximum extent possible under the law.
//  
// RESTRICTIONS 
//  
// THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES  
// ARE CONFIDENTIAL AND PROPRIETARY TRADE  
// SECRETS OF Strategy Builders Inc. 
//  
// THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED
// FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE  
// COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE 
// AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  
// AND PERMISSION FROM Strategy Builders Inc. 
//  
// CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON 
// ADDITIONAL RESTRICTIONS.
//  

function UserLoginControl(id) {
    ContainerControl.call(this, id);

    this._emailTextBox = new FormControl('EmailTextBox');
    this._passwordTextBox = new FormControl('PasswordTextBox');
    this.addControl(this._emailTextBox);
    this.addControl(this._passwordTextBox);

    this._userEmail = '';
}

UserLoginControl.prototype = new ContainerControl();

UserLoginControl.prototype.setEmailTemplate = function(template) {
    this._emailTextBox.setTemplate(template);
}

UserLoginControl.prototype.setPasswordTemplate = function(template) {
    this._passwordTextBox.setTemplate(template);
}

UserLoginControl.prototype.setUser = function(user) {
    this.setAttribute('user', user);
}

UserLoginControl.prototype.getUser = function() {
    return this.getAttribute('user');
}

UserLoginControl.prototype.getUserEmail = function() {
    return this._emailTextBox.getAttribute('UserEmail');
}

UserLoginControl.prototype.getUserPassword = function() {
    return this._passwordTextBox.getAttribute('Password');
}


UserLoginControl.prototype.setPreDefinedEmailTemplate = function() {
    this.setEmailTemplate('UserLoginEmailTemplate');
}

UserLoginControl.prototype.setPreDefinedPasswordTemplate = function() {
    this.setPasswordTemplate('UserLoginPasswordTemplate');
}

UserLoginControl.prototype.showWelcomeMessageToLoggedInUser = function () {
    this.setTemplate('UserLoginWelcomeMessageToLoggedInUserTemplate');
    this._emailTextBox.setTemplate('');
    this._passwordTextBox.setTemplate('');
}

UserLoginControl.prototype.showWelcomeMessageToNewUser = function () {
    this.setTemplate('UserLoginWelcomeMessageToNewUserTemplate');
    this._emailTextBox.setTemplate('');
    this._passwordTextBox.setTemplate('');
}

UserLoginControl.prototype.showEmailBox = function () {
    this.setTemplate('UserLoginEmailBoxTemplate');
    this.setEmailTemplate('UserLoginEmailBox2Template');
    this._passwordTextBox.setTemplate('');
}

UserLoginControl.prototype.ShowLoginLink = function () {
    this.setTemplate('UserLoginLinkTemplate');
    this.setEmailTemplate('');
    this._passwordTextBox.setTemplate('');
}

UserLoginControl.prototype.showEmailAndPasswordBox = function() {
    this.setTemplate('UserLoginEmailAndPasswordBoxTemplate');
    this.setEmailTemplate('UserLoginEmailAndPasswordBox2Template');
    this.setPreDefinedPasswordTemplate();
}

UserLoginControl.prototype.showNewUserBox = function() {
    this.setTemplate('UserLoginNewUserGroupTemplate');
    this.setEmailTemplate('UserLoginNewUserEmailTemplate');
    this._passwordTextBox.setTemplate('UserLoginNewUserPasswordTemplate');
}

UserLoginControl.prototype.showRegisterNewUserBox = function () {
    this.setTemplate('UserLoginNewUserGroupTemplate');
    this.setEmailTemplate('UserLoginRegisterNewUserEmailTemplate');
    this._passwordTextBox.setTemplate('UserLoginNewUserPasswordTemplate');
}

UserLoginControl.prototype.showForgotPasswordBox = function () {
    this.setTemplate('ForgotPasswordTemplate');
    this.setEmailTemplate('');
    this._passwordTextBox.setTemplate('');
}

UserLoginControl.prototype.showResetPasswordSuccessMessage = function (msg) {
    this.setTemplate('ForgotPasswordResultTemplate');
    this.setEmailTemplate('');
    this._passwordTextBox.setTemplate('');
    this.setAttribute('ForgotEmailMessage', msg);
}


UserLoginControl.prototype.ValidateEmail_EventHandler = function(e) {

    var targ;
    if (!e) e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me._emailTextBox.setAttribute('UserEmail', targ.value);
    me.getEventHandlers()['ValidateEmail'](me);
}

UserLoginControl.prototype.ValidatePassword_EventHandler = function(e) {

    var targ;
    if (!e) e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me._passwordTextBox.setAttribute('Password', targ.value);

    me.getEventHandlers()['ValidatePassword'](me);
}

UserLoginControl.prototype.SendPasswordResetEmail_EventHandler = function (e) {

    var targ;
    if (!e) e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    alert($get('UserLoginControl_EmailTextBox_0'));
    me._emailTextBox.setAttribute('UserEmail', $get('UserLoginControl_EmailTextBox_0').value);

    me.getEventHandlers()['SendPasswordResetEmail'](me);
}


UserLoginControl.prototype.RegisterEvents = function () {

    for (var ControlEventName in this.getRegisterEventExpressions()) {
        //alert(this.getRegisterEventExpressions()[ControlEventName]);
        eval(this.getRegisterEventExpressions()[ControlEventName]);
    }
    for (var i = 0; i < this.getChildCount(); i++) {
        //alert(this.getChild(i).getId());
        for (ControlEventName in this.getChild(i).getRegisterEventExpressions()) {

            eval(this.getChild(i).getRegisterEventExpressions()[ControlEventName]);
        }
    }

}
