var UserName = '';
var BlogId = 0;

function ChangeDisplay(val)
{
	var expireDate = new Date(); 
	var expireString = ""; 
	expireDate.setTime(expireDate.getTime() + (1000 * 60 * 60 * 24 * 7)); 

//	SetCookie("comment_filter", val, expireDate, "/", "qj.net", '');

	for(i = 0; i < Cid.length; i++)
	{
		id = Cid[i].toString();

		if(CStars[i] < val)
		{
			DisableComment(id);
		}
		else
		{
			EnableComment(id);
		}

	}
}

function ChangeByDisplayUser(val)
{
	if(val == 1)
	{
		for(i = 0; i < Cid.length; i++)
		{			
			if(!inArray(Cid[i], CommIdsReg))
			{
				HideComments(Cid[i]);
			}
			else
			{
				DisplayComments(Cid[i]);
			}
		}
	}
	else if(val == 2)
	{
		for(i = 0; i < Cid.length; i++)
		{			
			if(!inArray(Cid[i], ComIdsNotReg))
			{
				HideComments(Cid[i]);
			}
			else
			{
				DisplayComments(Cid[i]);
			}
		}
	}
	else
	{
		for(i = 0; i < Cid.length; i++)
		{			
			DisplayComments(Cid[i]);			
		}
	}
}

function HideComments(CommId)
{
	$('comm_' + CommId).style.display = 'none';
}

function DisplayComments(CommId)
{
	$('comm_' + CommId).style.display = 'block';
}

function inArray(needle, haystack)
 {
	for (var i in haystack)
	{
		if (needle == haystack[i])
			return true;
	}
 }

function DisableComment(id)
{	
	if($('b' + id).className.indexOf('_disabled') == '-1')
	{
		$('b' + id).className = $('b' + id).className + '_disabled';
		$('star' + id).src = $('star' + id).src.replace(BlogId, "disabled");
		$('no' + id).src = $('no' + id).src.replace(BlogId, "disabled");
		$('yes' + id).src = $('yes' + id).src.replace(BlogId, "disabled");
		$('ar' + id).src = $('ar' + id).src.replace("up.gif", "down.gif").replace(BlogId, "disabled");

		if ($('ccd' + id) != null)
		{
			$('ccd' + id).className += '_disabled';
		}
		else
			$('cc' + id).className += '_disabled';

	}

	$('cc' + id).style.display = 'none';

}

function EnableComment(id)
{
	if ((pos = $('b' + id).className.indexOf('_disabled')) > 0)
	{
		$('b' + id).className = $('b' + id).className.substr(0, pos);
		$('cc' + id).style.display = 'block';
		$('star' + id).src = $('star' + id).src.replace("disabled", BlogId);
		$('no' + id).src = $('no' + id).src.replace("disabled", BlogId);
		$('yes' + id).src = $('yes' + id).src.replace("disabled", BlogId);
		$('ar' + id).src = $('ar' + id).src.replace("down.gif", "up.gif").replace("disabled", BlogId);

		if ($('ccd' + id) != null)
		{
			$('ccd' + id).className = $('ccd' + id).className.substr(0, $('ccd' + id).className.indexOf('_disabled'));
		}
		else
		{
			$('cc' + id).className = $('cc' + id).className.substr(0, $('cc' + id).className.indexOf('_disabled'));
		}

	}
		$('cc' + id).style.display = 'block';
}

function ShowComment(id)
{
	if($('cc' + id).style.display == 'none')
	{
		$('cc' + id).style.display = 'block';
		$('ar' + id).src = $('ar' + id).src.replace("down.gif", "up.gif");
	}
	else
	{
		$('cc' + id).style.display = 'none';
		$('ar' + id).src = $('ar' + id).src.replace("up.gif", "down.gif");
	}
}

