if (!$team) {
    error_page(tra("You need to be a member of a team to access this page."));
}
page_head(tra("Request foundership of %1", $team->name));
$now = time();
// it should never happen, but just in case
//
if (!$team->userid) {
    $team->update("userid={$user->id}, ping_user=0, ping_time=0");
    echo tra("You are now founder of team %1.", $team->name);
    page_tail();
    exit;
}
if ($user->id == $team->ping_user) {
    echo "<p>" . tra("You requested the foundership of %1 on %2.", $team->name, date_str($team->ping_time)) . "\n    </p>";
    if (transfer_ok($team, $now)) {
        echo tra("60 days have elapsed since your request, and the founder has not responded. You may now assume foundership by clicking here:") . "<form method=\"post\" action=\"team_founder_transfer_action.php\">\n            <input type=\"hidden\" name=\"action\" value=\"finalize_transfer\">\n            <input class=\"btn btn-default\" type=\"submit\" value=\"" . tra("Assume foundership") . "\">\n            </form>\n        ";
    } else {
        echo "<p>" . tra("The founder was notified of your request. If he/she does not respond by %1 you will be given an option to become founder.", date_str(transfer_ok_time($team))) . "</p>";
    }
} else {
    if (new_transfer_request_ok($team, $now)) {
        echo "<form method=\"post\" action=\"team_founder_transfer_action.php\">";
        echo "<p>" . tra("If the team founder is not active and you want to assume the role of founder, click the button below. The current founder will be sent an email detailing your request, and will be able to transfer foundership to you or to decline your request. If the founder does not respond in 60 days, you will be allowed to become the founder.<br /><br />\n                       Are you sure you want to request foundership?") . "</p>";
        echo "<input type=\"hidden\" name=\"action\" value=\"initiate_transfer\">\n            <input class=\"btn btn-default\" type=\"submit\" value=\"" . tra("Request foundership") . "\">\n            </form>\n        ";
    } else {
        if ($team->ping_user) {
            if ($team->ping_user < 0) {
                $team->ping_user = -$team->ping_user;
            }
            $ping_user = BoincUser::lookup_id($team->ping_user);
         page_head(tra("Requesting foundership of %1", $team->name));
         $success = send_founder_transfer_email($team, $user);
         // Go ahead with the transfer even if the email send fails.
         // Otherwise it would be impossible to rescue a team
         // whose founder email is invalid
         //
         $team->update("ping_user={$user->id}, ping_time={$now}");
         echo "<p>" . tra("The current founder has been notified of your request by email and private message.<br /><br />\n                       If the founder does not respond within 60 days you will be allowed to become the founder.") . "</p>\n";
     } else {
         error_page(tra("Foundership request not allowed now"));
     }
     break;
 case "finalize_transfer":
     $team = BoincTeam::lookup_id($user->teamid);
     $now = time();
     if ($user->id == $team->ping_user && transfer_ok($team, $now)) {
         page_head(tra("Assumed foundership of %1", $team->name));
         $team->update("userid={$user->id}, ping_user=0, ping_time=0");
         echo tra("Congratulations, you are now the founder of team %1. Go to %2Your Account page%3 to find the Team Admin options.", $team->name, "<a href=\"" . URL_BASE . "home.php\">", "</a>");
     } else {
         error_page(tra("Foundership request not allowed now"));
     }
     break;
 case "decline":
     $teamid = post_int("teamid");
     $team = BoincTeam::lookup_id($teamid);
     require_founder_login($user, $team);
     page_head(tra("Decline founder change request"));
     if ($team->ping_user) {
         $ping_user = BoincUser::lookup_id($team->ping_user);
         $team->update("ping_user=0");