<?php
include("../session.php");
include("../connect.php");
date_default_timezone_set ("Singapore"); 
$date=date("d-m-Y", time());
$date2=date("Y-m-d", time());
$time2=date("H:i:sa", time());
$sorttime=$date2." ".$time2;
function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
}

////////////////////////////////////////////////////////////////////////
$user=$_POST["companyId"];
$productid=$_POST["productId"];
$productName=$_POST["productName"];
$producttag=$_POST["searchtag"];
$producttype2=$_POST["type2"];
$producttype=$_POST["type"];


$productprice=$_POST["price"];
$productsign=$_POST["dollorsign"];
$productstore=$_POST["store"];
$productdetail=$_POST["detail"];
$producturl=$_POST["urllink"];
//$filename = basename ($_FILES["file"]["name"]);	
$status = "available";

/////////////////////////////////////////////////////////////////////////////////
$filename = $_FILES['file']['name'];
if($filename){
$uploadedfile = $_FILES['file']['tmp_name'];

$ext = findexts ($_FILES["file"]["name"]) ; 
if($ext=="jpg" || $ext=="jpeg" || $ext=="JPG" || $ext=="JPEG")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($ext=="png" || $ext=="PNG")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else if($ext=="gif" || $ext=="GIF")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);


$newwidth=150;
$newheight=150;
//$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);



$ran2 = $productid."."; 
$target_path = "album/"; 
$target_path = $target_path . $ran2.$ext;
$filename2 = $ran2.$ext;

imagejpeg($tmp,"album/". $filename,100);

imagedestroy($src);
imagedestroy($tmp);

rename ("album/$filename", "album/$filename2");
}
else{
$ext = findexts ($_FILES["file"]["name"]) ;
$ran2 = $productid."."; 
$target_path = "album/"; 
$target_path = $target_path . $ran2.$ext;
$filename2 = $ran2.$ext;

}
//move_uploaded_file($_FILES['file']['tmp_name'], $target_path) ;

mysql_query("INSERT INTO product (id,pName,type,type2,companyID,price,detail,productPic,status,dateInsert,searchtag,dollar,producturl,sorttime,limitstore)VALUES('$productid','$productName','$producttype','$producttype2','$user','$productprice','$productdetail','$filename2','$status','$date','$producttag','$productsign','$producturl','$sorttime','$productstore')");

mysql_query("INSERT INTO promotion (pID,pname,normalprice,dollar,status)VALUES('$productid','$productName','$productprice','$productsign','off')");

	
echo "<script language=javascript>alert('Insert Complete');window.location='insertproduct.php';</script>";
	
include("../closeconnection.php");	

?>