// JavaScript Document
/* Google Map API*/
 /* หาค่า lat lng */
var COLORS = [["red", "#ff0000"]];
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;
var featureTable_;
var map;
var geocoder;

function select(buttonId) {
  document.getElementById("hand_b").className="unselected";
  document.getElementById("placemark_b").className="unselected";
  document.getElementById(buttonId).className="selected";
}

function stopEditing() {
  select("hand_b");
}

function getColor(named) {
  return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}

function getIcon(color) {
  var icon = new GIcon();
  icon.image = "admin/images/pin1.png";
  icon.iconSize = new GSize(40, 54);
  icon.iconAnchor = new GPoint(20, 54);
  return icon;
}

function addFeatureEntry(name, color) {
  currentRow_ = document.createElement("tr");
  var colorCell = document.createElement("td");
  currentRow_.appendChild(colorCell);
  colorCell.style.backgroundColor = color;
  colorCell.style.width = "1em";
  var nameCell = document.createElement("td");
  currentRow_.appendChild(nameCell);
  nameCell.innerHTML = name;
  var descriptionCell = document.createElement("td");
  currentRow_.appendChild(descriptionCell);
  featureTable_.appendChild(currentRow_);
  return {desc: descriptionCell, color: colorCell};
}

function startDrawing(poly, name, onUpdate, color) {
  map.addOverlay(poly);
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  GEvent.addListener(poly, "endline", function() {
    select("hand_b");
    var cells = addFeatureEntry(name, color);
    GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
    GEvent.addListener(poly, "click", function(latlng, index) {
      if (typeof index == "number") {
        poly.deleteVertex(index);
      } else {
        var newColor = getColor(false);
        cells.color.style.backgroundColor = newColor
        poly.setStrokeStyle({color: newColor, weight: 4});
      }
    });
  });
}


function placeMarker() {
  select("placemark_b");
  var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
    if (latlng) {
      select("hand_b");
      GEvent.removeListener(listener);
      var color = getColor(true);
      var marker = new GMarker(latlng, {icon: getIcon(color), draggable: true});
      map.addOverlay(marker);
      var cells = addFeatureEntry("Placemark " + (++markerCounter_), color);
      updateMarker(marker, cells);
      GEvent.addListener(marker, "dragend", function() {
        updateMarker(marker, cells);
      });
      GEvent.addListener(marker, "click", function() {
        updateMarker(marker, cells, true);
      });
    }
  });
}

function updateMarker(marker, cells, opt_changeColor) {
  if (opt_changeColor) {
    var color = getColor(true);
    marker.setImage(getIcon(color).image);
    cells.color.style.backgroundColor = color;
  }
	var latlng = marker.getPoint();
	var lng = latlng.lng(); 
	var lat = latlng.lat(); 
	document.getElementById("marker_lng").value = lng;
	document.getElementById("marker_lat").value = lat;
	"lat="+lat;
	"lng="+lng;
  cells.desc.innerHTML = "(" + Math.round(latlng.y * 100) / 100 + ", " +
  Math.round(latlng.x * 100) / 100 + ")";
}
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
	geocoder = new GClientGeocoder();
    map.setCenter(new GLatLng(13.751724222399008, 100.51769256591797), 12);
    map.addControl(new GMapTypeControl());
	map.addControl(new myZoomControl()); 
    map.clearOverlays();
    featureTable_ = document.getElementById("featuretbody");
    select("hand_b");
  }
}
function number_Change(amphur_id)
{
	showAddress(amphur_id);
	document.value.submit();
}
function showAddress(amphur_id) {
	 if (geocoder) {
	 geocoder.getLatLng(amphur_id, 
		function(point){ 
			if (!point){ 
			alert("กรุณาเลือกอำเภอใกล้เครียงกับที่ท่านจะประกาศ"); 
			}else{ 
			map.setCenter(point, 13); 
			}
		} );
	 }
 }
/*หาค่า lat lng*/
 /* Button Zoomin Zoomout*/
