<?php

include "header.php";

//include "header2.php";

// Database connection variables

$selectuser = $_SESSION['sorter_username'];

$tableRep = $_SESSION['sorter_username'];



$dbServer = $mysqlserver;

$dbDatabase = $mysqldatabase;

$dbDatabase2 = $mysqldatabase2;

$dbDatabase3 = $mysqldatabase3;

$dbUser = $mysqlusername;

$dbPass = $mysqlpassword;

$sConn = mysql_connect($dbServer, $dbUser, $dbPass) 

or die("Couldn't connect to database server"); 

$dConn = mysql_select_db($dbDatabase, $sConn)

or die("Couldn't connect to database $dbDatabase");



 if (isset($_GET['sortby']))

 $sortwith = $_GET['sortby']; // this be the real magic

 else if (isset($sortby)) $sortwith = $sortby;

 else $sortwith = "unitsSoldArt";

 

 if (isset($_GET['remove']) && $_GET['remove'] == "all")

 {

 /*$queryCheck = "UPDATE `hiresolution_$tableRep` SET `checked` = '0' WHERE 1=1;";

 $skuResultCheck = mysql_query($queryCheck);*/

 

 $dbSet = "DROP TABLE IF EXISTS `hiresolution_$tableRep`";

mysql_query($dbSet) or die("Couldn't add file to database");

 }



//amazon.com

if(isset($_GET['amazon_filter']) && $_GET['amazon_filter'] == 'Yes')

{

$amazon_checked = "checked";

$amazon_filter_cnd = " AND `SKU` NOT IN (SELECT `SKU` FROM `amazon_onsite`) AND `SKU` NOT IN (SELECT `SKU` FROM `amazon_approval`)";

}

else

{

$amazon_checked = "";

$amazon_filter_cnd = "";

} 



//overstock.com

if(isset($_GET['overstock_filter']) && $_GET['overstock_filter'] == 'Yes')

{

$overstock_checked = "checked";

$overstock_filter_cnd = " AND `SKU` NOT IN (SELECT `SKU` FROM `overstock_onsite`) ";

}

else

{

$overstock_checked = "";

$overstock_filter_cnd = "";

}



//shopnbc.com

if(isset($_GET['shopnbc_filter']) && $_GET['shopnbc_filter'] == 'Yes')

{

$shopnbc_checked = "checked";

$shopnbc_filter_cnd = " AND `SKU` NOT IN (SELECT `SKU` FROM `shpoNBC_onsite`) ";

}

else

{

$shopnbc_checked = "";

$shopnbc_filter_cnd = "";

}



//ATG Stores

if(isset($_GET['atg_filter']) && $_GET['atg_filter'] == 'Yes')

{

$atg_checked = "checked";

$atg_filter_cnd = " AND `SKU` NOT IN (SELECT `SKU` FROM `atg_onsite`) ";

}

else

{

$atg_checked = "";

$atg_filter_cnd = "";

}



//wayfair

if(isset($_GET['wayfair_filter']) && $_GET['wayfair_filter'] == 'Yes')

{

$wayfair_checked = "checked";

$wayfair_filter_cnd = " AND `SKU` NOT IN (SELECT `CatalogSK` FROM `wayfair_onsite`) ";

}

else

{

$wayfair_checked = "";

$wayfair_filter_cnd = "";

}



//luvbutton

if(isset($_GET['luvbutton_filter']) && $_GET['luvbutton_filter'] == 'Yes')

{

$luvbutton_checked = "checked";

$luvbutton_filter_cnd = " AND `SKU` NOT IN (SELECT `CatalogSK` FROM `luvbutton_onsite`) ";

}

else

{

$luvbutton_checked = "";

$luvbutton_filter_cnd = "";

}



//kohls.com

if(isset($_GET['kohls_filter']) && $_GET['kohls_filter'] == 'Yes')

{

$kohls_checked = "checked";

$kohls_filter_cnd = " AND `SKU` NOT IN (SELECT `SKU` FROM `kohls_onsite`) ";

}

else

{

$kohls_checked = "";

$kohls_filter_cnd = "";

}



//sears canada

if(isset($_GET['sears_ca_filter']) && $_GET['sears_ca_filter'] == 'Yes')

{

$sears_ca_checked = "checked";

$sears_ca_filter_cnd = " AND `SKU` NOT IN (SELECT `CatalogSK` FROM `sears_canada_onsite`) ";

}

else

{

$sears_ca_checked = "";

$sears_ca_filter_cnd = "";

}



//heyneedle.com

if(isset($_GET['heyneedle_filter']) && $_GET['heyneedle_filter'] == 'Yes')

{

$heyneedle_checked = "checked";

$heyneedle_filter_cnd = " AND `SKU` NOT IN (SELECT `CatalogSK` FROM `heyneedle_onsite`) ";

}

else

