<?php
include("connect.php");
include("login_cookie.php");

$buyquery = $db_community;

if ($_GET["choose"] == "buy" && isset($_GET[buyid])) {

if (mysql_num_rows($buyquery) == 1) {

while($check_logon_com = mysql_fetch_array($buyquery)) {


if ($_COOKIE[pass] == $check_logon_com[kode] && $_COOKIE[com] == "1" || mysql_num_rows($db_community) == 0) {

	if ($check_logon_com["exist"] == "1") {

$getuser = mysql_query("SELECT * FROM `pytox_community` WHERE `bruger` LIKE '{$_COOKIE[user]}' LIMIT 1") or die(mysql_error());

 while ($bruger = mysql_fetch_array($getuser)) {
$timerialt = $bruger[timer]/60;

///////////////////////////////////
///////////////////////////////////
///////////////////////////////////

$t = $timerialt;

if ($bruger[admin] == "1") {
$lvl = "21";
} else if ($t >= "10000") {
$lvl = "21";
} else if ($t >= "7500") {
$lvl = "20";
} else if ($t >= "6000") {
$lvl = "19";
} else if ($t >= "5000") {
$lvl = "18";
} else if ($t >= "3600") {
$lvl = "17";
} else if ($t >= "2600") {
$lvl = "16";
} else if ($t >= "2000") {
$lvl = "15";
} else if ($t >= "1500") {
$lvl = "14";
} else if ($t >= "1100") {
$lvl = "13";
} else if ($t >= "900") {
$lvl = "12";
} else if ($t >= "650") {
$lvl = "11";
} else if ($t >= "450") {
$lvl = "10";
} else if ($t >= "300") {
$lvl = "9";
} else if ($t >= "200") {
$lvl = "8";
} else if ($t >= "130") {
$lvl = "7";
} else if ($t >= "80") {
$lvl = "6";
} else if ($t >= "50") {
$lvl = "5";
} else if ($t >= "20") {
$lvl = "4";
} else if ($t >= "10") {
$lvl = "3";
} else if ($t >= "3") {
$lvl = "2";
} else if ($t == "0") {
$lvl = "1";
} else {
$lvl = "0";
}
///////////////////////////////////
///////////////////////////////////
///////////////////////////////////

$getthing = mysql_query("SELECT * FROM `pytox_tingdb` WHERE `id` LIKE '{$_GET[buyid]}' LIMIT 1") or die(mysql_error());

  while ($tingen = mysql_fetch_array($getthing)) {
if ($bruger[coins] < $tingen[price]) {
print "Du har ikke råd til denne ting";
} else if ($lvl < $tingen[level]) {
print "Du er ikke i højt nok level!";
} else if ($tingen[instore] <= 0) {
print "Der er ikke flere ting, af denne slags, på lager!";
} else if ($bruger[coins] >= $tingen[price] && $lvl >= $tingen[level]) {

$newcoins = $bruger[coins]-$tingen[price];

mysql_query("INSERT INTO `pytox_ting` (`id`, `exist`, `owner`, `bought`, `thingid`) VALUES ('', '1', '$bruger[id]', '$tingen[price]', '$tingen[id]')") or die (mysql_error());

mysql_query("UPDATE `pytox_community` SET `coins`='{$newcoins}' WHERE `bruger` LIKE '{$_COOKIE[user]}' LIMIT 1") or die(mysql_error());

$newinstore = $tingen[instore]-1;

mysql_query("UPDATE `pytox_tingdb` SET `instore`='$newinstore' WHERE `id` LIKE '{$tingen[id]}' LIMIT 1") or die(mysql_error());

print "Du har nu købt tingen!";
print "<br />";
print "<a href=\"?site=Shop\">Tilbage til shoppen</a>";


} else {
print "En fejl opstod!";
}
  }

 }

 	} else {//exist
	header("Location: index.php?message=failure&errorid=60065");
	}//exist
} else {

print "Du skal være logget ind, før du kan købe ting!";
}

}//whilen 

} else {

print "Du skal være logget ind, før du kan købe ting!";
}

} else if ($_GET["choose"] == "resale" && isset($_GET["saleid"])) {

//print "Stadium 1";

$gettting = mysql_query("SELECT * FROM `pytox_ting` WHERE `id` LIKE '{$_GET['saleid']}' LIMIT 1") or die(mysql_error());

while ($theting = mysql_fetch_array($gettting)) {

//print "Stadium 1.2";

$minething = mysql_query("SELECT * FROM `pytox_community` WHERE `id` LIKE '{$theting['owner']}' LIMIT 1") or die(mysql_error());

while ($ado = mysql_fetch_array($minething)) {

if ($ado['bruger'] == $_COOKIE['user']) {

//print "Stadium 2";

$getttingid = mysql_query("SELECT * FROM `pytox_tingdb` WHERE `id` LIKE '$theting[thingid]' LIMIT 1") or die(mysql_error());

while ($reting = mysql_fetch_array($getttingid)) {

//print "Stadium 3";

$pris = $reting[price]/2;

$getuser = mysql_query("SELECT * FROM `pytox_community` WHERE `bruger` LIKE '$_COOKIE[user]' LIMIT 1") or die(mysql_error());

 while ($user = mysql_fetch_array($getuser)) {

//print "Stadium 4";

$newprice = $user[coins]+$pris;

//print "Newprice=".$newprice;

$newinstore = $reting[instore]+1;

mysql_query("UPDATE `pytox_community` SET `coins`='{$newprice}' WHERE `bruger` LIKE '$user[bruger]' LIMIT 1") or die(mysql_error());

mysql_query("UPDATE `pytox_tingdb` SET `instore`='{$newinstore}' WHERE `id` LIKE '{$reting[id]}' LIMIT 1") or die(mysql_error());

mysql_query("DELETE FROM `pytox_ting` WHERE `id` LIKE '{$theting['id']}' LIMIT 1") or die(mysql_error());

print "Tingen blev solgt for gensalgsværdien.";
///////////////////////////
///////////////////////////

 }

}
} else {
print "Du kan kun sælge dine egne ting!";
}
}
}

} else {

?>
<strong>Ting du kan købe:</strong>
<br />
<br />
<table class="blackborder" cellpadding="0" cellspacing="1">
 <tr>
<?php
$all = mysql_query("SELECT * FROM `pytox_ting`") or die(mysql_error());

$perlinje = 4;

	for ($i = 0;$i <= mysql_num_rows($all);$i = $i+$perlinje) {

//$thingstobuy = mysql_query("SELECT * FROM `pytox_tingdb` ORDER BY `price`") or die(mysql_error());
//while ($ting = mysql_fetch_array($thingstobuy)) {

$inry = "SELECT * FROM `pytox_tingdb` ORDER BY `id` ASC LIMIT ".$i.", ".$perlinje;
$query['tagwall'] = mysql_query($inry) or die(mysql_error());

while ($ting = mysql_fetch_array($query['tagwall'])) {

if ($u == 0) {
$u = 1;
$bgcolor = "#CCC";
$color = "#000";
} else if ($u == 1) {
$u = 2;
$bgcolor = "#FFF";
$color = "#000";
} else if ($u == 2) {
$u = 0;
$bgcolor = "#EEE";
$color = "#000";
} else {
$u = 1;
$bgcolor = "#CCC";
$color = "#000";
}

?>
  <td valign="top" align="center" style="background-color:<?=$bgcolor?>;">
<strong><?=$ting[name]?></strong> <div class="copy" style="display:inline;">[<a href="javascript:;" onclick="showThing('ting<?=$ting[id]?>')">vis/skjul</a>]</div>
<br />
<div id="ting<?=$ting[id]?>" style="display:block;">
<img src="<?=$ting[image]?>" class="blackborder" />
<br />
Pris: <?=$ting[price]?>
<br />
Denne ting kræver level <?=$ting[level]?>
<br />
På lager: <?=$ting[instore]?>
<br />
<br />
<a href="?site=Shop&amp;choose=buy&amp;buyid=<?=$ting[id]?>" title="Køb denne ting"><strong>Køb denne ting</strong></strong>
</div>
  </td>
<?php
}
print "</tr>";
}
?>
</table>
<?php
}//$_GET?
?>