<?php

	/*
	** File: item
	** Description: show details about an item
	** Version: $Revision: 1.26 $
	** Created: 04/22/1999
	** Author: Leon Atkinson, Shannon -jj Behrens,
	** Email: leon@clearink.com, jj@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/03/07 17:26:13 $
	** Log : $Log: item,v $
	** Log : Revision 1.26  2000/03/07 17:26:13  leon
	** Log : Added code to get item info, changed require() to include()
	** Log :
	** Log : Revision 1.25  2000/02/05 00:23:47  leon
	** Log : Removing number_format in favor of formatMoney
	** Log :
	** Log : Revision 1.24  2000/02/04 17:50:41  leon
	** Log : Added a comment explaining the code for making a link around the item image.
	** Log :
	** Log : Revision 1.23  2000/02/03 19:53:26  leon
	** Log : Added code to get item info
	** Log :
	** Log : Revision 1.22  2000/02/02 18:59:49  leon
	** Log : Fixing bugs reported by Ted Henigson.
	** Log :
	** Log : Revision 1.21  2000/02/01 18:56:10  leon
	** Log : Added code from Isaac Reuben that does better tests for javascript.
	** Log :
	** Log : Revision 1.20  1999/12/07 21:45:10  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.19  1999/11/18 21:24:27  jj
	** Log : Oops, how did that get in there (stray semicolon).
	** Log :
	** Log : Revision 1.18  1999/11/08 22:32:26  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.17  1999/11/04 21:35:42  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.16  1999/10/30 22:07:16  leon
	** Log : Fixed some problems with images associated with items and departments.
	** Log : Defined the directories for items and departments inside global_settings.
	** Log :
	** Log : Revision 1.15  1999/10/26 22:32:21  leon
	** Log : Added "Add to Wishlist" button to item and quick_order pages.
	** Log :
	** Log : Revision 1.14  1999/10/26 17:31:36  leon
	** Log : Removed constants defining variations.  Instead functions wrap the tables.
	** Log : They use caching to reduce the number of database queries.
	** Log :
	** Log : Revision 1.13  1999/10/13 22:19:03  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.12  1999/10/13 00:42:30  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.11  1999/10/04 20:03:47  leon
	** Log : This is the first step in re-organizing include files.  The browser cloaking
	** Log : variables have been moved in to include/init.  Whether they are generated
	** Log : at all is controlled by constant in global_settings.  They get set into
	** Log : constants just to be proper, since they shouldn't be changed.  Also, the
	** Log : check_sid module has been moved into include/init, which gets rid of an
	** Log : include().
	** Log :
	** Log : Revision 1.10  1999/09/29 23:51:15  leon
	** Log : GetItemInfo gets relationships now.  Added code to item to show Cross-Sell
	** Log : and Accessory related items.
	** Log :
	** Log : Revision 1.9  1999/08/18 22:32:17  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.8  1999/08/13 23:42:39  leon
	** Log : Added code to show large image if available.  User clicks on normal
	** Log : image and a smaller windows is opened using javascript.
	** Log :
	** Log : Revision 1.7  1999/08/09 18:11:49  leon
	** Log : Got rid of reference to bath hardware
	** Log :
	** Log : Revision 1.6  1999/07/30 19:06:15  leon
	** Log : Created constant to control the ID for the giftwrap attribute.  Fixed
	** Log : references to giftwrap in item and order_info
	** Log :
	** Log : Revision 1.5  1999/07/28 19:27:32  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	//get attribute functions
	include(APPLICATION_ROOT . "/modules/include/attribute_functions");

	//get item functions
	include(APPLICATION_ROOT . "/modules/include/item_functions");

	//get item info
	$Item = GetItemInfo($item);

	if( !isset($department) )
	{
		//get the department for this item
		$department = $Item["Department"][0];

	}

	$department = intval($department);


	function showRelationship($Item, $relationship, $description)
	{
		/*
		** Related items
		*/
		$CrossSellCount=0;
		if(isset($Item['Relationship'][$relationship]))
		{
			$CrossSellCount = count($Item['Relationship'][$relationship]);
		}

		if($CrossSellCount > 0)
		{
			print("<BR>\n");
			print("$description\n");
			print("<UL>\n");
		
			for($n=0; $n<$CrossSellCount; $n++)
			{
				$RelatedItem = GetItemInfo($Item['Relationship'][$relationship][$n]);

				print("<LI><A HREF=\"".ScreenURL('item', FALSE, array('item'=>$RelatedItem['ID']))."\">");

				print($RelatedItem['Name']);
				
				print("</A>");
			}		

			print("</UL>\n");
		}	
	
	
	}


	
	if(isset($Item["ID"]))
	{
		/*
		** Output the item name and description. 
		*/
		print("<H3>". prepareText($Item["Name"]) . "</H3>\n");
		
		if($Item["Graphic"] != "")
		{

			//If a LargeGraphic exists, then make the medium graphic
			//clickable.  If we can do JavaScript, then we'll use a
			//trick to call the popupwindow function because it allows us
			//to set the size of the window.  In the case of a browser
			//being JavaScript capable, but it's turned off, then we'll
			//just let a window open as default with target=detail
			if(isset($Item["LargeGraphic"]))
			{
				print("<A HREF=\"".ScreenURL("item_details", FALSE, array("item"=>$item))."\" ");
 
 				if(GENERATE_CLOAKING_CONSTANTS AND BROWSER_JAVASCRIPTOK)
 				{
 					print(" onClick=\"popupwindow(this.href); return false;\"");
 				}

				print(" TARGET=\"detail\">");
			}

			$size = GetImageSize($Item["Graphic"]);
			print("<IMG SRC=\"" . $Item["Graphic"] . "\" $size[3] BORDER=\"0\" ALIGN=\"left\" HSPACE=\"5\">");		

			if(isset($Item["LargeGraphic"]))
			{
				print("</A>");
			}
		}
		
		
		print($Item["Description"]);
		print("<BR CLEAR=\"ALL\"><BR>\n");


		//get sku giftwrap-ability
		$Query = "SELECT SKU ";
		$Query .= "FROM sku_variation ";
		$Query .= "WHERE Variation = " . getVariationID(getAttributeID("Gift Wrap"), "Available") . " ";
		$Query .= "AND SKU in (0";
		for($index = 0; $index < count($Item["SKU"]["ID"]); $index++)
		{
			$Query .= ", " . $Item["SKU"]["ID"][$index];
		}		
		$Query .= ") ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$GiftWrap[($DatabaseRow[0])] = TRUE;
		}
		


		//list SKUs

		
		$BackorderedItems = FALSE;
		
		print("<TABLE BORDER=\"0\">\n");
		
		for($index = 0; $index < count($Item["SKU"]["ID"]); $index++)
		{
			print("<TR>\n");
			
			print(StartForm("item", 'post', 'ADD_BASKET', FALSE, 
				array("department"=>$department, 
					"item"=>$item, 
					"sku"=>$Item["SKU"]["ID"][$index])));

			//gift wrap status		
			print("<TD VALIGN=\"center\">");
			if( isset($GiftWrap[($Item["SKU"]["ID"][$index])]) )
			{
				print("<FONT SIZE=\"1\">" . L_ITEM_GIFTWRAP . "</FONT>");
			}
			else
			{
				print("&nbsp;");
			}
			print("</TD>\n");

			print("<TD VALIGN=\"center\">");
			print(prepareText($Item["SKU"]["Name"][$index]));
			print("(" .prepareText($Item["SKU"]["ExternalSKU"][$index]). ")");
			print("</TD>\n");

			print("<TD VALIGN=\"center\">");
			if($Item["SKU"]["SalePrice"][$index] > 0)
			{
				print(" " . formatMoney($Item["SKU"]["SalePrice"][$index]));
			}
			else
			{
				print(" " .  formatMoney($Item["SKU"]["ListPrice"][$index]));
			}

			if($Item["SKU"]["Shipping"][$index] > 0)
			{
				print(" (" . formatMoney($Item["SKU"]["Shipping"][$index]) . ")");
			}

			print("</TD>\n");



			/*
			** get SKU attributes with more than one variation
			*/
			
			//get list of attributes as comma-separated list
			//this allows us to drop it right into the SQL
			$MultiAttributes = "0"; //seed with dummy value
			
			$Query = "SELECT v.Attribute, COUNT(v.Attribute) ";
			$Query .= "FROM sku_variation sv, variation v ";
			$Query .= "WHERE sv.Variation = v.ID ";
			$Query .= "AND sv.SKU = " . $Item["SKU"]["ID"][$index] . " ";
			$Query .= "GROUP BY v.Attribute ";
			$Query .= "ORDER BY 2 DESC ";
			$DatabaseResult = mysql_query($Query, $DatabaseLink);
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				if($DatabaseRow[1] > 1)
				{
					$MultiAttributes .= ", $DatabaseRow[0]";
				}
			}
			
			
			//get full variation info
			$Query = "SELECT a.ID, a.Name, v.ID, v.Name ";
			$Query .= "FROM sku_variation sv, variation v, attribute a ";
			$Query .= "WHERE sv.SKU = " . $Item["SKU"]["ID"][$index] . " ";
			$Query .= "AND sv.Variation = v.ID ";
			$Query .= "AND v.Attribute = a.ID ";
			$Query .= "AND a.ID in ($MultiAttributes) ";
			$Query .= "ORDER BY a.DisplayPrecedence, a.Name ";
			/* Execute query. */
			if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
			{
				print("<!-- " . mysql_errno() . ": " . mysql_error() . 
					"Query was $Query -->\n");
			}
			else
			{
				if ( !mysql_num_rows($DatabaseResult))
				{
					/* No select. */ 
				}
				else
				{
					$last_attr = 0;
					while($DatabaseRow = mysql_fetch_row($DatabaseResult))
					{
						$tAttributeID = $DatabaseRow[0];
						$tAttributeName = $DatabaseRow[1];
						$tVariationID = $DatabaseRow[2];
						$tVariationName = $DatabaseRow[3];

						if($last_attr != $tAttributeID)
						{
							if($last_attr != 0)
							{
								print("</SELECT>\n");
								print("</TD>\n");
							}

							print("<TD VALIGN=\"center\">\n");
							print("<SELECT NAME=\"inputVariation[]\">\n");
							print("<OPTION VALUE=\"$tVariationID\" SELECTED>");
							print(prepareText($tAttributeName));
							print("\n");

							$last_attr = $tAttributeID;
						} 

						print("<OPTION VALUE=\"$tVariationID\">");
						print(prepareText($tVariationName));
						print("\n");
					}
					print("</SELECT>\n");
					print("</TD>\n");
				}
			}	



			//submit button for adding item to basket
			print("<TD VALIGN=\"center\">");

			//check inventory levels
			//Total items in warehouse = Available + Reserve
			if($Item["SKU"]["Available"][$index] + $Item["SKU"]["Reserve"][$index]  > 0)
			{
				print("<INPUT TYPE=\"SUBMIT\" NAME=\"basket_button\" VALUE=\"Add\">");
			}
			elseif($Item["SKU"]["Backorder"][$index] == 'Y')
			{
				print("<INPUT TYPE=\"SUBMIT\" NAME=\"basket_button\" VALUE=\"Backorder *\">");
				$BackorderedItems = TRUE;
			}
			else
			{
				print("<B>Out of Stock</B>");
			}

			//user must be logged in to use wishlist
			if($UserInfo["ID"] > 0)
			{
				print("<INPUT TYPE=\"SUBMIT\" NAME=\"wishlist_button\" VALUE=\"Add To Wishlist\">");
			}
					
			print("</TD>\n");
			
			print("</FORM>\n");
			
			print("</TR>\n");
		}
		
		print("</TABLE>\n");
		
		if($BackorderedItems)
		{
			print(L_ITEM_BACKORDERED . "<BR>");
		}

		showRelationship($Item, 'Cross-Sell', "Similar Items:");

		showRelationship($Item, 'Accessory', "Accessories:");
	
	}
	else
	{
		print(L_ITEM_UNAVAILABLE);	
	}
	
?>
