var ajax = false;
	
if (window.XMLHttpRequest) {
	ajax = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	try {
		ajax = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e1) {
		try {
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) { }
	}
	
}

function addComment(video_id, comment_id) {

	var errors = new Array();
	comment = false;
	name = false;
	
	comment_entered = document.getElementById('comment-box-' + comment_id).value;
	name_entered = document.getElementById('name-box-' + comment_id).value;
	
	if (name_entered.length == 0) {
		errors.push('Enter your name!');
	} else if(name_entered.length < 2) {
		errors.push('Your name is too short!');
	} else if (name_entered.length > 500) {
		errors.push('Your name is too long!');
	} else {
		name = true;
	}
	
	if (comment_entered.length == 0) {
		errors.push('Enter a comment!');
	} else if(comment_entered.length < 2) {
		errors.push('Your comment is too short!');
	} else if (comment_entered.length > 500) {
		errors.push('Your comment is too long!');
	} else {
		comment = true;
	}

	if (comment && name) {
		if (ajax) {
			ajax.open('post', 'http://tapespace.com/processors/addcomments.php', true);
			
			ajax.onreadystatechange = function () {
				
				
				if ((ajax.readyState == 4) && (ajax.status == 200)) {	
					var outcome = ajax.responseText;
					
					//document.getElementById('comment-box').value = '';
					video_comments = document.getElementById('video-comments').innerHTML;
					document.getElementById('video-comments').innerHTML = parseInt(video_comments) + 1;
					

					document.getElementById('comment-box-' + comment_id).value = '';
					document.getElementById('name-box-' + comment_id).value = '';
					
					
					document.getElementById('comments-area').innerHTML = outcome;
					
					if (comment_id == 0) {
						document.getElementById('outcome-box-0').style.display = 'none';
						document.getElementById('indicator-0').style.display = 'none';
						document.getElementById('comment-button-0').disabled = false;
					}
					
				
										
				}
			};
			
			var post_data = 'video_id=' + video_id + '&comment_id=' + comment_id + '&name=' + name_entered + '&comment=' + comment_entered;
			
			ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			ajax.send(post_data);

		}
	} else {

		var outcome_box = document.getElementById('outcome-box-' + comment_id)
		
		var error = '';
		
		for (i=0; i<errors.length; i++) {
			error += errors[i];
			if (i != errors.length) error += '<br />';
		}
		
		outcome_box.innerHTML = error;
		outcome_box.style.display = '';
	}
}

function toggleReply(video_id, comment_id) {
			
	/*var comment_replies = document.getElementById('comment-replies-' + comment_id);
	if (comment_replies.innerHTML == '') {
		comment_replies.style.display = '';
		if (ajax) { 
			ajax.open('post', 'showreply.php', true);
			
			ajax.onreadystatechange = function() {
				if ((ajax.readyState == 4) && (ajax.status == 200)) {		
					var outcome = ajax.responseText;
					document.getElementById('comment-replies-' + comment_id).innerHTML = outcome;			
				}
			};
			
			var post_data = 'video_id=' + video_id + '&comment_id=' + comment_id;
			
			ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			ajax.send(post_data);

	
		}
	} else {
		comment_replies.style.display = (comment_replies.style.display != 'none' ? 'none' : '' );
	}*/
			
	var comment_replies = document.getElementById('comment-replies-' + comment_id);
	//comment_replies.style.display = (comment_replies.style.display != 'none' ? 'none' : '' );
	if (comment_replies.style.display != 'none') {
		comment_replies.style.display = 'none';
		document.getElementById('toggle-reply-' + comment_id).innerHTML = 'show replies';
	} else {
		comment_replies.style.display = '';
		document.getElementById('toggle-reply-' + comment_id).innerHTML = 'hide replies';
	}
	
}

/*function showComments(video_id, pages, page) {
	if (ajax) { 
		ajax.open('post', 'showcomments.php', true);
		
		ajax.onreadystatechange = function() {
				if ((ajax.readyState == 4) && (ajax.status == 200)) {		
					var outcome = ajax.responseText;
					document.getElementById('comments-area').innerHTML = outcome;
					document.getElementById('response').style.display = 'none';
							
				}
			};
		
		var post_data = 'video_id=' + video_id + '&pages=' + pages + '&page=' + page;
		
		ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		ajax.send(post_data);

	}
}*/


function toggleReplyBox(video_id, comment_id) {

	var replybox = document.getElementById('reply-' + comment_id);
	
	if (replybox.innerHTML == '') {
		replybox.style.display = '';
		replybox.innerHTML =  '<div style="width:100%"><div class="outcome" id="outcome-box-' + comment_id + '" style="display:none"></div><div class="box-name-reply">Name:</div><input type="text" id="name-box-' + comment_id + '" size="20" maxlength="20" /><div class="box-name-reply" style="margin-top:5px;">Reply:</div><textarea class="inputbox" id="comment-box-' + comment_id + '" rows="4" style="width:80%"></textarea><br /><input class="button" id="comment-button-' + comment_id + '" type="button" value="add reply" onclick="addComment(\'' + video_id + '\', \'' + comment_id + '\');" /><span id="indicator-' + comment_id + '" style="display:none;"><img src="http://tapespace.com/images/activity.gif" alt="activity" /></span></div>';
	} else {
		replybox.style.display = (replybox.style.display != 'none' ? 'none' : '' );
	}

}

function toggleEmail() {
	var emailbox = document.getElementById('email');
	emailbox.style.display = (emailbox.style.display != 'none' ? 'none' : '' );
}

