//Generate HTML for the comments feed section on all article pages
//called after the xml response object has been received
//inserts HTML code into a hardcoded div id "xmlfeed_article_comments"
function generateHTMLArticleComments(req, num) {
	/***** Feed to parse - http://forum.omy.sg/omy_viewcomment.php?threadid=28 *****/
	/***** XML response object - /common/php/curl_proxy.php?url=http%3A//forum.omy.sg/omy_viewcomment.php?threadid=28 *****/
	/***** HTML to generate *****
	  <a name="comment"></a>
	  <!-- comments -->
	  <div id="commentdisplay">
		<p class="right"><a href="#" class="hilight">View All</a> <a href="javascript:postcomment();" class="hilight">Post a Comment</a></p>
		<!-- Pagination -->
		<h5>Comments</h5>
		<!-- END Pagination -->
		<!-- table of comments -->
		<div class="flowthis"><table cellpadding="0" cellspacing="0">
		  <tbody>
		  
		  <tr>
			<td class="icon"><img src="/common/imgs/misc/user_thumbnail.gif" border="0" class="thumbnail" /></td>
			<td>
			  <p class="postedby">Posted by: <a href="#">yeoweelee</a> on Tue Jul 03 10:29:05 SGT 2007</p>
			  <p>comment content blah blah blah</p>
			</td>
		  </tr>
		  
		  </tbody>
		</table></div>
		<!-- END table of comments -->
	  </div>
	  <!-- END comments -->
	*****************************/
	
	var xmlDoc = req.responseXML;
	//alert(xmlDoc.getElementsByTagName('comment').length);
	
	//even if no comments, parse the XML for the thread url
	var aryStr = new Array(); //the array that will store all the HTML snippets
	var aryThreadNode = xmlDoc.getElementsByTagName("thread");
	if (aryThreadNode[0].hasChildNodes()) {
		for (var j=0; j<aryThreadNode[0].childNodes.length; j++) { //for the child nodes of this <comment> node
			var aryThreadChild = aryThreadNode[0].childNodes[j];
			if (aryThreadChild.nodeType==1) { //#text
				switch (aryThreadChild.nodeName) {
					case "url":
						var thread_url = (aryThreadChild.hasChildNodes()) ? aryThreadChild.childNodes[0].nodeValue : "";
						break;
					case "threadid":
						var thread_threadid = (aryThreadChild.hasChildNodes()) ? aryThreadChild.childNodes[0].nodeValue : "";
						break;
					default:
						break;
				}
			}
		}
	}
	
	//if no user comments for this thread yet, dont show HTML section,
	//and also link the top "forum" button to the forum thread.
	if (xmlDoc.getElementsByTagName('comment').length==0) {
		//change the "forum" button in mediactrl to link to forum thread instead of anchoring down
		var aryAnchor = document.getElementById("mediactrl").getElementsByTagName("a");
		for (var i=0; i<aryAnchor.length; i++) {
			if (aryAnchor[i].href.indexOf("#comment")!=-1) {
				aryAnchor[i].href = thread_url;
			}
		}
		return; //no comments for this thread, dont show comment section at all
	}
	
	var headerStr='';
	headerStr+='	  <a name="comment"><\/a>';
	headerStr+='	  <!-- comments -->';
	headerStr+='	  <div id="commentdisplay">';
	headerStr+='		<p class="right"><a href="'+thread_url+'" class="hilight">View All<\/a> <a href="javascript:postcomment('+thread_threadid+');" class="hilight">Post a Comment<\/a><\/p>';
	headerStr+='		<!-- Pagination -->';
	headerStr+='		<h5>Comments<\/h5>';
	headerStr+='		<!-- END Pagination -->';
	headerStr+='		<!-- table of comments -->';
	headerStr+='		<div class="flowthis"><table cellpadding="0" cellspacing="0">';
	headerStr+='		  <tbody>';
	
	var aryCommentNode = xmlDoc.getElementsByTagName("comment");
	var numOfCommentsToShow = (num && num<=aryCommentNode.length) ? num : aryCommentNode.length; //if "num" specified AND not out of bounds, use it to limit num of comments displayed
	for (var i=0; i<numOfCommentsToShow; i++) {
		for (var j=0; j<aryCommentNode[i].childNodes.length; j++) { //for the child nodes of this <comment> node
			var aryCommentChild = aryCommentNode[i].childNodes[j];
			if (aryCommentChild.nodeType==1) { //#text
				switch (aryCommentChild.nodeName) {
					case "name":
						var comment_name = (aryCommentChild.hasChildNodes()) ? aryCommentChild.childNodes[0].nodeValue : "";
						break;
					case "avatarSrc":
						var comment_avatarSrc = (aryCommentChild.hasChildNodes()) ? aryCommentChild.childNodes[0].nodeValue : "/common/imgs/misc/user_thumbnail.gif";
						break;
					case "profileUrl":
						var comment_profileUrl= (aryCommentChild.hasChildNodes()) ? aryCommentChild.childNodes[0].nodeValue : "";
						break;
					case "datetime":
						var comment_datetime = (aryCommentChild.hasChildNodes()) ? aryCommentChild.childNodes[0].nodeValue : "";
						break;
					case "content":
						var comment_content = (aryCommentChild.hasChildNodes()) ? aryCommentChild.childNodes[0].nodeValue : "";
						break;
					default:
						break;
				}
			}
		}
		
		var str='';
		str+='		  <tr>\n';
		str+='			<td class="icon"><a href="'+comment_profileUrl+'"><img src="'+comment_avatarSrc+'" border="0" class="thumbnail" \/><\/a><\/td>\n';
		str+='			<td>\n';
		str+='			  <p class="postedby">Posted by: <a href="'+comment_profileUrl+'">'+comment_name+'<\/a> on '+comment_datetime+'<\/p>\n';
		str+='			  <p>'+comment_content+'<\/p>\n';
		str+='			<\/td>\n';
		str+='		  <\/tr>\n';
		
		aryStr.push(str);
	}
	
	var footerStr='';
	footerStr+='		  <\/tbody>';
	footerStr+='		<\/table><\/div>';
	footerStr+='		<!-- END table of comments -->';
	footerStr+='	  <\/div>';
	footerStr+='	  <!-- END comments -->';

	//append the comment to the DOM
	document.getElementById("xmlfeed_article_comments").innerHTML = headerStr + aryStr.join("") + footerStr;
}
