<?php
    $GLOBALS['highlight'] = 'userinfo';

    require_once('include/functions/pages.php');
            
    $members = mysqli_query_logged("SELECT * FROM members WHERE id = '" . $GLOBALS['auth']['id'] . "'");
    if (!$members_row = mysqli_fetch_assoc($members))
    {
        header('Location: ./?s=finished');
        die;
    }
    $members_extras = mysqli_query_logged("SELECT * FROM members_extras WHERE user_id = '" . $GLOBALS['auth']['id'] . "'");
    $members_extras_row = mysqli_fetch_assoc($members_extras);
    
    include('include/parts/header.php');
?>

<div class="header">User Information</div>
<?php
    if ($_notice)
    {
        echo '<div class="content">';
        echo '<span style="color: red;">None of your changes have been changed because...</span><br />';
        echo '<span style="color: red;">' . $_notice . '</span>';
        echo '</div>';
    }
?>
<div class="content">
<form action="?a=userinfo" method="post" style="margin: 0px;">
<table width="100%" border="0">
<tr><td width="150" style="text-align: right;">Full Name:</td><td><input name="fullname" size="40" value="<?php echo htmlspecialchars($members_extras_row['fullname']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">Birthday:</td><td><select name="month" style="width: 100px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;">
<option value="0">Month:</option>
<?php
    for ($i = 1; $i <= 12; $i++)
    {
        $selected = (substr($members_extras_row['birthday'], 5, 2) == $i) ? ' selected' : '';
        echo '<option value="' . date("m", mktime(0, 0, 0, $i, 1, 2000)) . '"' . $selected . '>' . date("F", mktime(0, 0, 0, $i, 1, 2000)) . '</option>';
    }
?>
</select>
<select name="day" style="width: 75px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;">
<option value="0">Day:</option>
<?php
    for ($i = 1; $i <= 31; $i++)
    {
        $selected = (substr($members_extras_row['birthday'], 8, 2) == $i) ? ' selected' : '';
        echo '<option value="' . date("d", mktime(0, 0, 0, 1, $i, 2000)) . '"' . $selected . '>' . date("d", mktime(0, 0, 0, 1, $i, 2000)) . '</option>';
    }
?>
</select>
<select name="year" style="width: 75px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;">
<option value="0">Year:</option>
<?php
    for ($i = date('Y') - 12; $i >= 1920; $i--)
    {
        $selected = (substr($members_extras_row['birthday'], 0, 4) == $i) ? ' selected' : '';
        echo '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
    }
?>
</select></td></tr>
<tr><td width="150" style="text-align: right;">Gender:</td><td><select name="gender" style="width: 150px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;">
<option value="0"<?php echo ($members_extras_row['gender'] == 0) ? ' selected' : '' ?>>Not Disclosed</option>
<option value="1"<?php echo ($members_extras_row['gender'] == 1) ? ' selected' : '' ?>>Male</option>
<option value="2"<?php echo ($members_extras_row['gender'] == 2) ? ' selected' : '' ?>>Female</option>
</select></td></tr>
<tr><td width="150" style="text-align: right;">Country:</td><td id="country_area"><select name="country" id="country" style="width: 250px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;" onChange="update_region(document.getElementById('country').value);">
<option value=""></option>
<?php
    $country_id = 0;
    $geo_countries = mysqli_query_logged("SELECT * FROM geo_countries ORDER BY country_id");
    while ($geo_countries_row = mysqli_fetch_assoc($geo_countries))
    {
        $selected = ($geo_countries_row['country_name'] == $members_extras_row['country']) ? ' selected' : '';
        if ($selected)
        {
            $country_id = $geo_countries_row['country_id'];
        }
        echo '<option value="' . $geo_countries_row['country_id'] . '"' . $selected . '>' . $geo_countries_row['country_name'] . '</option>';
    }    
?>
</select></td></tr>
<tr><td width="150" style="text-align: right;">Region:</td><td id="region_area"><select name="region" id="region" style="width: 250px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;" onChange="update_city(document.getElementById('region').value);">
<?php
    if ($country_id)
    {
        $regions = mysqli_query_logged("SELECT region_id, region_name FROM geo_regions WHERE country_id = '" . $country_id . "' ORDER BY region_name");
        if (mysqli_num_rows($regions))
        {
            while ($region_row = mysqli_fetch_array($regions))
            {
                $selected = ($members_extras_row['region'] == $region_row['region_name']) ? ' selected' : '';
                if ($selected)
                {
                    $region_id = $region_row['region_id'];
                }
                echo '<option value="' . $region_row['region_id'] . '"' . $selected . '>' . $region_row['region_name'] . '</option>';
            }
        }
        else
        {
            echo '<option value="">No Regions Available</option>';
        }
    }
    else
    {
        echo '<option value="">Choose a Country</option>';
    }
?>
</select></td></tr>
<tr><td width="150" style="text-align: right;">City:</td><td id="city_area"><select name="city" id="city" style="width: 250px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;">
<?php
    if ($region_id)
    {
        $cities = mysqli_query_logged("SELECT city_id, city_name FROM geo_cities WHERE region_id = '" . $region_id . "' ORDER BY city_name");
        if (mysqli_num_rows($cities))
        {
            while ($cities_row = mysqli_fetch_array($cities))
            {
                $selected = ($members_extras_row['city'] == $cities_row['city_name']) ? ' selected' : '';
                echo '<option value="' . $cities_row['city_id'] . '"' . $selected . '>' . $cities_row['city_name'] . '</option>';
            }
        }
        else
        {
            echo '<option value="">No Cities Available</option>';
        }
    }
    else
    {
        echo '<option value="">Choose a Region</option>';
    }
