 <?php 
    require_once("headUserAcc.php");
   if(isset($_POST['post']) || isset($_POST['submit']))
 {

if( isset($_POST['admin']))
  {
 $shopid="";//20160828035211";//$_POST['shopid'];
// $owniD=$_POST['owniD'];
 
//////////////////
 //////////////

   $vendor="";
   $itemname="";
   $itemtype= "";
   $itemcategory ="";
   $expiredate = "";   
   $itemdescription = "";
   $itemcost ="";
   $currency="";
   $post=$_POST['post'];

   if($post=="Submit"){

   ini_set('upload_max_filesize', '2M');
   ini_set('post_max_size', '2M');
   ini_set('max_input_time', 300);
   ini_set('max_execution_time', 300);
  
    $fn = $_FILES["file"]["name"]; 
    $type=$_FILES["file"]["type"];
    $size=$_FILES["file"]["size"];
    $error=$_FILES["file"]["error"];
    $tmp_name=$_FILES["file"]["tmp_name"];
    $ext = pathinfo($fn, PATHINFO_EXTENSION);
    $allowedtype=array('png','webp','jpg','jpeg','gif','jpeg','JPG','JPEG','GIF','PNG');

    
        /////////////////

        /////////////////


   $itemname=$_POST['itemname'];
   $itemtype=$_POST['itemtype'];
   $itemcategory =$_POST['itemcategory'];
   $expiredate =$_POST['expiredate']; 
   $itemdescription =$_POST['itemdescription'];
   $itemcost =$_POST['itemcost'];
   $currency=$_POST['currency'];
   $vendor=$_POST['vendor'];
   $itemid =substr(Date("Ymdhis"),2);

if($econ->vImageType($ext,$allowedtype) && $econ->vImageError($error) && $econ->vImageSize($size) && $econ->vImageExist($fn) && $econ->vImageSelectF($fn) ){
//echo "string";
   if(!$scon->vvendor($vendor) || !$scon->vitemname($itemname) || !$scon->vitemcategory($itemcategory) || !$scon->vitemtype($itemtype) || !$scon->vitemdescription($itemdescription) || !$scon->vitemcost($itemcost,$currency))
      {
  ////////////// || !$econ->vexpiredate($expiredate)
       // echo "string";
        ?>
         <center>
         <div>
         <sup>
             <?php
             echo  $scon->evendor($vendor)."<p/>"; 
              echo  $scon->eitemname($itemname)."<p/>";
              echo  $scon->eitemtype($itemtype)."<p/>";
              echo  $scon->eitemcategory($itemcategory)."<p/>";
              echo  $scon->eitemdescription($itemdescription)."<p/>";
              echo  $scon->eitemcost($itemcost,$currency)."<p/>";
             // echo  $econ->eexpiredate($expiredate)."<p/>";
    
             ?>
       </sup>
       </div>
       </center> 

        <?php
        /////////////

      }
      else{
        
        ?>


         <center>
         <div>
        
        <?php 
         $r= $scon->addItem($itemid,$itemdescription,$itemcost,$itemcategory,$itemname,$itemtype,$fn,$currency,$vendor);
         if($r->rowCount()>0)
         {
             //$expiredate,
           echo $scon->uploadImage($fn,$itemname, $itemid,$tmp_name);
           $itemname="";
           $itemtype= "";
           $itemcategory ="";
           $expiredate = "";   
           $itemdescription = "";
           $itemcost ="";
           $currency="";
           $vendor="";
         }else{
         // echo "Submission error!..";
          $u=$r->ErrorInfo();
          print_r($u);//"Error occurred while saving data!..";
              }
        ?> 
       </div>
       </center> 

        <?php
    
      }
       
    }
    else{
///////////////////////
      ?>
         <center>
         <div>
         <sup>
             <?php
              echo $econ->eImageType($ext,$allowedtype)."<p/>";
              echo $econ->eImageError($error)."<p/>";
              echo $econ->eImageSize($size)."<p/>";
              echo $econ->eImageExist($fn)."<p/>"; 
              echo $econ->eImageSelectF($fn)."<p/>";
      //////////////////
       ?>
       </sup>
       </div>
       </center> 

        <?php
        }

}
 
 

        //////////////////////

        ////////////////////
 
     
?> 
 
  
 
   <!--NEW USER REGISTRATION FORM, DISPLAYED BY DEFAULTS IN PAGE LOAD-->


<center>


<!--HEADING-->
 
   <?php
    require_once('superShopNavLink.php');
  ?>
 


<!--TAIL-->

  <fieldset id="new_item_reg_form">
<label id="reg_form_tile_label">Add new item here </label></p/>
<i>Please fill the form below to add new item-</i>
<form name="new_user_registration" id="new_user_registration" action="postNewItem.php" method="POST" autocomplete="on" enctype="multipart/form-data">

<table border="0" id="reg_form_table">

   <input class="hi" type="hidden" name="admin"  value="<?php echo $admin;?>">


    <input class="hi" type="hidden" name="shopid"  value="<?php  echo $shopid;?>"> 

        
    <tr>
  
  <td height="40px" width="20%" valign="top">

        <label id="mini_label">Name:<sup>*</sup></label><br>

  </td> 


  <td id="reg_form_table_cell">
     
         <input type="text" name="itemname" id="itemname" class="prof_info" value="<?php echo $itemname;?>"  /><br/>
         <sup id="si"></sup>
         
  </td>
  <td rowspan="4" width="35%">
    
                 <div id="deta" ><img id="itemimage">  </div>
  </td>

</tr>
  
<tr>
  
  <td height="40px" width="20%" valign="top">

        <label id="mini_label">Type:<sup>*</sup></label><br>

  </td> 


  <td id="reg_form_table_cell">
     
         <input type="text" name="itemtype" id="itemtype" class="prof_info" value="<?php echo $itemtype;?>"  /><br/>
         <sup id="ln"></sup>
         
  </td>

</tr>
  <tr>
 
  <td height="40px"   valign="top">

        <label id="mini_label">Category:<sup>*</sup></label><br>

  </td> 

  <td id="reg_form_table_cell">

    
         <select name="itemcategory" id="itemcategory" class="prof_info" >
          <option value="">Select</option>
          <option value="Sound system">Sound system</option>
          <option value="Smart phone">Smart phone</option>
          <option value="Power generator">Power generator</option>
          <option value="Water pump generator">Water pump generator</option>
          <option value="Television">Television</option>
          <option value="Laptop computer">Laptop computer</option>
          <option value="Ipad">Ipad</option>
          <option value="Desktop computer">Desktop computer</option>
          <option value="Office and Stationary">Office and Stationary</option>
          <option value="Printers">Printers</option>
          <option value="Camera">Camera</option>
          <option value="Printer cartridge">Printer Cartridge</option> 
          <option value="Video Camera">Video Camera </option>
          <option value="Laminator Office">Laminator Office</option>
          <option value="Agrochemical Fertilizers">Agrochemical</option>
          <option value="Agro seeds">Agro seeds</option>
          <!--?php/*Touch screen smartphone
          $HD=$scon->getShopCategory(); 
    
        if($HD->rowCount()>0){
          $ro=$HD->fetchAll();
          foreach ($ro as $key => $v) {
          echo "<option value='".$v['category']."'>".$v['category']."</option>";
          }
         }
           */?--> 
</select>
          <br/>
         <sup id="rt"></sup>
  </td>

  </tr>


<tr>
 
  <td height="40px"   valign="top">

        <label id="mini_label">Expire date(optional):</label><br>

  </td> 

  <td id="reg_form_table_cell">

    
         <input type="date" name="expiredate" id="expiredate" class="prof_info" value="<?php echo $expiredate;?>"  /><br/>
         <sup id="rv"></sup>
  </td>

  </tr>
  <tr>
  <td>
    Vendor:
  </td>
  <td>
    <input type="text" name="vendor" id="vendor" class="prof_info"  value="<?php echo $vendor;?>" /><br/>
         <sup id="vd"></sup>
  </td>
   <td><label id="mini_label">Choose image:<sup>*</sup></label><br/>

    
              <input type="file" class="prof_info" name="file" id="file"  onchange="readURL(this);"/>
              <sup id="mg"></sup>
  </td>
</tr>

  <tr>
 
  <td height="40px"    valign="top">

        <label id="mini_label">Price:<sup>*</sup></label><br>

  </td> 

  <td id="reg_form_table_cell">

    
         <input type="text" name="itemcost" id="itemcost" class="prof_info"  value="<?php echo $itemcost;?>" /><br/>
         <sup id="rdn"></sup>
  </td>
  

 
   <td align="right"> Currency:<select  name="currency" id="currency" >
        <option value="TZS">TZS</option>
        <option value="USD">USD</option>
        </select>
       
   </td>
 </tr>

  <tr>
 
  <td height="40px"   valign="top">

        <label id="mini_label">Description:<sup>*</sup></label><br>

  </td> 

  <td id="reg_form_table_cell" colspan="2">

    
         <textarea   name="itemdescription" id="itemdescription"  width="95%"  height="60px"><?php echo $itemdescription;?></textarea><br/>
         <sup id="rd"></sup>
  </td>

  </tr>

   
<tr>

  <td colspan="3" align="center">
        <input type="submit"  name="post" value="Submit" id="submit" />
  </td>

</tr>

</table>
 
</form>
</fieldset> </center>
  <?php
 
    }
    else{
    echo "Error occurred!";
  }
  }
  else{
    echo "Something want wrong!";
  }
  ?>
