var ajax_login_r = make_new_ajax_request("login_backend.php", login_callback);

function handle_login_text_box_changed(keyCode)
{  if (keyCode == 13) // enter
   {  el = document.getElementById("login_form");
      el.submit();
   }
}

function check_login(cnf_code)
{  
   command = K.TAG_CMD_USER_BOOT;
   payload = '';
   if (cnf_code)
   {  payload += "<CnfCode>" + xml_escape(cnf_code) + "</CnfCode>";
   }

   send_ajax_request(ajax_login_r, command, payload);
}

function process_register(user_in)
{
   command = 'Register';
   payload  = "<User>" + xml_escape(user_in) + "</User>";

   send_ajax_request(ajax_login_r, command, payload);
}

function login_callback()
{
   r = process_ajax_response(ajax_login_r);

   if (!r)
   {  return;
   }

   if (r.command == 'Logout')
   {  if (!r.command_ok)
      {  showAlert("something went wrong with the logout");
      }
      else
      {  
         user = new User();
//         update_left_bar();
         g_il.set_seeds(user.root_item);
      // proper item update here
      }
   }
   else if (r.command == 'AccountEdit')
   {
      if (!r.command_ok)
      {  showAlert(r.command_error);
      }
      else
      {  
         user = new User();
         user.update_from_xml(r.doc);

         update_left_bar();
         update_login_bar();

         ItemsSection.show();
      // proper item update here
      }
   }
   else if (r.command == K.TAG_CMD_USER_BOOT)
   {
      if (!r.command_ok)
      {  showAlert(r.command_error);

         user = new User();
         //update_left_bar();
         update_login_bar();
      }
      else
      {  
         user = new User();
         user.update_from_xml(r.doc);
   
      // if we have an item_id from the URL, use it.  Else
      //  use the one we got from user_boot
         var seeds = [];
         if (url_root_item !== null) {
            g_il.set_seeds(url_root_item);
         } else if (user.root_item !== null) {
            g_il.set_seeds(user.root_item);
         }
         if (user.must_change_passwd)
         {  AccountSection.show();
// AFTER this, need a way to get back to user_boot
         }
         else
         {  
            if (jump && sections[jump]) {
               var jump_to = sections[jump];
               jump_to.show();
            } else  {
               ItemsSection.show();
            }
      // proper item update here
         }
         next_load();
      }
   }
   else if (r.command == 'Invite')
   {  
      if (!r.command_ok)
      {  loginShowError(true, r.command_error, null);
      }
      else
      {  invite_callback(r.doc);
      }   
   }
   else if (r.command == 'ResetPassword' || r.command == 'RequestConfirmEmail')
   {  if (!r.command_ok)
      {
         show_login_error_by_code(get_xml_value(r.doc, K.TAG_LOGIN_STATUS));
      }
      else
      {  if (r.command == 'ResetPassword')
         {  showAlert("Password reset.  " +
               "Check your email account for a message from us."); 
         }
         else
         {  showAlert("Confirmation re-sent.  " +
               "Check your email account for a message from us."); 
         }
         show_last_section();
      }
   }
   else if (r.command == 'Login')
   {  if (!r.command_ok)
      {
         show_login_error_by_code(get_xml_value(r.doc, K.TAG_LOGIN_STATUS));
      }
      else
      {  
         user = new User();
         user.update_from_xml(r.doc);

         g_il.set_seeds(user.root_item);

         if (user.must_change_passwd)
         {  AccountSection.show();
         }
         else
         {  ItemsSection.show();
   // !!!!!!!! proper item update   
         }
      }
   }
   else if (r.command == 'Register')
   {  if (!r.command_ok)
      {  show_register_error(r.command_error);
      }
      else
      {  show_register_error("");
         showAlert("Thank you!  You'll receive an email shortly!");
         show_last_section();
      }
   }
   else
   {  showAlert("Unknown command '" + r.command + "'");
   } 

   update_login_bar();
}

function update_login_bar()
{  
   var pt = section_link(AboutSection, "(about)");
   document.getElementById("about_span").innerHTML = pt;

   var t = 'Welcome, ' + 
      (user.is_guest ? "guest" : 
        section_link(AccountSection, user.email));

   if (user.is_guest)
   {
      t += '&nbsp;';
      t += section_link(LoginSection, "(login)");
      t += '&nbsp;|&nbsp;';
   }
   else
   {
      t += '&nbsp;<A class="top_bar" HREF="javascript: logout()">(sign out)</A>';
      t += '&nbsp;|&nbsp;';
   }

   t += section_link(AboutSection, "about");
   t += '&nbsp;|&nbsp;';

   if (user.is_guest || root_item_under_users_tree == 0)
   {
      t += '<A class="top_bar" HREF="/mw.php">' + (user.is_guest ? "home" : "your stuff") + 
         '</A>';
   }
   else  
   { t += section_link(ItemsSection, "home"); 
   }

   t += '&nbsp;|&nbsp;';
   t += section_link(APISection, "API");
   t += '&nbsp;|&nbsp;';

   if (!user.is_guest)
   {  
      t += section_link(InviteSection, "invite");
      t += '&nbsp;|&nbsp;';


      if (user.is_admin)
      {  t += '<A class="top_bar" HREF="/webadmin/main.php">admin</A>';
         t += '&nbsp;|&nbsp;';
      }

      t += '<A class="top_bar" HREF="/mw.php?id=677">FEATURES / BUGS</A>';
   }
   else
   {
      t += section_link(RegisterSection, 'join memway!</A>');
   }
   document.getElementById("welcome_tag").innerHTML = t;
}


function logout()
{
   send_ajax_request(ajax_login_r, 'Logout', '');
}