function ShowReplyForm(CommentId, UsrName, ArticleId, BlgId)
{
	UserName = UsrName;	
	BlogId = BlgId;
	
	new Ajax.Request
	(
		'/MainModules/Ajax/CanPostMessage.php',
		{
			method: 'post',
			parameters: '&aid='+ ArticleId + '&id=' + CommentId,
			onSuccess: PostShowReplyFrom,
			onFailure: FailedSubmitReply
		}
	);	
	return false;
}

function PostShowReplyFrom(response)
{
	eval("ResponseVal=" + response.responseText);
	CommentId = ResponseVal.Id;
	ArticleId = ResponseVal.ArticleId;

	if (!ResponseVal.CanPost)
	{
		DisplayMessage("You have to wait before you can post comments again in this article.", 'err' + CommentId);
		return false;
	}
	
	if(!ResponseVal.NotLogged)
	{
		DisplayMessage("You have to Login first!", 'err' + CommentId);
		return false;
	}
	
//	MyForm = document.createElement("form");
//	MyForm.action = "#comments";
//	MyForm.method = "POST";
		mytbl = document.createElement("table");
		mytblbody = document.createElement("tbody");
		row = document.createElement("tr");
		cell = document.createElement("td");
		cell.innerHTML = "Your Name:";
		row.appendChild(cell);
		cell = document.createElement("td");
		inp = document.createElement("input");
		inp.type = "text";
		inp.size = "52";
		inp.id = "name" + CommentId;
		inp.name = "name" + CommentId;
		inp.value = UserName;
	
		inp.className = "input_odd";
		cell.appendChild(inp);
		row.appendChild(cell);
		mytblbody.appendChild(row);
	
		
		row = document.createElement('tr');
		cell = document.createElement('td');
		cell.innerHTML = 'Subject:';
		row.appendChild(cell);
		cell = document.createElement("td");
		inp = document.createElement("input");
		inp.type = "text";
		inp.size = "52";
		inp.id = "subject" + CommentId;
		inp.name = "subject" + CommentId;
		inp.className = "input_odd";
		cell.appendChild(inp);
		row.appendChild(cell);
		mytblbody.appendChild(row);
	
		row = document.createElement('tr');
		cell = document.createElement('td');
		cell.innerHTML = 'Comment:';
		row.appendChild(cell);
		cell = document.createElement("td");
		inp = document.createElement("textarea");
		inp.cols = "50";
		inp.rows = "4";
		inp.id = "comment" + CommentId;
		inp.name = "comment" + CommentId;
		inp.className = "input_odd";
		cell.appendChild(inp);
		row.appendChild(cell);
		mytblbody.appendChild(row);
	
		row = document.createElement('tr');
		cell = document.createElement("td");
		row.appendChild(cell);
		cell = document.createElement("td");
		cell.setAttribute("align", "right");
		inp = document.createElement("input");
		inp.type = "submit";
		inp.value = 'Submit reply';
		inp.id = "sr" + CommentId;
		inp.name = "sr" + CommentId;
		inp.className = "input_odd";
	
		inp.setAttribute("oclpar", CommentId);
		inp.onclick = function () {
			SubmitReply(ArticleId, this.getAttribute("oclpar"));
		}
	
		cell.appendChild(inp);
		
		var span = document.createElement("span");
	
		span.innerHTML = '&nbsp;';
	
		cell.appendChild(span);
	
		inp = document.createElement("input");
	
		inp.type = "button";
		inp.value = 'Cancel';
	
		inp.id = "cancelreply" + CommentId;
		inp.name = "cancelreply" + CommentId;
	
		inp.className = "input_odd";
	
	 	cell.appendChild(inp);
		row.appendChild(cell);
		mytblbody.appendChild(row);
	
		mytbl.appendChild(mytblbody);
		mytbl.setAttribute("bgcolor", '#f2f2f2');

//	MyForm.appendChild(mytbl);
		
	$('replyForm' + CommentId).innerHTML = '';
	$('replyForm' + CommentId).appendChild(mytbl);
	$('replyForm' + CommentId).setAttribute("bgcolor", '#f2f2f2');
	
	inp.setAttribute("oclpar", CommentId);
	inp.onclick = function () {
		Effect.SlideUp('RollDown' + CommentId);
	}

	Effect.SlideDown('RollDown' + CommentId);

	return false;
}

