function change_player_name($newname, $old = false)
{
    if ($newname == "") {
        $newname = get_player_basename($old);
    }
    $newname = str_replace("`0", "", $newname);
    $title = get_player_title($old);
    if ($title) {
        $x = strpos($newname, $title);
        if ($x === 0) {
            $newname = trim(substr($newname, $x + strlen($title)));
        }
        $newname = $title . " " . $newname . "`0";
    }
    return $newname;
}
function titlechange_form()
{
    $context = httpget("context");
    $otitle = get_player_title();
    if ($otitle == "`0") {
        $otitle = "";
    }
    output("Your title is currently: ");
    rawoutput($otitle);
    output_notl("`0`n");
    output("`0`nWhich renders as: %s`n`n", $otitle);
    rawoutput("<form action='runmodule.php?module=hunterslodge_customtitle&op=confirm&context={$context}' method='POST'>");
    rawoutput("<input id='input' name='newname' width='25' maxlength='25' value='" . htmlentities($otitle, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "'>");
    rawoutput("<input type='submit' class='button' value='Preview'>");
    rawoutput("</form>");
    addnav("", "runmodule.php?module=hunterslodge_customtitle&op=confirm&context={$context}");
}
function titlechange_run()
{
    require_once "lib/sanitize.php";
    require_once "lib/names.php";
    global $session;
    $op = httpget("op");
    page_header("Hunter's Lodge");
    $pointsavailable = $session['user']['donation'] - $session['user']['donationspent'];
    $permcost = get_module_setting("permanent");
    if ($op == "permanent") {
        page_header("Unlimited Title Changes");
        output("For %s Donator Points, you can change your Title as often as you like without paying again.`n`n", $permcost);
        addnav("Unlimited Changes");
        if ($pointsavailable >= $permcost) {
            addnav(array("Get permanent free Title changes (%s Points)", $permcost), "runmodule.php?module=titlechange&op=permanentconfirm");
        } else {
            addnav(array("Sorry, but you need %s more Donator Points for this option.", $permcost - $pointsavailable), "");
        }
        addnav("Cancel", "lodge.php");
        page_footer();
    }
    if ($op == "permanentconfirm") {
        page_header("Unlimited Title Changes");
        output("You've got unlimited Title Changes!");
        addnav("Back to the Lodge", "lodge.php");
        set_module_pref("permanent", 1);
        $session['user']['donationspent'] += $permcost;
        page_footer();
    }
    if ($op == "titlechange") {
        if (get_module_pref("timespurchased")) {
            $cost = get_module_setting("extrapoints");
        } else {
            $cost = get_module_setting("initialpoints");
        }
        if (get_module_pref("permanent")) {
            $cost = 0;
        }
        if ($cost <= $session['user']['donation'] - $session['user']['donationspent']) {
            output("`3`bCustomize Title`b`0`n`n");
            output("`7Because you have earned sufficient points, you have been granted the ability to set a custom title of your choosing.");
            output("The title must be appropriate, and the admin of the game can reset if it isn't (as well as penalize you for abusing the game).");
            output("The title may not be more than 25 characters long including any characters used for colorization!.`n`n");
            $otitle = get_player_title();
            if ($otitle == "`0") {
                $otitle = "";
            }
            output("`7Your title is currently`^ ");
            rawoutput($otitle);
            output_notl("`0`n");
            output("`7which looks like %s`n`n", $otitle);
            if (httpget("err") == 1) {
                output("`\$Please enter a title.`n");
            }
            output("`7How would you like your title to look?`n");
            rawoutput("<form action='runmodule.php?module=titlechange&op=titlepreview' method='POST'>");
            rawoutput("<input id='input' name='newname' width='25' maxlength='25' value='" . htmlentities($otitle, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "'>");
            rawoutput("<input type='submit' class='button' value='Preview'>");
            rawoutput("</form>");
            addnav("", "runmodule.php?module=titlechange&op=titlepreview");
        } else {
            output("Sorry, but you don't have enough points to do that!`n`n");
        }
    } elseif ($op == "titlepreview") {
        $ntitle = rawurldecode(httppost('newname'));
        $ntitle = newline_sanitize($ntitle);
        if ($ntitle == "") {
            if (get_module_setting("blank")) {
                $ntitle = "`0";
            } else {
                redirect("runmodule.php?module=titlechange&op=titlechange&err=1");
            }
        }
        if (!get_module_setting("bold")) {
            $ntitle = str_replace("`b", "", $ntitle);
        }
        if (!get_module_setting("italics")) {
            $ntitle = str_replace("`i", "", $ntitle);
        }
        $ntitle = sanitize_colorname(get_module_setting("spaceinname"), $ntitle);
        $ntitle = preg_replace("/[`][cHw]/", "", $ntitle);
        $ntitle = sanitize_html($ntitle);
        $nname = get_player_basename();
        output("`7Your new title will look like this: %s`0`n", $ntitle);
        output("`7Your entire name will look like: %s %s`0`n`n", $ntitle, $nname);
        output("`7Is this how you wish it to look?");
        addnav("`bConfirm Custom Title`b");
        addnav("Yes", "runmodule.php?module=titlechange&op=changetitle&newname=" . rawurlencode($ntitle));
        addnav("No", "runmodule.php?module=titlechange&op=titlechange");
    } elseif ($op == "changetitle") {
        $ntitle = rawurldecode(httpget('newname'));
        $fromname = $session['user']['name'];
        $newname = change_player_ctitle($ntitle);
        $session['user']['ctitle'] = $ntitle;
        $session['user']['name'] = $newname;
        addnews("%s`^ has become known as %s.", $fromname, $session['user']['name']);
        // modifications by sixf00t4
        if (!get_module_pref("permanent")) {
            if (get_module_setting("take")) {
                $cost = 0;
                if (get_module_pref("timespurchased")) {
                    $cost = get_module_setting("extrapoints");
                    debuglog("bought another custom title for {$cost} points");
                } else {
                    $cost = get_module_setting("initialpoints");
                    debuglog("bought first custom title for {$cost} points");
                }
                $session['user']['donationspent'] += $cost;
            }
        }
        set_module_pref("timespurchased", get_module_pref("timespurchased") + 1);
        output("Your custom title has been set.");
        modulehook("namechange", array());
    }
    addnav("L?Return to the Lodge", "lodge.php");
    page_footer();
}