/*****************************************************************************/
/*** Login / logout **********************************************************/
/*****************************************************************************/
function toggleLoginPanel()
{
  var extend = g_master_divLoginContent.style.display == "none";
  g_master_divLoginContent.style.display = extend ? "" : "none";
  g_master_lnkLoginToggle.className = extend ? "master_login_shr" : "master_login_ext";
}  
function hideError()
{
  g_master_divLoginError.innerHTML = "";
  g_master_divLoginError.style.display = "none";
}
function showError( msg )
{
  g_master_divLoginError.innerHTML = msg;
  g_master_divLoginError.style.display = "";
}
function adjustLoginControls( isSignedIn )
{
  g_master_divLogin.style.display = isSignedIn ? "none" : "";
  g_master_lnkLoginSignout.style.display = isSignedIn ? "" : "none";
}
function setAuthCallBacks()
{
  Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback( onLoginCompleted );
  Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback( onLogoutCompleted );
  Sys.Services.AuthenticationService.set_defaultFailedCallback( onAuthFailed );
}

function refreshMenu()
{
  location = g_master_lnkbtnRefreshMenu.href;
}  
function signIn()
{
  setAuthCallBacks();
  var userName = g_master_txtLoginUser.value;
  var password = g_master_txtLoginPassword.value;
  Sys.Services.AuthenticationService.login( userName, password, false, null, null, null, null, "Empty Context" );
}
function signOut()
{
 // Compose cookie.
  var date = new Date();
  date.setMonth( date.getMinutes() + 3 );      
  var cookie = "ShowExpiredWarning=false";
  cookie += ";expires=" + date.toGMTString();
  cookie += ";Path=/";
  document.cookie = cookie;  
  
  setAuthCallBacks();  
  Sys.Services.AuthenticationService.logout( null, null, null, null ); 
}
function onAuthFailed( error, userContext, methodName )
{			
  adjustLoginControls( false );
  showError( "Authentication operation failed." +
             " Message = " + error.get_message() + 
             " Timed out = " + error.get_timedOut() +   
             " Status code = " + error.get_statusCode() );
}
function onLoginCompleted( validCredentials, userContext, methodName )
{
  g_master_txtLoginPassword.value = "";
  if ( true == validCredentials ) 
  {
    var LoginUser = g_master_txtLoginUser.value;
    g_master_txtLoginUser.value = "";
    hideError();
    adjustLoginControls( true );
    refreshMenu();
    loginbox_RequestRedirection();
  }
  else 
  {
    showError( "Invalid credentials" );
    adjustLoginControls( false );
  }
}
function onLogoutCompleted( result )
{
  hideError();
  adjustLoginControls( false );
  //top.window.location=top.window.location.protocol+"//"+top.window.location.hostname+":"+top.window.location.port+"/STC/Pages/Public/Home.aspx";
  window.location=document.getElementById("lnkRoot").href+"Pages/Public/Home.aspx";
}        

// Redirection
function loginbox_RequestRedirection()
{  
  var ret = STC.Services.Public.RedirectionService.GetRedirectionUrl(loginbox_OnRequestRedirectionComplete );
}
function loginbox_OnRequestRedirectionComplete( result )
{
  if ( typeof( result ) == "string" && "" != result )
  {
    var location = document.getElementById("lnkRoot").href + result;    
    suppressSignInErrorAlert();
    window.location= location ;
  }
}
    

// Event handlers
function SignInKeyDown( ctrl, event )
{  
  if(navigator.appName=='Microsoft Internet Explorer')
  { 
    if(event.keyCode == 13)
    {
      // Compose cookie.
      var date = new Date();
      date.setMonth( date.getMinutes() + 3 );      
      var cookie = "ShowExpiredWarning=false";
      cookie += ";expires=" + date.toGMTString();
      cookie += ";Path=/";
      document.cookie = cookie;  
      g_master_btnSignIn.click();
    }
  } 
  else 
  {
    if ( event.keyCode == 13 )
    {    
        // Compose cookie.
      var date = new Date();
      date.setMonth( date.getMinutes() + 3 );      
      var cookie = "ShowExpiredWarning=false";
      cookie += ";expires=" + date.toGMTString();
      cookie += ";Path=/";
      document.cookie = cookie;  
      skipSubmitAndNavigate( "javascript:{signIn();}" );
    }
  }
}

function skipSubmitAndNavigate( url )
{
  var frm = document.forms[0];
  frm.originalSubmitHandler = frm.onsubmit;
  frm.urlToNavigate = url;
  frm.onsubmit = function() 
                 { 
                   var url = this.urlToNavigate;
                   this.onsubmit = this.originalSubmitHandler;
                   this.originalSubmitHandler = null;                         
                   this.urlToNavigate = null;
                   window.location.assign( url );
                   return false; 
                 }
}

//onScriptReady();
//Skip FF error http://blogs.teamb.com/yaminov/2008/07/30/pagerequestmanagerservererrorexception-in-firefox/
function suppressSignInErrorAlert()
{
//alert("onScriptReady - " + window.location);
  if (!document.all) {
    window.onbeforeunload = function() {
      Sys.WebForms.PageRequestManager.getInstance().add_endRequest(master_endRequest);
    }
  }
}
function master_endRequest(sender, e) {
//alert("endRequest - " + window.location);
    err = e.get_error();
    if (err){
      if (err.name == "Sys.WebForms.PageRequestManagerServerErrorException") {
        e.set_errorHandled(true);
      }
    }
  }
