<?php
require_once("../head2Alts.php");


if(isset($_POST['itemid']) && isset($_POST['shopid']) && isset($_POST['owniD']))
  {
 $itemid=$_POST['itemid'];
 $shopid=$_POST['shopid'];
 $owniD=$_POST['owniD'];
 $post=$_POST['post'];
 //$result=0; 
 $rQ="";     //require_once("buda_connect.php");

       //$con=new connect_class();

$item=$myAgrocon->getThisItem($itemid);
if($item->rowCount()<=0){ 
          

          echo "Page time out";
          }
       else
          {
  

///////////////////////IF THE UPLOAD BUTTON IS CLICK///////////////////
            ///////////////////////////////////

  /////////////
if ($post=="UPLOAD") 
{
  $result=0;
   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','jpg','jpeg','gif','jpeg','JPG','JPEG','GIF','PNG');

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

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


 $itemname=$_POST['title']; 

if($myAgrocon->vImageType($ext,$allowedtype) && $myAgrocon->vimagename($itemname) && $myAgrocon->vImageError($error) && $myAgrocon->vImageSize($size) && $myAgrocon->vImageExist($fn) && $myAgrocon->vImageSelectF($fn) )
{
$result=0;
           $rQ= $myAgrocon->uploadImage($fn,$itemname, $itemid,$tmp_name,$shopid);
           if ($rQ->rowCount()>0) 
           {
             $result=$rQ->rowCount();
           }
           else{
            $result=$rQ ;
           }

}
else{
     //////////////
        ?>
         <center>
         <div>
         <sup>
             <?php 

              echo  $myAgrocon->eimagename($itemname)."<p/>";
              echo $myAgrocon->eImageType($ext,$allowedtype)."<p/>";
              echo $myAgrocon->eImageError($error)."<p/>";
              echo $myAgrocon->eImageSize($size)."<p/>";
              echo $myAgrocon->eImageExist($fn)."<p/>"; 
              echo $myAgrocon->eImageSelectF($fn)."<p/>";
             ?>
       </sup>
       </div>
       </center> 

        <?php
}


}


 if ($post=="UPLOAD" && $result>0) {
   require('editItemSucessfull.php');
 }
 else{
 if ($post=="UPLOAD") {
   //echo $rQ;
   print_r($result);
 }
 $itm=$item->fetch();
          ?>
 

<center><table  class="himgf" >
        
          <tr>
               <td colspan="2">

               <label id="reg_form_tile_label"> Upload more images for <?php echo $itm['itemname'];?> here:- </label>

               </td>

               <td rowspan="4" width="30%">
               	 <div id="deta" ><img id="image">  </div> 
               </td>
          </tr>

         <form  name="upload_form" id="upload_form" action="addMoreItemImages.php"   method="post"   autocomplete="on" enctype="multipart/form-data">
  
         <input class="hi" type="text" name="owniD"  value="<?php echo $ownID;?>">


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

        <input class="hi" type="text" name="itemid"  value="<?php echo $itemid;?>"> 

          <tr>

              <td valign="top" id="hf"> 

                   <label id="mini_label">Image name(Title):<sup>*</sup></label>

              </td>

              <td valign="top">
              
              <input type="text" name="title" id="title"  class="prof_info"/>
              <sup id="nf"></sup>
              </td>
          </tr>
 

          <tr>
              <td valign="top" id="hf"> 

              <label id="mini_label">Choose image:<sup>*</sup></label>

              </td>

              <td valign="top">

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

           <tr> 

               <td colspan="2">

                 <center>

                 <input type = "submit" value="UPLOAD" name="post" id="submit"  />

                 </center>
               </td>
            </tr>

           </form> 


 
  </table>
<!--<table><tr><td><div id="detail" > </div></td></tr> </table>-->
</center>
 <?php
}
 }
 
}
else{
	echo "Oops page not found!";
}

?>

<script>

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

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

    // read the image file as a data URL.
    reader.readAsDataURL(this.files[0]);
}
 
$(document).ready(function() {

	
  $(" #upload_form").on('submit',(function(e){
     // e.preventDefault();
	var floc= document.getElementById('file').value
	var tit = document.getElementById('title').value; 
	var nV =/[A-Za-z0-9\s]/i;
    var j=0,k=0, g=0,i=0;
if (floc=="") 
    {
	 e.preventDefault();
    document.getElementById('mg').innerHTML="Image file is required!..";
    }
    else
    {

    document.getElementById('mg').innerHTML="";
    }
                                                         //TITLE VALIDATION
if(tit=="")
{
 e.preventDefault();

document.getElementById('nf').innerHTML="Title is required!..";
 } 
 else
 {

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

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

       
        document.getElementById('nf').innerHTML="Typed title with error!.."; 


              }
         else{

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

              }
   }
 
    j=0, i=0;
                                                                               ///MIDDLE NAME VALIDATION
 

	/*
	if($.trim(hid) !=""  && $.trim(tit) !="" && $.trim(floc) !=""){
	 var formData = new FormData($("#upload_form")[0]);
	  
      formData.append("hotelid",hid);
	  formData.append("room",roo);
	  formData.append("title",tit);
		$.ajax({
			url:"image_upload_form_pro.php",
			
			type: "POST",
			
			data: formData,
			//async:false,
			contentType: false,
			cache: false,
			processData: false,
			
			success:function(data){
				$("#detail").html(data).show();
			}
			
		});
 
	}else{alert("You are trying to send incomplete information!..") ;}*/
	})); 
	 
}); 
</script>
<?php
 require_once('../footerIns.php');
?>