<?php
session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Purchase.php");
include("Forms_DataGrid1.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


$vform   	 = $_GET['vform']; if ($vform == ""){$vform = $_POST["vform"];}
$sc	 = $_GET['sc']; if ($sc == ""){$sc = $_POST["sc"];}

$actionmode  = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}
$picturename = $_POST["picturename"]; if ($picturename == ""){$picturename = $_GET["picturename"];}

if($vform == 'Passport') $title = "Passport";
else if($vform == 'inssurance') $title = "inssurance";
else if($vform == 'CerpacInfo') $title = "Cerpac Information";
else if($vform == 'TempCard') $title = "Temporary Card Information";
else if($vform == 'VisaInfo') $title = "Visa Information"; 
else if($vform == 'Alien') $title = "Alien"; 
else if($vform == 'CV') $title = "CV"; 
else if($vform == 'Attachements') $title = "Attachements"; 

$dest_dir='photos/'.$sc;
if (!is_dir($dest_dir))
	mkdir($dest_dir, 0700);
														
														

if ($actionmode=='upload' and $sc !=''){

	$fileType1 = $_FILES['userfile1']['type'];$fileName1 = $_FILES['userfile1']['name'];
	$tmpName1  = $_FILES['userfile1']['tmp_name'];$fileSize1 = $_FILES['userfile1']['size'];
	
	$dest_dir='photos/'.$sc;
	$fileType1 = $_FILES['userfile1']['type'];$fileName1 = $_FILES['userfile1']['name'];
	
	$foldername = "./photos/".$sc;
		
	$foldername = 'photos';
	$foldername1 = 'photos'."/".$sc;
	if($vform == 'Passport') $foldername2 = 'photos'."/".$sc."/"."Passport";
	else if($vform == 'inssurance')	$foldername2 = 'photos'."/".$sc."/"."inssurance";					
	else if($vform == 'CerpacInfo') $foldername2 = 'photos'."/".$sc."/"."Cerpac_Information";					
	else if($vform == 'TempCard') $foldername2 = 'photos'."/".$sc."/"."TempCard_Information";					
	else if($vform == 'VisaInfo') $foldername2 = 'photos'."/".$sc."/"."Visa_Information";					
	else if($vform == 'Alien') $foldername2 = 'photos'."/".$sc."/"."Alien";					
	else if($vform == 'CV') $foldername2 = 'photos'."/".$sc."/"."CV";	
	else if($vform == 'Attachements') $foldername2 = 'photos'."/".$sc."/"."Attachements";	
		
	if (!is_dir($foldername))
			mkdir($foldername, 0700);
			
	if (!is_dir($foldername1))
			mkdir($foldername1, 0700);

	if (!is_dir($foldername2))
			mkdir($foldername2, 0700);
			
	$fp1 = fopen($tmpName1, 'r');
	$content1 = fread($fp1, $fileSize1);
	$content1 = addslashes($content1);
	fclose($fp1);
	if(!get_magic_quotes_gpc())
		$fileName1 = addslashes($fileName1);
	
	$dest1 = $foldername2.'/'.$fileName1;
	$r1 = move_uploaded_file ($tmpName1, $dest1);	
	$actionmode='';	
	
								

}

 if ($actionmode=='removeFile' and $sc !='' ){
	$picremoved = $_POST['picremoved'];
	$filepath1 = './photos/'.'/'.$sc.'/';
	if($vform == 'Passport') $filepath1 .= 'Passport/';
	else if($vform == 'inssurance') $filepath1 .= 'inssurance/';
	else if($vform == 'CerpacInfo')  $filepath1 .= 'Cerpac_Information/';
	else if($vform == 'TempCard') $filepath1 .= 'TempCard_Information/';					
	else if($vform == 'VisaInfo')  $filepath1 .= 'Visa_Information/';					 
	else if($vform == 'Alien') $filepath1 .= 'Alien/';					 
	else if($vform == 'CV') $filepath1 .= 'CV/';	
	else if($vform == 'Attachements') $filepath1 .= 'Attachements/';	
	
	$filepath1 .= $picremoved;
	
	unlink ($filepath1);
	$actionmode='';
	$picremoved = '';
	$r1 = '2';
}

print <<<HERE

<head><title>Upload Image</title>
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" language="javascript">