{

$heyneedle_checked = "";

$heyneedle_filter_cnd = "";

}



 if (isset($_GET['triger2']) && $_GET['triger2'] != "")

 {

 $total_sale_tr = $_GET['triger2'];

$sale_cnd = " AND `dollarsSold` > '$total_sale_tr'"; 

 }

 else

 {

$total_sale_tr = "";

$sale_cnd = "";

 }

 

 if (isset($_GET['triger3']) && $_GET['triger3'] != "")

 {

 $qty_sold_tr = $_GET['triger3'];

 $qty_cnd = " AND `unitsSoldArt` > '$qty_sold_tr'";

 }

 else

 {

 $qty_sold_tr = "";

 $qty_cnd = "";

 }



 if (isset($_GET['filter1']) && $_GET['filter1'] != "")

 {

 $filter1 = $_GET['filter1'];

 $filter1_cnd = " AND `SKU` LIKE '" . $filter1 . "%'";

 }

 else {

 $filter1 = "";

 $filter1_cnd = "";

 }

 

 if (isset($_GET['offer_filter']) && $_GET['offer_filter'] == 'Yes')

 {

 $filterOffer = "checked";

 /*$filterOffer_cnd = " AND `SKU` LIKE '" . $filter1 . "%'";*/

 }

 else if (isset($_GET['offer_filter']) && $_GET['offer_filter'] == 'No'){

 $filterOffer = "";

 }

 else {

 $filterOffer = "checked";

 /*$filterOffer_cnd = "";*/

 }



?>

<style>

#headerset div {

	float:left;

	font-weight:bold;

	border:solid #FFFFFF 1px;

	height:80px;

	width: 920px;

}

</style>

<script language="Javascript" src="cal/calendar.js"></script>

<script language="Javascript">

<!--

checked = false;

function checkedAll () 

{

    if (checked == false){checked = true}else{checked = false}

	for (var i = 0; i < document.getElementById('create_order').elements.length; i++) {

	  document.getElementById('create_order').elements[i].checked = checked;

	}

}



function UpdateTotal(totalRows) {

  var sum = 0;

  var gn, elem;

  for (i=1; i<totalRows; i++) {

    gn = 'check'+i;

    elem = document.getElementById(gn);

    if (elem.checked == true) { sum += 1; } //Number(elem.value);

  }

  document.getElementById('totalRows').value = sum;

} 



function show_on_renew()

{

var r=confirm("Are you sure you wish to refresh the list?");

	if (r==true)

	  {

	window.location='hi-res-sku-creator.php';

	  }

}

function show_on_remove()

{

var r=confirm("Are you sure you want to erase your picks?");

	if (r==true)

	  {

	window.location='hi-res-sku-list.php?remove=all';

	  }

}



//-->

</script>

<div style="font-size:14px; font-weight:normal; color:#333333;text-align:center;">

 <form name="sorting" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get">

 Filter Amazon SKUs?

    <input type="checkbox" name="amazon_filter" value="Yes" <?php echo $amazon_checked;?> />

 Filter Heyneedle SKUs?

    <input type="checkbox" name="heyneedle_filter" value="Yes" <?php echo $heyneedle_checked;?> />

Filter Overstock.com SKUs?

    <input type="checkbox" name="overstock_filter" value="Yes" <?php echo $overstock_checked;?> />

Filter ShopNBC SKUs?

    <input type="checkbox" name="shopnbc_filter" value="Yes" <?php echo $shopnbc_checked;?> />

<br /> Filter ATG SKUs?

    <input type="checkbox" name="atg_filter" value="Yes" <?php echo $atg_checked;?> />

Filter Wayfair SKUs?

    <input type="checkbox" name="wayfair_filter" value="Yes" <?php echo $wayfair_checked;?> />

Filter LuvButton SKUs?

    <input type="checkbox" name="luvbutton_filter" value="Yes" <?php echo $luvbutton_checked;?> />

Filter Kohls SKUs?

    <input type="checkbox" name="kohls_filter" value="Yes" <?php echo $kohls_checked;?> />

Filter Sears Canada SKUs?

    <input type="checkbox" name="sears_ca_filter" value="Yes" <?php echo $sears_ca_checked;?> />

