function doPhotoAlbum(){

	window.photo_album = window.open("photo_album/index.php","photo");
	window.photo_album.focus();

	
}

function doQuery(){
	location.href="sora_query.php";
}




/**
	ฟังก์ชั่นสำหรับหน้าสืบค้น sora_query.php

*/

function getContent(){ return document.getElementById("div_sora_query");}
function doBar(elm){
	var tds = document.getElementsByTagName("TD");
	for(var i=0;i<tds.length;i++){
		if( tds[i].className=="left_gbar") tds[i].className = "left_bar";
		if( tds[i].className=="center_gbar") tds[i].className = "center_bar";
		if( tds[i].className=="right_gbar") tds[i].className = "right_bar";
	}
	var id = parseInt(elm.id.replace("T",""));
	document.getElementById("T"+id).className = "center_gbar";
	//alert(elm.previousSibling.previousSibling.className);
	document.getElementById("T"+(id-1)).className = "left_gbar";
	document.getElementById("T"+(id+1)).className = "right_gbar";
}

function printQueryIntro(){
	getContent().innerHTML = "<div style='padding:5 20px;'><p>"+
		"เมนูสืบค้นข้อมูลสิ่งแวดล้อมท้องถิ่น (ปรับปรุงใหม่) สามารถสืบค้นข้อมูลได้ดังรายละเอียดต่อไปนี้"+
			"<ul>"+
				"<li><strong>สืบค้นข้อมูลสิ่งแวดล้อม</strong><br/><span style='color:blue;'>สำหรับอ่านข้อมูลสิ่งแวดล้อมที่สมาชิก อปท. รายงานในแต่ละปี</span><br/></li>"+
				"<li><strong>สืบค้นตัวชี้วัดสิ่งแวดล้อม</strong><br/><span style='color:blue;'>สำหรับสืบค้นตัวเลขตัวชี้วัดสิ่งแวดล้อม โดยผู้ใช้สามารถกำหนดเงื่อนไขในการค้นหาได้</span><br/></li>"+
				"<!--li><strong>สำหรับ สสภ.</strong><br/><span style='color:blue;'>สำหรับ สสภ. อ่านข้อมูล อปท. ในพื้นที่</span><br/></li-->"+
			"</ul>"+
		"</p></div>";
}

