<?php
	session_start();    
	if(empty($_SESSION['p0B_c0ff33'])) header('location:login.php?goto=index');
	include "../db_function.php";
    include('templates/header.php'); 
	include('templates/menu.php'); 	

$table="tbl_beauty";

$imagelocation = "../Site/images/beauty/";
$bigimagelocation = "../Site/images/beauty/b/";

if(isset($_POST['submit'])){

//////Resize Picture
error_reporting(0);
$change="";
$abc="";
define ("MAX_SIZE","1024");
function getExtension($str) {
	 $i = strrpos($str,".");
	 if (!$i) { return ""; }
	 $l = strlen($str) - $i;
	 $ext = substr($str,$i+1,$l);
 return $ext;
 }
 $errors=0;
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image =$_FILES["image"]["name"];
	$uploadedfile = $_FILES['image']['tmp_name'];
	 ///Rename Image
	 if(!empty($image)){
				$i=1;
				$str="SELECT * FROM $table WHERE image ='".$image."' ";
				$sql=mysql_query($str);
				while ($row=mysql_fetch_array($sql)){
				$str="SELECT * FROM $table WHERE image ='".$image."' ";
				$sql=mysql_query($str);
				$num_rows = mysql_num_rows($sql);
					 if($num_rows > 0) {
						$nimage=$_FILES["image"]["name"];
						list ($iname,$ext)=preg_split('/[\.,]+/',$nimage);
						$image=$iname."_".$i.".".$ext;
					 }

					$i++;
				}
			///End Rename/////////////////////////////////////////////////////////
	}
 	if ($image)
 	{
 		$filename = stripslashes($image);
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);

 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
 		{
 			$change='<div class="msgdiv">Unknown Image extension</div> ';
 			$errors=1;
 		}
 		else
 		{

$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1440)
{
	$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);}
else if($extension=="png")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
	if($width > 600 ){
		$newwidth = 600;
	}else{
		$newwidth = $width;
	}
	$newheight=($height/$width)*$newwidth;
	$tmp=imagecreatetruecolor($newwidth,$newheight);

	$newwidth1=308;
	$newheight1=($height/$width)*$newwidth1;
	$tmp1=imagecreatetruecolor($newwidth1,$newheight1);


	if($extension=="png" || $extension=="gif")
	{
		imagealphablending($tmp, false);
		imagesavealpha($tmp,true);
		$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
		imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);

		imagealphablending($tmp1, false);
		imagesavealpha($tmp1,true);
		$transparent = imagecolorallocatealpha($tmp1, 255, 255, 255, 127);
		imagefilledrectangle($tmp1, 0, 0, $newwidth, $newheight, $transparent);
		
	}

	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
	
	$filename1 = $imagelocation.$image;
	$filename = $bigimagelocation.$image;

	if($extension=="png" || $extension=="gif")
	{
		imagepng($tmp,$filename);
		imagepng($tmp1,$filename1);
	}
	else{
		imagejpeg($tmp,$filename,90);
		imagejpeg($tmp1,$filename1,90);
	}

	imagedestroy($src);
	imagedestroy($tmp);
	imagedestroy($tmp1);
}}

}


if(!$errors) {
	$str9="SELECT id FROM $table ORDER BY id DESC LIMIT 1";
			$sql9=mysql_query($str9);
			while($row9=mysql_fetch_array($sql9)){
			$gid=$row9['id'];
			}
			$gid=$gid+1;
			$name=$_POST['name'];
			$die="Please Add Information";
			$des="";

			$query="INSERT INTO $table VALUES ('$gid','$image','".mysql_real_escape_string($name)."','$des')";
			$suc = mysql_query($query);
			if($suc){
				$masg=1;													
			}
			else{
				$masg=0;
			}	
	}
}	
?>

<script>	
	$(document).ready(function() {
		// SUCCESS AJAX CALL, replace "success: false," by:     success : function() { callSuccessFunction() }, 
		
		$("#formID").validationEngine()						// CLOSE ALL OPEN PROMPTS
	});