?>
</select></td></tr>
<tr><td width="150" style="text-align: right;">Website:</td><td><input name="website" size="30" value="<?php echo htmlspecialchars($members_extras_row['website']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">AOL Messenger:</td><td><input name="aim" size="20" value="<?php echo htmlspecialchars($members_extras_row['aim']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">ICQ Messenger:</td><td><input name="icq" size="20" value="<?php echo ($members_extras_row['icq'] != 0) ? htmlspecialchars($members_extras_row['icq']) : '' ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">MSN Messenger:</td><td><input name="msn" size="30" value="<?php echo htmlspecialchars($members_extras_row['msn']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">Yahoo Messenger:</td><td><input name="yahoo" size="30" value="<?php echo htmlspecialchars($members_extras_row['yahoo']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">Google Talk:</td><td><input name="gtalk" size="30" value="<?php echo htmlspecialchars($members_extras_row['gtalk']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
<tr><td width="150" style="text-align: right;">Options:</td><td>
<input name="privacy" type="checkbox" value="1"<?php echo $members_extras_row['privacy'] ? ' checked' : '' ?>> Show Messenger Info Only To My Friends<br />
<input name="notification" type="checkbox" value="1"<?php echo $members_extras_row['notification'] ? ' checked' : '' ?>> Email Notification On New Message<br />
</td></tr>
<tr><td width="150" style="text-align: right;"></td><td><input type="submit" value="Save User Info" style="border: solid 1px green; background: black; color: green;"></td></tr>
</table>
</form>
</div>
    
<script type="text/javascript">
    if (true)
    {
        init_province();
        init_city();
    }
    function init_region()
    {
        document.getElementById('region_area').innerHTML = '<select name="region" id="region" style="width: 250px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;" onChange="update_city(document.getElementById(\'region\').value);"><option value="">Choose a Country</option></select>';
    }
    function init_city()
    {
        document.getElementById('city_area').innerHTML = '<select name="city" id="city" style="width: 250px; border: solid 2px black; margin: 0px; padding-top: 1px; padding-bottom: 1px;"><option value="">Choose a Region</option></select>';    
    }
    function update_region(id)
    {
        init_region();
        init_city();        
        doAjax('index.php?j=regions&p=1&i=' + id, function(AJAX) { document.getElementById('region_area').innerHTML = AJAX.responseText; });
    }
    function update_city(id)
    {
        init_city();
        doAjax('index.php?j=regions&p=2&i=' + id, function(AJAX) { document.getElementById('city_area').innerHTML = AJAX.responseText; });
    }
    
</script>

<?php
    $community_permissions = mysqli_query_logged("SELECT * FROM community_permissions, community WHERE community_permissions.user_id = '" . $GLOBALS['auth']['id'] . "' AND community_permissions.permission = 'administrator' AND community_permissions.community_id = community.community_id AND community.community_deleted = '0'");
    if (mysqli_num_rows($community_permissions))
    {
        echo box_outside_top('Active Communities');
        echo box_inside_top();
        while ($community_permissions_row = mysqli_fetch_assoc($community_permissions))
        {
            echo '<a href="?s=community_modify&i=' . $community_permissions_row['community_id'] . '">' . htmlentities($community_permissions_row['community_name']) . '</a><br />';
        }
        echo box_inside_bottom();
        echo box_outside_bottom();
    }
?>    
    
<div class="header">Account Settings (Validation Required)</div>

<div class="content">
    <form action="?a=account" method="post" style="margin: 0px;">
    <table width="100%" border="0">
    <tr style="background: #444444;"><td width="150" style="text-align: right;">Old Password:</td><td><input name="old" size="40" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
    <tr><td width="150" style="text-align: right;">Username:</td><td><input name="username" size="40" value="<?php echo htmlspecialchars($members_row['username']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
    <tr><td width="150" style="text-align: right;">New Password:</td><td><input name="pass1" size="40" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
    <tr><td width="150" style="text-align: right;">Retype Password:</td><td><input name="pass2" size="40" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
    <tr><td width="150" style="text-align: right;">Email:</td><td><input name="email" size="40" value="<?php echo htmlspecialchars($members_row['email']) ?>" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>
    <tr><td width="150" style="text-align: right;"></td><td><input type="submit" value="Save Account Settings" style="border: solid 1px green; background: black; color: green;"></td></tr>
    </table>
    </form>
    <br />* Leave your password blank to keep it as it was. If your email address is being changed you will have to validate it before the changes will take effect.<br />
<?php
    $members_create = mysqli_query_logged("SELECT * FROM members_create WHERE user_id = '" . $GLOBALS['auth']['id'] . "'");
    if ($members_create_row = mysqli_fetch_array($members_create))
    {
        echo '<div style="padding: 10px 0px 0px 0px;"></div>';
        echo '<span style="color: #c7ff70; font-weight: bold;">Email Validation</span><br />';
        echo '<div style="padding: 10px 0px 0px 0px;"></div>';
        echo '<form action="?a=email" method="post" style="margin: 0px;">';
        echo 'In order to change your email to ' . $members_create_row['email'] . ' you must submit the validation code which was emailed to you. This will be cancelled in 12 hours if you do not receive it and you can try again.<br />';
        echo '<div style="padding: 10px 0px 0px 0px;"></div>';
        echo '<table width="100%" border="0">';
        echo '<tr><td width="150" style="text-align: right;">Validation Key:</td><td><input name="cache" size="40" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>';
        echo '<tr><td width="150" style="text-align: right;"></td><td><input type="submit" value="Update Email Address" style="border: solid 1px green; background: black; color: green;"></td></tr>';
        echo '</table>';
        echo '</form>';
    }
?>
    </td></tr></table>
</div>

<?php
    include('include/parts/footer.php');
?>