<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">
<HTML>
<HEAD>
<TITLE>Shopping Cart Contents - Update Cart</TITLE>
</HEAD>
<BODY>
<?
/***************************************************************************
 *			shop_updatecarts.php                    	   *
 *                     	          				           *
 *   File Info:		  Update shopping cart                             *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include ("config.php");
include ("./tests.php");
$PHPSESSID = session_id();
$bad = "false";
$conts = "TRUE";

###### handle deletion if update quantity = 0 ######
 if ($_SERVER['REQUEST_METHOD'] == "POST") 
{
        $delete = addslashes($_POST["delete"]);
 }    
$strs = getenv("QUERY_STRING");
$pos = strpos($strs,'=');
$sel_items = substr($strs,$pos+1);
$sel_items= str_replace("%20"," ",$sel_items);

$index = substr($strs,0,$pos);

if ($delete == "delete")
{
	include_once("./your-header.inc");
	$table= "myorders";
	$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
	$rs1 = @mysql_select_db($database,$conn) 
	or die("Unable to select the database 'mydata'...Please press 'Create Database' to create new database.");
	$sqls = "SELECT * from $table where user_id = '$PHPSESSID'";
	$sql_results = mysql_query($sqls,$conn) 
	or die ("SQL command failed - Couldn't get list of order records!");
$nrows = mysql_num_rows($sql_results);


if ($nrows == 0)
{
	$msgs = "Unable to perform deleting of product !!!<br> Maybe the cart may be empty or product item already deleted!!!";
	dispmsgs($msgs,$conn);
	$conts = "FALSE";
	$bad = "true";
	include_once("./footer.inc");
}
else
{
	$delsql = "DELETE from $table where sel_item = '$sel_items' and  user_id = '$PHPSESSID'";
	$dels = @mysql_query($delsql,$conn)
		or die("SQL command failed - unable to delete product item!");
	$msgs = "<center>Deletion of single item '$sel_items' successful....<br><br><a href=./store.php>Continue Shopping</a></center>";
	dispmsgs($msgs,$conn);
	$conts = "FALSE";
	$bad = "true";
	include_once("./footer.inc");
}
	
}

if ($bad == "false"){

###### handle deletion if update quantity = 0 ######


 if ($_SERVER['REQUEST_METHOD'] == "POST") 
{
        $sel_item_qty = addslashes($_POST["sel_item_qty"]);
        $sel_qty = $sel_item_qty;
}

$bads = "false";

// check if incorrect characters inputted for quantity //

$badone = isBadStr($sel_qty);
if ($badone)
{
	$msgs = "Item Quantity must contain digits 0 - 9...Try input again<br>Press the [BACK] button to go back to previous page.";
        dispmsgs($msgs,$conn);	
        $bads = "true";
}

if (!$sel_item_qty && $delete != "delete") 
{
	$msgs = "<font color = navy size=2 face=verdana>The quantity you wish to update cannot be a null value!!!<br>Please go back and re-input a new quantity for updating!!!<br>Ignore this message and press 'Delete Item' to remove item.</font><center><form method=post action = ./shop_updatecarts.php?$strs><input type = hidden name=delete value='delete'>
<input type = submit value='Delete Item'></form></center>";
	dispmsgs($msgs,$conn);
	$conts = "FALSE";
	$bad = "true";
	$bads = "true";
}
if ($sel_item_qty < 0) 
{
	$msgs = "<font color = navy face=verdana size=2>The quantity you wish to update cannot be a negative value!!!<br>Please go back and re-input a new quantity for updating!!!</font><br><center>Click icon to go to previous page</font><a href=javascript:history.back()><img src=images/back-to.gif border = 0 alt=Back></a></center>";
	dispmsgs($msgs,$conn);
	$conts = "FALSE";
	$bad = "true";
	$bads = "true";
}
if ($bads == "false"){
$table= "myorders";
$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.");
$sql = "SELECT * from $table where user_id = '$PHPSESSID'";
$sql_result = mysql_query($sql,$conn) 
	or die ("SQL command failed - Couldn't get list of order records!");
$nrow = mysql_num_rows($sql_result);


if ($nrow == 0)
{
	$msgs = "<strong><center>Unable to perform updating of the shopping cart !!! - the cart may be empty</center></strong>";
	dispmsgs($msgs,$conn);
	$conts = "FALSE";
	$bad = "true";
	$bads = "true";
}
else
{
	$a=0;
	$prods = array();
	$quant = array();
	$total = array();
	while($row=mysql_fetch_array($sql_result))
	{
		$a += 1;
		$sel_item_qty = $row["sel_item_qty"];
		$sel_item = $row["sel_item"];	
		$sel_item_title = $row["sel_item_title"];
		$sel_item_price = $row["sel_item_price"];
		$sel_item_price = number_format($sel_item_price,2);
		$sel_item_totalprice = $row["sel_item_totalprice"];
		$fmt_item_totalprice = sprintf("%0.2f",$sel_item_totalprice);
		$prods[$a] = $sel_item;
		$quant[$a] = $sel_item_qty;
		$total[$a] = $sel_item_totalprice;
	}
	# get the details for chosen row
	$sqlchk = "Select * from $table where sel_item = '$prods[$index]' and sel_item_qty = $quant[$index]";
	
	$result = mysql_query($sqlchk,$conn) 
	or die ("SQL command failed - Couldn't get order details!");
	$row1 = mysql_num_rows($result);
	$bad = "FALSE";
	if ($row1 == 0)
	{
		$msgs = "<strong><center>Unable to perform updating of the shopping cart !!! - the cart may be empty</center></strong>";
		dispmsgs($msgs,$conn);
		$conts = "FALSE";
		$bad = "true";
		$bads = "true";
	}
	else
	{
		$data = mysql_fetch_row($result);
		$sel_item = $data[1];
		$sel_item_qty = $data[3];
		$sel_item_price = $data[4];
		$sel_item_price = number_format($sel_item_price,2);
		if ($stockopt == "YES")
		{		
			$table = "stocks";
			$sqlone = "SELECT * from $table where sitem = '$sel_item'";
			$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 ($sel_qty > $stocknum)
		{	if ($stocknum > 1)
			{
			$msgs = "<font color = navy face=verdana size=2>
You exceed the stock quantity for this product item<br><font color=darkred>Product Item:</font> '$sel_item'<br>The quantity should be a value from 1 to $stocknum<br>Please go back using [BACK] button and reinput quantity!</font>";
			dispmsgs($msgs,$conn);
			$conts = "FALSE";
			$bad = "true";
			}
			else
			{

				$msgs = "<font color = navy face=verdana size=2>
You exceeded the stock quantity for this product item<br><font color=navy>You cannot add the product item '$sel_item' <br>since we are OUT OF STOCK of this item.<br>Please go back using [BACK] button and reinput quantity!<br></font><font color=black face=verdana size=2><center>Click icon to go to previous page</font><a href=javascript:history.back()><img src=images/back-to.gif border = 0 alt=Back></a></center>";
			dispmsgs($msgs,$conn);
			$conts = "FALSE";
			$bad = "true";
			}
		}
		if ($bad == "FALSE") {
		$table = "myorders";
		$oldtotal = $sel_item_qty * $sel_item_price;
		$oldtotal = number_format($oldtotal,2);
		$newtotal = $sel_qty * $sel_item_price;
		$newtotals = $newtotal;
		if ($wcurrency == "NO")
		{
		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=450 border=0>
       <TBODY>";
	echo("<br><tr><td>Item: $sel_item</td></tr><tr><td>Original Item Quantity: $sel_item_qty</td></tr><tr><td>Original Item Price: $currency$sel_item_price</td></tr><tr><td>Original Item Total Price: $currency$oldtotal</td></tr>");
		}
		if ($wcurrency != "NO")
		{
		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=450 border=0>
       <TBODY><tr><td align=center><center><img src=./images/keybord1.gif border=0><font face=verdana size=3 color = navy> Updating shopping cart</font></center></td></tr>";	
	echo "<tr><td>$msgs</td></tr>";
	echo("<br><tr><td>Item: $sel_item</td></tr><tr><td>Original Item Quantity: $sel_item_qty</td></tr><tr><td>Original Item Price: $currency$sel_item_price</td></tr><tr><td>Original Item Total Price: $currency$oldtotal</td></tr>");
		}
		$table = "myorders";
	
		if ($wcurrency == "NO") 
		echo("<tr><td><strong><font color = darkgreen>New Item Quantity: $sel_qty</td></tr><tr><td>New Item Total Price: $currency$newtotals</td></tr></font></strong></center>");
		if ($wcurrency != "NO")
		echo("<tr><td><strong><font color = darkgreen>New Item Quantity: $sel_qty</td></tr><tr><td>New Item Total Price: $wcurrency$newtotals</td></tr></font></strong></center>");
		$sqlnew = "UPDATE $table set sel_item_qty = $sel_qty where sel_item = '$sel_item'";
		$result = mysql_query($sqlnew,$conn) 
			or die ("SQL command failed - Couldn't update order details!");
	
		$sqlnew1 = "UPDATE $table set sel_item_totalprice = $newtotal where sel_item = '$sel_item'";
		$result = mysql_query($sqlnew1,$conn) 
			or die ("SQL command failed - Couldn't update order details!!");
		
		echo("<tr><td><br></td></tr></strong><tr><td align=center><font color = navy face = Arial>Updating your shopping cart.To review updated results click 'View Cart' button link.</font></strong></td></tr><tr><td align=center>Click icon to view cart<a href=shop_viewcart.php><img src=images/carts.gif border=0 alt=Cart></a></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>";
	}}
	
}


$date_added = date("jS M, Y");
if ($bad == "false")
echo "<br><center><b>Click icon to go back to previous page.</b><a href=javascript:history.back()><img src=images/back-to.gif border = 0 alt=Back></a></center><br></tr></td></table>";

echo"<br>"; 

}
}
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/keybord1.gif border=0><font face=verdana size=3 color = navy> Updating shopping cart</font></center></td></tr>";	
	echo "<tr><td>$msgs</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;
}	

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;
    }
}

?>

</BODY>
</HTML>