</script>

 
<!-- start content-outer -->
<div id="content-outer">
<!-- start content -->
<div id="content">


<div id="page-heading">
  <h1>Add Beauty & Women</h1></div>


<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
<tr>
	<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
	<th class="topleft"></th>
	<td id="tbl-border-top">&nbsp;</td>
	<th class="topright"></th>
	<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
</tr>
<tr>
	<td id="tbl-border-left"></td>
	<td>
		<div id="content-table-inner">
	<!--  start content-table-inner -->
	<?php
	if(isset($masg) && $masg==1 ){
	echo'
	<div id="message-green">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td class="green-left">
				You added: <b>'.$name.'<span style="display:none>'.$gid.'</span>"</b> successful!.
				<a href="edit.php?table='.$table.'&row=detail&fildID=id&id='.$gid.'" rel="facebox" >
					Please add description !
				</a>		
			</td>
			<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
		</tr>
		</table>
	</div>';
	}
	else if(isset($masg) && $masg==0 ){
	echo'
	<div id="message-red">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
					<td class="red-left">Fail. Please try again.</td>
				<td class="red-right"><a class="close-red"><img src="images/table/icon_close_red.gif"   alt="" /></a></td>
			</tr>
		</table>
	</div>';
	}else if(isset($masg) && $masg==2){
	echo'
	<div id="message-yellow">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
					<td class="yellow-left">'.$change.'</td>
				<td class="yellow-right"><a class="close-yellow"><img src="images/table/icon_close_yellow.gif"   alt="" /></a></td>
			</tr>
		</table>
	</div>';
	}
	?>

	<table border="0" width="100%" cellpadding="0" cellspacing="0">
	<tr valign="top">
	<td>
		<!-- start id-form -->
	 	<form name="form" id="formID" method="POST" action="" enctype="multipart/form-data">	
            <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
            
                <tr>
                    <th valign="top"><font class="note">* </font> Title :</th>
                    <td><input type="text" class="validate[required] inp-form" id="name" name="name" style="width: 600px;" /></td>
                    <td></td>
                </tr>
                
                <tr>
                    <th valign="top"><font class="note">* </font> Image :</th>
                    <td>
                        <input name="image" id="image" class="imagefial" size="30" type="file" /> <div class="bubble-left"></div><div class="bubble-inner">JPG, PNG, GIF Size (600 x 340)px</div><div class="bubble-right"></div>
                    </td>
                    <td></td>
                </tr>                 
                            
                <th>&nbsp;</th>
                <td valign="top"><br /><br />
                    <input type="submit" value="" name="submit" class="form-submit" />
                    <input type="reset" value="" class="form-reset"  />
                </td>
                	<td></td>
                </tr>
            </table>
        <!-- end id-form  -->
        </form>		
	</td>
	<td>

	<?php include('templates/related.html'); ?>
</td>
</tr>
<tr>
<td><img src="images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
<td></td>
</tr>
</table>
 
<div class="clear"></div>
 

</div>
<!--  end content-table-inner  -->
</td>
<td id="tbl-border-right"></td>
</tr>
<tr>
	<th class="sized bottomleft"></th>
	<td id="tbl-border-bottom">&nbsp;</td>
	<th class="sized bottomright"></th>
</tr>
</table>

<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer -->

 

<div class="clear">&nbsp;</div>
    
 <?php include('templates/footer.php'); ?>
  <?php 
if(isset($masg) && $masg==1 ){
	$linkUpload='&table=tbl_imagegallery&fid=id&ftype=type&falbumname=aid&fitem=item&folder_th=gallery_th&folder=gallery&folder_sth=gallery_sth';
 echo'
	<div id="mydiv" style="display:none;">
		<iframe src="addmoreimage/index.php?album='.$album.'&aid='.$gid.$linkUpload.'" width="800" height="440" style="border:0;" /></iframe>
	</div>
';
}
?>