
// JavaScript Document
var log_request,gtab_id=0,ajax_status="ready",comment_page = 1;maxcomment_page = 0 ;
function fload()
{
	setReadyState();
	get_comment(1);
	showImage();
}
function limit_size(Images1,maxH,maxW)
{
	resized = new Object();
	wi = Images1.width;
	hi = Images1.height;
	resized.W = wi ;
	resized.H = hi ;
	if (wi > hi)
	{
		if (wi>maxW)
		{
			partial = wi/maxW ;
			resized.W = maxW;
			resized.H = Math.floor(hi/partial);
		}
	}
	else
	{
		if (hi>maxH)
		{
			partial 		= hi/maxH ;
			resized.H	 	= maxH;
			resized.W	  	= Math.floor(wi/partial);	
		}
	}
	return resized ;
}
function imgover(el)
{
	if (el != null)
	{
		imgPBox = document.getElementById("dvPreview");
		descx = document.getElementById("dsc");
		descx.innerHTML = el.title ;
		imgPBox.innerHTML = "";
		imgP = document.createElement("img");
		oImage  = img[el.id] ;
		rz = limit_size(oImage,300,400);
		imgP.src    = oImage.src ;
		imgP.width  = rz.W ;
		imgP.height = rz.H ;
		mleft = Math.ceil((400-rz.W)/2);
		mtop  = Math.ceil((300-rz.H)/2);
		imgP.style.marginTop = mtop+"px";	
		imgP.style.marginLeft = mleft+"px";
		imgPBox.appendChild(imgP);
	}
}
function image_out(oimg)
{
		imgPar  = document.getElementById("dvImageList");
		dv = document.createElement("div");
		dv.className = "ibox";
		image1 = document.createElement("img");
		image1.src = oimg.src ;
		image1.id  = i ;
	//	alert(oimg.dsc);
		image1.title = oimg.dsc ;
		rz = limit_size(oimg,50,50);
		image1.width = rz.W;
		image1.height = rz.H;
		image1.className = "imgshow";
		image1.onclick = function() { imgover(this)	}
		mleft = Math.ceil((50-rz.W)/2);
		mtop  = Math.ceil((50-rz.H)/2);
		image1.style.marginTop = mtop+"px";	
		image1.style.marginLeft = mleft+"px";		
		dv.appendChild(image1);
		dv.title = oimg.name;
		imgPar.appendChild(dv);	
}
function showImage()
{
	
	imglen =  img.length ;
//	alert(imglen);
	for(i=0;i<imglen;i++)
	{
		image_out(img[i]);
	}
	zeroimg = document.getElementById("0");
	imgover(zeroimg);

}
function get_answer(xml_data,xml_data2)
{
	switch (log_request)
	{
		case "req_info" :
//			alert(xml_data2);			
			Aj_show_info(xml_data);
		break;
		case "req_review" :
//			alert(xml_data2);			
			Aj_show_review(xml_data);
		break;		
		case "req_comment" :
//			alert(xml_data2);		
			Aj_req_comment(xml_data);
		break;			
		case "vote" :
//			alert(xml_data2);		
			AjVote(xml_data);
		break;				
		
	}
	ajax_status="ready"	;	
}
function get_shop_info()
{
	status1 = document.getElementById("comment_status");
	i = 1;
	inner1 =status1.innerHTML;
	if (ajax_status=="wait")
	{
		setTimeout("get_shop_info()",100);
	}
	else
	{	
		param = "id="+shop_id+"&step=req_info";
		log_request	= "req_info";
		url =  "/ajax_info.php?"+param;
		ajax_connection(url,"get");		
		ajax_status="wait"	;
	}
}