function ShowReplies(id)
{
	if ($('Replies'+id).style.display == 'block')
	{
		$('Replies'+id).style.display = 'none';
		$('replyLink'+id).innerHTML = 'Show Replies';
	}
	else
	{
		$('Replies'+id).style.display = 'block';
		$('replyLink'+id).innerHTML = 'Hide Replies';
	}
	return false;
}

function SubmitReply(ArticleId, Id)
{			
	//$('sr' + Id).disabled = true;
		
	name = $('name' + Id).value.replace(new RegExp("\\+",'g'), "__PLUS__");
	name = name.replace(new RegExp("\\&",'g'), "__AND__");
	name = name.replace(new RegExp("\\=",'g'), "__EQUAL__");

	subject = $('subject' + Id).value.replace(new RegExp("\\+",'g'), "__PLUS__");
	subject = subject.replace(new RegExp("\\&",'g'), "__AND__");
	subject = subject.replace(new RegExp("\\=",'g'), "__EQUAL__");

	comment = $('comment' + Id).value.replace(new RegExp("\\+",'g'), "__PLUS__");
	comment = comment.replace(new RegExp("\\&",'g'), "__AND__");
	comment = comment.replace(new RegExp("\\=",'g'), "__EQUAL__");
		
	new Ajax.Request
	(
		'/MainModules/Ajax/MessageActions.php',
		{
			method: 'post',
			parameters: '&aid=' + ArticleId +'&id=' + Id + '&name=' + escape(name) + '&subject=' + escape(subject) + '&comment=' + escape(comment) + '&blogid=' + BlogId, 
			onSuccess: PostSubmitReply5,
			onFailure: FailedSubmitReply
		}
	);
}


function SubmitComment(ArticleId, BlgId)
{
	BlogId = BlgId;
	
	if (Trim($('poster').value) == '')
	{
		alert('User name is empty');
		return false;
	}
	
	name = $('poster').value.replace(new RegExp("\\+",'g'), "__PLUS__");
	name = name.replace(new RegExp("\\&",'g'), "__AND__");
	name = name.replace(new RegExp("\\=",'g'), "__EQUAL__");

	subject = $('description').value.replace(new RegExp("\\+",'g'), "__PLUS__");
	subject = subject.replace(new RegExp("\\&",'g'), "__AND__");
	subject = subject.replace(new RegExp("\\=",'g'), "__EQUAL__");

	comment = $('content').value.replace(new RegExp("\\+",'g'), "__PLUS__");
	comment = comment.replace(new RegExp("\\&",'g'), "__AND__");
	comment = comment.replace(new RegExp("\\=",'g'), "__EQUAL__");
		
	$('SubmitCommentBtn').disabled = true;
	
	new Ajax.Request
	(
		'/MainModules/Ajax/MessageActions.php',
		{
			method: 'post',
			parameters: '&aid=' + ArticleId +'&id=' + 0 + '&name=' + escape(name) + '&subject=' + escape(subject) + '&comment=' + escape(comment) + '&blogid=' + BlogId,
			onSuccess: PostComment,
			onFailure: FailedPostComment
		}
	);
}