// ก่อนอื่นสร้าง function myZoomControl ขึ้นมาก่อน
function myZoomControl() {
}
myZoomControl.prototype = new GControl();
myZoomControl.prototype.initialize = function(map) {
  
  var container = document.createElement("div");
  container.style.width = "37px";
  container.style.height = '300px';
 
  var zoomInDiv = document.createElement("div");
  zoomInDiv.className = 'buttonup';
  zoomInDiv.id = 'zoomin';
  container.appendChild(zoomInDiv);
  GEvent.addDomListener(zoomInDiv, "click", function() {
    map.zoomIn();
  });
 
  var zoomOutDiv = document.createElement("div");
  zoomOutDiv.className = 'buttonup';
  zoomOutDiv.id = 'zoomout'; 
  container.appendChild(zoomOutDiv);
  GEvent.addDomListener(zoomOutDiv, "click", function() {
    map.zoomOut();
  });
  map.getContainer().appendChild(container);
  return container;
}
 /* Button Zoomin Zoomout*/
 /* Google Map API*/
 function number_formatnum( number, decimals, dec_point, thousands_sep ) {   
	  var n = number, prec = decimals, dec = dec_point, sep = thousands_sep;  
	  n = !isFinite(+n) ? 0 : +n;  prec = !isFinite(+prec) ? 0 : Math.abs(prec);  
	  sep = sep == undefined ? ',' : sep;  
	  var s = n.toFixed(prec),      abs = Math.abs(n).toFixed(prec),      _, i;  
	  if (abs > 1000) {      
		  _ = abs.split(/\D/);     
		  i = _[0].length % 3 || 3;      
		  _[0] = s.slice(0,i + (n < 0)) +            
		  _[0].slice(i).replace(/(\d{3})/g, sep+'$1');      
		  s = _.join(dec || '.');  
	} else {      
	s = abs.replace('.', dec_point);  
	}  
	return s;
	}
 
function ajaxSubmit(url,getid,sendid){
	var sel = document.getElementById(getid);
	var idx = sel.selectedIndex;
	var data = "&getid="+sel.options[idx].value+"&sendid="+sendid;
	var URL = url;
	ajaxLoad('get',URL,data,'alert');
}
function ajaxSubmitP(url,province_id,amphur_id){
	var sel = document.getElementById(province_id);
	var idx = sel.selectedIndex;
	var data = "&province_id="+sel.options[idx].value+"&amphur_id="+amphur_id;
	var URL = url;
	ajaxLoad('get',URL,data,'alert');
}
function ajaxSubmitS(url,S_province,S_amphur){
	var sel = document.getElementById(S_province);
	var idx = sel.selectedIndex;
	var data = "&S_province="+sel.options[idx].value+"&S_amphur="+S_amphur;
	var URL = url;
	ajaxLoad('get',URL,data,'alert');
}
function ajaxSubmitS2(url,S_province2,S_amphur2){
	var sel = document.getElementById(S_province2);
	var idx = sel.selectedIndex;
	var data = "&S_province2="+sel.options[idx].value+"&S_amphur2="+S_amphur2;
	var URL = url;
	ajaxLoad('get',URL,data,'alert');
}

function sendData(url){
	var URL = url;
	var data = getFormData('frm');
	ajaxLoad('post',URL,data,'alert');
}

function DeleteData(url,id){
	if(confirm("ต้องการลบใช้หรือไม่")){
		var URL = url;
		var data = "&id="+id;
		ajaxLoad('get',URL,data,'alert');
	}
}
function DeleteDataC(url,id,display){
	if(confirm("ต้องการลบใช้หรือไม่")){
		var URL = url;
		var data = "&id="+id+"&display="+display;
		ajaxLoad('get',URL,data,'alert');
	}
}

function ChangeStatus(url,id,display){
	if(confirm("ต้องการเปลี่ยนสถานะใช่หรือไม่")){
		var URL = url;
		var data = "&id="+id+"&displayid="+display;
		ajaxLoad('get',URL,data,'alert');
	}
}

