// JavaScript Document
var xmlHttp; // กำหนดตัวแปรแบบ Public
var t;
var c=0;
// ฟังก์ชันสำหรับตรวจสอบ Web Browser 
function createXMLHttpRequest(){ 
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
}
function getRequestBody(pForm) {
	var nParams = new Array();
	for (var i=0 ; i < pForm.elements.length; i++) {
		var pParam = encodeURIComponent(pForm.elements[i].name);
		pParam += "=";
		pParam += encodeURIComponent(pForm.elements[i].value);
		nParams.push(pParam);
	} 
	return nParams.join("&");        
}
// ฟังก์ชันสำหรับแจ้งเตือนข้อผิดพลาด
function errResult(pMessage) { 
	alert(pMessage);  // แสดง Alert Box
}

//ฟังก์ชันสำหรับ setFocus Login
function setFocus(){
	if(document.getElementById("username").value="รหัสผู้ใช้งาน"){
		document.getElementById("username").value="";
	}
}
function loseFocus(){
	if(document.getElementById("username").value==""){
		document.getElementById("username").value="รหัสผู้ใช้งาน";	
	}
}

// ฟังก์ชันสำหรับตรวจสอบการกรอกข้อมูลเพื่อเข้าสู่ระบบ
function validateLogin(){
	createXMLHttpRequest();
	var user = document.getElementById("username");
	var pwd  = document.getElementById("pwd");
	if(isEmpty(user,"กรุณากรอกรหัสผู้ใช้งานด้วยค่ะ !!")) {
		user.focus();
		if(isEmpty(pwd,"กรุณากรอกรหัสผ่านด้วยค่ะ !!")) {
			pwd.focus();
			processLogin();
		}
	}
}
function processLogin(){
	createXMLHttpRequest();
	var pForm=document.forms[0];
	var pBody=getRequestBody(pForm);
	xmlHttp.open("post", pForm.action, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				if(xmlHttp.responseText=="Incorrect"){
					errResult("คุณกรอกชื่อผู้ใช้งาน หรือรหัสผ่านไม่ถูกต้อง โปรดตรวจสอบ !!");
					document.getElementById("pwd").focus();
				}else if(xmlHttp.responseText=="Correct"){
					window.location.href="php-scripts/index.php";
				}
			}else{
				errResult("พบข้อผิดพลาด" + xmlHttp.responseText);
			}
		}
	};
xmlHttp.send(pBody); 
}
//ฟังก์ชันแสดงบทความ
function processBlogList(url,id){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange=function(){
		//alert(xmlHttp.responseText);
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				displayContent(xmlHttp.responseText,id);	
			}else{
				errResult("เกิดข้อผิดพลาด " + xmlHttp.responseText);	
			}
		}
	}
	xmlHttp.open("GET",url + "?ran=" + Math.random(),true)
	xmlHttp.send(null);
	
}
//ฟังก์ชันแสดงบทความ
function processBlogViews(url,id,src){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				displayContent(xmlHttp.responseText,src);	
			}else{
				errResult("เกิดข้อผิดพลาด " + xmlHttp.responseText);	
			}
		}
	}
	xmlHttp.open("GET",url + "?id=" + id + "&ran=" + Math.random(),true)
	xmlHttp.send(null);
	
}
function validInsertBlog(){
	createXMLHttpRequest();
	var art_title = document.getElementById("article_title");
	var art_brief = document.getElementById("article_brief");
	if(isEmpty(art_title,"กรุณากรอกชื่อบทความด้วยค่ะ")){
		if(isEmpty(art_brief,"กรุณากรอกเนื้อหาโดยสรุปด้วยค่ะ")){
			processInsert();
		}
	}
}
function processInsertBlog(){
	createXMLHttpRequest();
	var pForm=document.forms[0];
	var pBody=getRequestBody(pForm);
	xmlHttp.open("post", pForm.action, true);
	xmlHttp.setRequestHeader("Content-Type", "multipart/form-data");
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				if(xmlHttp.responseText=="Success"){
					errResult("ผลการทำงาน\nบันทึกข้อมูลเรียบร้อยแล้วค่ะ");
					//processBlogList('php-scripts/blog/mod_article_list.php','displaycontent');
				}else if(xmlHttp.responseText=="Fail"){
					errResult("เกิดข้อผิดพลาด\nไม่สามารถบันทึกข้อมูลได้ค่ะ");
				}
			}else{
				errResult("พบข้อผิดพลาด :" + xmlHttp.responseText);	
			}
		}
	};
