<?
/***************************************************************************
 *			shop_addtocarts.php                                *
 *                     	          				           *
 *   File Info:		  Add to cart                                      *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include ("config.php");

if ($_SERVER['REQUEST_METHOD'] == "POST") 
{
        # escape data and set variables
        $sel_items = addslashes($_POST["sel_item"]);
        $sel_item_title = addslashes($_POST["sel_item_title"]);
        $sel_item_qty = addslashes($_POST["sel_item_qty"]);
        $sel_item_price=addslashes($_POST["sel_item_price"]);
}

$cont = "NO";
$sel_item = getenv("QUERY_STRING");
$rule = substr($sel_item,0,3);
$rule = strtolower($rule);
$bad = "false";
$over = "false";

if ($rule == "sel")
{
	$cont = "YES";
}
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database 'mydata'...Please press 'Create Database' to create new database.");

####### check the product is in myorders table and get quantity #####

$table = "myorders";
$userid = session_id();
$sqls = "SELECT * from $table where user_id = '$userid' and sel_item = '$sel_items'";
$ast = @mysql_query($sqls,$conn)
or die("SQL command failed - Couldn't check for product record i.e stocks quantity !");
while ($row = mysql_fetch_array($ast)) 
{
	$selitems = $row["sel_item"];
	$qty = $row["sel_item_qty"];
	if ($selitems == $sel_items)
		$qtys = $qty;
}

// display error message if user put 0 as quantity //

if ($sel_item_qty == 0)
{
	$msgs = "Adding product to shopping cart</font></center></td></tr><tr><td align=center>You attempted to input $sel_item_qty quantity for product '$sel_items. Please go back using [BACK] button and reinput quantity!";
       dispmsgs($msgs,$conn);	
       $bad = "true";
}

// continue adding if no errors //

if ($bad == "false") {
if ($cont == "NO"){
if ((!($sel_item)) || (!($sel_item_title)) || (!($sel_item_qty)) || (!($sel_item_price))) {
	@header("Location: .\shop_showall.php");
	exit;
}}
if ($cont == "NO")
$badone = isBadStr($sel_item_qty);
$pidnews = getenv("QUERY_STRING"); 
include ("./tests.php");
@session_start();
@header("Cache-Control: private");

if ($cont == "YES")
{
	$stockopt = "YES";
	$sel_item_qty = 1;
	$table = "products";
	$sel_item = str_replace("%20"," ",$sel_item);

	$sitem = substr($sel_item,9);
	$sqls = "SELECT * from $table where pitem = $sitem";
	
	$rest = @mysql_query($sqls,$conn);

	while ($row = mysql_fetch_array($rest))
	{
		$sel_item_title = $row["pdes"];
		$sel_item_price = $row["price"];
		$sel_item = $row["pitem"];
	}
	$pidnews = "sitem = '$sel_item'";
}
if ($cont == "NO"){
if ($badone)
{
	$msgs = "Stock Quantity must contain digits 0 - 9...Try input again<br>Press the [BACK] button to go back to previous page.";
       dispmsgs($msgs,$conn);	
       $bad = "true";
}
if ($sel_item_qty < 1)
{
	$msgs = "You attempted to input $sel_item_qty quantity for product '$sel_items'<br>Please go back using [BACK] button and reinput quantity!";
       dispmsgs($msgs,$conn);	
       $bad = "true";
}}

$color = "#00CC00";
$sel_item_totalprice = $sel_item_qty * $sel_item_price;
if ($sel_item_totalprice > 999.99)
$sel_item_totalprices = number_format($sel_item_totalprice,$precision,".","");
else
$sel_item_totalprices = number_format($sel_item_totalprice,2);

$sel_item_totalprice = number_format($sel_item_totalprice,2);

$date_added = date("Y-m-d");
# need more advanced way of adding	

# check for stock levels

if ($bad == "false"){
if ($stockopt == "YES")
{
	$table = "stocks";
	$sqlone = "SELECT * from $table where $pidnews";
	$as = mysql_query($sqlone,$conn)
	or die("SQL command failed - Couldn't check for product record i.e stocks quantity !");
	$getrow = mysql_fetch_row($as);
	$stocknum = $getrow[4];
	
}

if ($cont == "YES"){
if ($sel_item_qty > $stocknum)
{	if ($stocknum >= 1 or $qty > $stocknum)
	{
		$msgs = "You exceed the stock quantity for this product item '$sel_items'<br>The quantity should be a value from 1 to $stocknum<br>Please go back using [BACK] button and reinput quantity!";
		dispmsgs($msgs,$conn);	
       		$bad = "true";
	}
	else
	{
		$msgs = "You cannot add the product item '$sel_items' - OUT OF STOCK<br>Please go back using [BACK] button and reinput quantity!";
		dispmsgs($msgs,$conn);	
       		$bad = "true";
	}
	
}}

// ******************* users setting stockopt = "NO" will get an error in the
// code below when adding a product item. we suggest you alter the code to
// if ($cont == "NO"){ if ($sel_item_qty > $stocknum && $stockopt == "YES") 
// This should permit you add a product item without do stock inventory

if ($cont == "NO"){
if (($sel_item_qty > $stocknum && $stockopt == "YES") or ($stocknum - $qtys < 1 && $stockopt == "YES"))
{
	if ($stocknum >= 1 or $stocknum - $qtys < 1)
	{
		$msgs = "You exceed the stock quantity for this product item '$sel_items'<br>The quantity should be a value from 1 to $stocknum<br>Please go back using [BACK] button and reinput quantity!";
		dispmsgs($msgs,$conn);	
       		$bad = "true";
	}
	else
	{
		$msgs = "You cannot add the product item '$sel_items' - OUT OF STOCK<br>Please go back using [BACK] button and reinput quantity!";
		dispmsgs($msgs,$conn);	
       		$bad = "true";
		
	}
}}


// continue if no errors //

if ($bad == "false"){
$PHPSESSID=session_id();
$table= "myorders";
if (strlen($sel_items) > 0)
	$sel_item = $sel_items;
if ($cont == "NO") {

$sqlchk = "SELECT * from $table where sel_item = '$sel_item' and user_id = '$PHPSESSID'";}

#do for adding button method
if ($cont == "YES") {

$sqlchk = "SELECT * from $table where sel_item = '$sel_item' and user_id = '$PHPSESSID'";}
$result = @mysql_query($sqlchk,$conn);
	
$nrow = mysql_numrows($result);

if ($nrow == 0)
{
	$sql = "INSERT INTO $table (user_id,sel_item,sel_item_title,sel_item_qty,sel_item_price,sel_item_totalprice,date_added)
	VALUES( '$PHPSESSID', '$sel_item', '$sel_item_title', 
	'$sel_item_qty', '$sel_item_price', '$sel_item_totalprices', 
	'$date_added')";
	$sql_result = mysql_query($sql,$conn) 
	or die ("SQL command failed - Couldn't insert record!");
}	
else
{
	$result = mysql_fetch_row($result);
	$quantity = $result[3];
	$quantity += $sel_item_qty;
	$newtotal = $quantity * $result[4];
	$sqlnew1 = "UPDATE $table set sel_item_totalprice = $newtotal where sel_item = '$sel_item'";
	$sql_result = mysql_query($sqlnew1,$conn) 
		or die ("SQL command failed - Couldn't update record!");
	$sqlnew = "UPDATE $table set sel_item_qty = $quantity where sel_item = '$sel_item'";
	$sql_result = mysql_query($sqlnew,$conn) 
	or die ("SQL command failed - Couldn't update record!");
}


	
$item_count = "SELECT SUM(SEL_ITEM_QTY) FROM $table
		WHERE USER_ID =  \"$PHPSESSID\"";

$item_result = mysql_query($item_count) 
	or die("SQL command failed - Couldn't get count");

$item_count_total = mysql_result($item_result,0,"SUM(SEL_ITEM_QTY)");
if ($item_count_total == 0)
{
	$msgs = "If you see this error message, the shopping cart needs to be reset...<br>TIP: Press the 'Refresh' button twice on this page to reset the cart.<br>Failure to do so may result in items not added to the cart...";
	dispmsgs($msgs,$conn);	
	session_destroy();
	session_start();
	header("Location:shop_addtocart.php");
}}}
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Shopping Menu: Product Added to Cart</TITLE>
</HEAD>
<BODY>

<?php
if ($bad == "false") {
echo "<center><TABLE cellSpacing=0 cellPadding=0 border=0><TR>
      <TD background=./images/border_cnr_tl.gif><IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_top.gif><IMG height=8 alt= src=./images/px.gif width=120 border=0></TD>
      <TD background=./images/border_cnr_tr.gif><IMG height=8 alt= src=./images/px.gif width=8          border=0></TD></TR><TR><TD background=./images/border_left.gif>
       <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD>
       <TD width=120>
       <TABLE cellSpacing=0 cellPadding=4 width=480 border=0>
       <TBODY><tr><td align=center><center><img src=images/mouses.gif border=0><font face = Verdana size =4 > Shopping Cart : Product Added to Cart</center></font></td></tr><tr><tr><td><br></td></tr>";

  if ($wcurrency == "ARS")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/argntina.gif></font></td></tr>";
     if ($wcurrency == "BRL")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/brazil.gif></font></td></tr>";
     if ($wcurrency == "BND")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/brunei.gif></font></td></tr>";
     if ($wcurrency == "CNY")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/china.gif></font></td></tr>";
     if ($wcurrency == "EGY")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/egypt.gif></font></td></tr>";
     if ($wcurrency == "HKD")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/hngkng97.gif></font></td></tr>";
     if ($wcurrency == "INR")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/india.gif></font></td></tr>";
     if ($wcurrency == "IDR")
	echo "<tr><td align=center<font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/indonsia.gif></font></td></tr>";
     if ($wcurrency == "KWD")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency   <img src = ./images/kuwait.gif></font></td></tr>";
     if ($wcurrency == "MYR")
	echo "<tr><td align =center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/malaysia.gif></font></td></tr>";
     if ($wcurrency == "NZD")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/newzeal.gif></font></td></tr>";
     if ($wcurrency == "NOR")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/norway.gif></font></td></tr>";
     if ($wcurrency == "PKR")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/pakistan.gif></font></td></tr>";
     if ($wcurrency == "PHP")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/phillipn.gif></font></td></tr>";
     if ($wcurrency == "PLN")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/poland.gif></font></td></tr>";
     if ($wcurrency == "ROL")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/romania.gif></font></td></tr>";
     if ($wcurrency == "RUR")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/russia.gif></font></td></tr>";
     if ($wcurrency == "SAR")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/saudiar.gif></font></td></tr>";
     if ($wcurrency == "ZAR")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency  <img src = ./images/safrica.gif></font></td></tr>";
     if ($wcurrency == "KRW")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/skorea.gif></font></td></tr>";
    if ($wcurrency == "SEK")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/sweden.gif></font></td></tr>";
   if ($wcurrency == "CHF")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/switzrld.gif></font></td></tr>";
   if ($wcurrency == "TWD")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/taiwan.gif></font></td></tr>";
  if ($wcurrency == "THB")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/thailand.gif></font></td></tr>";
  if ($wcurrency == "VND")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/vietnam.gif></font></td></tr>";
 if ($wcurrency == "USD")
	echo "<tr><td align=center><font face=verdana size=2 color=666666>Shopping Cart using world currency <img src = ./images/usa.gif></font></td></tr>";

echo "<tr><td align=center>........................................................................................</td></tr>";

if ($wcurrency == "NO")
{
$form = "<tr><td align=center><font face=verdana size=2 color=666666>You have added the following item to your shopping cart:</font></td></tr>";
$form.="<tr><td><strong>Product Item:</strong>$sel_item<br><strong>Product Description:</strong>$sel_item_title<br>";
$form.="<strong>Quantity:</strong>$sel_item_qty<br>";
$form.="<strong>Single Unit Price:</strong>$currency$sel_item_price<br>";
$form.="<strong>Total Price:</strong>$currency$sel_item_totalprice</td></tr>";
echo $form;
}
else
{
$form = "<tr><td align=center><font face=verdana size=2 color=666666>You have added the following item to your shopping cart:</font></center></td></tr>";
$form.="<tr><td><strong>Product Item:</strong>$sel_item<br><strong>Product Description:</strong>$sel_item_title<br>";
$form.="<strong>Quantity:</strong>$sel_item_qty<br>";
$form.="<strong>Single Unit Price:</strong>($wcurrency) $sel_item_price<br>";
$form.="<strong>Total Price:</strong>($wcurrency) $sel_item_totalprice</td></tr>";
echo $form;
}}
?>

<?php 	

// continue adding product if no errors //

if ($bad == "false") 
{
	echo "<tr><td align = center><img src =./images/cart.gif border=0>";

	if ($item_count_total == "1") 
	{	
		echo "Your shopping cart now contains <strong>1</strong> item.</td></tr>";
	} 
	else 
	{ 
		echo "Your shopping cart now contains <strong>$item_count_total</strong> items.</td></tr>"; 
	} 
}

function isBadStr($str)
{
    if(strlen($str)>=1&&strlen($str)<=150){
      $valid_chars = " 0123456789-";
      for($i=0;$i<strlen($str);$i++){
        if(strchr($valid_chars,substr($str,$i,1))==false) $x++;
      }
      if($x>0) return true;
    }else{
      return true;
    }
}

// function to display box showing error message //

function dispmsgs($msgs,$conn)
{
	$table = "products";
	$sql = "SELECT * from $table";
	$products = mysql_query($sql,$conn);
	$prods = mysql_num_rows($products);

	$items = array();
	$count = 1;

	// only show items when there are items to show !!!
	if ($prods >= 1)
	{
		while ($row = mysql_fetch_array($products))
		{
	
			$items[$count] = $row["pid"];
			$count += 1;
		}
		srand(microtime() * 1000000);
		$num = rand(1,$prods);
		if ($num == 0) $num = 1;
		$sqls = "SELECT * from $table where pid = $items[$num]";
		$details = mysql_query($sqls,$conn);

		while ($rows = mysql_fetch_array($details))
		{
			$pid = $rows["pid"];
			$pitem = $rows["pitem"];
			$pdes = $rows["pdes"];
		}
		$table = "stocks";
		$sql = " SELECT pid,sitem,scat,spic,stock,color,size from $table where pid=$items[$num]";
		$rs = @mysql_query($sql,$conn)
		or die("Could not execute SQL query!!! Possibly SQL query already been executed.");

		$row=mysql_fetch_array($rs);
		$pics = $row["spic"];
		$scat=$row["scat"];
		$stocknum = $row["stock"];
	}
	else
	{
		$pitem = "N/A";
		$pdes = "N/A";
		$scat = "N/A";
		
	}
	if (strlen($pics) < 1)
	{	
		$pics="./images/nopic.jpg";
	}
	if (fopen($pics,"r") == NULL)
	$pics = "./images/nopic.jpg";
	if (strlen($scat) < 1)
	{
		$scat = "N/A";
	}
	
	echo "<center><TABLE cellSpacing=0 cellPadding=0 border=0><TR>
      <TD background=./images/border_cnr_tl.gif><IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_top.gif><IMG height=8 alt= src=./images/px.gif width=120 border=0></TD>
      <TD background=./images/border_cnr_tr.gif><IMG height=8 alt= src=./images/px.gif width=8          border=0></TD></TR><TR><TD background=./images/border_left.gif>
       <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD>
       <TD width=120>
       <TABLE cellSpacing=0 cellPadding=4 width=480 border=0>
       <TBODY><tr><td align=center><center><img src=images/mouses.gif border=0><font face = Verdana size =4 > Adding a product to shopping cart</center></font></td></tr><tr><tr><td><br></td></tr><tr><td align=center><font face=verdana size=1 color=navy>$msgs</font></td></tr>";

	echo "<td align=center>........................................................................................</td></tr><tr><td align=center><font face=verdana size=1 color=navy>We recommend the following product item</font></td></tr>";

echo "<tr><td align=center><img src=$pics border=0 alt=Picture></td></tr>";
if ($prods >= 1)
echo "<tr><td><font face=verdana size=1><b>Product Item ID:</b>	$pid <a href = ./shop_showitem.php?pid=$pid></font><font color = $highlight><u>Show Item</u></font></a>";
else
echo "<tr><td><font face=verdana size=1><b>Product Item ID:</b>	N/A";	
echo "</td></tr><tr><td><font face=verdana size=1><b>Product Item   :</b>    $pitem</font>";
echo "</td></tr><tr><td><font face=verdana size=1><b>Product Description:</b> $pdes</font></td></tr>";
echo "<tr><td><font face=verdana size=1><b>Product Category:</b> $scat</font></td></tr>";
if ($stocknum >=1)
echo "<tr><td><font face=verdana size=1><b>Stock: </b> $stocknum</td></tr>";
if ($stocknum < 1)
echo "<tr><td><font face=verdana size=1><b>Stock: </b> <font face=verdana size=1 color=red>Out of Stock!</font></td></tr>";
echo "</TBODY></TABLE></TD><TD background=./images/border_right.gif>
      <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD></TR>
      <TR><TD background=./images/border_cnr_bl.gif>
      <IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_bottom.gif>
      <IMG height=8 alt= src=./images/px.gif width=121 border=0></TD>
      <TD background=./images/border_cnr_br.gif>
      <IMG height=8 alt= src=./images/px.gif 
      width=8 border=0></TD></TR></TABLE></center><br><br>";
	return true;
}

?>
<?

if ($bad == "false")
{
echo "<tr><td align=center>........................................................................................</td></tr>";
echo "<tr><td align=center>You can <a href=shop_checkout.php><img src=images/carts.gif border = 0><b>checkout</b></a> now or view your <a href =shop_viewcart.php><img src=images/searchs.gif border = 0><b>shopping cart</b></a></td></tr><tr><td><br></td></tr> ";
echo "</TBODY></TABLE></TD><TD background=./images/border_right.gif>
      <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD></TR>
      <TR><TD background=./images/border_cnr_bl.gif>
      <IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_bottom.gif>
      <IMG height=8 alt= src=./images/px.gif width=121 border=0></TD>
      <TD background=./images/border_cnr_br.gif>
      <IMG height=8 alt= src=./images/px.gif 
      width=8 border=0></TD></TR></TABLE></center><br><br>";
}
?>
<?php
echo "<center><font face=verdana size=1 color=666666>Click icon to go back to previous page.    <b><a href=javascript:history.back()><img src=images/back-to.gif border = 0></a></font></center><br>";
?>
</BODY>
</HTML>




