// JavaScript Document
function checkSubInfo(name)
{
	if(document.getElementById('comment' + name).value == "")
	{
		alert("Please enter comment!");
		document.getElementById('comment' + name).focus();
		return false;
	}
	else document.getElementById('frm' + name).submit();
}
function showComment(listing,user,level,id,login)
{
	var newid = listing +""+ user +""+ level +""+ id
	document.getElementById('subId').value = newid;
	if(document.getElementById('subComment' + newid).style.display == 'block')
		document.getElementById('subComment' + newid).style.display = 'none';
	else		makeRequest('subComments.php?user=' + user + '&listing=' + listing + '&level=' + level + '&login=' + login);
}

function addComment(listing,user,level,id,login)
{
	var newid = listing +""+ user +""+ level +""+ id
	if(document.getElementById('addComment' + newid).style.display == 'block')
		document.getElementById('addComment' + newid).style.display = 'none';
	else		document.getElementById('addComment' + newid).style.display = 'block';

}


function makeRequest(url)
{	
	if (window.XMLHttpRequest) request = new XMLHttpRequest();
	else if (typeof ActiveXObject != "undefined")	request = new ActiveXObject("Microsoft.XMLHTTP");
	sendRequest(url);
}

function sendRequest(url)
{
	request.onreadystatechange = onResponse;
	request.open("GET",url,true);
	request.send(null);
}

function onResponse()
{
	if(checkReadyState(request))
	{
		var response = request.responseXML.documentElement;
		var count = response.getElementsByTagName('count')[0].firstChild.data;
		var total = response.getElementsByTagName('total')[0].firstChild.data;
		var login = response.getElementsByTagName('login')[0].firstChild.data;
		var listing_id = response.getElementsByTagName('listing_id')[0].firstChild.data;
		var str = '<table><tr><td height=5></td></tr><tr><td width=10></td><td>';
		str += '<table>';
		for(i=1;i<count;i++)
		{
			id = response.getElementsByTagName('userid' + i)[0].firstChild.data;
			comid = response.getElementsByTagName('comid' + i)[0].firstChild.data;
			name = response.getElementsByTagName('username' + i)[0].firstChild.data;
			date = response.getElementsByTagName('date' + i)[0].firstChild.data;
			comment = response.getElementsByTagName('comment' + i)[0].firstChild.data;
			level = response.getElementsByTagName('level' + i)[0].firstChild.data;
			total_vote = response.getElementsByTagName('total_vote' + i)[0].firstChild.data;
			
			str += '<tr><td><table bgcolor="#eaf0f5" style="border-top: 1px solid #c0c7cd;"><tr>';
			str += '<td width="20"><img src="images/face.gif" width=15 style="border: 1px solid #ccc;"/></td>';
			str += '<td><font color="#52718B">Post by <strong><a href="" class="c-active">'+name+'</a> on '+date+'</strong></font>&nbsp; | &nbsp;</td>';
			str += '<td align=left><div id="vote' + comid + '">' + total_vote + ' Votes</div></td>';
			str += '</tr></table></td></tr><tr><td>'+comment+'</td></tr>';
			if(login == 1)
			{
				str += '<tr><td><img src="img/arrow.gif" align="absmiddle" /> <a onclick=addComment("'+listing_id + '","' + id + '","' + level + '","' + i +'")>';
				str += 'Reply to this comment</a></td></tr><tr><td><div id="addComment'+listing_id + '' + id + '' + level + '' + i+'" style="display:none">';
				str += '<form action="comments.php" name="frm'+listing_id + '' + id + '' + level + '' + i+'" method=post><table><tr><td valign=middle>';
				str += '<input type="hidden" name="action" value="comment">';
				str += '<input type="hidden" name="listing_id" value="'+listing_id+'">';
				str += '<input type="hidden" name="nameid" value="'+listing_id + '' + id + '' + level + '' + i+'">';
				str += '<input type="hidden" name="level" value="'+level+'">';
				str += '<input type="hidden" name="sup_user" value="'+id+'">';
				str += '<textarea name="comment'+listing_id + '' + id + '' + level + '' + i+'" cols=40 rows=3></textarea> &nbsp; &nbsp;';
				str += '<input type=button value="button" class="button" style="cursor:pointer" onClick=checkSubInfo("'+listing_id + '' + id + '' + level + '' + i +'")>';
				str += '</td></tr></table></form>';
				str += '</div></td></tr>';
			}
			if(total > 0)
			{
				str += '<tr><td><img src="img/arrow.gif" align="absmiddle" /> <a onclick=showComment("'+listing_id + '","' + id + '","' + level + '","' + i +'","'+login+'")>';
				str += 'View	'+ total +' reply to this comment</a></td></tr>';				
				str += '<tr><td><div id="subComment'+listing_id + '' + id + '' + level + '' + i+'" style="display:none"></div></td></tr>';
			}
		}
		var subId = document.getElementById('subId').value;
		document.getElementById('subComment' + subId).style.display = 'block';
		document.getElementById('subComment' + subId).innerHTML = str + "</table>";
	}
}

function checkReadyState(obj)
{
	var subId = document.getElementById('subId').value;
	if(obj.readyState == 0){}
	if(obj.readyState == 1){}
	if(obj.readyState == 2){}
	if(obj.readyState == 3){}
	if(obj.readyState == 4)
		if(obj.status == 200)	return true;
		else if(obj.status == 404)	document.getElementById('subComment' + subId).innerHTML = "File not found";
		else document.getElementById('subComment' + subId).innerHTML = "there was a problem retrieving the XML";
}

