<?
include('config.php');

$f = '';

function show_items_up($us, $type) {
  $r .= '';
  $level = $type-1;
  $items = array();
  if($type < 8 && $type > 13) {
    $r = '<tr><td>Error code #1</td></tr>';
  } elseif($type >=8 && $type <= 13) {
    $sp = mysql_query('SELECT `id`, `name`, `nlevel` FROM `inventory` WHERE `owner` = "'.$us.'" AND (`prototype` >= 143 AND `prototype` <= 153) AND `point` > 0 AND `nlevel` = "'.$level.'" AND `dressed` = 0 ORDER BY `id` DESC');
    if(mysql_num_rows($sp) > 0) {
      $r .= '<span id="menu">';
      while($pl = mysql_fetch_array($sp)) {
        $r .= '<tr><td><div id="'.$pl['id'].'"><input type="radio" id="'.$pl['id'].'" name="item" class="select" /><b>'.$pl['name'].' ['.$pl['nlevel'].']</b> (id:'.$pl['id'].')</div></td></tr>';
      }
      $r .= '<tr><td><input type="button" value="Выбрать" onclick="GoUpdate(); return false;" /></td></tr>';
      $r .= '<span>';
    } else {
      $r = '<tr><td>У вас нет артефактов для улучшения.</td></tr>';
    }
  }
  return $r;
}

function show_items_down($us, $type) {
  $r .= '';
  $level = $type+1;
  $items = array();
  if($type > 12 && $type < 7) {
    $r = '<tr><td>Error code #1</td></tr>';
  } elseif($type >= 7 && $type <= 12) {
    $sp = mysql_query('SELECT `id`, `name`, `nlevel` FROM `inventory` WHERE `owner` = "'.$us.'" AND (`prototype` >= 143 AND `prototype` <= 153) AND `point` > 0 AND `nlevel` = "'.$level.'" AND `dressed` = 0 ORDER BY `id` DESC');
    if(mysql_num_rows($sp) > 0) {
      $r .= '<span id="menu">';
      while($pl = mysql_fetch_array($sp)) {
        $r .= '<tr><td><div id="'.$pl['id'].'"><input type="radio" id="'.$pl['id'].'" name="item" class="select" /><b>'.$pl['name'].' ['.$pl['nlevel'].']</b> (id:'.$pl['id'].')</div></td></tr>';
      }
      $r .= '<tr><td><input type="button" value="Выбрать" onclick="GoDowngrade(); return false;" /></td></tr>';
      $r .= '<span>';
    } else {
      $r = '<tr><td>У вас нет артефактов для понижения уровня.</td></tr>';
    }
  }
  return $r;
}

if($_GET['user_id']) {
  if($_GET['up'] == 1) {
    $usid = abs($_GET['user_id']);
    $level = abs($_GET['level']);
    $us = mysql_fetch_array(mysql_query('SELECT `id` FROM `users` WHERE `id` = "'.$usid.'" LIMIT 1'));
    if(isset($us['id'])) {
      $f = show_items_up($us['id'], $level);
    } else {
      $f = '<tr><td>Авториируйтесь в игре</td></tr>';
    }
  } elseif($_GET['down'] == 1) {
    $usid = abs($_GET['user_id']);
    $level = abs($_GET['level']);
    $us = mysql_fetch_array(mysql_query('SELECT `id` FROM `users` WHERE `id` = "'.$usid.'" LIMIT 1'));
    if(isset($us['id'])) {
      $f = show_items_down($us['id'], $level);
    } else {
      $f = '<tr><td>Авториируйтесь в игре</td></tr>';
    } 
  }
}

?>
<table width="100%" cellspacing="1" cellpadding="2" border="0" bgcolor="#FFF6DD">
  <tbody>
    <?=$f;?>
  </tbody>
</table>