<?php
    session_start();          
    if(empty($_SESSION['p0B_c0ff33'])) header('location:login.php?goto=index');
?>

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

include "../db_function.php";
$table="tblslide";

//$smallestimagelocation = "../Site/images/banner/smallest/";
//$imagelocation = "../Site/images/banner/small/";
$bigimagelocation = "../Site/images/slide/";

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 > 530 ){
		$newwidth = 530;
	}else{
		$newwidth = $width;
	}
	$newheight=($height/$width)*$newwidth;
	$tmp=imagecreatetruecolor($newwidth,$newheight);

	//$newwidth1=137;
	//$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)){
		$id=$row9['id'];
		}
		$id=$id+1;
        $des="";
		$query="INSERT INTO $table values ($id, '$image','$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>

<script language="javascript" type="text/javascript">
// Roshan's Ajax dropdown code with php
// This notice must stay intact for legal use
// Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}

		return xmlhttp;
    }

	function getType(id) {		
		
		var strURL="findcategoryNF.php?type="+id;
		var req = getXMLHTTP();

		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('type').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}	
	
</script>
 
<!-- start content-outer -->
<div id="content-outer">
<!-- start content -->
<div id="content">

<div id="page-heading">
  <h1>Add New Banner Slide</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 Image: <b>'.$image.'</b> success!
                <a href="edit.php?table='.$table.'&row=detail&fildID=id&id='.$id.'" 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="title" /></td>
			<td></td>
		</tr>-->

		<tr>
            <th valign="top"><font class="note">* </font>Image :</th>
            <td>
            	<input name="image" class="imagefial" size="30" type="file" /> <div class="bubble-left"></div><div class="bubble-inner">JPG, PNG, GIF Size (530 x 344)px</div><div class="bubble-right"></div>
            </td>
			<td></td>
		</tr>
		<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>

		<tr height="20">
			<th valign="top"></th>
			<td></td>
			<td></td>
		</tr>

		</table>
	<!-- end id-form  -->
	</form>
	</td>
	<td>

	<!--  start related-activities -->
	<div id="related-activities">

		<!--  start related-act-top -->
		<div id="related-act-top">
		<img src="images/forms/header_related_act.gif" width="271" height="43" alt="" />
		</div>
		<!-- end related-act-top -->

		<!--  start related-act-bottom -->
		<div id="related-act-bottom">

			<!--  start related-act-inner -->
			<div id="related-act-inner">

				<div class="left"><a href=""><img src="images/forms/icon_plus.gif" width="21" height="21" alt="" /></a></div>
				<div class="right">
					<h5><font color="red">Note</font></h5>
					 <font size="2">You must fill inthe fields marked with a (<font color="red"> * </font>)</font>
				</div>
				
				<div class="clear"></div>
				<div class="lines-dotted-short"></div>

				<div class="left"><a href=""><img src="images/forms/icon_minus.gif" width="21" height="21" alt="" /></a></div>
				<div class="right">
					<font size="2"><h5>More problems</h5>
					If you have any problems please contact us :<br /><br />
					<font color="#4e4e4e"><b>- Designer : +855 81 6000 61<br />- Hotline &nbsp;&nbsp;&nbsp;: +855 23 994 119<br />
                    	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+855 23 5000 470<br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+855 23 63 64 678
                    </b></font>
					</font>
				</div>
				
				<div class="clear"></div>
				
			
				
			</div>
			<!-- end related-act-inner -->
			<div class="clear"></div>
		
		</div>
		<!-- end related-act-bottom -->
	
	</div>
	<!-- end related-activities -->

</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>