function show_picture(vimagename){

document.upload.actionmode.value='showpicture';
document.upload.picturename.value=vimagename;
document.upload.submit();
}

function uploadimage(){
document.upload.actionmode.value='upload';
document.upload.submit();
}
function removefile(vimagename){
	document.upload.actionmode.value = 'removeFile';
	document.upload.picremoved.value = vimagename;
	document.upload.submit();
}


</script>
</head>
<body>
<form name='upload' method='post' enctype='multipart/form-data'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='picturename' value='$picturename'>
<input type='hidden' name='vform' value='$vform'>
<input type='hidden' name='sc' value='$sc'>
<input type='hidden' name='picremoved' value='$picremoved'>
HERE;

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' style='table-layout:fixed' width='950px;'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; ' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Upload $title Images</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

	  
echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";  	  
		 
echo "<table border=0 width=450px><col width=250><col width=250><tr><td valign=top>";
echo "<table border=0 width=400px>";


$foldername = "./photos/".$sc;
if($vform == 'Passport') $foldername = $foldername."/Passport";
else if($vform == 'inssurance') $foldername = $foldername."/inssurance";
else if($vform == 'CerpacInfo') $foldername = $foldername."/Cerpac_Information";
else if($vform == 'TempCard')  $foldername = $foldername."/TempCard_Information";		 
else if($vform == 'VisaInfo')  $foldername = $foldername."/Visa_Information";		 
else if($vform == 'Alien') 	$foldername = $foldername."/Alien";		 
else if($vform == 'CV') 	$foldername = $foldername."/CV";	
else if($vform == 'Attachements') 	$foldername = $foldername."/Attachements";	

$arr=scandir($foldername);
 $i=0;   
 
 
foreach ($arr as $filename) {

 if ($i>1)
	 echo"<tr>		
		<td align='left'><span class='style1'><input type='button' value='$filename' style='width:250px' onclick=\"show_picture('$filename')\">
		<img src='img/delete.png' width='20' height='20' onmouseover=\"style.cursor='pointer'\" style='cursor: pointer;' title='Remove File' onclick=\"removefile('$filename');\" >
		<a href='$foldername/$filename' download><img src='icon/open-folder.png' width='20' height='20' onmouseover=\"style.cursor='pointer'\" style='cursor: pointer;' title='Download File' >
		</a>
		</span></td>
		</tr>";




$i+=1;
 }


echo"<tr><td><hr></td></tr>";
echo"<td><input type='file'  name='userfile1'    id='userfile1' style='width:200px;' accept='image/jpeg,image/gif'>		</td>";
	echo"<tr>		
<td colspan=1 align='center'><span class='style1'><input type='button' value='Submit' onclick='uploadimage()'></span>";


		if($r1==1)
			echo "<font color=red>Image Uploaded Successfully..</font>";
		else if($r1==2){ 
			echo "<font color=red> Image Was Successfully Deleted!</font>";			
			
		} $r1=0;
echo"</td>
</tr>";

 echo"</table>";
echo"<td>";


$foldername = "./photos/".$sc;
if($vform == 'Passport') $foldername = $foldername."/Passport";
else if($vform == 'inssurance')	$foldername = $foldername."/inssurance";
else if($vform == 'CerpacInfo')	$foldername = $foldername."/Cerpac_Information";
else if($vform == 'TempCard')  $foldername = $foldername."/TempCard_Information";	
else if($vform == 'VisaInfo')  $foldername = $foldername."/Visa_Information";	
else if($vform == 'Alien') $foldername = $foldername."/Alien";	
else if($vform == 'CV') $foldername = $foldername."/CV";	
else if($vform == 'Attachements') $foldername = $foldername."/Attachements";	
	
$filepath = $foldername.'/'.$picturename;
 //echo"<img src='$filepath' width='450px' height='500px'></td></tr>"; 
if ($picturename != '') 
echo "<iframe src='$filepath' width='450px' height='500px'></iframe></td></tr>";
 else echo "<div style='border:1px solid darkgray;width:450px;height:500px;'></div></td></tr>";
echo"</td></tr>";






echo "</table>";
 echo "</td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";			 
	echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:560px;' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;' valign='top'></td>";
echo" </tr>";
print <<<HERE
</form>
</body>

HERE;


function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n];
return $exts; 
}
?>