<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
error_reporting(0);
?>
<script>
function showResult(str) {
  if (str.length==0) { 
    document.getElementById("livesearch").innerHTML="";
    document.getElementById("livesearch").style.border="0px";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("livesearch").innerHTML=this.responseText;
      document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
  xmlhttp.open("GET","modules/plan/check/livesearch.php?q="+str,true);
  xmlhttp.send();
}
</script>

<?php
echo "<br />";
echo "<Center>";
echo "<Font color='#006666' Size='3'><B>สิ่งก่อสร้างสถานศึกษา</B></Font>";
echo "</Cener>";
 		
		echo  "<table width='90%' border='0' align='center'>";
		echo "<form id='frm1' name='frm1'>";
		echo "<Tr><td align='right' width='50%'>";
		?>
		ค้นหาโรงเรียน <input type="text" size="15" onkeyup="showResult(this.value)">
		<div id="livesearch"></div>
		<?php
		echo "<div align='right'>";
	
			echo "<Select  name='school_index' size='1' onchange='goto_index(1)'>";
			echo  "<option  value = ''>ทุกโรงเรียน</option>" ;
		$sql = "select * from  system_school  where school_type='1' order by  school_code";
		$dbquery = mysqli_query($connect,$sql);
		While ($result = mysqli_fetch_array($dbquery))
		   {
		   			if($result['school_code']==$_REQUEST['school_index']){
					echo  "<option value = $result[school_code] selected>$result[school_code] $result[school_name]</option>";
					}
					else{
					echo  "<option value = $result[school_code]>$result[school_code] $result[school_name]</option>";
					}
			}
		echo "</select>";
		echo "&nbsp;<INPUT TYPE='button' name='smb' value='เลือก' onclick='goto_index(1)'>";
		echo "</div>";
		echo "</form>";
		echo "</td></Tr></Table>";
		//จบ
		
if(isset($_REQUEST['school_index'])){
		$path="modules/plan/picture/".$_REQUEST['school_index'];
		$objScan = scandir("$path");
		echo  "<table width='90%' border='0' align='center'>";
		$x=1;
		foreach ($objScan as $value) {
		$pic_name = explode(".", $value);
		
		if($pic_name[0]==""){
		continue;
		}
		
		//หาขนาดไฟล์
		$size=getimagesize("modules/plan/picture/$_REQUEST[school_index]/$value"); 
		$ori_w=$size[0];  
		$ori_h=$size[1];  
	//	echo $size[0]; ;
		//*ลดขนาดภาพ
				$ori_file="modules/plan/picture/$_REQUEST[school_index]/$value";
					if($ori_w>700){
					$new_w=700;
					$new_h=round(($new_w/$ori_w)*$ori_h);
					$ori_img=imagecreatefromjpeg($ori_file);
					$new_img=imagecreatetruecolor($new_w, $new_h);
					imagecopyresized($new_img, $ori_img,0,0, 0,0, $new_w, $new_h, $ori_w, $ori_h);
					$new_file=$ori_file;
					imagejpeg($new_img, $new_file);
					imagedestroy($ori_img);
					imagedestroy($new_img);
					}	
		//*
				if($x==1){
				echo "<tr align='center'>";
				}
				
		echo "<td alig='center' width='50%'><img src='$path/$value' border='0' width='500'><br><b>$pic_name[0]</b><br><br></td>";
				if($x==2){
				echo "</tr>";
				}
		$x++;
				if($x==3){
				$x=1;
				}
		}  //foreach
		if($x==2){
		echo "</tr>";
		}
		echo "</Table>";
}

?>
<script>

function goto_index(val){
	if(val==1){
		callfrm("?option=plan&task=check/building"); 
		}
}

</script>