xmlHttp.send(pBody); 
}
function displayContent(pMessage,src){
	document.getElementById(src).innerHTML=pMessage;	
}
function processEdit(url,id,src){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				displayContent(xmlHttp.responseText,src);
			}else{
				errResult("เกิดข้อผิดพลาด : " + xmlHttp.responseText);	
			}
		}
	}
	xmlHttp.open("GET",url + "?id=" + id + "&ran=" + Math.random(),true);
	xmlHttp.send(null);
}

//ฟังก์ชันสำหรับลบข้อมูล
function processDelete(url,id,url1){
	createXMLHttpRequest();
	if (confirm("คุณต้องการลบข้อมูลระเบียนนี้ใช่หรือไม่ ?")==true){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				if(xmlHttp.responseText=="del_success"){
					errResult("ผลการทำงาน\nลบข้อมูลระเบียนนี้เรียบร้อยแล้วค่ะ");
					//window.location.reload();
					processBlogList(url1,'displaycontent');
				}else if(xmlHttp.responseText=="del_fail"){
					errResult("เกิดข้อผิดพลาด\nไม่สามารถลบข้อมูลในขณะนี้ได้ค่ะ");
				}
			}
		}
	}else{
		return false;	
	}

	var xurl=url
	xurl+="?id=" + id + "&ran=" + Math.random();
	xmlHttp.open("GET",xurl,true);
	xmlHttp.send(null);
}
function colorMouseOver(id,color)
{	
	document.getElementById(id).style.backgroundColor=color;
	document.getElementById(id).style.cursor="hand";
}

function colorMouseOut(id)
{
	old_color=document.getElementById(id).bgColor;
	//alert(old_color);
	document.getElementById(id).style.backgroundColor="";
}
//ฟังก์ชันแสดงบทความ
function processNewsList(url,id){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				displayContent(xmlHttp.responseText,id);	
			}else{
				errResult("เกิดข้อผิดพลาด " + xmlHttp.responseText);	
			}
		}
	}
	xmlHttp.open("GET",url + "?ran=" + Math.random(),true)
	xmlHttp.send(null);
	
}
function processLogout(){
	createXMLHttpRequest();
	if(confirm("ยืนยันการออกจากระบบ") == false){
		return false;
	}else{
		xmlHttp.onreadystatechange= function () {
			if(xmlHttp.readyState == 4) {
				if(xmlHttp.status == 200) {
					window.location.href="../index.php";
				}else{
					errResult("พบข้อผิดพลาด " + xmlHttp.statusText);
				}
			}            
		}
		xmlHttp.open("get", "mod_process_logout.php", true);
		xmlHttp.send(null);
	}
}
function processInsert(fileid,bnt,msg){
	if(document.getElementById(fileid).value.length==0){
		errResult("กรุณาเลือกไฟล์รูปภาพด้วยค่ะ");
		return false;
	}
	
	document.getElementById(msg).innerHTML="กำลังประมาลผล กรุณารอสักครู่ ...";
	document.getElementById(bnt).disabled=true;
	return true;
}
function uploadok(pathfile,fileid,msg,bnt,frm)
{
document.getElementById(fileid).value ="";
document.getElementById(msg).innerHTML = 'Upload เสร็จแล้ว <br> file อยู่ที่<Br><a href="'+pathfile+'" target="_blank">'+pathfile+'</a><br>ระบุ File ใหม่ต่อไปได้เลย' ;
document.getElementById(bnt).value = "Upload เลยเด่ะ";
document.getElementById(bnt).disabled = false;
document.getElementById(frm).reset() ;
return true ;
}
function savefunc(frm,url)
{
	if (confirm("คุณต้องการประเมินแบบสอมถามนี้ใช่หรือไม่ ?")==true){
		$.post(url,$(frm).serialize(),function(data) {
				alert(data);
				processBlogList('php-scripts/questionnaire/questionaire_info.php','displaycontent');
			});
	}else{
		return false;
	}
}


