function sendComment() {
	var comment = document.getElementById('comment').value;
	if(comment.length > 255) {
		alert("Your comment is too long.");
	} else {
		AJAXRequest("POST","/comments.php","cmd=send&comment="+comment+"&rid="+document.getElementById('rid').value+"&type="+document.getElementById('item').value, sendCommentHandler, true);
	}
}

function sendCommentHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')) {
		switch(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			case '0':
				document.getElementById('page_status').innerHTML = '<span style="color: #069">Thank you for your comment</span>';
				document.getElementById('csb').value = 'Comment posted!';
				document.getElementById('csb').disabled = true;
				document.getElementById('comment').disabled = true;
				document.getElementById('comment').style.background = "#eef";
				break;
			case '1':
				alert("An error occured. The comment has not been sent.");
				break;
			case '-1':
				alert("You cannot send empty comments.");
				break;
		}
	}
}

function getComments(page,type) {
	AJAXRequest("POST","/comments.php","cmd=list&page="+page+"&rid="+document.getElementById('rid').value+"&type="+type,getCommentsHandler,true);
}

function getCommentsHandler(xmlObject) {
	var display = document.getElementById('comments');
	var html='<table border="0" cellspacing="0" style="border: 0px; font-size: small; width: 350;">';
	var id = 0;
	var name = '';
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('comment')) {
			var comments = xmlObject.responseXML.getElementsByTagName('comment');
			for(var i=0; i<comments.length; i++) {
				id = comments[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				name = comments[i].getElementsByTagName('name')[0].firstChild.nodeValue;
				text = (comments[i].getElementsByTagName('text')[0].firstChild)?comments[i].getElementsByTagName('text')[0].firstChild.nodeValue:'';
				timestamp = comments[i].getElementsByTagName('timestamp')[0].firstChild.nodeValue;
				rating = comments[i].getElementsByTagName('rating')[0].firstChild.nodeValue;
				if(comments[i].getElementsByTagName('rated')[0].firstChild.nodeValue==1) {
					ratingBtns = '<img src="/images/dcv.png" alt="disabled" />';
				} else {
					ratingBtns = '<a href="javascript:rateComment('+id+',-1)"><img src="/images/down.png" alt="down" border="0" /></a>&nbsp;<a href="javascript:rateComment('+id+',1)"><img alt="up" border="0" src="/images/up.png" /></a>&nbsp;';
				}
				if(comments[i].getElementsByTagName('ulevel')[0].firstChild.nodeValue==2) {
					adminbtns = '<a href="javascript:markAsSpamComment('+id+')">mark as spam</a>';
				} else if(comments[i].getElementsByTagName('ulevel')[0].firstChild.nodeValue==1) {
					adminbtns = '<a href="javascript:delComment('+id+')">[ delete ]</a>';
				} else {
					adminbtns = '';
				}
				if(rating > 0) {
					ratingcolor = '069';
				} else if(rating == 0) {
					ratingcolor = 'aaf';
				} else {
					ratingcolor = 'c00';
				}
				html += '<tr id="comment_head_'+id+'" style="border-bottom: 1px solid #ccc"><td><div style="display:inline;"><a href="/users/'+name+'">'+name+'</a> @ '+timestamp+'</div><div style="display:inline;float:right; color:#'+ratingcolor+'" id="crating_'+id+'">( '+rating+' )</div><div style="display:inline;float:right" id="comment_rating_btns_'+id+'">'+ratingBtns+'</div>&nbsp;'+adminbtns+'</td></tr><tr style="background: #eef"><td id="comment_body_'+id+'">'+text+'</td></tr>';
			}
			html += '</table>';
			html += xmlObject.responseXML.getElementsByTagName('pages')[0].firstChild.nodeValue;
			display.innerHTML = html;
		} else {
			if(xmlObject.responseXML.getElementsByTagName('status')) {
				display.innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
			}
		}
	}
}

function rateComment(id,n) {
	document.getElementById("comment_rating_btns_"+id).innerHTML = '<img src="/images/dcv.png" border="0" />';
	AJAXRequest("POST","/comments.php","cmd=rate&n="+n+"&id="+id,rateCommentHandler,true);
}

function rateCommentHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue!=0) {
		document.getElementById('page_status').innerHTML = '<span style="color: #c00">'+xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue+'</span>';
	} else {
		rating = xmlObject.responseXML.getElementsByTagName('rating')[0].firstChild.nodeValue;
		cid = xmlObject.responseXML.getElementsByTagName('id')[0].firstChild.nodeValue;
		document.getElementById('crating_'+cid).innerHTML = '( '+rating+' )';
		if(rating > 0) {
					document.getElementById('crating_'+cid).style.color = '#069';
				} else if(rating == 0) {
					document.getElementById('crating_'+cid).style.color = '#aaf';
				} else {
					document.getElementById('crating_'+cid).style.color = '#c00';
				}
	}
}

function markAsSpamComment(id) {
	AJAXRequest("POST","/comments.php","cmd=spam&id="+id,markAsSpamCommentHandler,true);
}

function markAsSpamCommentHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue==0) {
		cid = xmlObject.responseXML.getElementsByTagName('id')[0].firstChild.nodeValue;
		document.getElementById('comment_body_'+cid).innerHTML = '<b>Marked as spam</b>';
		document.getElementById('comment_body_'+cid).style.background = '#fee';
		document.getElementById('comment_head_'+cid).style.display='none';
	}
}
