<?php

include "includes/core.php";

define('INCLUDE_CHECK',1);
require "connect.php";

if(!$_POST)
{
	if($_SERVER['HTTP_REFERER'])
	header('Location : '.$_SERVER['HTTP_REFERER']);
	
	exit;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Parfait, tu passes &agrave; la suite</title>

<link rel="stylesheet" type="text/css" href="style.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>

<script type="text/javascript" src="simpletip/jquery.simpletip-1.3.1.pack.js.txt"></script>


<script type="text/javascript" src="boutique/script.js"></script>

</head>

<body>

<div id="main-container">

    <div class="container">
    
    	<span class="top-label">
            <span class="label-txt">Ta commande</span>
        </span>
        
        <div class="content-area">
    
    		<div class="content">
            	
                <?php
				
				$cnt = array();
				$products = array();
				
				foreach($_POST as $key=>$value)
				{
					$key=(int)str_replace('_cnt','',$key);
				
					$products[]=addslashes($key);
					$cnt[$key]=addslashes($value);
				}

				$result = mysql_query("SELECT * FROM internet_shop WHERE id IN(".join($products,',').")");
				
				if(!mysql_num_rows($result))
				{
					echo '<h1>Une erreur s\'est produite durant la commande :</h1>';
				}
				else
				{
					$result2 = mysql_query("SELECT * FROM users WHERE name='".$user->name."'");
					while($row2=mysql_fetch_assoc($result2))
					{
	
						
							// $total+=$cnt[$row['id']]*$row['price'];
							
						
						while($row=mysql_fetch_assoc($result))
						{
						$total+=$cnt[$row['id']]*$row['price'];
						
						echo $total*2;
						if ($row2['bobz'] < $total*2) { echo"<script>alert('Tu n\'as pas assez de bobz');</script>"; exit;}
							echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>';
							
							if ($row['type'] = "mobi" OR $row['type'] = "mobis" OR $row['type'] = "item" OR $row['type'] = "items") {
							$tid = $row['plus'];
							mysql_query("INSERT INTO furniture (tid,ownerid,roomid) VALUES ('".$tid."','".$user->id."','0')"); }
							if ($row['type'] = "vip") { mysql_query("UPDATE users SET vipday=vipday+".$row['plus']." WHERE name='".$user->name."'");
							mysql_query("UPDATE users SET rank=3 WHERE name='".$user->name."' AND rank <= 3"); 
							mysql_query("INSERT INTO users_badges (userid, badgeid) VALUES ('".$user->id."','VIP1')"); }
							
							$total+=$cnt[$row['id']]*$row['price'];
						}
						
							mysql_query("UPDATE users SET bobz=bobz-".$total." WHERE name='".$user->name."'");
							
							echo '<h1>Merci, ta commande est valid&eacute;e et pay&eacute;e</h1>';
							
						
						
						// echo '<div class="product"><img src="img/products/'.$row['img'].'" alt="'.htmlspecialchars($row['name']).'" width="128" height="128" class="pngfix" /></div>';
					}
					
					
					
					while($row=mysql_fetch_assoc($result))
					{
						echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>';
						
						$total+=$cnt[$row['id']]*$row['price'];
					}
		
					//echo '<h1>Total: '.$total.' Bobz</h1>';
				}
				?>
                <meta http-equiv="Refresh" content="5;URL=/me">
                
       	        <div class="clear"></div>
            </div>

        </div>
        
        <div class="bottom-container-border">
        </div>

    </div>

</div>

</body>
</html>