<br /> Filter Available Frames Only?

  <input type="hidden" name="offer_filter" value="No" />

    <input type="checkbox" name="offer_filter" value="Yes" <?php echo $filterOffer;?> />	



 Sort By:

 <select name="sortby" size="1" style="margin: 0px; padding: 0px; width: 125px; font-size: 11px;">

 <option value="unitsSoldArt" <? if($sortwith=="unitsSoldArt") echo "selected=\"selected\"";?> >Units Sold Art</option>

 <option value="unitsSoldFrames"  <? if($sortwith=="unitsSoldFrames") echo "selected=\"selected\"";?>>Units Sold Frames</option> 

 <option value="dollarsSold"  <? if($sortwith=="dollarsSold") echo "selected=\"selected\"";?>>Sales Art</option> 

 <option value="qtyOnHandArt"  <? if($sortwith=="qtyOnHandArt") echo "selected=\"selected\"";?>>Qty on Hand</option>

  <option value="artSKU"  <? if($sortwith=="artSKU") echo "selected=\"selected\"";?>>Art Part Number</option>

   <option value="frameSKU"  <? if($sortwith=="frameSKU") echo "selected=\"selected\"";?>>Frame Part Number</option>

 </select> 

 &nbsp;&nbsp;

 Total Sales over: <input name="triger2" style="margin: 0px; padding: 0px; width: 60px; font-size: 11px;" type="text" value="<?php echo $total_sale_tr; ?>">

 &nbsp;&nbsp;

 Qty Sold over: <input name="triger3" style="margin: 0px; padding: 0px; width: 60px; font-size: 11px;" type="text" value="<?php echo $qty_sold_tr; ?>">

 &nbsp;&nbsp;

 Show art that Begins with: <input name="filter1" style="margin: 0px; padding: 0px; width: 60px; font-size: 11px;" type="text" value="<?php echo $filter1; ?>">

 &nbsp;&nbsp;

 <input type="submit" name="submitreport" value="Run Items"/>

 </form>

</div>

&nbsp;&nbsp;

<button onClick="show_on_renew();" style="color:#AE0011;">Renew the Hi-Res List</button>&nbsp;&nbsp;

<button onClick="window.location='hi-res-sku-list-checked.php';">See Your Choosen List</button>

<button onClick="show_on_remove();">Remove all from your List</button>

<br /><br />

<?php

	$query3 = "SELECT * FROM `hiresolution` WHERE `checked` = '0' $qty_cnd $sale_cnd $filter1_cnd $amazon_filter_cnd $heyneedle_filter_cnd $overstock_filter_cnd $shopnbc_filter_cnd $atg_filter_cnd $wayfair_filter_cnd $luvbutton_filter_cnd $kohls_filter_cnd $sears_ca_filter_cnd"; //WHERE SKU='KL837-FR-655G20X24'

	//echo $query3;

	$result2 = mysql_query($query3) or die("Couldn't add row 1");

	$num_rows = mysql_num_rows($result2);

	while($myrow = mysql_fetch_array($result2))

			{		

				$SKU = $myrow["SKU"];

				$artName = $myrow["artName"];

				$artSKU = trim($myrow["artSKU"]);

				$frameSKU = trim($myrow["frameSKU"]);

				

				//continue if already choosen...

			$querySKU = "SELECT * FROM `hiresolution_$tableRep` WHERE `SKU` = '" . $SKU . "' LIMIT 1;";

			//echo $querySKU;

			$skuResult = mysql_query($querySKU);

			$num_checked = mysql_num_rows($skuResult);

			if ($num_checked > 0) 

			{

				//echo "<BR>IN THE LOOP<BR>";

				$num_rows = $num_rows - 1;

				continue;

			}

				//echo $frameSKU . "<br>";

				

		$query5 = "SELECT * FROM `catalogcity` WHERE UPPER(CatalogSK) = '" . $artSKU . "' || UPPER(CatalogSK) = '" . $artSKU . "P' || UPPER(CatalogSK) = '" . $artSKU . "E' || UPPER(SUBSTRING(CatalogSK,1,7)) = '" . $artSKU . "' ORDER BY LENGTH(CatalogSK) ASC LIMIT 1;";

					//echo $query5 . "<br>";

					$result5 = mysql_query($query5);

					if (!mysql_num_rows($result5)){ 

					$num_rows = $num_rows - 1; 

					continue;

					}

					

					$query4 = "SELECT * FROM `catalogcity` WHERE UPPER(CatalogSK) = '" . $frameSKU . "' || UPPER(CatalogSK) = 'FR-" . $frameSKU . "' LIMIT 1;";

					//echo $query4 . "<br>";

					$result4 = mysql_query($query4);

					if (!mysql_num_rows($result4)) { $num_rows = $num_rows - 1; continue;}

			}



?>

<form name="create_order" id="create_order" action="hi-res-update-checked.php" method="post">

  <table cellspacing="0" cellpadding="1" border="1" width="100%">

    <tr valign="top">

      <td valign="top"><table cellspacing="0" cellpadding="1" border="1" width="100%">

          <tr>

            <td class="subsection" colspan=8><b>HI-END SKU CREATION</b> &nbsp;&nbsp;<?php echo "number of records: " . $num_rows; ?></td>

          </tr>

          <TR bgcolor="lightgrey">

            <TD colspan="8" style="padding-left: 0px;"><div id="headerset">

                <?php	

$countItems = 1;

