
var loginEnabled = true; //Stores whether or not the login control is enabled
var LoginType;

function enableLogin() { loginEnabled = true; } // Enables the login control
function disableLogin() { loginEnabled = false; } // Disables the login control

//Informs the user that the login process is taking place
function notifyLogin(loginType) {
    var feedbackPanelID;

    try {
        switch (loginType) {
            case "HOME":
                feedbackPanelID = 'log-in';
                break;
            default:
                feedbackPanelID = 'splash-error';
        }

        var ff = $(feedbackPanelID);

        if (feedbackPanelID == 'log-in') {
            // this is the new homepage
            // show default class for login div 
            ff.style.display = 'none';
            $('new-log-in-error').style.display = 'none';
            $('loadingbar-home').style.display = '';
        }
        else {
            // splash page, 
            ff.innerHTML = 'Logging in - please wait';
        }
    }
    catch (e) {
        return false;
    }
}

/* Allow submission of the login details to the login webservice */
function submitLogin(loginType, SplashGoto) {

    if (loginEnabled) {
        //Ensure nulls are not passed to the WebMethod, as this will cause it to fail
        if (loginType == null) {
            loginType = '';
        }
        if (SplashGoto == null) {
            SplashGoto = '';
        }

        //Give feedback to the user	
        notifyLogin(loginType);
        //Update LoginType global variable
        LoginType = loginType;
        // Prevent multiple login attempts
        //disableLogin();

        //Make a call to login webservice
        WeeWorld.Web.services.login.LoginUser($(signin_username).value, $(signin_password).value, $(cb_remember_me).checked, SplashGoto, loginType, submitLogin_AjaxCallback);
        //alert('submitting login - ' + loginType);

    }
}

//Ajax callback method, handles successful or un-successful login attempts
function submitLogin_AjaxCallback(res) {


    var loginType;
    var ff; //Stores the id of the feedback details

    if (res.error == null) {
        var UserInfo = res.value;

        window.location = UserInfo.URL;

    }
    else {
        switch (res.error.Type) {
            case "WeeWorld.Web.services.InvalidLoginException":
                ff = $('log-in');
                $('loadingbar-home').style.display = 'none';
                ff.className = 'log-in-error';
                ff.style.display = '';
                $('new-log-in-error').style.display = '';
                $('home-error').innerHTML = res.error.Message;
                $('not-registered').style.display = '';
                break;

            case "WeeWorld.Web.services.InvalidSplashLoginException":
                ff = $('splash-error'); // Error message panel
                ff.style.display = 'block';
                ff.innerHTML = res.error.Message;
                break;

            default:
                $('home-error').innerHTML = res.error.Message;
                $('not-registered').style.display = 'none';
                break;
        }
        enableLogin(); // Allow the user to try logging in again			    
    }

}








