﻿// Hook up the click events of the log in and log out buttons.
$addHandler($get('btnLogIn'), 'click', loginHandler);
$addHandler($get('LinkButton_logout'), 'click', logoutHandler);

var ssa = Sys.Services.AuthenticationService;
if (ssa.get_isLoggedIn()) {
    $get('logged').style.display = '';
    $get('AnonymousView').style.display = 'none';
    $get('messages').style.display = 'none';
    document.getElementById('login').style.display = 'none';
} else {
    $get('AnonymousView').style.display = '';
}

function postBackHF2(hiddenFieldID) {
    var hiddenField = $get(hiddenFieldID);
    if (hiddenField) {
        hiddenField.value = (new Date()).getTime();
        __doPostBack(hiddenFieldID, '');
    }
}

function loginHandler() {
  
    var username = $get('txtUsername').value;
    var password = $get('pwdPassword').value;
    var isPersistent = $get('chkRememberMe').checked;
    var customInfo = null;
    var redirectUrl = null;
    document.getElementById('loggeduser').innerHTML = username;
    // Log them in.
    ssa.login(username,
              password,
              isPersistent,
              customInfo,
              redirectUrl,
              onLoginComplete);
}

function logoutHandler() {
    // Log them out.
    var redirectUrl = "http://www.pop.it/Default.aspx";
    var userContext = null;
    ssa.logout(redirectUrl,
               onLogoutComplete,
               onError,
               userContext);
}

function onLogoutComplete(result, context, methodName) {
    // Logged out.  Hide the logged in view.
    $get('logged').style.display = 'none';
    $get('AnonymousView').style.display = '';
}

function onError(error, context, methodName) {
}

function ShowLoginDiv() {
    document.getElementById('AnonymousView').style.display = 'none';
    document.getElementById('login').style.display = '';
}

function handleKeyPress(e, form) {
    var key = e.keyCode || e.which;
    if (key == 13) {
        loginHandler();
    }
}