$count = 1;

	if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ){ ?>

                <DIV style="WIDTH: 100px;text-align:center;"><input type='checkbox' name='checkall' onclick='checkedAll();'>No.</DIV>

                <DIV style="WIDTH: 200px;text-align:center;">Art Info</DIV>

                <DIV style="WIDTH: 300px;text-align:center;">Frame Info</DIV>

                <DIV style="WIDTH: 370px;text-align:center;">Image</DIV>

                <DIV style="WIDTH: 78px;text-align:center;">Total Sales</DIV>

                <DIV style="WIDTH: 90px;text-align:center;">Qty on Hand</DIV>

                <DIV style="WIDTH: 90px;text-align:center;">Qty on Order</DIV>

                <DIV style="WIDTH: 77px;text-align:center;">Qty</DIV>

                <?php } 

		else{ ?>

                <DIV style="WIDTH: 100px;text-align:center;"><input type='checkbox' name='checkall' onclick='checkedAll();'>No.</DIV>

                <DIV style="WIDTH: 200px;text-align:center;">Art Info</DIV>

                <DIV style="WIDTH: 300px;text-align:center;">Frame Info</DIV>

                <DIV style="WIDTH: 370px;text-align:center;">Image</DIV>

                <DIV style="WIDTH: 78px;text-align:center;">Total Sales</DIV>

                <DIV style="WIDTH: 90px;text-align:center;">Qty on Hand</DIV>

                <DIV style="WIDTH: 90px;text-align:center;">Qty on Order</DIV>

                <DIV style="WIDTH: 77px;text-align:center;">Qty</DIV>

                <?php } ?>

              </div></TD>

          </TR>

        </table></td>

    </tr>

    <tr valign="top">

      <td valign="top"><div style="width: 100%; height: 600px; overflow:auto">

          <table cellspacing="0" cellpadding="1" border="1" width="100%">