function PostComment(response)
{	
	eval("ResponseVal = " + response.responseText);
	
	if(ResponseVal.Error == 1)
	{
		$('SubmitCommentBtn').disabled = true;
		window.location.href = ResponseVal.Referer + '?#rpl_' + ResponseVal.InsertedId; 
		self.close();
//		
//			MyDiv = document.createElement("div");
//			MyDiv.className = 'com';
//			MyDiv.setAttribute("align", "center");			
//				MyDiv2 = document.createElement("div");
//				MyDiv2.className = 'bar';
//				MyDiv2.id = 'b' + ResponseVal.InsertedId;
//				MyDiv2.setAttribute("align", "center");
//					MyTable = document.createElement("table");
//					MyTable.style.width = '100%';
//					MyTable.cellSpacing = '0';
//					MyTable.cellPadding = '0';
//					MyTable.vAlign  = 'top';
//						row = document.createElement("tr");
//							cell = document.createElement("td");
//							cell.className = 'arrows';
//								MyImg = document.createElement("Img");
//								MyImg.src = 'http://img.qj.net/img/comments/' + BlogId + '/up.gif'
//								MyImg.id = 'ar' + ResponseVal.InsertedId;
//								MyImg.onclick = function () {
//									ShowComment(ResponseVal.InsertedId);
//								}							
//								cell.appendChild(MyImg);
//							row.appendChild(cell);
//							
//							cell = document.createElement("td");
//							cell.className = 'comtitle';
//							cell.vAlign  = 'top';
//								MySpan = document.createElement("span");
//								MySpan.className = 'comspan';
//								MySpan.innerHTML = '&nbsp;&nbsp; by <b>' + ResponseVal.UserName + '</b> - 1 min ago';							
//								cell.appendChild(MySpan);
//							row.appendChild(cell);
//							
//							cell = document.createElement("td");
//							cell.className = 'star';
//							cell.vAlign  = 'top';
//							cell.setAttribute("align", "right");
//								MyImg = document.createElement("Img");
//								MyImg.src = 'http://img.qj.net/img/comments/' + BlogId + '/star2.5.gif';							
//								MyImg.style.border = '0';
//								MyImg.id = 'star' + ResponseVal.InsertedId;
//								cell.appendChild(MyImg);
//							row.appendChild(cell);
//							
//							cell = document.createElement("td");
//							cell.className = 'yesno';
//								MyImg = document.createElement("Img");
//								MyImg.src = 'http://img.qj.net/img/comments/' + BlogId + '/yes_disabled.gif';
//								MyImg.id = 'yes' + ResponseVal.InsertedId;
//								MyImg.style.border = '0';
//								MyImg.className = 'combutdisabled';
//								MyImg.onclick = function () {
//									VoteComment(ResponseVal.InsertedId, 1);
//								}	
//								cell.appendChild(MyImg);
//								
//								MyImg = document.createElement("Img");
//								MyImg.src = 'http://img.qj.net/img/comments/' + BlogId + '/no_disabled.gif';
//								MyImg.id = 'no' + ResponseVal.InsertedId;
//								MyImg.style.border = '0';
//								MyImg.className = 'combutdisabled';	
//								MyImg.onclick = function () {
//									VoteComment(ResponseVal.InsertedId, 0);
//								}														
//								cell.appendChild(MyImg);							
//							row.appendChild(cell);
//						MyTable.appendChild(row);
//					MyDiv2.appendChild(MyTable);
//				MyDiv.appendChild(MyDiv2);
//				
//				MyDiv2 = document.createElement("div");
//				MyDiv2.setAttribute("align", "left");	
//				MyDiv2.className = 'comcontent';
//				MyDiv2.id = 'ccd' + ResponseVal.InsertedId;
//					MySpan = document.createElement("span");
//					MySpan.id = 'cc' + ResponseVal.InsertedId;
//						MySpan2 = document.createElement("span");
//						MySpan2.id = 'intelliTXT';
//						MySpan2.innerHTML = '<b>&nbsp;&raquo;&nbsp;' + ResponseVal.Title + '</b><br><br>' + ResponseVal.Comment;
//						MySpan.appendChild(MySpan2);
//					MyDiv2.appendChild(MySpan);
//				MyDiv.appendChild(MyDiv2);
//		$('FullComments').appendChild(MyDiv);
		
		DisplayMessage(ResponseVal.ErrorMessage, 'CommMessage');

		$('PostedYes').style.display = 'block';
		$('PostAComment').style.display = 'none';	
	}
	else
	{
		$('SubmitCommentBtn').disabled = false;
		$('CommMessage').innerHTML = ResponseVal.ErrorMessage;
		$('PostedYes').style.display = 'none';
	}
}

