function displayForm() {
	$(postCommentButtonDiv).style.display="none";
	$(postCommentFormDiv).style.display="block";
}

function showComments() {
	$(postCommentFormDiv).style.display="none";
	$(postCommentButtonDiv).style.display="block";
	$(validationText).style.display="none";
}

function clearValidationText()
{
	$(validationText).style.display='none';
}

function buildCommentTable(commentsObj)
{

	commentsDiv = $(viewComments);
   	for(var i = 0; i < commentsDiv.childNodes.length; i++)
   	{   
  	    $(commentsDiv).removeChild(commentsDiv.childNodes[i]);
  	}
  	
	table     = document.createElement("table");
	table.id = 'commentTable';
    tablebody = document.createElement("tbody");
    
    for (var i=0; i<commentsObj.data.length; i++)
    {
    	row = document.createElement("tr");
    	cell = document.createElement("td");
    	cell.className="nameCell";
    	text = document.createTextNode(commentsObj.data[i].name + ":");
       
        cell.appendChild(text);        
        row.appendChild(cell);
        tablebody.appendChild(row);
        
        row = document.createElement("tr");
     	cell = document.createElement("td");
     	cell.className="commentCell";
     	text = document.createTextNode(commentsObj.data[i].comment);
     	
     	cell.appendChild(text);
     	row.appendChild(cell);   
           
        tablebody.appendChild(row);
    }

    table.appendChild(tablebody);
    commentsDiv.appendChild(table);
}

function loadComments() {
	//new Ajax.Updater('viewComments', 'showComments.php', { method: 'get' });
	new Ajax.Request('showComments.php', {method: 'post', 
		onSuccess: function(transport) {
			commentsObj = eval('(' + transport.responseText + ')');
			buildCommentTable(commentsObj);
		}
	});
}

function showValidationError(text)
{
	$(validationText).innerHTML=text; 
	$(validationText).style.display='block';
}

function sendComment() {
	new Ajax.Request('addComment.php', { method: 'post',
		parameters: { userName: $(userName).value, comment: $(comment).value },
		onSuccess: function(transport) { 
			if ( transport.responseText != "") {
				showValidationError(transport.responseText);
			} else {
				loadComments(); 
				showComments();
			} },
		onFailure: function() { alert ("Sorry, I could not add your comment at this time");
								loadComments();
		}
		});
}