<?php	



	$query3 = "SELECT * FROM `hiresolution` WHERE `checked` = '0' $qty_cnd $sale_cnd $filter1_cnd $amazon_filter_cnd $heyneedle_filter_cnd $overstock_filter_cnd $shopnbc_filter_cnd $atg_filter_cnd $wayfair_filter_cnd $luvbutton_filter_cnd $kohls_filter_cnd $sears_ca_filter_cnd ORDER BY `" . $sortwith . "` DESC, `SKU` DESC;";

	 //ORDER BY `" . $sortwith . "` DESC,`dollarsSold` DESC, `SKU` DESC;"; 

	 //WHERE SKU='KL837-FR-655G20X24' 

	//echo $query3;

	$result2 = mysql_query($query3) or die("Couldn't add row 1");					

	while($myrow = mysql_fetch_array($result2))

			{

				$SKU = $myrow["SKU"];

				$artName = $myrow["artName"];

				$artSKU = trim($myrow["artSKU"]);

				$frameSKU = trim($myrow["frameSKU"]);

				$frameTitle = "";

				$frameExt = "";

				$main_img = "";

				$th_img = "";

				

				$dConn = mysql_select_db($dbDatabase, $sConn) or die("Couldn't connect to database $dbDatabase2");



				//continue if already choosen...

			$querySKU = "SELECT * FROM `hiresolution_$tableRep` WHERE `SKU` = '" . $SKU . "' LIMIT 1;";

			//echo $querySKU;

			$skuResult = mysql_query($querySKU);

			$num_checked = mysql_num_rows($skuResult);

			if ($num_checked > 0) 

			{

				//echo "<BR>IN THE LOOP<BR>";

				continue;

			}

				

				$query5 = "SELECT * FROM `catalogcity` WHERE UPPER(CatalogSK) = '" . $artSKU . "' || UPPER(CatalogSK) = '" . $artSKU . "P' || UPPER(CatalogSK) = '" . $artSKU . "E' || UPPER(SUBSTRING(CatalogSK,1,7)) = '" . $artSKU . "' ORDER BY LENGTH(CatalogSK) ASC LIMIT 1;";

					//echo $query5 . "<br>";

					$result5 = mysql_query($query5);

					if (!mysql_num_rows($result5)) continue;

					while($row5 = mysql_fetch_array($result5))

					{

					$artSKU = trim($row5["CatalogSK"]);

					$artSize = $row5["SubDeptName4"];			

					}

					

		$dConn = mysql_select_db($dbDatabase3, $sConn) or die("Couldn't connect to database $dbDatabase2");

		

		$now = time();



		$artSKUTest = $artSKU;

		if (substr($artSKUTest,-1) == 'P' || substr($artSKUTest,-1) == 'E')

		$artSKUTest = substr($artSKUTest,0,-1);

		

		 if (substr($artSKUTest,0,3) == 'FR-' || substr($artSKUTest,0,4) == 'GALW')

		$checkTable = "frameSKU";

		else

		$checkTable = "artSKU";

		

		$queryCount = "SELECT SUM(qty) FROM `eventItems` WHERE `$checkTable`= '" . $artSKUTest . "' AND `DateEnd` > $now;";

								$resultCount = mysql_query($queryCount);

								$rowCount = mysql_fetch_array($resultCount);

								//echo $queryCount;

								$countEventItems = $rowCount[0];

						if ($countEventItems > 0)

							{

							$onEventTxt = "<div style=\"clear: both\";></div><div style=\"float:right;padding:10px;\"><img src=\"images/clock.svg\" align=\"right\" style=\"margin-top:10px; margin-right:10px; margin-bottom:10px;\"><a href=\"http://www.overstockart.cn/communicator/eventItems.php?partnosearch=" . $artSKUTest . "\" target=\"_blank\" style=\"color:#293b74;font-weight:bold;\">" . $countEventItems . " On Hold for Events</a></div>";

							}

						else $onEventTxt = "";

								

		$dConn = mysql_select_db($dbDatabase, $sConn) or die("Couldn't connect to database $dbDatabase");

					

					$queryCount = "SELECT SUM(qty) AS sumQty,SUM(qtyReceived) AS sumRec,SUM(qtyShipped) AS sumShip,SUM(qtyCanceled) AS sumCancel FROM `amazonvOrders` WHERE `SKU` LIKE '" . $artSKUTest . "%' AND `qty` > (  `qtyReceived` +  `qtyShipped` +  `qtyCanceled` ) ";

									$resultCount = mysql_query($queryCount);

									$rowCount = mysql_fetch_array($resultCount);

									//echo $queryCount;

									$countAmazonOrder = $rowCount[0];

									$countAmazonRec = $rowCount[1];

									$countAmazonShip = $rowCount[2];

									$countAmazonCancel = $rowCount[3];

					$countAmazonItems = $countAmazonOrder - ($countAmazonRec + $countAmazonShip + $countAmazonCancel);

									if ($countAmazonItems > 0)

									$onAmazonTxt = "<div style=\"clear: both\";></div><div style=\"float:right;padding:10px;\"><img src=\"images/amazon.png\" align=\"right\" style=\"margin-top:10px; margin-right:10px; margin-bottom:10px;\"><a href=\"http://www.overstockart.net/sorter/amazon-sku-list.php?qty_filter=Yes&sortby=date_for_sort&filterPO=all&filterFC=all&filter1=" . $artSKUTest . "&submitreport=Run+Items\" target=\"_blank\" style=\"color:#333333;font-weight:bold;\">" . $countAmazonItems . " On Order by Amazon</a></div>";

									else $onAmazonTxt = "";



					$queryCount = "SELECT SUM(qty) AS sumQty FROM `amazonvOrders` WHERE `SKU` LIKE '" . $artSKUTest . "%' AND `staged` = 1";

									$resultCount = mysql_query($queryCount);

									$rowCount = mysql_fetch_array($resultCount);

									//echo $queryCount;

									$countAmazonOrderStaged = $rowCount[0];

					//$countAmazonItems = $countAmazonOrder - ($countAmazonRec + $countAmazonShip + $countAmazonCancel);

									if ($countAmazonOrderStaged > 0)

									$onAmazonTxtStaged = "<div style=\"clear: both\";></div><div style=\"float:right;padding:10px;\"><img src=\"images/amazon-staging.png\" align=\"right\" style=\"margin-top:10px; margin-right:10px; margin-bottom:10px;\"><a href=\"http://www.overstockart.net/sorter/amazon-sku-list.php?qty_filter=Yes&sortby=date_for_sort&filterPO=all&filterFC=all&filter1=" . $artSKUTest . "&submitreport=Run+Items\" target=\"_blank\" style=\"color:#333333;font-weight:bold;\">" . $countAmazonOrderStaged . " Staged for Amazon</a></div>";

									else $onAmazonTxtStaged = "";

									

					//for gallery wraps...

				if (substr($frameSKU,0,3) == "GAL") {

				//	echo $frameSKU . "<br>";

						if ($artSize == 'Customary  16" X 20"' ) $frameSKU = "GALWRP14X18";

						else if ($artSize == 'Classic 20" X 24"' ) $frameSKU = "GALWRP18X22";

						else if ($artSize == 'Square 24" X 24"' ) $frameSKU = "GALWRP22X22";

						else if ($artSize == 'Large 24" X 36"' ) $frameSKU = "GALWRP22X34";

						else if ($artSize == 'Extra Large 30" X 40"' ) $frameSKU = "GALWRP28X38";	

						else if ($artSize == 'Oversized 36" X 48"' ) $frameSKU = "GALWRP34X46";							

				}

					

					$query4 = "SELECT * FROM `catalogcity` WHERE UPPER(CatalogSK) = '" . $frameSKU . "' || UPPER(CatalogSK) = 'FR-" . $frameSKU . "' LIMIT 1;";

					//echo $query4 . "<br>";

					$result4 = mysql_query($query4);

					//if (!mysql_num_rows($result4)) continue;

					if($row4 = mysql_fetch_array($result4))

					{

					$frameSKU = trim($row4["CatalogSK"]);

					$frameTitle = trim($row4["Ordertext"]);

					$frameExt = $row4["FrameWidth"] . '" X ' . $row4["FrameHeight"] . '"'; 					

					$frameInStockMsg = "CAN BE OFFERED";

					$msgColor = "green";

					$msgTotal = "Total price:";

					}

					else

					{

					if ($filterOffer == "checked") continue; //continue if filtering of none available frame items.

					$frameInStockMsg = "CANNOT BE OFFERED";

					$msgColor = "#AE0011";

					$msgTotal = "Painting only price:";

					}

										

				$artPrice = $myrow["artPrice"];

				$framePrice = $myrow["framePrice"];

				$totalPrice = $myrow["totalPrice"];				

				$unitsSoldFrames = $myrow["unitsSoldFrames"];

				$qtyRemain = $myrow["qtyRemain"];

				$qtyOnHandFrames = $myrow["qtyOnHandFrames"];

				$idFrame = $myrow["idFrame"];				

				

	

			$altImage = "overstock/rooms/" . $SKU . ".jpg";

			if (!file_exists($altImage)) $altImage = "overstock/rooms/" . $SKU . "R.jpg";

			if (file_exists($altImage)) {

			$altImageDone2 = "http://www.overstockart.net/sorter/" . $altImage;

			$checkAlt2 = '<a href="' . $altImageDone2 . '" target="_blank">ROOM IMAGE AVAILABLE</a>'; 

			}

			else {

			$checkAlt2 = 'NO ROOM IMAGE';

			$altImageDone2 = "";

			}

		

			/***************START UPC MODULE******************/

			//select from database all the referers

			/*$dbQuery = "SELECT * FROM `upc_manager` ORDER BY `upc5` DESC LIMIT 1";  

			$resultUPC = mysql_query($dbQuery) or die("Couldn't get the Contacts 2");

			$rowUPC = mysql_fetch_array($resultUPC); 

			$upc5 = $rowUPC['upc5'] + 1; //find the last upc and add 1 for frames*/

			

			//Fetch first available upc5 codes:

			$dbQuery = "SELECT `upc5` FROM `upc_open` WHERE 1 ORDER BY `upc5` ASC LIMIT 1";  

			$result = mysql_query($dbQuery) or die("Couldn't get the Contacts");

			$row = mysql_fetch_array($result); 

			$upc5 = $row['upc5']; //find the next open upc

			

			//check if SKU has a UPC code in the UPC manager:

			$num_rows_upc = 0;

			$queryCheck = "SELECT * FROM `upc_manager` WHERE `CatalogSK` = '" . $SKU . "'";

			$resultCheck = mysql_query($queryCheck);

			$num_rows_upc = mysql_num_rows($resultCheck);

			if ( $num_rows_upc > 0) 

			{

			$queryUPC = "SELECT * FROM `upc_manager` WHERE `CatalogSK` = '" . $SKU . "' LIMIT 1";

			$resultUPC = mysql_query($queryUPC);

				while($rowUPC = mysql_fetch_array($resultUPC))

				{

				if ($rowUPC["UPC"] != "")

				$upc = $rowUPC["UPC"];

				else

					{

					$upc = createUPC($upc5);

					$query = "UPDATE `upc_manager` SET `UPC` = '" . $upc . "',  `upc5` = '" . $upc5 . "' WHERE `CatalogSK` = '" . $SKU . "';";

					//$query = "INSERT INTO upc_manager (CatalogSK, UPC) VALUES ('$SKU', '$upc');";

					mysql_query($query) or die("Couldn't MOD row to UPC manager $SKU, $upc");

					$queryDelete = "DELETE FROM `upc_open` WHERE `upc5` = '$upc5';";

				 	mysql_query($queryDelete); 

					} 

				}

			}

			else {

			$upc = createUPC($upc5);

			$query = "INSERT INTO upc_manager (CatalogSK, UPC, upc5) VALUES ('$SKU', '$upc', '$upc5');";

			mysql_query($query) or die("Couldn't add row to UPC manager $SKU, $upc");  

			//echo $query . "<br><Br>";

			$queryDelete = "DELETE FROM `upc_open` WHERE `upc5` = '$upc5';";

			mysql_query($queryDelete);  

			}

			/***************END UPC MODULE******************/

	 		

 		$recomended_qty = 0;

 		$qtyAcceptedText = "";

		$dConn = mysql_select_db($dbDatabase2, $sConn) or die("Couldn't connect to database $dbDatabase2");

				

 		$queryTrans = "SELECT partid,closeout,active,qty_on_hand,unit_sold,sales,qty_available FROM parts WHERE partno ='" . $artSKUTest . "' LIMIT 1;";

 		$transResult = mysql_query($queryTrans);

 		$row = mysql_fetch_array($transResult, MYSQL_NUM);

 		$partidTest = $row[0];

 		$closeout = $row[1];

		$active = $row[2];

		$qtyOnHandArt = $row[3];

		$unitsSoldArt = $row[4];

		$dollarsSold = $row[5];

		$qtyAvailable = $row[6];

				

 		if($partidTest == "") $border = "#c6f2bd";

 		else $border = "#FFFFFF";

		

		$queryQtyAccepted = "SELECT line_id, orderid, quantity, qtyaccepted, qtyReceived FROM orderlines WHERE partid ='" . $partidTest . "' AND qtyaccepted > 0 AND `status` <> 'Cancelled' AND `status` <> 'Closed';";

 		$qtyAcceptedResult = mysql_query($queryQtyAccepted);



 		while($row = mysql_fetch_array($qtyAcceptedResult, MYSQL_NUM))

 		{

		$orderline = $row[0];

 		$orderid = $row[1];

		$quantityOnOrder = $row[2];

		$qtyaccepted = $row[3];

		$qtyOnOrderReceived = $row[4];

		$qtyShipped = 0;

		$qtyInRoute = 0;

		

		$query3 = "SELECT * FROM shipment_line WHERE orderline_id = '" . $orderline . "';"; 

		//echo $query2;

		$oResult2 = mysql_query($query3,$db);

		while($row2 = mysql_fetch_array($oResult2))

		{

			$qtyShipped += $row2["qty"];

        }



		$qtyOnOrderRemain = $quantityOnOrder - $qtyOnOrderReceived;

		

		 if ($qtyShipped > $qtyOnOrderReceived)

		  $qtyInRoute = ($qtyShipped - $qtyOnOrderReceived); 

		

		if($qtyaccepted < $qtyOnOrderRemain)

		$qtyAcceptedText .= "<br>+ EO Qty:<a href=\"http://www.overstockart.cn/communicator/edit.php?op=orderlineedit&line_id=" . $orderline . "\" target=\"_blank\">" . $qtyaccepted . "</a>";  //viewdetail.php?orderid=" . $orderid

		else

		$qtyAcceptedText .= "<br>+ EO Qty:<a href=\"http://www.overstockart.cn/communicator/edit.php?op=orderlineedit&line_id=" . $orderline . "\" target=\"_blank\">" . $qtyOnOrderRemain . "</a>"; //viewdetail.php?orderid=" . $orderid

		}

		

		if($qtyInRoute > 0)

		{

		$qtyAcceptedText .= "<br>+ Qty Shipped:<a href=\"http://www.overstockart.cn/communicator/edit.php?op=orderlineedit&line_id=" . $orderline . "\" target=\"_blank\">" . $qtyInRoute . "</a>"; //viewdetail.php?orderid=" . $orderid

		}

		

 		if($closeout == '1')

 		$closeoutdisplay = "<br><b style=\"color:#293b74\">closeout</b>";

 		else

 		$closeoutdisplay = "";

 		

	$borderOnHand = "";

 

 	if ($qtyOnHandArt > 0)

 	{

 	 $borderOutofStock = "";

 	 $boldOutofStock = "#000000;";

 	}

 	else

 	{

 	//echo "less then 1" . ;

 	 $borderOutofStock = "border: medium #ae0011 dashed;";

 	 $boldOutofStock = "#ae0011;";

 	}

 	

 		$i == 0 ? $color = "#FFFFFF" : $color = "#DDDDDD";

 		$i == 0 ? $class = "normal" : $class = "normal2";

        $i == 0 ? $i = 1 : $i = 0;

?>
<style>
.normal > div{
border: 1px solid #000;
height: 100%;
min-height: 325px;
float:left;
}
</style>
            <tr onMouseOver=this.className='highlight' onMouseOut=this.className="<? echo $class; ?>" bgcolor="<? echo $color; ?>" style="color:<?php echo $flag; ?>; ">
<td>
            <DIV style="width: 100px;"> <?php echo "<input type='checkbox' name='check" . $countItems . "' id='check" . $countItems . "' value='" . $SKU . "' onclick=\"UpdateTotal(" . $num_rows . ")\"> &nbsp;" . $countItems; ?> </DIV>

              <DIV style="WIDTH: 100px;background-color:<?php echo $border; ?>;<?= $borderOnHand; ?>"> <? echo "<a target=_blank href=\"http://www.overstockart.cn/communicator/viewparts.php?partid=$partidTest\" target=\"_blank\">" . $artSKU . "</a><br>" . $artName . "<br>Price: $" . $artPrice . "<br><br>" . $closeoutdisplay; ?> 

			  <br><br>

                  <? echo "<center style=\"font-weight:bold; color:#293b74; font-size:10px;\">" . $SKU . "<br><br>" . $upc . "<br><br>" . $msgTotal . " $" . $totalPrice . "<br>" . $checkAlt2 . "</center>"; ?>

			  <br /><br /> 

		<?php  if($active == '0')

		echo "<b style=\"color:#ae0011\">DISCONTINUED!</b>"; ?>

		</DIV>

         <DIV style="WIDTH: 180px"> <? echo $frameSKU . "<br>" . $idFrame . "<br><br><b style=\"color:" . $msgColor . ";\">" . $frameInStockMsg . "</b><br><b>" . $frameTitle . "</b><br>Ext. Dimensions: " . $frameExt . "<br>Price: $" . $framePrice; ?><br>

                  <br>

                  Frame units sold: <? echo  $unitsSoldFrames; ?> <br>

                  Frames Qty on Hand: <? echo $qtyOnHandFrames ; ?><br>

				  

				  <!--On Event txt -->

				  <?= $onEventTxt; ?>

				  <!--On Amazon txt -->

				  <?= $onAmazonTxt; ?>

                  

                  <?= $onAmazonTxtStaged; ?>

				  

                  </DIV>

				  <?php 

				 $main_img = "overstock/" . $SKU . ".jpg";

				 //echo $main_img;

				 //$th_img = "http://www.overstockart.net/sorter/overstock/" . $SKU . "_tn.jpg";

				 if (file_exists($main_img))

				  {

 				  $th_img = "overstock/" . $SKU . "_tn.jpg";				 				 

				  }

				 else

				 {

				  $main_img = "overstock/" . $SKU . "_1P.jpg";

				  $th_img = "overstock/" . $SKU . "_1P_tn.jpg";

				 }

				  ?>

            <DIV style="WIDTH: 200px"> <? echo "<a href=\"" . $main_img . "\" target=\"_blank\"><img src=\"" . $th_img . "\" border=\"0\" alt=\"" . $artName . "\"></a>"; ?> </DIV>

            <DIV style="WIDTH: 69px"><? echo "$" . $dollarsSold ; ?><br>

                  Units sold: <? echo  $unitsSoldArt; ?></DIV>

             <DIV style="WIDTH: 56px;<?= $borderOutofStock; ?>"><b style="color:<?= $boldOutofStock; ?>;font-size:10px;"><? echo $qtyOnHandArt  . "</b>

			  <br><br><b style=\"color:#AE0011;font-size:10px;\">Qty Available: " . $qtyAvailable; ?></b>

              <?php echo "<b style=\"color:#293b74;font-size:10px;\">" . $qtyAcceptedText . "</b>"; ?>

              </DIV>

              <DIV style="WIDTH: 53px"><? echo $qtyRemain; //echo " " . $qtyRemain . " = " . $qty_on_order . " - " . $qty_received; ?></DIV>

              <!--   <td width="60"><? echo  $unitsSoldArt; ?></td>-->

              <!--     <td width="60"><b><? echo  $recomended_qty; ?></b></td>-->

            <DIV style="WIDTH: 66px"><?php echo "<input type='text' size='5' name='qty" . $SKU . "' id='qty" . $SKU . "' value='$qtyAvailable'>"; ?></DIV>
</td>
            </tr>

            <?php 

 $countItems++;

 		}

 ?>

          </table>

        </div></td>

    </tr>

  </table>

  <table cellspacing="0" cellpadding="1" border="1" width="920">

    <tr valign="top">

      <td valign="top"><table cellspacing="0" cellpadding="1" border="1" width="920">

          <tr>

            <td style="font-size:11px; font-weight:normal; color:#333333;">Total # Items:

				  <input size="10" style="margin-left:10px; font-size:11px; font-weight:normal; color:#ae0011;" type="Text" name="totalRows" id="totalRows" class="totalRows" value=""></td>

           <!-- <td style="font-size:11px; font-weight:normal; color:#333333;">Event/ Retailer Name:

              <input size="15" style="margin-left:10px; font-size:11px; font-weight:normal; color:#333333;" type="Text" name="eventName" id="eventName" value=""></td>

            <td style="font-size:11px; font-weight:normal; color:#333333;">Start Date:

              <input size="10" style="margin-left:10px; margin-right:10px;" type="Text" name="startdate" >

              <a href="javascript: void(0);" onClick="return getCalendar(document.create_order.startdate);" ><img src="cal/calendar.png" border="0" /></a></td>-->

            <td class=menu><!--<input type="hidden" name="status" value="Initiated" />-->

              <input onClick="return confirm('Are you sure you want to add this selection?')" type='submit' NAME='createcsv' value='Save Your Selection' ></td>

          </tr>

        </table></td>

    </tr>

  </table>

</form>

<center>

<br>

<br>

<button onClick="window.location='index.php'">Back to Main Menu</button>

