// login box management functions
  
// sends the request to login
function sendLogin()
{
  var login = obtainElementById( 'login' ).value;
  var password = obtainElementById( 'password' ).value;
  
  // checking to have something to do
  if ( login == "" || login == defaultLoginValue )
  {
    alert( "Вы забыли ввести имя пользователя" );
    return;
  };
  if ( password == "" || password == defaultPasswordValue )
  {
    alert( "Вы забыли ввести пароль" );
    return;
  };
        
  // building the list of params
  var params = new Array();
  params[PARAM_USER_ID] = login;
  params[PARAM_PASSWORD] = password;
  
  // adding the resource to the document
  sendUrlResponseTextToFunction
  ( 
    sendLoginRequestUrl, 
    function( response )
    {
      if ( response == RESPONSE_REQUEST_PROCEEDED_SUCCESSFULLY )
      {
        // removing the password by the sequrity reasons
        obtainElementById( 'login' ).value = "";
        obtainElementById( 'password' ).value = "";
        // saving the new login value
        updatePageForLogin( login )
      }
      else
        alert( response );
    },
    params
  );
}

// sends the request to log out
function sendLogout()
{
  sendUrlResponseTextToFunction
  ( 
    sendLogoutRequestUrl, 
    function( response )
    {
      if ( response == RESPONSE_REQUEST_PROCEEDED_SUCCESSFULLY )
      {
        // saving the new login value
        updatePageForLogin( DEFAULT_USER_ID )
      }
      else
        alert( response );
    }
  );
}

// sends the request to get the user screen name and to show it
function updateUserScreenName( userId )
{
  sendUrlResponseTextToFunction
  ( 
    getUserScreenNameRequestUrl + escape( userId ), 
    function( response )
    {
      if ( response != "" )
      {
        USER_SCREEN_NAME = response;
        // displaying the user name
        obtainElementById( 'userScreenName' ).innerHTML = USER_SCREEN_NAME;
      };
    }
  );
}

// sends the request to get the user rights and to update some 
// of the page's controls
function updateUserRights()
{
  sendUrlResponseXmlToFunction
  ( 
    getUserRightsRequestUrl + USER_ID, 
    function( response )
    {
      if ( typeof( response ) == "object" )
      {
        var temp = 0;
        
        temp = response.getElementsByTagName("USER_RIGHTS_READ");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_READ = true;
        else
          USER_RIGHTS_READ = false;
          
        temp = response.getElementsByTagName("USER_RIGHTS_EDIT");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_EDIT = true;
        else
          USER_RIGHTS_EDIT = false;
          
        temp = response.getElementsByTagName("USER_RIGHTS_ADD");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_ADD = true;
        else
          USER_RIGHTS_ADD = false;
          
        temp = response.getElementsByTagName("USER_RIGHTS_DELETE");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_DELETE = true;
        else
          USER_RIGHTS_DELETE = false;
          
        temp = response.getElementsByTagName("USER_RIGHTS_COMMENT");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_COMMENT = true;
        else
          USER_RIGHTS_COMMENT = false;
          
        temp = response.getElementsByTagName("USER_RIGHTS_SETUP");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_SETUP = true;
        else
          USER_RIGHTS_SETUP = false;
          
        temp = response.getElementsByTagName("USER_RIGHTS_MODERATE");
        if ( temp.length > 0 && temp[0].firstChild && temp[0].firstChild.nodeValue == "1" )
          USER_RIGHTS_MODERATE = true;
        else
          USER_RIGHTS_MODERATE = false;
        
        // updating the dependent controls
        // the following funtions may just not to be in some modes
        try
        {
          updateManagementBoxButtons( "managementbox" );
        } catch (e) {};
        try
        {
          updateRegisterBox( "registerbox" );
        } catch (e) {};
        try
        {
          updateUserInfo();
        } catch (e) {};
        try
        {
          updateCallback( true );
        } catch (e) {};
      };
    }
  );
}

// updates all the page settings, which depend on user's ID:
// login forms, management buttons, etc.
// if updateAnyWay is set to true, this routine will be forced to run
function updatePageForLogin( userId, updateAnyWay )
{
  if ( userId != USER_ID || updateAnyWay )
  {
    USER_ID = userId;
        
    // updating the user screen name and depending page controls
    updateUserScreenName( userId, LANGUAGE );
    // updating the access rights and depending page controls
    updateUserRights();
    // updating the profile management button
    try
    {
      updateProfileButton();
    } catch (e) {};
    // updating the accessors list
    try
    {
      updateAccessorsList();
    } catch (e) {};
    
    // updating the login form view
    if ( userId == DEFAULT_USER_ID )
    {
      // updating the login form
      obtainElementById( 'loginform' ).style.display = "block";
      obtainElementById( 'logoutform' ).style.display = "none";
    }
    else
    {
      // updating the login form
      obtainElementById( 'loginform' ).style.display = "none";
      obtainElementById( 'logoutform' ).style.display = "block";
    };
    try
    {
      clearDocumentData();
      updateCallback(true);
      prepareCommentBoxForDefaultFunctionality();
    } catch (e) {};
    try
    {
      displayAdvertisements();
    } catch (e) {};
    try
    {
      displayNews(); 
    } catch (e) {};
  };
}

// obtains the current user's ID and performs the reconfiguration
// if the user ID, obtained, differs from the stored one
function updateCurrentUserId()
{
  // adding the resource to the document
  sendUrlResponseTextToFunction
  ( 
    getUserIdRequestUrl, 
    function( response )
    {
      // displaying the message for the user to know, that he's
      // not authorized any more
      if ( USER_ID != DEFAULT_USER_ID && 
           USER_ID != "" && 
           response != DEFAULT_USER_ID && 
           response != USER_ID )
        alert( "Вы не авторизованы." );

      // updating the configuration
      if ( response != USER_ID )
      {
        updatePageForLogin( response );
      };
    }
  );
}