function swap_comment()
{
	html = '<a href="javascript:swap_main()"><img src="/images/arrow_undo.gif"></a>';
	html +='New comment: <input type="text" id="commentT" maxlength="255" size="49">';
	html +='<input class="postbtn" type="button" value="Post" onclick="javascript:prepareCommentT(document.getElementById(\'commentT\').value)">';
	
	document.getElementById("toolbar").innerHTML = html;
    swap_comment_mainbox();
}

function swap_main()
{
	html ='<a href="javascript:check_rating()"><img src="'+getRating(1)+'"><img src="'+getRating(2)+'"><img src="'+getRating(3)+'"><img src="'+getRating(4)+'"><img src="'+getRating(5)+'"> Rate this Clip</a>';
	html +='&nbsp;&nbsp;&nbsp;<a href="javascript:check_comment_toolbar()"><img alt="" align="middle" src="/images/comment.gif"/> Post a Comment</a>';
	html += '&nbsp;&nbsp;&nbsp;<a href="javascript:s2af_popup()"><img alt="" align="top" src="/images/email.gif"/> Send to a Friend</a>';
	
	document.getElementById("toolbar").innerHTML = html;
}


function check_comment_toolbar()
{
	if(login==0)
		swap_login();
	else
		swap_comment();
}

function check_mainbox()
{	
	if(login==0)
		swap_login_mainbox();
	else
		swap_comment_mainbox();
}
function swap_comment_mainbox()
{	
	html ='New comment: <input type="text" id="commentM" maxlength="255" size="49">';
	html +='<input class="postbtnm" type="button" value="Post" onclick="javascript:prepareCommentM(document.getElementById(\'commentM\').value)">';
	
	document.getElementById("maincommentinput").innerHTML = html;
}

function setCommentHandlers(error,success, repeat)
{
	commentErrorH = error;
	commentSuccessH = success;
	commentRepeatPostH = repeat;
}

function post_comment_error_M()
{
	html = '<img onClick="javascript:swap_comment_mainbox()" src="/images/arrow_undo.gif">';
	html +=' Post failed, please try again later.';
	
   document.getElementById('maincommentinput').innerHTML = html;
}
function post_comment_repeat_post_M()
{
	html = '<a href="javascript:swap_comment_mainbox()"><img src="/images/arrow_undo.gif"><\/a>';
	html +=' Post failed, you must wait 5 minutes before you can comment on this video again.';
	
   document.getElementById('maincommentinput').innerHTML = html;
}
function post_comment_success_M()
{
	// this is a hack to work for IE, having a <a > tag fails. worked around by using onclick event instead
	html = '<img onClick="javascript:swap_comment_mainbox()" src="/images/arrow_undo.gif"> Thank you for your comment';

   document.getElementById('maincommentinput').innerHTML = html;
}

function post_comment_error_T()
{
	html = '<a href="javascript:swap_main()"><img src="/images/arrow_undo.gif"><\/a>';
	html +=' Posting comment failed, try again later.';
	
   document.getElementById('toolbar').innerHTML = html;
}
function post_comment_repeat_post_T()
{
	html = '<a href="javascript:swap_main()"><img src="/images/arrow_undo.gif"><\/a>';
	html +=' Post failed, you must wait 5 minutes before you can comment on this video again.';
	
   document.getElementById('toolbar').innerHTML = html;
   post_comment_repeat_post_M();
}
function post_comment_success_T()
{
	// this is a hack to work for IE, having a <a > tag fails. worked around by using onclick event instead
	html = '<a href="javascript:swap_main()"><img onClick="javascript:swap_main()" src="/images/arrow_undo.gif"></a> Thank you for your comment';

   document.getElementById('toolbar').innerHTML = html;
   post_comment_success_M();
}

// send comment, user and fingerprint is sent via cookies
// receive XML response - error 
//						- or full comment info

// use xml comments to redraw the comment boxes.

// have comments populated normally with php so noscripters work
// use id tags in javascript to replace new content
// how do we add elements in javascript - make the list grow at runtime?

function postComment(id,videoID,comment,element)
{
	if(comment.length <= 1)
	{
		document.getElementById(element).innerHTML = "Comment too short";
		return;
	}
	else if(comment.length >= 255)
	{
		document.getElementById(element).innerHTML = "Comment too long";
		return;
	}
	document.getElementById(element).innerHTML = "Posting comment...";
	

	if (window.XMLHttpRequest) // Mozilla, Safari, ... 
	{
		http_request = new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject) // IE
	{
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	http_request.onreadystatechange = handlePostCommentResponse;     
	url='/postcomment.php';
	data = 'id='+id;
	data += '&videoID='+videoID;
	data += '&comment='+escape(comment);
	http_request.open('POST', url,true);
	http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	http_request.send(data);
}

function handlePostCommentResponse()
{ 
	if (http_request.readyState == 4) //response received
    { 
    	if (http_request.status==200)
		{
			var tmp = http_request.responseText;
			if(tmp.match(/error/))
			{
				// error
				//document.getElementById('toolbar').innerHTML = tmp;
				
				if(tmp.match(/repeat/))
					commentRepeatPostH();
				else
					commentErrorH();
			}
			else
			{
				var sections  = tmp.split('|%|%%|');
				
				document.getElementById('latestContainer').innerHTML = sections[0];
				document.getElementById('commentContainer').innerHTML = sections[1];
			    commentSuccessH();
			    //document.getElementById('toolbar').innerHTML = tmp;
			}
		}
		else
		{
			// error
			commentErrorH();
		}	
	}
}