<?
ob_start();
session_start();
 include("../include/db.inc.php"); 
 include("thumb_function.php"); 
   $db1=new db;
   $db1->open();
    mysql_query("SET NAMES 'utf8'"); 		
    if($_SESSION['uname']=='' || $_SESSION['pwd']=='')
	{
		header("location:login.php");
	}
		
        $pid=trim($_GET['id']);	
		if(isset($_FILES['thumbimg1']) || isset($_FILES['thumbimg2']) || isset($_FILES['thumbimg3']))
		{	$featured=$_POST[featured];
    	 	$not_uploaded="";
			if(is_uploaded_file($_FILES['thumbimg1']['tmp_name'])){
				if(!$_FILES['thumbimg1']['size']){
					$not_uploaded.=$_FILES['thumbimg1']['name'].", ".$_FILES['img1']['name'].", ";
				}else{
					if($pid)
					{
						$maindir = "../images/product/";
						$thumbnail = $_FILES['thumbimg1']['name'];
						if($thumbnail<>"") 
						{
								$randstr= rand(11111,99999);
								$uploaddir = $maindir.$pid;	
								$uploadfile = $uploaddir."/".$thumbnail;
							
								if(copy($_FILES['thumbimg1']['tmp_name'], $uploadfile))
								{
									$ext = substr($thumbnail,-3,3);
									$upload_file1 = $uploaddir."/".$pid."_".$randstr."_tmb1.".$ext;
									$upload_file2 = $uploaddir."/".$pid."_".$randstr."_tmb5.".$ext;
									
									
									$file_tbl_name = $pid."_".$randstr."_tmb.".$ext;
									
									$feature=0;
									mysql_query("update t123go_product set image1 ='".$file_tbl_name."' where pid='$pid'");	
									
									if($ext=='jpg'){	
										jpgimage_new($uploadfile,$upload_file1,$upload_file2);
									}else if($ext=='gif'){
										gifimage_new($uploadfile,$upload_file1,$upload_file2);
									}else if($ext=='png'){
										pngimage_new($uploadfile,$upload_file1,$upload_file2);
									}	
								}
						}
					}	

				}
			}
			if(is_uploaded_file($_FILES['thumbimg2']['tmp_name'])){
				if(!$_FILES['thumbimg2']['size']){
					$not_uploaded.=$_FILES['thumbimg2']['name'].", ".$_FILES['img2']['name'].", ";
				}else{					
					if($pid)
					{
						$maindir = "../images/product/";
						$thumbnail = $_FILES['thumbimg2']['name'];
						if($thumbnail<>"") 
						{
								$randstr= rand(11111,99999);
								$uploaddir = $maindir.$pid;	
								$uploadfile = $uploaddir."/".$thumbnail;
							
								if(copy($_FILES['thumbimg2']['tmp_name'], $uploadfile))
								{
									$ext = substr($thumbnail,-3,3);
									$upload_file1 = $uploaddir."/".$pid."_".$randstr."_tmb1.".$ext;
									$upload_file2 = $uploaddir."/".$pid."_".$randstr."_tmb5.".$ext;
									
									
									$file_tbl_name = $pid."_".$randstr."_tmb.".$ext;
									
									$feature=0;
									mysql_query("update t123go_product set image2 ='".$file_tbl_name."' where pid='$pid'");	
									
									if($ext=='jpg'){	
										jpgimage_new($uploadfile,$upload_file1,$upload_file2);
									}else if($ext=='gif'){
										gifimage_new($uploadfile,$upload_file1,$upload_file2);
									}else if($ext=='png'){
										pngimage_new($uploadfile,$upload_file1,$upload_file2);
									}	
								}
						}
					}					
				}
			}
			if(is_uploaded_file($_FILES['thumbimg3']['tmp_name'])){
				if(!$_FILES['thumbimg3']['size']){
					$not_uploaded.=$_FILES['thumbimg3']['name'].", ".$_FILES['img3']['name'].", ";
				}else{					
					if($pid)
					{
						$maindir = "../images/product/";
						$thumbnail = $_FILES['thumbimg3']['name'];
						if($thumbnail<>"") 
						{
								$randstr= rand(11111,99999);
								$uploaddir = $maindir.$pid;	
								$uploadfile = $uploaddir."/".$thumbnail;
							
								if(copy($_FILES['thumbimg3']['tmp_name'], $uploadfile))
								{
									$ext = substr($thumbnail,-3,3);
									$upload_file1 = $uploaddir."/".$pid."_".$randstr."_tmb1.".$ext;
									$upload_file2 = $uploaddir."/".$pid."_".$randstr."_tmb5.".$ext;
									
									
									$file_tbl_name = $pid."_".$randstr."_tmb.".$ext;
									
									$feature=0;
									mysql_query("update t123go_product set image3 ='".$file_tbl_name."' where pid='$pid'");	
									
									if($ext=='jpg'){	
										jpgimage_new($uploadfile,$upload_file1,$upload_file2);
									}else if($ext=='gif'){
										gifimage_new($uploadfile,$upload_file1,$upload_file2);
									}else if($ext=='png'){
										pngimage_new($uploadfile,$upload_file1,$upload_file2);
									}	
								}
						}
					}
		       }
			}
			
			$not_uploaded=substr($not_uploaded,0,strlen($type)-2);
			if($not_uploaded){			
					$action=base64_encode("Error to upload these images ( ".$not_uploaded." )");					
					header("Location: add_pimage.php?id=".$pid."&action=".$action);
					exit;
			}else{
				$action=base64_encode("All images have been uploaded successfully");
				header("Location: product_list.php?id=".$pid."&action=".$action);						
				exit; 
			}
									
	}
