<?php
require "./Cart.php";

if (isset($Update)) {
     if (empty($II)) {
          mysql_query("INSERT INTO Items (ItemSKU) VALUES ('$ItemSKU')");
          $II = mysql_insert_id();
     }

     $ItemCost=ereg_replace( "[^0-9.]", "", $ItemCost);
     $ShippingCost=ereg_replace( "[^0-9.]", "", $ShippingCost);
     if (!empty($ItemPicture)) {
          $ItemGraphic="<img src=\"../images/$ItemPicture\">";
     }
     $sql =    "UPDATE Items SET ItemSKU = '$ItemSKU',
          ItemName='$ItemName',
          ItemDescription='$ItemDescription',
          ItemCost='$ItemCost',
          Category='$Category',
          ShippingCost='$ShippingCost',
          ItemGraphic='$ItemGraphic',
          PaymentCycle='$PaymentCycle'
          WHERE ItemID='$II'";
     mysql_query($sql);
}

adminHeader("$Company","Product description");

$result = mysql_query("SELECT * FROM Items WHERE ItemID = '$II'");
list($IS,$IN,$ID,$IC,$Ca,$SC,$II,$IG,$PC) = mysql_fetch_row($result);
mysql_free_result($result);

echo "<table border='0' cellpadding='10' cellspacing='10'>";


switch ($PC) {
case 'O': $PC = 'one time'; break;
case 'W': $PC = 'weekly subscription'; break;
case 'M': $PC = 'monthly subscription'; break;
case 'Y': $PC = 'yearly subscription'; break;
}

if (!empty($IG)) {
     echo "<tr><td></td><td></td><td rowspan=\"5\" valign=\"bottom\">";
     echo "<p><br><p><br>$IG</td></tr>";
}


echo "<tr><td>";
redFont ("Arial","Product Code: ");
echo "</td><td>";
blueFont("Arial"," $IS");
echo "</td><td></td></tr>";
echo "<tr><td>";
redFont ("Arial","Product Name: ");
echo "</td><td>";
blueFont("Arial"," $IN");
echo "</td><td></td></tr>";
echo "<tr><td>";
redFont ("Arial","Price: ");
echo "</td><td>";
$NC=number_format($IC,"2",".","thousands_sep");
blueFont("Arial"," $$NC ($PC)");
echo "</td><td></td></tr><tr><td>";
redFont ("Arial","Shipping: ");
echo "</td><td>";
if ($SC == "0.00") {
blueFont("Arial"," $NoShipping");
} else {
$SC=number_format($SC,"2",".","thousands_sep");
blueFont("Arial"," $$SC");
}
echo "</td><td></td></tr>";
echo "<tr><td>";
if ($ID != "") {
redFont ("Arial","Description: "); }
echo "</td><td colspan='2'>";
blueFont("Arial"," $ID");
echo "</td></tr>";

echo "</table>";

echo "<FORM ACTION=\"\" METHOD=\"POST\">";

echo '<table>';
$result=mysql_query("SELECT OptionID, OptionName, OptionType FROM ItemOption WHERE ItemID = $II ORDER BY OptionID");
while (list($OptionID, $OptionName, $OptionType)=mysql_fetch_row($result)) {
     echo "<tr><td valign=top>$OptionName</td><td valign=top>";

     $option_value = 'OV_' . ereg_replace(" ", "_", $OptionName);
     switch ($OptionType) {
     case 'text':
          echo "<input type=\"text\" name=\"$option_value\">";
          break;
     case 'textarea':
          echo "<textarea rows=\"3\" cols=\"40\" name=\"$option_value\"></textarea>";
          break;
     case 'select':
          echo "<select name=\"$option_value\">";
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<option value=\"$vname\"> $vname";
          }
          mysql_free_result($ov);
          echo '</select>';
          break;
     case 'selectm':
          echo "<select name=\"${option_value}[]\" size=\"5\" multiple>";
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<option value=\"$vname\"> $vname";
          }
          mysql_free_result($ov);
          echo '</select>';
          break;
     case 'checkbox':
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<input name=\"${option_value}[]\" type=\"checkbox\" value=\"$vname\"> $vname<br>";
          }
          mysql_free_result($ov);

          break;
     case 'radio':
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<input name=\"${option_value}\" type=\"radio\" value=\"$vname\"> $vname<br>";
          }
          mysql_free_result($ov);
          break;
     }

     echo '</td></tr>';
}
mysql_free_result($result);
echo '</table></form>';

adminFooter($Relative);
?>