function checkEmail(input){
	 var RE = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/;
 	return (RE.test(input)); 
}
function checkCharEn(input){
	 var RE = /^[a-zA-Z0-9]+$/;
 	return (RE.test(input)); 
}
function checkPassword(input){
	 var RE = /^[a-zA-Z0-9]{6,14}$/;
 	return (RE.test(input)); 
}

function chkUser(id,displayid)
  {
	var url = "admin/func/chkuser.php";
	var data = "&uname="+document.getElementById(id)+"&id="+displayid;
	ajaxLoad('post',url,data,displayid);
  }
  
function checkCharEnInputU(id,displayid){
	if(!checkCharEn(document.getElementById(id).value)){
		document.getElementById(displayid).innerHTML = "กรอกข้อมูลไม่ถูกต้อง ต้องเป็นภาษาอังกฤษหรือตัวเลขเท่านั้น";
		document.getElementById(id).style.border = "1px solid #F00";
	}else{
		chkUser(id,displayid);
	}
}	

function checkEmailInput(id,displayid){
	if(!checkEmail(document.getElementById(id).value)){
		document.getElementById(displayid).innerHTML = "กรอกอีเมล์ไม่ถูกต้อง";
		document.getElementById(id).style.border = "1px solid #F00";
	}else{
		document.getElementById(displayid).innerHTML = "ผ่าน";
		document.getElementById(id).style.border = "1px solid #CCC";
	}
}
function checkCharEnInput(id,displayid){
	if(!checkCharEn(document.getElementById(id).value)){
		document.getElementById(displayid).innerHTML = "กรอกข้อมูลไม่ถูกต้อง ต้องเป็นภาษาอังกฤษหรือตัวเลขเท่านั้น";
		document.getElementById(id).style.border = "1px solid #F00";
	}else{
		document.getElementById(displayid).innerHTML = "ผ่าน";
		document.getElementById(id).style.border = "1px solid #CCC";
	}
}
function checkPasswordInput(id,displayid){
	if(!checkPassword(document.getElementById(id).value)){
		document.getElementById(displayid).innerHTML = "กรอกข้อมูลไม่ถูกต้อง ต้องเป็นตัวอักษรภาษาอังกฤษและตัวเลข 6-14 ตัวอักษรเท่านั้น";
		document.getElementById(id).style.border = "1px solid #F00";
	}else{
		document.getElementById(displayid).innerHTML = "ผ่าน";
		document.getElementById(id).style.border = "1px solid #CCC";
	}
}
function checkPasswordAgainInput(id,displayid){
	if(!checkPassword(document.getElementById(id).value)){
		document.getElementById(displayid).innerHTML = "กรอกข้อมูลไม่ถูกต้อง ต้องเป็นตัวอักษรภาษาอังกฤษและตัวเลข 6-14 ตัวอักษรเท่านั้น";
		document.getElementById(id).style.border = "1px solid #F00";
	}else if(document.getElementById(id).value!=document.getElementById('passwd').value){
		document.getElementById(displayid).innerHTML = "กรอกข้อมูลไม่ถูกต้อง";
		document.getElementById(id).style.border = "1px solid #F00";
	}else{
		document.getElementById(displayid).innerHTML = "ผ่าน";
		document.getElementById(id).style.border = "1px solid #CCC";
	}
}
function sendAdminData(url){
	var uname = document.getElementById('uname').value;
	var name = document.getElementById('name').value;
	var email = document.getElementById('email').value;
	if(uname == "" || name == "" || email == ""){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}
function sendAdminProfile(url){
		sendData(url);
}
function sendPositionData(url){
	var pname = document.getElementById('pname').value;
	if(pname == ""){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}
function sendCatData(url){
	var catname = document.getElementById('catname').value;
	if(catname == ""){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}

function sendrCatData(url){
	var catname = document.getElementById('rcatname').value;
	if(catname == ""){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}

function sendSCatData(url){
	var catname = document.getElementById('scatname').value;
	if(catname == ""){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}
function sendComData(url){
	var comname = document.getElementById('comname').value;
	var comlink = document.getElementById('comlink').value;
	if(comname == "" || comlink== "" ){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}
function sendLinkData(url){
	var linkdesc = document.getElementById('linkdesc').value;
	var linkurl = document.getElementById('linkurl').value;
	if(linkdesc == "" || linkurl== "" ){
		document.getElementById('alert').innerHTML = "กรอกข้อมูลไม่ครบ";
		alert("กรอกข้อมูลไม่ครบ");
	}else{
		sendData(url);
	}
}

function popup(url,name,windowWidth,windowHeight){    
	myleft=(screen.width)?(screen.width-windowWidth)/2:100;	
	mytop=(screen.height)?(screen.height-windowHeight)/2:100;	
	properties = "width="+windowWidth+",height="+windowHeight;
	properties +=",scrollbars=yes, top="+mytop+",left="+myleft;   
	window.open(url,name,properties);
}

function changemenulevel(key){
	var menulevel = document.getElementById('menulevel'+key).value;
	var menuid = document.getElementById('menuid'+key).value;
	var URL = "menu/func/changelevel.php";
	var data = "&menulevel="+menulevel+"&menuid="+menuid;
	ajaxLoad('get',URL,data,'alert');
}

function changbbmenu(key){
	var id_topic = document.getElementById('id_topic'+key).value;
	var id_menu = document.getElementById('id_menu'+key).value;
	var URL = "board/func/changelevel.php";
	var data = "&id_topic="+id_topic+"&id_menu="+id_menu;
	ajaxLoad('get',URL,data,'alert');
}

function changgruplinkmenu(key){
	var id_link_grup = document.getElementById('id_link_grup'+key).value;
	var id_menu = document.getElementById('id_menu'+key).value;
	var URL = "component/com_weblink/func/changegrupmenu.php";
	var data = "&id_link_grup="+id_link_grup+"&id_menu="+id_menu;
	ajaxLoad('get',URL,data,'alert');
}

function changelinkmenu(key){
	var linkid = document.getElementById('linkid'+key).value;
	var id_menu = document.getElementById('id_menu_link'+key).value;
	var URL = "component/com_weblink/func/changelinkmenu.php";
	var data = "&linkid="+linkid+"&id_menu_link="+id_menu;
	ajaxLoad('get',URL,data,'alert');
}

function changemenubanner(key){
	var bannerid = document.getElementById('bannerid'+key).value;
	var id_banner = document.getElementById('id_banner'+key).value;
	var URL = "component/com_banner/func/changemenu.php";
	var data = "&bannerid="+bannerid+"&id_banner="+id_banner;
	ajaxLoad('get',URL,data,'alert');
}

function resetpassword(adminid){
	var URL = "admin/func/resetpassword.php";
	var data = "&adminid="+adminid;
	ajaxLoad('get',URL,data,'alert');
}

function resetMemberpassword(memid){
	var URL = "member/func/resetpassword.php";
	var data = "&memid="+memid;
	ajaxLoad('get',URL,data,'alert');
}
function signin()
{
	var URL = 'func/signin.php';
	var data = getFormData("frmlogin");
	ajaxLoad('post', URL, data, 'alert');
}
var hh=0;
var inter;
function ShowBox()
{
	if(hh==170)
	{
	clearInterval(inter);
	return;
	}
 
	obj = document.getElementById("coverlogin");
	obj.style.visibility = 'visible';
	hh+=10;
	obj.style.height = hh + 'px';
}
function HideBox()
{
	obj = document.getElementById("coverlogin");
 
	if(hh==2)
	{
	obj.style.visibility = 'hidden';
	obj.style.height = '0.1em';
	clearInterval(inter);
	return;
	}
	hh-=10;
	obj.style.height = hh + 'px';
}
