window.addEvent('domready', function() {
	function showComments() {
	    if ($('comments_main')) {
	        var req = new Request({
	            method: 'get',	
	            url: '/inc/comments.php',
	            data: {
				'act'			: 'list',
				'comment_article_id'	: $('comment_article_id').value
			 },
	            onComplete: function(response) {
			$('comments_main').set('html', response);
		    }
	        }).send();
	    }
	}

    /* Submit Comment */
    $('comment_submit').addEvent('click', function(event) {
        // prevent the page from changing
        event.stop();

	// validate user input
	var comment_article_id = $('comment_article_id').value;
	var comment_author = $('comment_author').value;
	var comment_subject = $('comment_subject').value;
	var comment_text = $('comment_text').value;

        if (isNaN(comment_article_id)) return false;
        if (comment_author == '') {
                document.getElementById('comment_author').value = 'Anonymous';
                comment_author = 'Anonymous';
        }
        if (comment_subject == '') {
                alert('Please enter a subject for your comment.');
		$('comment_subject').focus();
                return false;
        }
        if (comment_text == '') {
                alert('Please enter the text for your comment.');
		$('comment_text').focus();
                return false;
        }

        // make the ajax call
        var req = new Request({
            method: 'get',
            url: '/inc/comments.php',
            data: {
			'act'			: 'add',
			'comment_article_id'	: comment_article_id,
			'comment_author'	: comment_author,
			'comment_subject'	: comment_subject,
			'comment_text'		: comment_text
		 },
            update: $('comment_msg'),
            onComplete: function(response) {
		$('comments_form').setStyle('display','none');
		$('comment_msg').setStyle('background','#CFFFFE');
		$('comment_msg').setStyle('border','1px solid #c5c5c5');
		$('comment_msg').set('html', response);
		showComments();
	    }
        }).send();
    });
showComments();
});

