<?php
  include "../includes/session.php";
  include "../includes/config.php";
  include "../includes/function.php";
  $tmpsiteid=$_SESSION['session_currenttempsiteid'];
  $templateid=$_SESSION['session_currenttemplateid'];
  $userid=$_SESSION["session_userid"];
  $var_type=($_GET["type"] != "")?$_GET["type"]:$_POST["type"];
  $basicLocation=fetchBasicLocation(1);
  if($var_type == "new") {
	  $rootserver=$basicLocation . "/workarea/tempsites";
	  $workLocation = "../workarea/tempsites/$tmpsiteid/images/";	
  }
  else {
	  $rootserver=$basicLocation . "/workarea/sites";
	  $workLocation="../workarea/sites/$tmpsiteid/images/";
  }
 // watermarkimage("./gallery/images/".$assignedname);
 if($_POST['selectedimage'] !=""){
 $size_per_user=0; 
 $allowed_space=0;		
	if(validateSizePerUser($userid,$size_per_user,$allowed_space,"../")) {
		 $filename=$_POST['selectedimage'];
		 $workLocation .= $filename;
		 copy("../systemgallery/".$filename,$workLocation);
		 chmod($workLocation,0777);
		 if($var_type == "new") {
			 watermarkimage($workLocation);
		 }
	    echo "<script>returnValue = new Object(); returnValue.url=\"$rootserver/$tmpsiteid/images/".$filename."\"; window.close();</script>";
	}
	else {
		echo "<script> returnValue = new Object(); returnValue.url=''; alert('Your space exceeded permitted limit." . 
				"(Space taken by you: " . human_read($size_per_user) . " " . 
				"Allowed space: " . human_read($allowed_space) . ") \\r\\n" . 
				"Delete unused images, or any/all of the sites created by you to proceed further.'); window.close(); </script>";
	}		
  }		 
?>
<html>
<head>
	<title>SiteBuilder::Upload Image</title>
	<link href="../style/site.css" rel="stylesheet" type="text/css">
	<SCRIPT>
	window.name="targ";
	
	 function closeGallery(){
	 		returnValue = new Object();
			returnValue.url = ""; 
	        window.close();
	  }
	  function savetblpro(){
			document.frmImageUpload.submit();
	        //opener.settablervalue(tborder,tborderclr,bgclr,cells,cellp,talign,twidth);
            //window.close();
	  }
	
	  function selectthisimags(id){
	  	 document.getElementById("selectedimage").value=id;
	     document.frmImageUpload.submit();
	  }	
	</SCRIPT>
</head>
<body>
<table width="100%" cellpadding=2 align=center>
   <tr>
      <td>
	                  <FIELDSET>
					      <legend class=maintextbold>System Gallery - Click on the image you want to select...</legend>
						    <table>
							<tr>
								<td align="center">
									<a href="#" onClick="javascript:closeGallery();" class="maintext">Cancel without selecting image</a>  
								</td>
							</tr>
							 <form  method="post"   name="frmImageUpload" target="targ">
							  <input type="hidden" name="type" id="type" value="<?php echo($var_type); ?>">
						     <?php
							   $dirpath="../systemgallery/";
							   $handle = opendir($dirpath);
							   while (false !== ($file = readdir($handle))) {
							   //echo $file;
							    if ($file != "." && $file != ".." && $file !="Thumbs.db" ) {
							 ?>
							     <tr>
								   <td>
								     <img id="<?php echo $file; ?>" src="../systemgallery/<?php echo $file; ?>" width=100 height=100 onclick="selectthisimags(this.id)">
								   </td>
								 </tr>
							 <?php	  
								}
							   }	
							 ?>
						    
							      
							      
								 <input type=hidden name=selectedimage id=selectedimage value="<?php echo($url); ?>">
								  </form>
							  </table>
						  
						  
					  </FIELDSET>
	  </td>
	</tr>
</table>	  				  	  

</body>
</html>
<script>
//window.reset();
/*document.getElementById("trows").value="";
document.getElementById("tcolumns").value="";
document.getElementById("tbgclr").value="";
document.getElementById("tcellpading").value="";
document.getElementById("tcellspacing").value="";
document.getElementById("tborder").value="";
document.getElementById("tborderclr").value="";*/
</script>
