function userdata($ir, $lv, $fm, $dosessh = 1)
    {
        global $db, $c, $userid, $set, $home, $user, $casino, $twoplayer, $arcade;
        $IP = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
        $db->query("UPDATE users SET laston=unix_timestamp(),lastip='{$IP}' WHERE userid='{$userid}'");
        $u = $ir['username'];
        $bgcolor = 'CDCDCD';
        if ($ir['guest'] == 0) {
            $logouttxt = sprintf('<a href="#" onClick="FB.Connect.logout(function() { refresh_page(); })">' . 'Logout' . '</a>', $_SERVER['REQUEST_URI']);
            $logouttxt = " | " . $logouttxt;
        } else {
            $fb_active_session = FALSE;
            // DISPLAY PAGE WHEN USER IS NOT LOGGED IN TO FB CONNECT
            $logouttxt = '&nbsp;&nbsp;<div class="fbconnect_login" style="display:inline;">';
            $logouttxt .= render_fbconnect_button('medium');
            $logouttxt .= '</div>';
        }
        print <<<EOF
\t\t<body>
\t\t<div id="wrapper">
\t\t\t<div id="nav">
\t\t\t\t<div id="logo">
\t\t\t\t\t<img src="./img/logo.jpg" width="55" height="15" alt="MRWQ" />
\t\t\t\t\t<br /><span id="logo_text">Play 2-player games against your friends!</span>
\t\t\t\t</div>
\t\t\t\t<div id="login">
\t\t\t\t\t<table><tr><td>Welcome, {$ir['username']}</td><td>{$logouttxt}</td></tr></table>
\t\t\t\t</div>
\t\t\t</div>
\t\t\t<div id="graphic">
\t\t\t\t<img src="./img/top-graphic.jpg" width="874" height="168" alt="MrWQ" />
\t\t\t</div>
\t\t\t<div id="main_links">
\t\t\t\t<ul>
EOF;
        $htext = "";
        $twoptext = "";
        $casinotext = "";
        $arctext = "";
        if ($home == 1) {
            $htext = " class=\"current\"";
        }
        if ($twoplayer == 1) {
            $twoptext = " class=\"current\"";
        }
        if ($casino == 1) {
            $casinotext = " class=\"current\"";
        }
        if ($arcade == 1) {
            $arctext = " class=\"current\"";
        }
        echo <<<EOF
\t\t\t\t\t<li {$htext}><a href="index.php"><span>Home</span></a></li>
\t\t\t\t\t<li {$twoptext}><a href="twoplayergames.php"><span>2-Player Games</span></a></li>
\t\t\t\t\t<li {$casinotext}><a href="casinogames.php"><span>Casino Games</span></a></li>
\t\t\t\t\t<li {$arctext}><a href="arcade.php"><span>Arcade Games</span></a></li>
\t\t\t\t</ul>
\t\t\t</div>
\t\t\t<div id="left_nav">
\t\t\t\t<table>
\t\t\t\t\t<tr>
\t\t\t\t\t\t<td class="header" colspan="2"><center>{$ir['username']}
EOF;
        print "\t\t\t\t\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr valign=\"top\">\n\t\t\t\t\t\t<td class=\"subinfo\" colspan=\"2\">\n\t\t\t\t\t\t\t<div>\n\t\t";
        print "<strong>Q-Cash:</strong> {$fm}";
        echo <<<EOF
\t\t
\t\t\t\t\t\t\t</div>\t\t\t
\t\t\t\t\t\t</td>
\t\t\t\t\t</tr>
\t\t\t\t</table>
EOF;
        global $challengeheadertext;
        if ($_GET['challengeid']) {
            include "challenge.php";
        }
        $pendingchal = $db->num_rows($db->query("SELECT `to` FROM challenge WHERE `to`={$userid} AND accepted!={$userid}"));
        if ($pendingchal) {
            $tick = $db->query("SELECT * FROM challenge WHERE `to`={$userid} AND accepted!={$userid} ORDER BY rand() LIMIT 1");
            $tic = $db->fetch_row($tick);
            $unamef = $db->fetch_row($db->query("SELECT username,laston FROM users WHERE userid={$tic['from']}"));
            $gnamef = $db->fetch_row($db->query("SELECT * FROM multgames WHERE short='{$tic['game']}'"));
            if ($unamef['laston'] > time() - 60 * 15 || $tic['time_left'] > 40) {
                if ($tic['time_left'] == 86400) {
                    $tleng = "1 day";
                } else {
                    if ($tic['time_left'] == 259200) {
                        $tleng = "3 days";
                    } else {
                        if ($tic['time_left'] == 604800) {
                            $tleng = "1 week";
                        } else {
                            $tleng = "30 seconds";
                        }
                    }
                }
                $challengeheadertext = "<table class='table' width=95%><tr>";
                $challengeheadertext .= "<td id='newsheader'><center><strong>{$unamef['username']} challenged you to a game of {$gnamef['name']} with turns of {$tleng} each!\n\t\t<br /><a href='index.php?challengeid={$tic['id']}&act=accept'><font color=green>Accept</font></a> | <a href='index.php?challengeid={$tic['id']}&act=reject'><font color=red>Reject</font></a></strong></center></td>";
                $challengeheadertext .= "</tr></table><br />";
            }
        }
        $challhead = $db->query("SELECT * FROM challenge WHERE (`to` = {$userid} OR `from`={$userid}) AND accepted!={$userid}");
        $challheada = $db->num_rows($challhead);
        if ($challheada > 0) {
            echo <<<EOF
\t\t\t\t<table>
\t\t\t\t\t<tr>
\t\t\t\t\t\t<td class="header" colspan="2">My Challenges</td>
\t\t\t\t\t</tr>
\t
EOF;
            while ($chh = $db->fetch_row($challhead)) {
                if ($userid == $chh['from']) {
                    if ($chh['accepted'] != $chh['to']) {
                        $usrfull = $db->fetch_row($db->query("SELECT username FROM users WHERE userid='{$chh['to']}'"));
                        $gamfull = $db->fetch_row($db->query("SELECT * FROM multgames WHERE short='{$chh['game']}'"));
                        if (!$usrfull['username']) {
                            $friendinf = $user->fbc_get_friend_name($chh['to']);
                            if (empty($friendinf)) {
                                $usrfull['username'] = "******";
                            } else {
                                $usrfull['username'] = $friendinf[0]['first_name'] . ' ' . $friendinf[0]['last_name'];
                            }
                        }
                        print "\n\t\t\t\t<tr>\n\t\t\t\t\t<td class=\"sub\" colspan=\"2\">» {$usrfull['username']} - {$gamfull['name']} <br />\n\t\t\t\t\t\t<center><i>Pending...</i><a href='index.php?challengeid={$chh['id']}&act=cancel'><font color=red>Cancel?</font></a></center></td>\n\t\t\t\t</tr>\n\t\t\t\t";
                    } else {
                        $usrfull = $db->fetch_row($db->query("SELECT username FROM users WHERE userid='{$chh['to']}'"));
                        $gamfull = $db->fetch_row($db->query("SELECT * FROM multgames WHERE short='{$chh['game']}'"));
                        print "\n\t\t\t\t<tr>\n\t\t\t\t\t<td class=\"sub\" colspan=\"2\">» {$usrfull['username']} - {$gamfull['name']} <br />\n\t\t\t\t\t\t<center><font color=green>Live! <a href='sb_game.php?g={$chh['game']}&act=joining&challengeid={$chh['id']}&id={$chh['room']}'>Join Game!</a></font></center></td>\n\t\t\t\t</tr>\n\t\t\t\t";
                    }
                } else {
                    $usrfull = $db->fetch_row($db->query("SELECT username,laston FROM users WHERE userid='{$chh['from']}'"));
                    $gamfull = $db->fetch_row($db->query("SELECT * FROM multgames WHERE short='{$chh['game']}'"));
                    if ($usrfull['laston'] > time() - 60 * 15 || $chh['time_left'] > 40) {
                        print "\n\t\t\t\t<tr>\n\t\t\t\t\t<td class=\"sub\" colspan=\"2\">» {$usrfull['username']} - {$gamfull['name']} <br />\n\t\t\t\t\t\t<center><a href='?challengeid={$chh['id']}&act=accept'><font color=green>Accept</font></a> / <a href='?challengeid={$chh['id']}&act=reject'><font color=red>Reject</font></a></center></td>\n\t\t\t\t</tr>\n\t\t\t\t";
                    } else {
                        print "\n\t\t\t\t<tr>\n\t\t\t\t\t<td class=\"sub\" colspan=\"2\">» {$usrfull['username']} - {$gamfull['name']} <br />\n\t\t\t\t\t\t<center><font color=grey>-Friend Currently Offline</font></center></td>\n\t\t\t\t</tr>\n\t\t\t\t";
                    }
                }
            }
            print "</table>";
        }
        echo <<<EOF
\t\t\t\t<table>
\t\t\t\t\t<tr>
\t\t\t\t\t\t<td class="header" colspan="2">2-Player Games I'm In</td>
\t\t\t\t\t</tr>
\t
EOF;
        $gan22 = $db->query("SELECT * FROM multgames ORDER BY name ASC");
        $tot = 0;
        while ($gan2 = $db->fetch_row($gan22)) {
            $short = $gan2['short'];
            $roomsin = check_room($short, $ir['userid']);
            if (is_array($roomsin)) {
                foreach ($roomsin as &$room) {
                    $ar = $db->fetch_row($db->query("SELECT p1,p2,turn FROM {$short}_room WHERE id={$room}"));
                    if ($ar['p1'] && $ar['p2']) {
                        if ($ar['turn'] == $ir['userid']) {
                            $acttxt = "<font color=green><b>Play</b></font>";
                        } else {
                            $acttxt = "Play";
                        }
                    } else {
                        $acttxt = "Waiting";
                    }
                    echo <<<EOF
\t\t\t\t\t<tr>
\t\t\t\t\t\t<td class="sub" colspan="2">» {$gan2['name']} [<a href='sb_game.php?g={$short}&id={$room}'>{$acttxt}</a>] [<a href='sb_rooms.php?g={$short}&act=leave&id={$room}'>Leave</a>]</td>
\t\t\t\t\t</tr>
EOF;
                }
                $tot++;
            }
        }
        if ($tot == 0) {
            echo <<<EOF
\t\t\t\t<tr>
\t\t\t\t\t<td class="sub" colspan="2"><i>None... :(</i></td>
\t\t\t\t</tr>
EOF;
        } else {
            print "\n\t\t<tr><td class=\"sub\" colspan=\"2\" align='center'>\n\t\tIf 'Play' is green, it's your turn.\n\t\t</td></tr>\n\t\t";
        }
        print "</table>";
        if ($ir['guest'] == 0) {
            $friends = $user->fbc_get_all_friends();
            $friusers = array();
            foreach ($friends as $friend) {
                $friusers[] = $friend['uid'];
            }
            $allfriends = implode(",", $friusers);
            if (empty($friend)) {
                $html = '<table>';
                $html .= '<tr><td class="header" colspan="2">' . $title . '</td></tr>';
                $html .= '<tr><td class="sub" colspan="2">No friends here!</td></tr>';
                $html .= '</table>';
                print $html;
            } else {
                $html = '<table>';
                $html .= '<tr><td class="header" colspan="2"><center>Friends on our Site</center></td></tr>';
                $fris = $db->query("SELECT * FROM users WHERE userid IN ({$allfriends}) ORDER BY laston DESC");
                while ($fri = $db->fetch_row($fris)) {
                    $newname = str_replace(" ", "_", $fri['username']);
                    $laston = $fri['laston'];
                    $ctime = time();
                    $last15 = time() - 60 * 15;
                    $last60 = time() - 60 * 60;
                    if ($laston > $last15) {
                        $lastonimg = "<img src='images/online.png'> ";
                    } else {
                        if ($laston > $last60) {
                            $lastonimg = "<img src='images/idle.png'> ";
                        } else {
                            $lastonimg = "<img src='images/offline.png'> ";
                        }
                    }
                    $html .= '<tr><td class="sub" colspan="2">' . $lastonimg . '<a href="javascript:void(0)" onclick="javascript:chatWith(\'' . $newname . '\');">
						' . $fri['username'] . '</a>
						[<a href="challenge.php?act=prechallenge&player=' . $fri['userid'] . '">C</a>] 
						[<a target="_blank" href="http://www.facebook.com/profile.php?id=' . $fri['userid'] . '">P</a>]</td></tr>';
                }
                $html .= '<tr><td class="sub" colspan="2" align="center"><b>C</b> = Challenge | <b>P</b> = Profile</td></tr>
					<tr><td class="sub" colspan="2" align="center">Click a friend\'s name to chat.</td></tr></table>';
                print $html;
            }
        } else {
            $fb_active_session = FALSE;
            // DISPLAY PAGE WHEN USER IS NOT LOGGED IN TO FB CONNECT
            print '<br /><div class="fbconnect_login"><center>';
            print render_fbconnect_button('medium');
            print '</center></div>';
        }
        echo <<<EOF
\t\t\t</div>
\t\t\t<div id="content">
EOF;
        print $challengeheadertext;
    }
         if ($numf['p2'] == 0) {
             $position = 'p2';
             $pp = 2;
         } else {
             die("Sorry, that room is full. <br /><a href=sb_rooms.php?g={$gpre}>&gt;Back</a>");
         }
     }
     $db->query("UPDATE {$gpre}room SET {$position}={$userid},play_time=unix_timestamp() WHERE id={$_GET['t']}", $c);
     $db->query("UPDATE users SET {$gpre}room={$_GET['t']}, money=money-{$numf['bet']} WHERE userid={$userid}", $c);
     $txt = "{$ir['username']} has joined the table.";
     $db->query("INSERT INTO {$gpre}chat ({$gpre}room, timestamp, txt) VALUES({$numf['id']}, unix_timestamp(), '{$txt}')");
     print "You have joined {$numf['name']}. <br /><a href=sb_game.php?g={$gpre}&id={$_GET['t']}>Redirecting (or click here)</a>...\n\t<META HTTP-EQUIV=\"refresh\" content=\"3;URL=sb_game.php?g={$gpre}&id={$_GET['t']}\">";
 } else {
     if ($_GET['act'] == 'leave') {
         $id = abs((int) $_GET['id']);
         $toleaverm = check_room($gpres, $ir['userid']);
         if (!is_array($toleaverm)) {
             die("You aren't currently in a room. <br />&gt;<a href=sb_rooms.php?g={$gpre}>Back</a>");
         }
         if (!in_array($id, $toleaverm)) {
             die("Bad room id. <br />&gt;<a href=sb_rooms.php?g={$gpre}>Back</a>");
         }
         $num2 = $db->query("SELECT * FROM {$gpre}room WHERE id={$id}");
         $nu = $db->fetch_row($num2);
         $gamestarted = $db->num_rows($db->query("SELECT {$gpre}room FROM {$gpre}game WHERE {$gpre}room={$id}"));
         if (!$gamestarted && ($nu['p1'] == $ir['userid'] && $nu['p2'] == 0 || $nu['p2'] == $ir['userid'] && $nu['p1'] == 0) || $nu['play_time'] == 0) {
             //person left before anyone else joined, give bet back OR if no moves yet made
             $db->query("UPDATE users SET money=money+{$nu['bet']} WHERE userid={$ir['userid']}");
         }
         /*****NEW STUFF****/
         if (($nu['p1'] > 0 && $nu['p2'] > 0 || $nu['p1'] > 0 && $nu['p2'] > 0) && $gamestarted == 1 && $nu['play_time'] > 0) {