// addComment
// ---
// adds a video comment

function ajaxComment( theForm )
{
	// if no comment is being passed, return an alert
	
	if( theForm.comment.value == "" )
	{
		alert("You must enter a comment");
		return false;
	}
	
	// if there is no comment id, this is a new comment being added
	// otherwise we are editing an existing comment
	
	var ajaxURL = '_comment.php';
	if( theForm.comment_id.value == "" )
	{
		ajaxURL += '?ajax=1&video_id='+theForm.video_id.value+'&comment_text='+escape(theForm.comment.value);
	}
	else
	{
		ajaxURL += '?ajax=1&comment_id='+theForm.comment_id.value+'&comment_text='+escape(theForm.comment.value);
	}
	ajax.open('get', ajaxURL,true);
	ajax.onreadystatechange = commentResponse;
	ajax.send(null);
	
	// clear the form field after the data has been submitted, otherwise 
	// the data will reappear the next time the comment form is called
	
	theForm.comment.value = "";
	
	// anticipate that the ajax call will be successful
	
	var commentCountSpan = document.getElementById('comment_count');
	commentCountSpan.innerHTML = (commentCountSpan.innerHTML * 1) + 1;
}

// commentResponse
// ---
// receives response from posting a comment and hides the comment box on success

function commentResponse()
{
	// defaultResponse parses the response for an error, redirect or success
	if( defaultResponse(ajax) )
	{
		showHidePopbox( 'addcommentbox' );
    getComments( ajax.responseText );
	}
}

// getComments
// ---
// gets the latest list of comments

function getComments( video_id )
{
	ajax.open('get', '_comments.php?video_id='+video_id,true);
	ajax.onreadystatechange = insertComments;
	ajax.send(null);
}

// insertComments
// ---
// insert comments into the video page to refresh the comment data

function insertComments()
{
	if( ajax.readyState == 4 )
	{
		document.getElementById('comments').innerHTML = ajax.responseText;
	}
}

// flagSpam
// ---
// calls ajax script that marks a video comment as spam
// this should also hide the comment or mark is as having been flagged
// and update the comment count (if needed)

function flagSpam( commentID )
{
	var ajaxURL = '_comment.php?ajax=1&spam=1&comment_id='+commentID;
	ajax.open('get', ajaxURL, true);
	ajax.onreadystatechange = flagSpamResponse;
	ajax.send(null);

	document.getElementById("comment" + commentID).style.display = 'none';
	var commentCountSpan = document.getElementById('comment_count');
	commentCountSpan.innerHTML = (commentCountSpan.innerHTML * 1) - 1;
}

// flagSpamResponse
// ---
// the ajax script that marks videos as spam may require one to login
// this will handle that

function flagSpamResponse()
{
	// defaultResponse parses the response for an error, redirect or success
	if( defaultResponse(ajax) )
	{
		// We don't really have anything to do here :)
	}
}