function get_shop_review()
{
	if (ajax_status=="wait")
	{
		if (i<50)
		{
			i++;
			setTimeout("get_shop_info()",100);
		}
		else
		{
			alert("status waitting timeout");
		}
	}
	else
	{		
		param = "id="+shop_id+"&step=req_review";
		log_request	= "req_review";
		url =  "/ajax_info.php?"+param;
		ajax_connection(url,"get");	
		ajax_status="wait"	;	
	}
}
function get_comment(page)
{

	status1 = "ready";

	i = 1;
	comment_page = page;
	inner1 =status1.innerHTML;
	if (ajax_status=="wait")
	{
		setTimeout("get_comment()",100);
	}
	else
	{
		param = "id="+shop_id+"&step=req_comment&page="+page;
		log_request	= "req_comment";
		url =  "/ajax_info.php?"+param;
		ajax_connection(url,"get");		
		ajax_status="wait"	;	
	}
}
function Aj_show_info(xml_data)
{

	Xowner = (xml_data.getElementsByTagName("shop_master")[0].firstChild!=null)?xml_data.getElementsByTagName("shop_master")[0].firstChild.nodeValue:"";
	Xtel = (xml_data.getElementsByTagName("shop_tel")[0].firstChild!=null)?xml_data.getElementsByTagName("shop_tel")[0].firstChild.nodeValue:"";
	Xemail = (xml_data.getElementsByTagName("email")[0].firstChild!=null)?xml_data.getElementsByTagName("email")[0].firstChild.nodeValue:"";
	Xurl = (xml_data.getElementsByTagName("url")[0].firstChild!=null)?xml_data.getElementsByTagName("url")[0].firstChild.nodeValue:"";	
	Xgroup = (xml_data.getElementsByTagName("shop_group")[0].firstChild!=null)?xml_data.getElementsByTagName("shop_group")[0].firstChild.nodeValue:"";		
	Xarea = (xml_data.getElementsByTagName("shop_zone")[0].firstChild!=null)?xml_data.getElementsByTagName("shop_zone")[0].firstChild.nodeValue:"";	
	Xservice_time = (xml_data.getElementsByTagName("service_time")[0].firstChild!=null)?xml_data.getElementsByTagName("service_time")[0].firstChild.nodeValue:"";		
//	alert("a");
	var tb1 = document.createElement("table");
	tb1.cellpadding = "0";
	tb1.cellspaceing = "0";	
	tb1.width = "100%"
	tb1.border = "0";	
//	alert("a1");	
	var row1= document.createElement("tr");
//	alert("a2");	
	var row2= document.createElement("tr");
	var row3= document.createElement("tr");	
	var row4= document.createElement("tr");		
	var cell1= document.createElement("td");	
	cell1.width="50%";
	var cell2= document.createElement("td");	
	var cell3= document.createElement("td");	
	var cell4= document.createElement("td");	
	var cell5= document.createElement("td");	
	var cell6= document.createElement("td");
	var cell7= document.createElement("td");	
	var cell8= document.createElement("td");	
	var cell9= document.createElement("td");	
	var cell10= document.createElement("td");	
	var cell11= document.createElement("td");	
	var cell12= document.createElement("td");
	var cell13= document.createElement("td");	
	var cell14= document.createElement("td");		
//	alert("b");	
	txt1 =document.createTextNode("เจ้าของ");
	txt7 =document.createTextNode(Xowner);	
	txt2 =document.createTextNode("ที่ตั้ง");	
	txt8 =document.createTextNode(Xarea);		
	txt3 =document.createTextNode("ประเภทกิจการ");	
	txt9 =document.createTextNode(Xgroup);		
	txt4 =document.createTextNode("เบอร์ติดต่อ");	
	txt10 =document.createTextNode(Xtel);		
	txt5 =document.createTextNode("email");	
	txt11 =document.createTextNode(Xemail);		
	txt13 = document.createTextNode("เวลาให้บริการ");	
	txt14 = document.createTextNode(Xservice_time);	
	txt6 = document.createTextNode("web");	
	txt12 = document.createTextNode(Xurl);	
//	alert("c");	
	var dv = document.getElementById("div1");
//	alert("d");	
	cell1.appendChild(txt1);
	cell1.width= "100";
	cell2.appendChild(txt2);
	cell2.width = "100";
	cell7.appendChild(txt7);
	cell7.width = "180";
	cell8.appendChild(txt8);	
	row1.appendChild(cell1);
	row1.appendChild(cell7);
	row1.appendChild(cell2);
	row1.appendChild(cell8);	
	row1.height = "22";
	cell3.appendChild(txt3);
	cell4.appendChild(txt4);
	cell9.appendChild(txt9);
	cell10.appendChild(txt10);	
	row2.appendChild(cell4);
	row2.appendChild(cell10);		
	row2.appendChild(cell3);	
	row2.appendChild(cell9);		
	row2.height = "22";	
	cell5.appendChild(txt5);
	cell6.appendChild(txt6);	
	cell11.appendChild(txt11);	
	cell12.appendChild(txt12);
	
	row3.appendChild(cell5);
	row3.appendChild(cell11);
	row3.appendChild(cell6);
	row3.appendChild(cell12);	
	row3.height = "22";	
	
	cell13.appendChild(txt13);
	cell14.appendChild(txt14);
	row4.appendChild(cell13);
	row4.appendChild(cell14);
	row4.height = "22";		
	
	tb1.appendChild(row1);
	tb1.appendChild(row2);	
	tb1.appendChild(row3);
	tb1.appendChild(row4);	
	dv.innerHTML="<table width='100%' border='0' callpadding='0' cellspacing='3' id='sinfo'>"+tb1.innerHTML+"</table>";
}
function Aj_show_review(xml_data)
{
	Xdetail = (xml_data.getElementsByTagName("shop_detail")[0].firstChild!=null)?xml_data.getElementsByTagName("shop_detail")[0].firstChild.nodeValue:"";	
	new_detail = "<P>"+Xdetail.replace(/\n/g,"</p><p>")+"</p>";
	var dv = document.getElementById("div1");
	dv.innerHTML = new_detail;
	
}
function gopage(page)
{
	get_comment(page);
}
function Aj_req_comment(xml_data)
{
	maxcomment_page = xml_data.getElementsByTagName("page")[0].firstChild.nodeValue;
	phtm = "";
	for (i=1;i<=maxcomment_page;i++)
	{
		if (i!=comment_page)
		{
			phtm = phtm + "<button class='page_btn' onClick='gopage("+i+")'>"+i+"</button>";
		}
		else
		{
			phtm = phtm+"&nbsp;"+i+"&nbsp";
		}
	}
	document.getElementById("allpage").innerHTML = phtm ;
	Xdetail = xml_data.getElementsByTagName("xcomment");
	comment_count = Xdetail.length ;
	divComment = document.getElementById("comment1");
	divComment.innerHTML = "";
	comment_plus="";
	if (comment_count>0)
	{
		for (i=0;i<comment_count;i++)
		{
			
			num = Xdetail[i].getElementsByTagName("rec_num")[0].firstChild.nodeValue;
			if (Xdetail[i].getElementsByTagName("comment_id")[0].firstChild!=null)
			{			
				com_id = Xdetail[i].getElementsByTagName("comment_id")[0].firstChild.nodeValue;
			}
			else
			{
				com_id = 0;
			}			
			if (Xdetail[i].getElementsByTagName("comment_detail")[0].firstChild!=null)
			{
				com_detail = Xdetail[i].getElementsByTagName("comment_detail")[0].firstChild.nodeValue;
			}
			else
			{
				com_detail = "";
			}
			new_detail = com_detail.replace(/\n/g,"<br>");
			com_dt = Xdetail[i].getElementsByTagName("date_time")[0].firstChild.nodeValue;	
			Acomdt = com_dt.split(" ");
//			alert("ping2");
			if (Xdetail[i].getElementsByTagName("username")[0].firstChild!=null)
			{
				com_name = Xdetail[i].getElementsByTagName("username")[0].firstChild.nodeValue;	
			}
			else
			{
				com_name = "ไม่ได้ระบุ";
			}
			comment_plus =comment_plus+"<div id='coment_"+com_id+"' class='comment'><div class='commentHeader'><div style='float:left;'>อันดับ "+num+"<b>ตอบโดย : </b></div><div style='float:left; width:150px'>"+com_name+"</div><div style='float:right; padding-right:10px' ><b>ตอบเมื่อ : </b><span style=' padding-left:15px;padding-right:15px'>"+Acomdt[0]+"</span> เวลา <span style='padding-left:15px'>"+Acomdt[1]+"</span></div></div><div class='commentDetail'>"+new_detail+"</div></div>";
		}
		divComment.innerHTML = comment_plus;
	}
	
	ajax_status = "ready";
}


