




function UtilComment(guid, forumid, width, returnpage) {
	var xmlhttp;
	var xmlDOM; 
	var totalView=0;
	var totalComment=0;
	var html="";
	var pageNum=1;
	var isLogin = false;
	var url = 'http://truemusic2.truelife.com/home/player/inc/inc_callcomment2.php';
	init();
	
	function init(){
		if((parseInt(width) > 2000 || parseInt(width) < 450)) { width = '2000'; }
		var str = "<TABLE valign='top' width='" + width + "' cellpadding='0' cellspacing='0' border='0' bgcolor='#CCCCCC'>";
		str = str + "<TR><TD width='" + width + "' height = '24' class='text_white' background='http://webboard.truelife.com/comment/images/bar_Title.jpg'>&nbsp;<span style='width:"+(parseInt(width) - parseInt(200))+"px;text-align:left;'><img src='http://webboard.truelife.com/comment/images/arrow.gif'>&nbsp;<strong>Comment</strong></span><span style='width:180px;text-align:right;'>จำนวนความคิดเห็น&nbsp;<strong><span id='comment_TotalComment'></span></strong></span></TD></TR>";
		str = str + "<TR><TD width='" + width + "' height = '10'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR>";
		str = str + "<TD width='" + width + "' align='center'><TABLE valign='top' width='" + (parseInt(width) - parseInt(20)) + "' cellpadding='0' cellspacing='0' border='0' style='border:1px solid #707070;'>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(20)) + "' height='25' align='center' class='text_white' bgcolor='#333333'><strong>แสดงความคิดเห็น</strong></TD></TR>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(20)) + "' height = '1' bgcolor='#707070'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(20)) + "' align='center' bgcolor='#FFFFFF'><div id='comment'><img src='http://webboard.truelife.com/comment/images/ajax-loader017.gif'/></div></TD></TR>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(20)) + "' height = '1' bgcolor='#707070'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(20)) + "' height='25' align='center' class='text_white' bgcolor='#333333'><strong>ตอบกระทู้</strong></TD></TR>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(20)) + "' height = '1' bgcolor='#707070'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR>";
		str = str + "<TD width='" + (parseInt(width) - parseInt(20)) + "' align='center' bgcolor='#FFFFFF'><TABLE valign='top' width='" + (parseInt(width) - parseInt(40)) + "' cellpadding='0' cellspacing='0' border='0'>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(40)) + "' height = '10' colspan='3'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR><TD width='11' height='11'><img src='http://webboard.truelife.com/comment/images/G_corner_01.gif' width='11' height='11'/></TD><TD width='" + ((parseInt(width) - parseInt(20)) - parseInt(22)) + "' height='11' background='http://webboard.truelife.com/comment/images/G_corner_02.gif'><img src='http://webboard.truelife.com/comment/images/blank.gif' width='11' height='11'/></TD><TD width='11' height='11'><img src='http://webboard.truelife.com/comment/images/G_corner_03.gif' width='11' height='11'/></TD></TR>";
		str = str + "<TR>";
		str = str + "<TD width='11' background='http://webboard.truelife.com/comment/images/G_corner_04.gif'><img src='http://webboard.truelife.com/comment/images/blank.gif' width='1' height='1'/></TD>";
		str = str + "<TD width='" + (parseInt(width) - parseInt(42)) + "' align='center' bgcolor='#DADCDB'><TABLE width='508' border='0' cellpadding='0' cellspacing='0'><TR><TD width='508'>";
		str = str + "<FORM name='Comment'  method='POST' action='http://comment.truelife.com/commentservice/response/responseComment.jsp' enctype='multipart/form-data' onSubmit='return funcSubmit();'>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(42)) + "' height='5' colspan='2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width='1' height='1'/></TD></TR>";
		str = str + "<TR><TD width = '25%' align='right' class='text_black'>ชื่อผู้ให้ความคิดเห็น&nbsp;:&nbsp;</TD><TD width = '75%'>&nbsp;<input type='text' name='UpdateBy' id='UpdateBy' value='' maxlength='100' style='width:360px;color:#000000;'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '25%' align='right' class='text_black'>พิมพ์ข้อความ&nbsp;:&nbsp;</TD><TD width = '75%'>&nbsp;<textarea name='msg' style='width:360;height:150;color:#000000;' id='textField'></textarea></TD></TR>";
		str = str + "<TR><TD width = '25%' align='right' class='text_black'>รูปภาพ<img src='http://webboard.truelife.com/images/picpost_ic.gif' width = '28' height = '18'>&nbsp;:&nbsp;<BR/>คลิปวีดีโอ<img src='http://webboard.truelife.com/images/vdo_ic.gif' width = '24' height = '18'>&nbsp;:&nbsp;</TD><TD width = '75%'>&nbsp;<input name='upload' type='file' style='width:360px;'></TD></TR>";
		str = str + "<TR><TD width = '25%' align='right' class='text_black'>คำบรรยายภาพ&nbsp;:&nbsp;</TD><TD width = '75%'>&nbsp;<input type='text' name='caption' value='' style='width:360px;' maxlength='400'></TD></TR>";
		str = str + "<TR><TD width = '25%' align='right' class='text_black'></TD><TD width = '75%' class='text_red'>&nbsp;*ขนาดไฟล์รูปภาพไม่เกิน 400KB ชนิดไฟล์ jpg, gif<BR>&nbsp;*ขนาดไฟล์คลิปวีดีโอไม่เกิน 800KB ชนิด wmv, 3gp, mp4, avi</TD></TR>";
		str = str + "<TR><TD width='25%' align='right' valign='middle' class='text_black'><img src='http://cgw.trueworld.net/captcha/captchaimage.asp?code=gp4djBcwDxdghW6d'>&nbsp;:&nbsp;</TD><TD width='75%'>&nbsp;<input type='text' name='vcode' value='' style='width:360px;color:#000000;'></TD></TR>";
		str = str + "<TR><TD width='25%' align='right' valign='middle' class='text_black'>&nbsp;</TD><TD width='75%' class='text_red'>&nbsp;*กรุณากรอกตัวเลขตามอักษรภาพที่ท่าเห็นให้ถูกต้อง</TD></TR>";
		str = str + "<TR><TD width = '25%' align='right' class='text_black'></TD><TD width = '75%'>&nbsp;</TD></TR>";
		str = str + "<TR>";
		str = str + "<TD colspan = '2' width='" + (parseInt(width) - parseInt(42)) + "'><a name='post_focus' id='post_focus'></a><div align='center'><TABLE valign = 'top' width='450' cellpadding='0' cellspacing='0' border='0'>";
		str = str + "<TR><TD width = '450' colspan = '2' class='text_red'>*กติกาในการแสดงความคิดเห็น การโพสรูป และวิดิโอคลิป</TD></TR>";
		str = str + "<TR><TD width = '450' height = '5' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR>";
		str = str + "<TD width = '450' colspan = '2' class='text_black' bgcolor='#FFFFFF'><TABLE valign = 'top' width='500' cellpadding='0' cellspacing='0' border='0'>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>1.&nbsp;</TD><TD width = '435' class='text_black'>งดเว้น การใช้คำหยาบคาย ส่อเสียด ดูหมิ่น กล่าวหาให้ร้าย ไม่ว่าจะเป็นทั้งข้อความ รูปภาพ อันจะ สร้างความแตกแยก หรือกระทบถึงสถาบันอันเป็นที่เคารพ อาทิ สถาบันชาติ ศาสนา พระมหากษัตริย์</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>2.&nbsp;</TD><TD width = '435' class='text_black'>งดเว้น การนำเสนอรูปภาพ วิดีโอหรือข้อความที่ลบหลู่ ดูหมิ่นศาสนา ไม่ว่าจะเป็นศาสนาใดๆก็ตาม</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>3.&nbsp;</TD><TD width = '435' class='text_black'>งดเว้น การนำเสนอรูปภาพหรือข้อความอันส่อไปในทางลามก อนาจาร หยาบคายหรือรุนแรง</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>4.&nbsp;</TD><TD width = '435' class='text_black'>งดเว้น การนำเสนอรูปภาพหรือข้อความที่เป็นการนินทา ใส่ร้าย วิพากษ์วิจารณ์บุคคลอื่นหรือต้องการยุยงส่งเสริมให้ผู้อื่นเกลียดชังบุคคล ในภาพเป็นอันขาด</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>5.&nbsp;</TD><TD width = '435' class='text_black'>งดเว้นการนำเสนอรูปภาพหรือข้อความที่เป็นการท้าทาย ชักชวน หรือเจตนาที่จะก่อให้เกิดความวุ่นวายแก่สังคม</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>6.&nbsp;</TD><TD width = '435' class='text_black'>งดเว้นการนำเสนอรูปภาพหรือข้อความพาดพิง อันจะทำให้บุคคลอื่นเกิดความเสียหาย หรือเสื่อมเสียชื่อเสียง</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>7.&nbsp;</TD><TD width = '435' class='text_black'>ทุกความคิดเห็นไม่เกี่ยวข้องกับผู้ดำเนินการเว็บไซต์ และไม่สามารถนำไปอ้างอิงทางกฎหมายได้</TD></TR>";
		str = str + "<TR><TD width = '450' height = '2' colspan = '2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "<TR valign='top'><TD width = '15' class='text_black' align='right'>8.&nbsp;</TD><TD width = '435' class='text_black'>ทีมงานเว็บมาสเตอร์ขอสงวนสิทธิ์ในการลบความคิดเห็น โดยไม่ต้องชี้แจงเหตุผลใดๆ ต่อเจ้าของความคิดเห็นนั้น</TD></TR>";
		str = str + "</TABLE></TD>";
		str = str + "</TR>";
		str = str + "</TABLE></div></TD>";
		str = str + "</TR>";
		str = str + "<TR><TD width = '25%' align='right' class='text_black'></TD><TD width = '75%'>&nbsp;</TD></TR>";
		if(isLogin) {
			str = str + "<TR><TD colspan = '2' align='center'><img src='http://webboard.truelife.com/images/send_ic.gif' width = '77' height = '21' onClick='funcSubmit()' style='cursor:hand;'></TD></TR>";
		} else {
			str = str + "<TR><TD colspan = '2' align='center' class='text_red'>**กรุณา Login ก่อนที่จะ ลงcomment นะค่ะ**<BR><a href='http://minihome.truelife.com/home/login?r=" + escape(returnpage) + "' class='text_red'>คลิกที่นี่ เพื่อล็อคอิน</a></TD></TR>";		
		}
		str = str + "<input type='hidden' name='topicID' value='"+guid+"'><input type='hidden' name='centralID' value='-'><input type='hidden' name='code' value='gp4djBcwDxdghW6d'><input type='hidden' name='forumID' value='"+forumid+"'><input type='hidden' name='rtp' value='"+returnpage+"'/>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(42)) + "' height='5' colspan='2'><img src='http://webboard.truelife.com/comment/images/blank.gif' width='1' height='1'/></TD></TR>";
		str = str + "</FORM>";
		str = str + "</TD></TR></TABLE></TD>";
		str = str + "<TD width='11' background='http://webboard.truelife.com/comment/images/G_corner_05.gif'><img src='http://webboard.truelife.com/comment/images/blank.gif' width='1' height='1'/></TD>";
		str = str + "</TR>";
		str = str + "<TR><TD width='11' height='11'><img src='http://webboard.truelife.com/comment/images/G_corner_06.gif' width='11' height='11'/></TD><TD width='508' height='11' background='http://webboard.truelife.com/comment/images/G_corner_07.gif'><img src='http://webboard.truelife.com/comment/images/blank.gif' width='11' height='11'/></TD><TD width='11' height='11'><img src='http://webboard.truelife.com/comment/images/G_corner_08.gif' width='11' height='11'/></TD></TR>";
		str = str + "<TR><TD width='" + (parseInt(width) - parseInt(40)) + "' height = '10' colspan='3'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "</TABLE></TD>";
		str = str + "</TR>";
		str = str + "</TABLE></TD>";
		str = str + "</TR>";
		str = str + "<TR><TD width = '" + width + "' height = '10'><img src='http://webboard.truelife.com/comment/images/blank.gif' width = '1' height = '1'></TD></TR>";
		str = str + "</TABLE>";
		width = (parseInt(width) - parseInt(20));
		if(forumid == null || forumid == '' || forumid == 'null' || forumid == '-') {
			url = url + '?topicid=' + guid + '&width=' + width;
		}
		else {
			url = url + '?topicid=' + guid + '&forumid=' + forumid + '&width=' + width;
		}
		var commentForm = null;
		commentForm = document.getElementById("commentForm");
		if (commentForm != null) { 
			commentForm.innerHTML = str;
			loadXMLDoc(1);
		}
	}

	function loadXMLDoc(pageNum) {
		xmlhttp=null
		// code for Mozilla, etc.
		if (window.XMLHttpRequest){
			  xmlhttp=new XMLHttpRequest();
		}// code for IE
		else if (window.ActiveXObject){
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		if (xmlhttp!=null) {
			xmlhttp.onreadystatechange=state_Change
			xmlhttp.open("POST",url + '&pageno=' + pageNum,true)
			xmlhttp.send(null)
		}else{
			processFail();
		}
	}

	function state_Change() {			
		if (xmlhttp.readyState==4){
			if (xmlhttp.status==200) {					
				xmlDOM = xmlhttp.responseXML;
				html = getContent();
				totalView = getVariable('totalview');
				totalComment =getVariable('totalcomment');					
				render();
			}else{ processFail(); }
		}
	}

	function getContent(){
		var content = "";
		if (xmlDOM.getElementsByTagName('commentobj')[0].childNodes.length > 1) { 
			//code for Mozilla, etc.
			content = xmlDOM.getElementsByTagName('commentobj')[0].childNodes[1].data;
		}else{
			content = xmlDOM.getElementsByTagName('commentobj')[0].childNodes[0].data;
		}
		return content;
	}

	function getVariable(name) {
		var variable;
		variable = xmlDOM.getElementsByTagName(name)[0].attributes[0].value;
		return variable;
	}

	function render() {
		var canvasComment = null;
		var canvasTotalView = null;
		var canvasTotalComment = null;
		var canvasTotalComment2 = null;
	
		canvasComment = document.getElementById("comment");
		canvasTotalView = document.getElementById("comment_TotalView");
		canvasTotalComment = document.getElementById("comment_TotalComment");
		canvasTotalComment2 = document.getElementById("totalreply");
		
		if (canvasComment != null)  canvasComment.innerHTML  = html;
		if (canvasTotalView != null)  canvasTotalView.innerHTML  = totalView;
		if (canvasTotalComment != null)  canvasTotalComment.innerHTML  = totalComment;
		if (canvasTotalComment2 != null) canvasTotalComment2.innerHTML  = totalComment;
	}

	function processFail() {
		totalView = 0;
		totalComment=0;
		html="Fail to retrive data";
		render();
	}

	this.getTotalView = function () {
		return totalView;
	}

	this.getTotalComment = function () {
		return totalComment;
	}
	
	this.getHtml = function () {
		return html;
	}

	this.changePage = function(x) {
		var loadpage = null;
		loadpage = document.getElementById("comment");
		if (loadpage != null) {
			loadpage.innerHTML  = "<img src='http://webboard.truelife.com/comment/images/ajax-loader017.gif'/>";
			loadXMLDoc(x);
		}
	}

	this.banComment = function (guid) {
		var ban = null;
		ban = document.getElementById("bancomment" + guid);
		if (ban != null) {
			ban.innerHTML = "<img src='http://webboard.truelife.com/comment2/response/responseBanComment.jsp?commentID="+guid+"' width='15' height='15'/>";
		}
	}
}

function funcSubmit()
{
	if(Comment.UpdateBy.value == "")
		{ alert("ใส่ข้อมูลไม่ครบ"); }
	else
	{
		var p = /(.*)(\S+)(.*)/gi;
		var o = document.getElementById("textField").value;
		if(o.match(p)) { 
			document.getElementById("textField").value = validateComment(document.getElementById("textField").value);
			document.getElementById("UpdateBy").value = validateComment(document.getElementById("UpdateBy").value);
			Comment.submit(); 
		}
		else { alert("ใส่ข้อมูลไม่ครบ"); }
	}
	return false;
}

function validateComment(msg){
	var strBuffer = '';
	var character ='';
	var asciiNumber = -1;
	var i =0;
	for (i=0; i < msg.length; i++){
		character = msg.charAt(i);
		try{
			asciiNumber = character.charCodeAt(0);
		}catch(e){
			asciiNumber = -1;
		}
		if 	(asciiNumber > -1){	
			if (asciiNumber > 31 && asciiNumber < 126) {
				strBuffer = strBuffer + character;
			}else if (asciiNumber > 3584 && asciiNumber < 3673) {
				strBuffer = strBuffer + character;
			}
		}
		character = '';
		asciiNumber = -1;
	}
	return strBuffer;
}

function reSizeImg(img){
	var IMAGEWIDTH = 400;
	if (img){
		var w = img.width;
		var h = img.height;
		if (w > IMAGEWIDTH){
			w = IMAGEWIDTH*100/w	;
			h = w * h/100	;
			img.width = IMAGEWIDTH	;
			img.height = h	;
		}
	}	
}