?>
<html>
<head>

<title>NEWADMIN</title>
<link href="../css/stylesheet.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../include/common.js"></script>
<script language="JavaScript">
		function valfrm(){

		if(document.imgfrm.thumbimg1.value=="" && document.imgfrm.img1.value=="" && document.imgfrm.thumbimg2.value=="" && document.imgfrm.img2.value=="" && document.imgfrm.thumbimg3.value=="" && document.imgfrm.img3.value==""){
			alert("You must enter atleast one thumbnail and one big image  ");
			document.imgfrm.thumbimg1.focus();
			return false;	
		}		
		if((document.imgfrm.thumbimg1.value!="" && document.imgfrm.img1.value=="") || (document.imgfrm.thumbimg1.value=="" && document.imgfrm.img1.value!="")){
			alert("You must enter both thumbnail and big image for first row");
			document.imgfrm.thumbimg1.focus();
			return false;				
		}		
		if((document.imgfrm.thumbimg2.value!="" && document.imgfrm.img2.value=="") || (document.imgfrm.thumbimg2.value=="" && document.imgfrm.img2.value!="")){
			alert("You must enter both thumbnail and big image for second row");
			return false;				
		}		
		if((document.imgfrm.thumbimg3.value!="" && document.imgfrm.img3.value=="") || (document.imgfrm.thumbimg3.value=="" && document.imgfrm.img3.value!="")){		
			alert("You must enter both thumbnail and big image for third row");
			return false;				
		}			

			if(document.imgfrm.thumbimg1.value!="" && valimage(document.imgfrm.thumbimg1.value)==false){
				alert('please browse gif or jpg type only');
				document.imgfrm.thumbimg1.focus();
				document.imgfrm.thumbimg1.select();
				return false;
			}
			if(document.imgfrm.img1.value!="" && valimage(document.imgfrm.img1.value)==false){
				alert('please browse gif or jpg type only');
				document.imgfrm.img1.focus();
				document.imgfrm.img1.select();
				return false;
			}						
			if(document.imgfrm.thumbimg2.value!="" && valimage(document.imgfrm.thumbimg2.value)==false){
				alert('please browse gif or jpg type only');
				document.imgfrm.thumbimg2.focus();
				document.imgfrm.thumbimg2.select();
				return false;
			}
			if(document.imgfrm.img2.value!="" && valimage(document.imgfrm.img2.value)==false){
				alert('please browse gif or jpg type only');
				document.imgfrm.img2.focus();
				document.imgfrm.img2.select();
				return false;
			}						
			if(document.imgfrm.thumbimg3.value!="" && valimage(document.imgfrm.thumbimg3.value)==false){
				alert('please browse gif or jpg type only');
				document.imgfrm.thumbimg3.focus();
				document.imgfrm.thumbimg3.select();
				return false;
			}
			if(document.imgfrm.img3.value!="" && valimage(document.imgfrm.img3.value)==false){
				alert('please browse gif or jpg type only');
				document.imgfrm.img3.focus();
				document.imgfrm.img3.select();
				return false;
			}						

		}
		
		function valimage(val){						
			var imgpth=val;
			var len=imgpth.length;
			var ext=imgpth.substr(len-3,3);
			  ext = ext.toLowerCase() 
			if(len>0 && ext!='jpg' && ext!='gif'){
				return false;				
			}else{
				return true;
			}		
		}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#ffffff">