// โหมดการทำงาน
function setMode(idx){
	if( idx == "intro" ){
		printQueryIntro();
	}else if( idx == "getEnvData" ){
		getContent().innerHTML = "";
	    new Ajax.Request("sora_component/sora.serverside.php",{
			parameters:{index:"getProvinceOptions"},
			onSuccess: function(ans){
				
				var sty = " style='font-size:10pt;vertical-align:top;' ";
				var str = "<div style='padding:15 60px;width:100%;'><table width='300px'>"+
				"<tr><td width='75px'>&nbsp;</td><td "+sty+"><strong>เลือก อปท.</strong></td></tr>"+
				"<tr><td "+sty+">จังหวัด</td><td "+sty+"><select id='selProvince' onchange='getMember(this.value);'><option value=''></option>"+ans.responseText+"</select></td></tr>"+
				"<tr><td "+sty+"><div id='div_member' style='display:none;'>หน่วยงาน</div></td><td "+sty+"><div id='div_select_member'></div></td></tr>"+
				"<tr><td "+sty+"><div id='div_year' style='display:none;'>ปี พ.ศ.</div></td><td "+sty+"><div id='div_select_year' style='display:none;'></div></td></tr>"+
				"</table></div>";
				getContent().innerHTML = str;
			}
		});
	
	}else if( idx == "queryEnvIndex" ){
		getContent().innerHTML = "";
		 new Ajax.Request("sora_component/sora.serverside.php",{
			parameters:{index:"getEnvIndex"},
			onSuccess: function(ans){
				var d = new Date();
				var curYear, selectYear;
				curYear = d.getYear();
				if( curYear<1900) curYear = curYear + 1900;
				if( curYear<2500) curYear = curYear +543;
				for(var i=curYear;i>=2548;i--){
					selectYear += "<option value='"+i+"' "+((i==curYear)?" selected='selected' ":"")+">"+i+"</option>";
				}
				var sty = " style='font-size:10pt;vertical-align:top;' ";
				var str = "<div style='padding:15 60px;width:100%;'><table width='750px'>"+
				"<tr><td width='75px'>&nbsp;</td><td "+sty+"><strong>เลือก ตัวชี้วัดที่ต้องการสืบค้น</strong>&nbsp: (รายละเอียดเกี่ยวกับค่าตัวชี้วัดต่างๆ... <a href='#' onclick=\"window.open('explain.php','open popup');\">อ่านต่อ</a>)</td></tr>"+
				"<tr><td "+sty+">ตัวชี้วัด</td><td "+sty+"><select id='selEnvIndex' onchange='selectEnvIndex(this.value);'><option value=''></option>"+ans.responseText+"</select></td></tr>"+
				"<tr><td "+sty+">ปี พ.ศ.</td><td "+sty+"><select id='selEnvIndexYear' >"+selectYear+"</select></td></tr>"+
				"<tr><td "+sty+"><div id='divQueryTitle' style='display:none;'>เงื่อนไข</div></td><td "+sty+"><div id='divQueryCondition'></div></td></tr>"+
				"</table></div><center><div id='queryResult' style='clear:both;padding:5 60px;width:800px;'></div></center>";
				getContent().innerHTML = str;
			}
		});
	
	
	}else if( idx == "queryByRegion" ){
		getContent().innerHTML = "";
		var sty = " style='font-size:10pt;vertical-align:top;' ";
		var str = "<div style='padding:15 60px;width:100%;'><table width='750px'>"+
		"<tr><td width='75px'>&nbsp;</td><td "+sty+"><strong>เลือก ตัวชี้วัดที่ต้องการสืบค้น</strong>&nbsp: (รายละเอียดเกี่ยวกับค่าตัวชี้วัดต่างๆ... <a href='#' onclick=\"window.open('explain.php','open popup');\">อ่านต่อ</a>)</td></tr>"+
		"<tr><td "+sty+">ตัวชี้วัด</td><td "+sty+"><select id='selEnvIndex' onchange='selectEnvIndex(this.value);'><option value=''></option>"+ans.responseText+"</select></td></tr>"+
		"<tr><td "+sty+">ปี พ.ศ.</td><td "+sty+"><select id='selEnvIndexYear' >"+selectYear+"</select></td></tr>"+
		"<tr><td "+sty+"><div id='divQueryTitle' style='display:none;'>เงื่อนไข</div></td><td "+sty+"><div id='divQueryCondition'></div></td></tr>"+
		"</table></div><center><div id='queryResult' style='clear:both;padding:5 60px;width:800px;'></div></center>";
		getContent().innerHTML = str;
	}
	
}

// ค้นหา อปท. จากจังหวัด
function getMember(prov_id){
	$("div_member").style.display = "none";
	$("div_year").style.display = "none";
	$("div_select_year").style.display = "none";
	$("div_select_member").innerHTML = "";
	new Ajax.Request("sora_component/sora.serverside.php",{
		parameters: {index: "getMemberOptionByProvince",member_province: prov_id},
		onSuccess: function(ans){
			var str = "<select id='selMember' onchange='getMemberDataYear(this.value);'>"+ans.responseText+"</select>";			
			$("div_select_member").innerHTML = str;
			$("div_member").style.display = "";																																					 
			
			if( $("selMember").options.length>0 ) getMemberDataYear($("selMember").options[0].value); 
		}	
	});
}

// หาปีที่มีข้อมูลจาก member_id
function getMemberDataYear(member_id){
	new Ajax.Request("sora_component/sora.serverside.php",{
		parameters: {index: "getDataYear",member_id: member_id},
		onSuccess: function(ans){
			var str = "<select id='selYear' >"+ans.responseText+"</select>";
			$("div_select_year").innerHTML = str+"<br/><br/><button onclick='getDataExcel();'>อ่านข้อมูล</button>";
			$("div_year").style.display = "";
			$("div_select_year").style.display = "";
		}	
	});
}																																		   

// อ่านข้อมูลสิ่งแวดล้อม Excel
function getDataExcel(){
	var _year = "";
	var _member_id = "";
	if( $("selYear") && $("selYear").value!="" && $("selMember")){
		_year = $("selYear").value;
		_member_id = $("selMember").value;
		 window.open("sora_component/excel_writer.php?year="+_year+"&member_id="+_member_id,"excel_data");
	}
}

// เมื่อผู้ใช้เลือกตัวชี้วัดฯ
function selectEnvIndex( idx ){
	$("queryResult").innerHTML = "";
	if( idx=="" ){
		$("divQueryCondition").innerHTML = "";
		$("divQueryTitle").style.display = "none";
		return false;
	}
	$("divQueryTitle").style.display="";
	var str = "<div><select id='selOperation' onchange='selectOperation(this.value);'><option value=''></option><option value='greaterthan'>มากกว่า</option><option value='lessthan'>น้อยกว่า</option><option value='between'>อยู่ระหว่าง</option></select>&nbsp;&nbsp;<span id='spConditionValue'></span></div>";
	$("divQueryCondition").innerHTML = str;
}

