<?php require_once 'protect.php'; ?>
<?php include 'itemprice.php'; ?>
<?php if ($available==0) { ?>
<?php echo $textnotavailable ?>
<?php } ?>

<?php if ($type==2 AND ($endprice!='0.00' OR $inquiry==1) AND $available==1) { ?>
<!--cart -->
<?php
$result11 = $mysqli->query("SELECT * FROM cart ORDER BY id desc");
$row = $result11->fetch_array();
$activeid1=$row['active_id'];
$activeid=$activeid1 + 1;
?>
<form name="formadd" method="post" class="formauto">
<input type="hidden" name="id" value="<?php echo $id ?>">
<input type="hidden" name="search" value="<?php echo $search ?>">
<!--sloop -->
<?php
$result12 = $mysqli->query("SELECT * FROM categories WHERE type=3 AND lang_id=$lang AND publish='yes' AND priority=1");
$nrrowscat = mysqli_num_rows($result12);
while($row = $result12->fetch_array()) {
$scatid=$row['insert_id'];
$scategory=$row['name'];
$scactive=$row['active'];

$result13 = $mysqli->query("SELECT id FROM extras WHERE type=1 AND lang_id=$lang AND item_id=$id AND category_id=ANY (SELECT insert_id FROM categories WHERE type=3 AND lang_id=$lang AND publish='yes' AND priority=1)");
$nrrowss = mysqli_num_rows($result13);

if (isset($_POST['submitcart'.$id]) && $nrrowss>0) {
$id = $mysqli->real_escape_string(stripslashes($_REQUEST['id']));	
$extraid = $mysqli->real_escape_string(stripslashes($_REQUEST["extraid$scatid"]));
$quantity = $mysqli->real_escape_string(stripslashes($_REQUEST['quantity'.$id]));

$result17 = $mysqli->query("SELECT * FROM extras WHERE type=1 AND insert_id='$extraid'");
$row = $result17->fetch_array();
$sprice=$row['price'];
$sweight=$row['weight'];

if ($sprice!='0.00' && $scactive==1) {
$endprice=$sprice;
}
if ($sweight!='0.00') {
$weight=$sweight;
}

$result14 = $mysqli->query("SELECT * FROM cart WHERE cookie_id='$cookieid' AND item_id=$id AND extra_id='$extraid' AND category_id='$scatid'");
$row = $result14->fetch_array();
$active_id=$row['active_id'];
$quantity1=$row['quantity'];
$quantity2=$quantity1+$quantity;

if ($quantity1>0 && $quantity>0 && $nrrowscat==1) {
$mysqli->query("UPDATE cart SET quantity=$quantity2 WHERE active_id='$active_id'");	
}
if ($extraid) {
$mysqli->query("INSERT INTO cart (active_id, cookie_id, item_id, category_id, extra_id, name, code, price, weight, quantity) VALUES ('$activeid', '$cookieid', '$id', '$scatid', '$extraid', '$name', '$code', '$endprice', '$weight', '$quantity')");	
}
if ($sprice && $sprice!='0.00' && $scactive==1) {
$mysqli->query("UPDATE cart SET price='$sprice' WHERE active_id=$activeid");	
}
}?>
<!--specs -->
<?php if ($nrrowss>0 AND (($endprice=='0.00' AND $inquiry==1) OR ($endprice!='0.00' AND $inquiry==0))) {
$result15 = $mysqli->query("SELECT id FROM extras WHERE type=1 AND insert_id NOT IN (SELECT extra_id FROM cart) AND item_id=$id AND category_id='$scatid' AND lang_id=$lang");
$nrrowss15 = mysqli_num_rows($result15);
?>
<?php if ($nrrowss15>0) {?>
<select name="extraid<?php echo $scatid ?>" class="formscart">
<?php
$result16 = $mysqli->query("SELECT * FROM extras WHERE type=1 AND insert_id NOT IN (SELECT extra_id FROM cart) AND item_id=$id AND category_id='$scatid' AND lang_id=$lang ORDER BY priority");
while($row = $result16->fetch_array()) {	
$sid=$row['insert_id'];
$extra=$row['name'];
$color=$row['color'];
$sprice=$row['price'];
?>
<option value="<?php echo $sid ?>"><?php echo $extra ?> <?php if ($sprice!='0.00' && $scactive==1){ echo "&raquo; ".$sprice." &#8364;"; } ?></option>
<?php } ?>
</select>
<?php }?>
<?php }?>
<!--end specs -->
<?php } ?>
<!--end sloop -->
<!--nospecs -->
<?php if ($nrrowscat==0 OR $nrrowss==0) {
	
if (isset($_POST['submitcart'.$id])) {
$id = $mysqli->real_escape_string(stripslashes($_REQUEST['id']));	
$quantity = $mysqli->real_escape_string(stripslashes($_REQUEST['quantity'.$id]));

$result14 = $mysqli->query("SELECT * FROM cart WHERE cookie_id='$cookieid' AND item_id=$id");
$row = $result14->fetch_array();
$active_id=$row['active_id'];
$quantity1=$row['quantity'];
$quantity2=$quantity1+$quantity;

if ($quantity1>0 && $quantity>0) {
$mysqli->query("UPDATE cart SET quantity=$quantity2 WHERE active_id='$active_id'");	
} else {
$mysqli->query("INSERT INTO cart (active_id, cookie_id, item_id, name, code, price, weight, quantity) VALUES ('$activeid', '$cookieid', '$id', '$name', '$code', '$endprice', '$weight', '$quantity')");		
}
}
}?>
<!--end nospecs -->
<?php
if (($endprice=='0.00' AND $inquiry==1) OR ($endprice!='0.00' AND $inquiry==0)) { ?>  
<input type="text" name="quantity<?php echo $id ?>" value="1" size="4" maxlength="11" title="<?php echo $textquantity ?>" class="formscart inputssmall"> 
<?php if ($unit=='') { echo $siteunit; } else { echo $unit; } ?>
<button type="submit" name="submitcart<?php echo $id ?>" class="<?php if ($inquiry==0) { echo "buttoncart"; } else { echo "button"; } ?>" <?php if ($inquiry==0){ ?>title="<?php echo $textaddcart ?>"<?php } ?>><?php if ($inquiry==0) { ?><span class="icon fa-<?php echo $iconcart ?>"></span><span class="hidden">.</span><?php }else{?><?php echo $textaddcart ?><?php } ?></button>
<?php } ?>
</form> 
<!--end cart -->
<?php } ?>
<?php if ($pagename=='item.php'){ ?><?php }else{?>
<?php if ($available==1 AND (($endprice!='0.00' AND $inquiry==1) OR ($endprice=='0.00' AND $inquiry==0) OR $type!=2) )  { ?>
<a href="<?php echo $siteform ?>-<?php echo $pageid ?>" class="button buttoni"><?php echo $buttonname ?></a>
<?php } ?><?php } ?>