﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.SignUp = function(element) {
    BandSite.SignUp.initializeBase(this, [element]);
}

BandSite.SignUp.prototype = {
    initialize: function() {
        BandSite.SignUp.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;

        this._signUpButton.add_buttonClicked(
            function(sender, e) {
                that._signUpButtonClicked();
            }
        );

    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.SignUp.callBaseMethod(this, 'dispose');
    },
    
    _signUpButtonClicked : function() {
        
        var that = this;
        
        var valid = true;
        var message = "";
        
        var firstName = this._firstNameInput.get_value();
        var lastName = this._lastNameInput.get_value();
        var email = this._emailInput.get_value();
        var password = this._passwordInput.get_value();
        var confirmPassword = this._confirmPasswordInput.get_value();
        
        if (firstName.length < 2) {
            valid = false;
            message += "First name required<br />";
        }
        if (lastName.length < 2) {
            valid = false;
            message += "Last name required<br />";
        }
        if (email.length < 5) {
            valid = false;
            message += "Email required<br />";
        }
        if (password.length < 6) {
            valid = false;
            message += "Password required<br />";
        }
        if (password != confirmPassword) {
            valid = false;
            message += "Password and confirmation password must match<br />";
        }
        
        if (valid) {
            
            VisitorService.SignUp(
                this._bandID,
                firstName,
                lastName,
                email,
                password,
                function(friend) {

                    that._messageLabel.set_value("");
                    // $get("signUpDiv").style.display = 'none';
                    // $get("choosePictureDiv").style.display = 'block';
                   
                    // Also we want to make it so they are actually signed in at this point
                    // and there should probably be a link 'Manage Profile' at the top with
                    // the welcome message.
                    // eg: "Welcome Rob! (Manage Profile)
                    
                    // How should they actually be signed in?
                    // Could store the friendID as a global variable, so we can then
                    // refer to that.  Or is there a better way.  Maybe it could be a
                    // property of the SignUp or (better) SignIn control.             
                    // (Whose state would be changed - perhaps with a method)
                    
                    $find("SignInPanel").signIn(friend);
                    
                    // alert("Should forward to profile manager here");
                    $find("MainPanel").get_mainPanelSet().showPanel("ProfileManagerPanel");
                    // showDiv("profileManagerSectionDiv");
                                        
                },
                function(error) {                
                    if (error.get_message() == "Email in use") {
                        alert("Email already in use");
                    } else {                
                        webServiceError(error);
                    }
                }
            );
            
        } else {
            this._messageLabel.set_value(message);
        }
                                
    },
    
    resetSection: function() {
        this._firstNameInput.set_value('');
        this._lastNameInput.set_value('');
        this._emailInput.set_value('');
        this._passwordInput.set_value('');
        this._confirmPasswordInput.set_value('');
    }
    
}
BandSite.SignUp.createProperty("bandID");
BandSite.SignUp.createProperty("bandSiteUrl");
BandSite.SignUp.createProperty("firstNameInput");
BandSite.SignUp.createProperty("lastNameInput");
BandSite.SignUp.createProperty("emailInput");
BandSite.SignUp.createProperty("passwordInput");
BandSite.SignUp.createProperty("confirmPasswordInput");
BandSite.SignUp.createProperty("signUpButton");
BandSite.SignUp.createProperty("messageLabel");
BandSite.SignUp.registerClass('BandSite.SignUp', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