// เมื่อเลือกเงื่อนไขแล้ว
function selectOperation( opt ){
	if(	 opt=="" ){
		$("spConditionValue").innerHTML = "";
		return false;	
	}
	var btn = "<br/><br/><button onclick='doEnvIndexQuery();'>ดำเนินการสืบค้น</button>";
	if( opt == "lessthan" || opt == "greaterthan" ){
		$("spConditionValue").innerHTML = ":&nbsp;<input size='5' id='inpValue' type='text' value=''/>"+btn;
	}else{
		$("spConditionValue").innerHTML = ":&nbsp;<input size='5' id='inpBetween' type='text' value=''/> - <input size='5' id='inpAnd' type='text' />"+btn;
	}
}

// สืบค้นตัวชี้วัด
function doEnvIndexQuery(){
	 //divQueryCondition
	 if( !$("queryResult") ) return false;
	 if( $("inpValue") ){
		if( $("inpValue").value=="" || isNaN($("inpValue").value) ) {alert("กรุณากรอกตัวเลข...");return false;}
	 }
	 if( $("inpBetween") ){
	 	 if( $("inpBetween").value=="" || isNaN($("inpBetween").value) ) {alert("กรุณากรอกตัวเลข...");return false;}
		 if( $("inpAnd").value=="" || isNaN($("inpAnd").value) ) {alert("กรุณากรอกตัวเลข...");return false;}
	 }
	 var _value1,_value2;
	 var _info_year = $("selEnvIndexYear").value;
	 var _topic_index = $("selEnvIndex").value;
	 var _operation = $("selOperation").value;
	 if( _operation == "lessthan" || _operation == "greaterthan" ){
		_value1 = $("inpValue").value;
		_value2 = 0;
	}else{
		_value1 = $("inpBetween").value;
		_value2 = $("inpAnd").value;
	}

	new Ajax.Request("sora_component/sora_query_envindex.php",{
		parameters:{
			info_year: _info_year,
			topic_index: _topic_index,
			operation: _operation,
			value1: _value1,
			value2: _value2
		},
		onSuccess: function(ans){
			$("queryResult").innerHTML = "<div class='sora-query-result'>"+ans.responseText+"</div>";		
		}
	});
}


function getReport(idx){
	var opt = [
		[{title: "สถานการณ์แต่ละท้องถิ่น", file:"search4.php"},{title: "เปรียบเทียบระหว่างองค์กรปกครองส่วนท้องถิ่น", file:"search1.php"}]
		,[{title: "สถานการณ์แต่ละท้องถิ่น", file:"search5.php"},{title: "เปรียบเทียบระหว่างองค์กรปกครองส่วนท้องถิ่น", file:"search2.php"}]
		,[{title: "สถานการณ์แต่ละท้องถิ่น", file:"search6.php"},{title: "เปรียบเทียบระหว่างองค์กรปกครองส่วนท้องถิ่น", file:"search3.php"}]
	];
	var html = "<table width='100%' style='font-size:10pt;'><tr><td><input type='radio' name='report' onclick=\"location.href='"+opt[idx][0].file+"';window.dialog.destroy();\"/></td><td>"+opt[idx][0].title+"</td></tr>"+
		"<tr><td><input type='radio' name='report' onclick=\"location.href='"+opt[idx][1].file+"';window.dialog.destroy();\"/></td><td>"+opt[idx][1].title+"</td></tr></table>";
	
	window.dialog = new Ext.Window({
		layout:'vbox',
		width:350,
		height:100,
		closeAction:'hide',
		//plain: true,
		title: "<span='font-weight:bold;font-size:10pt;'>กรุณาเลือกเมนูเพื่อแสดงรายงาน</span>",
		html: "<div style='padding:7 20px;'>"+html+"</div>"
	});
	window.dialog.show();
}


function checkMemberDup(){
	var sel = document.getElementsByTagName("SELECT");
	
	var ar = []; 
	for(var i=0;i<sel.length;i++){
		if(sel[i].name=="selMem"){
			ar.push( sel[i].value );
		}
	}
	
	for(var j=0;j<ar.length;j++){
		for(var k=0;k<ar.length;k++){
			if(j!=k){
				if(ar[j]==ar[k]){
					alert("มี อปท. ถูกเลือกซ้ำกัน...");
					return;
				}
			}
		}
	}

}