function AjVote(xml_data)
{
	answerz = parseXML(xml_data,"vote","");
	score = parseXML(xml_data,"point","0")
	if(answerz=="ok")
	{
		ajax_status="ready"	;		
		document.getElementById("show_score").innerHTML = score;
		vote_allow = false ;
//		get_score();
	}
	else
	{
		alert(answerz);
	}
	ajax_status="ready"	;	
//	alert(ajax_status);
}
function fill_comment_name(Xname)
{
	abc = document.getElementById("comment_user");
	abc.value = Xname ;
}
function vote_slide(event,el)
{
	v1 = document.getElementById("vote_text");
	vbar = document.getElementById("vote1");
	if (vote_active==1)
	{
		wi0 = event.clientX - el.offsetLeft;
		vote_point = Math.ceil(wi0/10)/2;
		if (vote_point>10)
		{
			vote_point = 10;
		}
		v1.innerHTML = vote_point;
		vbar.style.width = (wi0<200)?wi0+"px":"200px";
	}
	
}
function doVote()
{
	if (vote_allow)
	{
		if (confirm("ยืนยันการให้คะแนน"))
		{
			param = "id="+shop_id+"&step=vote";
			log_request	= "vote";
			url =  "/ajax_info.php?"+param;
			ajax_connection(url,"get");		
			ajax_status="wait"	;		
		}
	}
	else
	{
		vote_allow = false ;
		alert("ไม่สามารถโหวตได้ เนื่องจากคุณยังไม่ได้ login หรือ คุณใช้สิทธิ์ในการโหวตในวันนี้ไปแล้ว กรุณาโหวตใหม่ในวันพรุ่งนี้ครับ");
	}
}