<script>

   document.getElementById("file").onchange = function () {
    var reader = new FileReader();

    reader.onload = function (e) {
        // get loaded data and render thumbnail.
        document.getElementById("itemimage").src = e.target.result;
    };

    // read the image file as a data URL.
    reader.readAsDataURL(this.files[0]);
}
 
$(document).ready(function(){
 
$("#new_user_registration").on('submit',function(e){

// e.preventDefault();
var itemname =document.getElementById('itemname').value;
$.trim(itemname);
var vendor =document.getElementById('vendor').value;
$.trim(vendor);  
var itemtype =document.getElementById('itemtype').value;
$.trim(itemtype);  
 
var itemcategory =document.getElementById('itemcategory').value;
$.trim(itemcategory);

var expiredate =document.getElementById('expiredate').value;
$.trim(expiredate);  
var itemcost=document.getElementById('itemcost').value;
$.trim(itemcost);  

var currency=document.getElementById('currency').value;
$.trim(itemcost);

var itemdescription=document.getElementById('itemdescription').value;
$.trim(itemdescription);

var floc= document.getElementById('file').value
var vN =/[\(A-Za-z)]/g;
var nV =/[a-z\s]/i;
var nR =/[\sA-Za-z0-9]/i;
var nC =/[0-9]/i;
var j=0,k=0, g=0,i=0;
                      //////IMAGE VALIDATION

if (floc=="") 
    {
   e.preventDefault();
    document.getElementById('mg').innerHTML="Image file is required!..";
    }
    else
    {

    document.getElementById('mg').innerHTML="";
    }
if(vendor=="")
{
e.preventDefault();

document.getElementById('vd').innerHTML="Item vendor  required!..";
 } 
 else
 {

      for(i=0;i<vendor.length;i++)
       {
         if(!vendor[i].match(nR))
          {
             j++;

           }
        }
      
       if(j>0){
       e.preventDefault();

       
        document.getElementById('vd').innerHTML="You typed item vendor with error!.."; 


              }
         else{

        document.getElementById('vd').innerHTML="";

              }
   }
 
    j=0, i=0;

                                                         //ITEM NAME VALIDATION
if(itemname=="")
{
e.preventDefault();

document.getElementById('si').innerHTML="Item name  required!..";
 } 
 else
 {
/*
      for(i=0;i<itemname.length;i++)
       {
         if(!itemname[i].match(nR))
          {
             j++;

           }
        }
      
       if(j>0){
       e.preventDefault();

       
        document.getElementById('si').innerHTML="You typed item name with error!.."; 


              }
         else{*/

        document.getElementById('si').innerHTML="";

             // }
   }
 
    j=0, i=0;


                                                         //ROUTE START VALIDATION
if(itemtype=="")
{
e.preventDefault();

document.getElementById('ln').innerHTML="Item type required!..";
 } 
 else
 {

     /* for(i=0;i<itemtype.length;i++)
       {
         if(!itemtype[i].match(nV))
          {
             j++;

           }
        }
      
       if(j>0){
       e.preventDefault();

       
        document.getElementById('ln').innerHTML="Item type typed with error!.."; 


              }
         else{*/

        document.getElementById('ln').innerHTML="";

             // }
   }
 
    j=0, i=0;
                                                                               ///ROUTE LENGTH VALIDATION
 

if(itemcategory==""){
 e.preventDefault();

document.getElementById('rt').innerHTML="Item category required!..";
 }
 else{

     for(i=0;i<itemcategory.length;i++)
       {
         if(!itemcategory[i].match(nR))
          {
             j++;

           }
        }
      
       if(j>0){
       e.preventDefault();
 
        document.getElementById('rt').innerHTML="Item category typed with error!.."; 
              }
         else{
       

        document.getElementById('rt').innerHTML="";

              }
   }
 
    j=0, i=0;
      
 
                                                                            ///VIA VALIDATION


if(expiredate=="")
{
//e.preventDefault();

document.getElementById('rv').innerHTML="Expire date required!..";
 } 
 else
 {
 
 document.getElementById('rv').innerHTML="";
           
 }
 
    j=0, i=0;


                                                         //ITEM COST DESTINATION VALIDATION
var nC =/[0-9]/i;
if(itemcost=="" || currency==""){
 e.preventDefault();

document.getElementById('rdn').innerHTML="Both price and currency are required!..";
 
 
              }
 else{

     for(i=0;i<itemcost.length;i++)
       {
         if(!itemcost[i].match(nC))
          {
             j++;

           }
        }
      
       if(j>0){
        e.preventDefault();

        document.getElementById('rdn').innerHTML="Item typed with error!.."; 
              }
         else{

        document.getElementById('rdn').innerHTML="";

              }
   }
 
    j=0, i=0;
                                                                               ///DESTINATION DISTANCE FROM LOCATION VALIDATION
 

if(itemdescription==""){
 e.preventDefault();

document.getElementById('rd').innerHTML="Item description required!..";
 }
  
    else{
       

        document.getElementById('rd').innerHTML="";

               
   }
 
    j=0, i=0;
      
 
                                                                            ///ROOM ID VALIDATION
  
  
}
);
}
);  
    
 
</script>





