// JavaScript Document
var request1 ;
var cPage,cAll;
var ajx = new jaxX("/ajax_news.php","post");
ajx.Response = Response ;
function doLogout()
{
	request1 = "logout";
	params = "order=logout";
	ajx.send(params,"json");	
}
function doLogIn()
{
	ID = document.getElementById("uid");
	Pass = document.getElementById("pass");
	request1 = "login";
	params = "order=login&id="+ID.value+"&pass="+Pass.value;
	ajx.send(params,"json");	
}
function Response()
{
	dd = '(' + ajx.answer + ')';
	ans1 = eval(dd);
	switch (request1)
	{
		case "logout" :
			if (ans1.answer=='complete')
			{
				AccBox = document.getElementById("accBox");
				AccBox.innerHTML = "<div style='margin-top:3px;'>ID<br /><input type='text' name='uid' id='uid' /></div><div style='margin-top:7px;'>Password<br /><input type='password' name='pass' id='pass' /></div><div style='margin-top:7px; margin-bottom:7px'><a href='regist.php' style='color:#FFFFFF; font-size:10px;'>ลงทะเบียน</a><button type='button' onclick='doLogIn()'>Login</button></div>";
				document.getElementById("user_name").value = "";
			}
			else
			{
				alert("Error while logging out from system");
			}
		break;
		case "login" :
			if (ans1.answer=='complete')
			{
				AccBox = document.getElementById("accBox");
				AccBox.innerHTML = "<div class='logging_in'>คุณ "+ans1.cname+"</div><div class='idlink'><a>ข้อมูล</a> , <a onClick='doLogout()'>logout</a></div>";
				document.getElementById("user_name").value = ans1.cname;
			}
			else
			{
				alert("Error while logging out from system");
			}
		break;	
		case "comment_list" :
			if (ans1 != null)
			{
				show_comment(ans1);
			}
		break;
		case "post_comment" :
			if (ans1.result == "ok")
			{
				sp =  document.getElementById("selpage");
				if (sp.length > 1)
				{
					comment_req(sp[sp.length-1].value);
				}
				else
				{
					comment_req(1);
				}
			}
			else
			{
				alert("ผิดพลาดในการโพสข้อความ")
			}
		break;
	}
}
function Prev()
{
	sp =  document.getElementById("selpage").value;
	if (sp > 1)
	{
		sp--;
		comment_req(sp);
	}
}
function Next()
{
	sp =  document.getElementById("selpage");
//	alert(sp[sp.length-1].value+" :: "+sp.value);
	if (sp.value < sp[sp.length-1].value)
	{
		p = (sp.value*1)+1;
		comment_req(p);
	}
}
function goPage()
{
	sp =  document.getElementById("selpage").value;
	comment_req(sp);

}
function comment_req(page)
{
	request1 = "comment_list";
	params = "order=comment_list&id="+UID+"&p="+page;
	ajx.send(params,"json");				
}
function fill_page(al)
{
	opt = document.getElementById("selpage");
	tpage = opt.value ;
	opt.length = 0;
	for(i=0;i<al;i++)
	{
		if (tpage == i)
		{
			opt[i] = new Option(i+1,i+1,true,true);
		}
		else
		{
			opt[i] = new Option(i+1,i+1);
		}
			
	}
}
function show_comment(anx)
{
	comBox = document.getElementById("comment1");
	c = 0 ;
	comBox.innerHTML ="";
	html = "";
	fill_page(anx.all);
	ans = anx.list ;
	for(i=0;i< ans.length;i++)
	{
		cls1 = (c==0)?"comlist1":"comlist2";
		c	= (c==0)?1:0;
		html +=  "<div class='"+cls1+"'>";
		html += "<div class='comart'>";
		html += "<div class='posttime'>";
		html += "<b>ตอบเมื่อ </b>"+ans[i].cdate;
		html += "</div>";
		html += "<div class='postby'>";
		html += "<b>ตอบโดย </b>"+ans[i].cname;
		html += "</div>";
		html += "</div>";
		html += "<div class='comment_b'>";
		html += ans[i].comment;
		html += "</div></div>";
	}
	comBox.innerHTML = html ;
}
function subComment()
{
	uname 		= document.getElementById("user_name").value;
	commentx 	= document.getElementById("comment_text").value;
	if (uname!="")
	{
		request1 = "post_comment";
		params = "order=post&id="+UID+"&txt="+commentx;
		ajx.send(params,"json");	
	}
	else
	{
		alert("กรุณา Login ก่อนครับ");
	}
}
function Zoom_images(el)
{
	popX = document.getElementById("imgZ");
	popX.style.display = "block";
	popTop = el.offsetTop + 100;
	popX.style.top = popTop+"px";
	popIMG = document.getElementById("imgZX");
	popIMG.src = "/images.php?id="+el.id;
	popEx = document.getElementById("popExp");
	imgMsg1 = document.getElementById("msg_"+el.id);
	popEx.innerHTML = imgMsg1.innerHTML ;
}
function Close_images()
{
	popX = document.getElementById("imgZ");
	popX.style.display = "none";
}
function doVote(NewsID)
{
	request1 = "vote";
	params = "order="+request1+"&id="+NewsID;
	ajx.send(params,"json");	
}
