<?
$rez=include("setup.php");
if (! $rez) //if can't find setup.php or not chmod'ed 
 {
  echo "<font color=red>Check Your setup.php file !</font>";
	exit;
 }

$excellentwork=0;
//-------------- ADD new dir ---------------------
if (isset($updateme))		
{
$flag=1; 
$errorcounter=1;       
       //--------------------------- check date --------------------
       if (isset($month0) && isset($year0)):
       if (! checkdate ($month0,$day0,$year0))
       {
        echo "$errorcounter. Please check if arriving date is correct<br>";
        $errorcounter++;
        $flag=0;
       }
       else if (! checkdate ($month1,$day1,$year1))
       {
        echo "$errorcounter. Please check if discount from date is correct or set 'discount' to 'no'<br>";
        $errorcounter++;
        $flag=0;
       }
       else if (! checkdate ($month2,$day2,$year2))
       {
        echo "$errorcounter. Please check if discount till date is correct or set 'discount' to 'no'<br>";
        $errorcounter++;
        $flag=0;
       }
       else
       {
       $arrivingdate=$year0."-".$month0."-".$day0;
       $discountfromdate=$year1."-".$month1."-".$day1;
       $discounttilldate=$year2."-".$month2."-".$day2;
       }
       endif;
       //------------ end checking date --------------------

       if($discountzz_selnes=='no')   //if this product has no discount
       {
       $discountfromdate="0000-00-00";
       $discounttilldate="0000-00-00";
       $discountprocentzz=0;
       }
       
       if( check_if_exist($namezz_nes,"product","Name")>1)
       {
	print "$errorcounter. This product exists.Please write another name<br>";
        $errorcounter++;
        $flag=0;
       }

       if (! empty($pricewithoutzz) && empty($pricewithzz))
       {
              $pricewithzz=pricewithVAT($pricewithoutzz);
       }


       else if (empty($pricewithoutzz) &&  !empty($pricewithzz))
       {
       $pricewithoutzz=pricewithoutVAT($pricewithzz);
       }

       if ($pricewithoutzz >=$pricewithzz)
       {
       print "$errorcounter. Price without VAT cant be greater that with.You may not write price without VAT<br>";
       $errorcounter++;
       $flag=0;
       }

       if (is_clean_text($namezz_nes,2,30)==false)
       {
        echo "$errorcounter. Please check name of your product:must include alphabetic characters or digits only.2-30 characters are possible.<br>";
        $errorcounter++;
        $flag=0;
       }
       else addslashes(trim($descriptionzz_nes));

       if (is_clean_text($descriptionzz_nes,5,255)==false)
       {
        echo "$errorcounter. Please check description of your product:must include alphabetic characters or digits only.5-255 characters are possible.<br>";
        $errorcounter++;
        $flag=0;
       }
       else  addslashes(trim($descriptionzz_nes));

       if (is_numericit($pricewithzz,1,7)==false)
       {
        echo "$errorcounter. Please check price of your product:must include digits or '.' only.1-7 digits are possible.<br>";
        $errorcounter++;
        $flag=0;
       }
       else
       addslashes(trim($pricewithzz));

       if (is_numericit($countzz_nes,1,3)==false)
       {
        echo "$errorcounter. Please check quantity of your product:must include digits only.1-3 digits are possible.<br>";
        $errorcounter++;
        $flag=0;
       }
       else
       addslashes(trim($countzz_nes));

       if ((! empty($sumofcontinuezz)) && is_numericit($sumofcontinuezz,0,7)==false)
       {
        echo "$errorcounter. Please check payment to continue warranty service of your product:must include digits or '.' only.0-7 digits are possible.<br>";
        $errorcounter++;
        $flag=0;
       }
       else
        addslashes(trim($sumofcontinuezz));




      if($flag)
       {
       $result=mysql_query("
        update product set
  Name='$namezz_nes',
  Description='$descriptionzz_nes',
  Price='$pricewithzz',
  PriceWithoutVat='$pricewithoutzz',
  ArrivingDate='$arrivingdate',
  Count='$countzz_nes',
  CategoryID=$categoryzz_selnes,
  DiscountProcent='$discountprocentzz',
  DiscountFromDate='$discountfromdate',
  DiscountTillDate='$discounttilldate',
  GarantID='$garantzz_selnes',
  TermOfGarant='$termzz_selnes',
  SendGarant='$sendzz_selnes',
  SumOfContinue='$sumofcontinuezz',
  TermOfContinue='$termtocontinuezz'
  where ID=$id
         ") or die (mysql_error(). " : ".mysql_errno());

       if ( ($result) != 1)
       print "Sorry,You are trying to enter invalid data ";
        else
        {
         print "Product <b>$namezz_nes</b> was modified";
         $excellentwork=1;
              if ($Photo == "nothing") //i get this variable from hidden field.Must be '$Photo' (uppercase)
              {
                $lastid=$id; //upload images if $if_upload is set
//                echo "Last id is $lastid";
                 if ($if_upload=="yes")
                   require "upload.php";
               }
        }

     }

      else //if $flag==0 it means we have errors
      {
       print "<font color=red><br><b>Please correct ".($errorcounter-1)." mistake(s) above to continue</b></font>";
      }

              
}

//----------------------- end update data ----------------

// ---------------------- delete image ---------------------
 if (isset($delimage))
 {

$result=unlink("$dir_to_upload/$Photo");

       $result=mysql_query("
        update product set
         Photo='nothing'
         where ID=$id
         ") or die (mysql_error(). " : ".mysql_errno());

       if ( ($result) != 1)
       print "Sorry,can't delete the image ";
        else
        {
         print "Image was deleted sucsessfully.<br>If you want to upload a new image do it now,overwise close the window";
         }

 }

//----- end delete image -----------

// ------------ show upload images -------------

if (isset($delimage) && isset($Photo) && $Photo!="nothing")
{
            $lastid=$id; //upload images if $if_upload is set
                 if ($if_upload=="yes")
                   require "upload.php";
         exit;
}
// ------------- end show upload images --------


// --------------- dont show if second time reloaded --------
if (isset($id) &&  ($excellentwork==1))
     exit;
// --------------- end dont show below ----------------------

// --------------------- show current -----------------
	$sql = "SELECT * FROM product where ID=$loginis";	
        $result = mysql_query($sql) or  die (mysql_error(). " : ".mysql_errno());
        $row=mysql_fetch_object($result);

?>
<form action=<?=$PHP_SELF?> method=post name=productupdate >
<input type="hidden" name=updateme>
<input type="hidden" name='Photo' value='<?=$row->Photo;?>'>
<input type=hidden name=id value='<?=$row->ID?>'>
<input type=hidden name=loginis value='<?=$row->ID?>'>
<table width=420 border=0 cellpadding=2 cellspacing=4>
<tr>
	<td>*Name of product</td>
	<td><input type=text size=20 maxlength="22" name=namezz_nes value='<?=$row->Name?>'></td>
</tr>
<tr>
	<td>*Description of product</td>
	<td><textarea cols=20 rows=5 name="descriptionzz_nes"><?=$row->Description?></textarea></td>
</tr>
<tr>
	<td>Price with VAT (like xxx.yy) </td>
	<td><input type=text size=7 maxlength="7" name=pricewithzz value='<?=$row->Price?>'></td>
</tr>   
<tr>
	<td colspan=2 align=left><font color=red>If don't write anything in field below OR above the system will count it automatically.You may write prices in both fields too</font></td>
</tr>

<tr>
	<td>Price without VAT (like xxx.yy) </td>
	<td><input type=text size=7 maxlength="7" name=pricewithoutzz value='<?=$row->PriceWithoutVat?>'></td>
</tr>
<tr>
	<td>*Arriving date</td>
	<td>
       <? 
              currentdate($row->ArrivingDate);       
       ?>
       </td>
</tr>
<tr>
	<td>*Product quantity </td>
	<td><input type=text size=5 maxlength="3" name=countzz_nes value='<?=$row->Count?>'></td>
</tr>
<tr>
	<td>Category</td>
	<td>
<?
          $query1="select ID,CategoryName from category";
          $result1=mysql_query($query1)or die (mysql_error(). " : ".mysql_errno());     
         print "<select name=categoryzz_selnes><option value='nothing'>--Select one--";
          while ($row1=mysql_fetch_object($result1) )
          {
              if ($row->CategoryID==$row1->ID)
               print "<option value='$row1->ID' selected>$row1->CategoryName</option>";
              else
               print "<option value='$row1->ID'>$row1->CategoryName</option>";
          }             
          print "</select>";
?>
        </td>
</tr>
<tr>
	<td>*Discount<br><small>If you select 'no' - dont modify <font color=#d09c00><b>D</b></font> (discount % and dates) below</small></td>
	<td><select name=discountzz_selnes>
        <option value='nothing'>--Select one--
        <? 
        if ($row->DiscountProcent !=0)
             echo" <option value='yes' selected>Yes
                   <option value='no'>No";
       
        else
             echo" <option value='yes' >Yes
                   <option value='no' selected>No";
       
        ?>
        </select>       
        </td>
</tr>
<tr>
	<td><font color=#d09c00><b>D</b></font>iscount procent</td>
<?

echo "<td><select name='discountprocentzz'>";
       for ($i=0;$i<=99;$i++)
       {
       if  ($row->DiscountProcent==$i)       
        echo "<option  value=$i selected>$i</option>";
       else
        echo "<option  value=$i>$i</option>";
        }
echo "</select></td> ";
 ?>

</tr>
<tr>
	<td><font color=#d09c00><b>D</b></font>iscount from date</td>
	<td>
       <? 
              currentdate($row->DiscountFromDate);       
       ?>
       </td>
</tr>
<tr>
	<td><font color=#d09c00><b>D</b></font>iscount till date </td>
	<td><? 
              currentdate($row->DiscountTillDate);       
       ?>
       </td>

</tr>
<tr>
	<td>*Warranty service company</td>
	<td>
<?
          $query1="select ID,NameOfCompany from garant";
          $result1=mysql_query($query1)or die (mysql_error(). " : ".mysql_errno());     
          print "<select name=garantzz_selnes><option value='nothing'>--Select one--";
          while ($row1=mysql_fetch_object($result1) )
          {
              if ($row->GarantID==$row1->ID)
               print "<option value='$row1->ID' selected>$row1->NameOfCompany</option>";
              else
               print "<option value='$row1->ID'>$row1->NameOfCompany</option>";
          }             
          print "</select>";
?>
        </td>
</tr>
<tr>
	<td>*Warranty period</td>
    <td bgColor=white><SELECT name='termzz_selnes'>
     <?
        print "<option value='nothing'>--Select one--";
	while ($element = each($arr_term)):
	  if ($row->TermOfGarant==$element["key"])
            echo "<OPTION value=".$element["key"]." selected>".$element["value"]."</OPTION>";	
          else
            echo "<OPTION value=".$element["key"]." >".$element["value"]."</OPTION>";	
	endwhile;
     ?>
	</SELECT> </td>
</tr>
<tr>
	<td>*Send warranty coupon to warranty service company<br></td>
	<td><select name=sendzz_selnes>
        <option value='nothing'>--Select one--
        <? 
           if ($row->SendGarant=='yes')
             echo" <option value='yes' selected>Yes
                   <option value='no'>No";
           else
             echo" <option value='yes' >Yes
                   <option value='no' selected>No";
        ?>
        </select>       
        </td>
</tr>
<tr>
	<td>Payment to continue warranty period</td>
	<td><input type=text size=7 maxlength=7 name=sumofcontinuezz value='<?=$row->SumOfContinue;?>'></td>
</tr>
<tr>
	<td>Warranty period to continue</td>
    <td bgColor=white><SELECT name='termtocontinuezz'>
     <?
        reset($arr_term);
        print "<option value='nothing'>--Select one--";
	while ($element = each($arr_term)):
	  if ($row->TermOfContinue==$element["key"])
            echo "<OPTION value=".$element["key"]." selected>".$element["value"]."</OPTION>";	
          else
            echo "<OPTION value=".$element["key"]." >".$element["value"]."</OPTION>";	
	endwhile;
     ?>
	</SELECT> </td>
</tr>
<tr>
        <td align=center><br><input type=reset value=Reset></td>
	<td align=center><br><input type=submit name=submit value=Submit onclick="return validate_form_no_email('productupdate ')"></td> 
</tr>
</table>
</form>

<?
 if ($row->Photo != "nothing")
       {
?>
<br>
<form action=<?=$PHP_SELF?> name=imagedelete method=post>
<input type=hidden name=delimage >
<input type="hidden" name='Photo' value='<?=$row->Photo;?>'>
<input type=hidden name=id value='<?=$row->ID?>'>
<table align=center width=200>
<tr>
<td align=center>
<?
   echo "<img src='$dir_to_upload/$row->Photo'>";
?>
<br>
</td>
</tr>
   <tr><td align=center><input type=submit name=submit value="Delete image" onclick="javascript:return del()"></td></tr>

</table>
<?
       }
?>