<div class="admin_wrapper">
<div class="logo_show"><? include("minorlogout.php");?></div>
<div style="float:left; width:200px">
<div class="topg_links_show"><? include("left.php");	?></div>
<div class="top_links_show"><? include("belowleft1.php");	?></div></div>
<div style="float:left; width:800px">
<div class="maximum">

<!-- ImageReady Slices (NEWADMIN.psd) -->
<table id="Table_01" width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
	
    <tr>
		
	    <td valign="top" align="center">
<? if($_GET['action']){ echo '<strong>'.base64_decode($_GET['action']).'</strong>'; }?><br>
	  <form  action="" method="post" enctype="multipart/form-data" name="imgfrm">
	   <input type="hidden" name="featured" value="<?=$_GET['featured']?>">
       <table border="0" cellpadding="0" width="629">
      <tr> 
        <td valign="top" border="0"> <table width="100%" cellspacing="1" cellpadding="2" class="tbl-generic">
                              <tr bordercolor="#FFFFFF"> 
                                <td colspan="3"><font face="Arial, Helvetica, sans-serif" color="#FF0000" size="2"><br>
                                  <br>
                                  <br>
                                  </font> </td>
                              </tr>
                              <tr bordercolor="#FFFFFF"> 
                                <td>&nbsp;</td>
                                <td><strong>Thumbnail</strong></td>
                               
                              </tr>
                              <tr bordercolor="#FFFFFF"> 
                                <td width="2%"><font face="Arial, Helvetica, sans-serif" size="2">1</font></td>
                                <td width="45%"><input name="thumbimg1" type="file" id='thumbimgid1' size="35"></td>
                               
                              </tr>
                              <tr bordercolor="#FFFFFF"> 
                                <td width="2%"><font face="Arial, Helvetica, sans-serif" size="2"> 
                                  2:</font></td>
                                <td width="45%"><input name="thumbimg2" type="file"  id='thumbimgid2' size="35" ></td>
                                
                              </tr>
                              <tr  bordercolor="#FFFFFF"> 
                                <td><font face="Arial, Helvetica, sans-serif" size="2">3:</font></td>
                                <td><input name="thumbimg3" type="file"  id='thumbimgid3' size="35" ></td>
                                
                             
                            </table></td>
      </tr>
    </table><br>
<input type="submit" name="sub_btn" value="Upload Images" >&nbsp;&nbsp; 
<input name="button" type="button"  onClick="window.location.href='<? if($_GET['featured']){ echo "featured_product.php";}else{ echo "product_list.php";}?>'"
 value="Back To List">
</form>
        </td>
    </tr>
    
	</table>	
</div></div>
<div class="bottom_links_show"><? include("footer.php");	?></div>

	


<!-- End ImageReady Slices -->
</body>
</html>