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

$dir='admin';
checkauth($dir);

include "menu.htm";

//-------------- ADD new dir ---------------------
if (isset($insertme))		
{
$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"))
       {
	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($namezz_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 guarantee 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("
        insert into product(
  ID,
  Name,
  Description,
  Price,
  PriceWithoutVat,
  ArrivingDate,
  Photo,
  Count,
  CategoryID,
  DiscountProcent,
  DiscountFromDate,
  DiscountTillDate,
  GarantID,
  TermOfGarant,
  SendGarant,
  SumOfContinue,
  TermOfContinue
  ) 
       values (
  null, 
'$namezz_nes',
'$descriptionzz_nes',
'$pricewithzz',
'$pricewithoutzz',
'$arrivingdate',
'nothing',
'$countzz_nes',
$categoryzz_selnes,
'$discountprocentzz',
'$discountfromdate',
'$discounttilldate',
'$garantzz_selnes',
'$termzz_selnes',
'$sendzz_selnes',
'$sumofcontinuezz',
'$termtocontinuezz'
 )") or die (mysql_error(). " : ".mysql_errno());

       if ( ($result) != 1)
       print "Sorry,You are trying to enter invalid data ";
        else
        {
         print "New product was added";
         $lastid=mysql_insert_id(); //upload images if $if_upload is set
         if ($if_upload=="yes")
         {
              //require "upload.php";
              //echo "<script language=javascript>location.href='upload.php?lastid=$lastid';</script>";
              echo "<br>If you want to upload a new image do it now,overwise close the window ";
              echo "<script language=javascript>
              var urlis='upload.php?lastid='+$lastid;
              newwnd = window.open(urlis ,'cl', 'toolbar=0,resizable=1,status=0,menubar=0,scrollbars=1,left=120,top=5,width=500,height=520');
              newwnd.focus();</script>";
         }
         exit;
        }
       }

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

              
}

//----------------------- and insert new ----------------

?>
<form action=<?=$PHP_SELF?> method=post name=productadd >
<input type="hidden" name=insertme>
<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='<?=$namezz_nes?>'></td>
</tr>
<tr>
	<td>*Description of product</td>
	<td><textarea cols=20 rows=5 name="descriptionzz_nes"><?=$descriptionzz_nes?></textarea></td>
</tr>
<tr>
	<td>Price with VAT (like xxx.yy) </td>
	<td><input type=text size=7 maxlength="7" name=pricewithzz value='<?=$pricewithzz?>'></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='<?=$pricewithoutzz?>'></td>
</tr>
<tr>
	<td>*Arriving date</td>
	<td>
       <? 
       if (isset($arrivingdate))
              currentdate($arrivingdate);       
       else
              currentdate();
       ?>
       </td>
</tr>
<tr>
	<td>*Product quantity </td>
	<td><input type=text size=5 maxlength="3" name=countzz_nes value='<?=$countzz_nes?>'></td>
</tr>
<tr>
	<td>*Category</td>
	<td>
<?
          $query="select ID,CategoryName from category";
          $result=mysql_query($query)or die (mysql_error(). " : ".mysql_errno());     
         print "<select name=categoryzz_selnes><option value='nothing'>--Select one--";
          while ($row=mysql_fetch_object($result) )
          {
              if (isset($categoryzz_selnes) && $categoryzz_selnes==$row->ID)
               print "<option value='$row->ID' selected>$row->CategoryName</option>";
              else
               print "<option value='$row->ID'>$row->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 (isset($discountzz_selnes))
        {
           if ($discountzz_selnes=='yes')
             echo" <option value='yes' selected>Yes
                   <option value='no'>No";
           else
             echo" <option value='yes' >Yes
                   <option value='no' selected>No";
        }
        else
             echo" <option value='yes' >Yes
                   <option value='no'>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  (isset($discountprocentzz) && $discountprocentzz==$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>
       <? 
       if (isset($discountfromdate))
              currentdate($discountfromdate);       
       else
              currentdate();
       ?>
       </td>
</tr>
<tr>
	<td><font color=#d09c00><b>D</b></font>iscount till date </td>
	<td><? 
       $arr=getdate(time()+(7*24*60*60));
       $dateis=$arr["year"]."-".$arr["mon"]."-".$arr["mday"];
       if (isset($discounttilldate))
              currentdate($discounttilldate);       
       else
              currentdate($dateis);

       ?>
       </td>

</tr>
<tr>
	<td>*Warranty service company</td>
	<td>
<?
          $query="select ID,NameOfCompany from garant";
          $result=mysql_query($query)or die (mysql_error(). " : ".mysql_errno());     
          print "<select name=garantzz_selnes><option value='nothing'>--Select one--";
          while ($row=mysql_fetch_object($result) )
          {
              if (isset($garantzz_selnes) && $garantzz_selnes==$row->ID)
               print "<option value='$row->ID' selected>$row->NameOfCompany</option>";
              else
               print "<option value='$row->ID'>$row->NameOfCompany</option>";
          }             
          print "</select>";
?>

        </td>
</tr>
<tr>
	<td>*Warranty period</td>
    <td bgColor=white><SELECT name='termzz_selnes'>
     <?
	while ($element = each($arr_term)):
	  if (isset($termzz_selnes) && $termzz_selnes==$element["key"])
            echo "<OPTION value=".$element["key"]." selected>".$element["value"]."</OPTION>";	
	  else if ($element["key"] == "nothing")
            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 (isset($sendzz_selnes))
        {
           if ($sendzz_selnes=='yes')
             echo" <option value='yes' selected>Yes
                   <option value='no'>No";
           else
             echo" <option value='yes' >Yes
                   <option value='no' selected>No";
        }
        else
             echo" <option value='yes' >Yes
                   <option value='no'>No";

        ?>
        </select>       
        </td>
</tr>
<tr>
	<td>Fee to continue warranty service</td>
	<td><input type=text size=7 maxlength=7 name=sumofcontinuezz value='<?=$sumofcontinuezz;?>'></td>
</tr>
<tr>
	<td>Period of warranty to continue warranty service</td>
    <td bgColor=white><SELECT name='termtocontinuezz'>
     <?
        reset($arr_term);
	while ($element = each($arr_term)):
	  if (isset($termtocontinuezz) && $termtocontinuezz==$element["key"])
            echo "<OPTION value=".$element["key"]." selected>".$element["value"]."</OPTION>";	
	  else if ($element["key"] == "nothing")
            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('productadd ')"></td> 
</tr>
</table>
</form>