function FailedPostComment()
{
	
}

function PostSubmitReply5(response)
{	
	SubmitProcessRunning = false;
	eval("ResponseVal = " + response.responseText);
		
	DisplayMessage(ResponseVal.ErrorMessage, 'err' + ResponseVal.Id);
		
	if(ResponseVal.Error == 1)
	{			
		
		window.location.href = ResponseVal.Referer + '?#rpl_' + ResponseVal.InsertedId; 
		self.close();
	
		Effect.SlideUp('RollDown' + ResponseVal.Id);

		Prefix_dir = BlogId;

		//$('Replies' + ResponseVal.Id).innerHTML += '<br><div class="bar_reply" id="b' + ResponseVal.InsertedId + '"><table width="100%" cellspacing="0" cellpadding="0" valign="top"><tr><td class="arrows"><Img src="/img/comments/' + Prefix_dir + '/up.gif" id="ar' + ResponseVal.InsertedId + '" onclick="ShowComment' + ResponseVal.InsertedId + '"></td><td valign="top" class="comtitle"><span class="comspan">&nbsp;&nbsp; Re: <b>' + unescape(ResponseVal.UserName) + '</b> - 1 min ago</span></td><td align="right" class="star" valign="top"><Img src="/img/comments/' + Prefix_dir + '/star0.gif" style="border:none" id="star' + ResponseVal.InsertedId + '"></td><td class="yesno"><Img src="/img/comments/' + Prefix_dir + '/yes_disabled.gif" title="I like this comment" style="border:none" id="yes' + ResponseVal.InsertedId + '"  class="combut"><Img src="/img/comments/' + Prefix_dir + '/no_disabled.gif" id="no' + ResponseVal.InsertedId +'" style="border:none"  title="I dislike this comment" class="combut"></td></tr></table></div><div class="replycontent" id="cc' + ResponseVal.InsertedId + '" align=left><b>&nbsp;&raquo;&nbsp;' + unescape(ResponseVal.Title) + '</b><br><br>' + unescape(ResponseVal.Comment) + '</div>';
	}
	else
	{
		$('sr' + id).disabled = false;
	}
}


function FailedSubmitReply()
{

}


function VoteComment(CommentId, Vote)
{
	new Ajax.Request
	(
		'/MainModules/Ajax/VoteComment.php',
		{
			method: 'post',
			parameters: '&id=' + CommentId + '&Vote=' + Vote,
			onSuccess: PostVoteComment,
			onFailure: FailedVoteComment
		}
	);	
}

function PostVoteComment(response)
{
	eval('ResponseVal=' + response.responseText);
	
	if (ResponseVal.Error == 1) // success
	{
		src = $('yes' + ResponseVal.Id).src.replace("yes.gif", "yes_disabled.gif");
		$('yes' + ResponseVal.Id).src = src;
		$('yes' + ResponseVal.Id).style.cursor = '';
		$('yes' + ResponseVal.Id).onclick = '';
		src = $('no' + ResponseVal.Id).src.replace("no.gif", "no_disabled.gif");
		$('no' + ResponseVal.Id).src = src;
		$('no' + ResponseVal.Id).style.cursor = '';
		$('no' + ResponseVal.Id).onclick = '';
		
		pos = $('star' + ResponseVal.Id).src.indexOf("star");
		src = $('star' + ResponseVal.Id).src.substr(0, pos) + ResponseVal.UpdateStarImage;
		
		$('star' + ResponseVal.Id).src = src;

		for(i = 0; i < Cid.length; i++)
		{
			if (ResponseVal.Id == Cid[i])
			{
				CStars[i] = ResponseVal.StarValue;
				if (CStars[i] >= $('filter').value)
				{
					EnableComment(ResponseVal.Id);
				}
				else
				{
					DisableComment(ResponseVal.Id);
				}
				break;
			}
		}

	}
}

function FailedVoteComment()
{
	
}
