var IsIE = navigator.userAgent.toLowerCase().indexOf("msie") >= 0;
var LastOpened = 0;
var ProcessRunning = false;
var SubmitProcessRunning = false;
var SubmitCPR = false;

//Event.observe(window, 'load', DisplayCommentForm);
//
//function DisplayCommentForm()
//{
//	new Ajax.Request
//	(
//		'/modules/articles_module/AJAX/CanPostMessage.php',
//		{
//			method: 'post',
//			parameters: '&aid='+ ArticlesID[0] + '&id=' + 0,
//			onSuccess: ShowCommentForm
//		}
//	);	
//}

//function ShowCommentForm(response)
//{
//	eval("ResponseVal=" + response.responseText);
//	if(ResponseVal.Poster != '')
//	{
//		$('CommentForm').style.display = 'block';
//	}
//	else
//	{
//		$('CommentForm').style.display = 'none';
//		$('NotLogged').innerHTML = 'To post a comment you have to login first.';
//		$('NotLogged').style.color = 'red';
//	}
//}

function ShowReplyForm(id)
{
	new Ajax.Request
	(
		'/modules/articles_module/AJAX/CanPostMessage.php',
		{
			method: 'post',
			parameters: '&aid='+ ArticlesID[0] + '&id=' + id,
			onSuccess: PostShowReplyFrom,
			onFailure: FailedSubmitReply
		}
	);	
	
	return false;
}

function PostShowReplyFrom(response)
{

	eval("ResponseVal=" + response.responseText);
	id = ResponseVal.Id;
	
	if (!ResponseVal.CanPost)
	{
		DisplayMessage("You have to wait before you can post comments again in this article.", 'err' + ResponseVal.Id);
		return false;
	}

	if (ResponseVal.NeedRegister && !ResponseVal.Poster)
	{
	    DisplayMessage("You have to login first in order to reply on this comment.", 'err' + ResponseVal.Id);
	    return false;
	}
	
	if (ProcessRunning)
		return false;
	
	if (LastOpened == id)
		return false;
	
	ProcessRunning = true;

	DivSize = 0;
	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"+id;
	inp.value = ResponseVal.Poster;
	
	if(ResponseVal.Poster != '')
	{
		inp.readOnly = true;
	}

	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"+id;
	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"+id;
	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 = "button";
	inp.value = 'Submit reply';
	inp.id = "sr"+id;
	inp.className = "input_odd";
	
	inp.setAttribute("oclpar", id);
	inp.onclick = function () {
		SubmitReply(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" + id;

	inp.className = "input_odd";

 	cell.appendChild(inp);
	row.appendChild(cell);
	mytblbody.appendChild(row);

	mytbl.appendChild(mytblbody);
	mytbl.setAttribute("bgcolor", '#f2f2f2');
	
	$('replyForm'+id).innerHTML = '';
	$('replyForm'+id).appendChild(mytbl);
	$('replyForm'+id).setAttribute("bgcolor", '#f2f2f2');
	
	inp.setAttribute("oclpar", id);
	inp.onclick = function () {
		HideFormDiv('RollDown' + this.getAttribute("oclpar"));
		//LastOpened = 0;
	}

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


	ShowFormDiv('RollDown' + id);

	if(LastOpened > 0)
		HideFormDiv('RollDown' + LastOpened);

 	LastOpened = id;

}


function SubmitReply(id)
{
	if (SubmitProcessRunning)
		return false;
		
	$('sr'+id).disabled = true;
		
	SubmitProcessRunning = 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
	(
		'/modules/articles_module/AJAX/MessageActions.php',
		{
			method: 'post',
			parameters: '&aid='+ ArticlesID[0] +'&id=' + id + '&name='+escape(name)+'&subject='+escape(subject)+'&comment='+escape(comment),
			onSuccess: PostSubmitReply,
			onFailure: FailedSubmitReply
		}
	);
}

function PostSubmitReply(response)
{
	SubmitProcessRunning = false;
	eval("ResponseVal = " + response.responseText);
	
	DisplayMessage(ResponseVal.ErrorMessage, 'err'+ResponseVal.Id);

	if(ResponseVal.Error == 1)
	{
		HideFormDiv('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>';

		$('Replies'+id).style.display = 'block';
		$('replyLink'+id).innerHTML = 'Hide Replies';

		
		Cid[Cid.length] = ResponseVal.InsertedId;

		CStars[CStars.length] = 0;

	}
	else
	{
		$('sr'+id).disabled = false;
	}
}

function DisplayMessage(message, span)
{
	$(span).innerHTML = message;
	window.setTimeout("$("+span+").innerHTML = '';", 5000);
}


function FailedSubmitReply()
{
}

function CancelComment()
{
	
}

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 ShowFormDiv(id)
{
 	ob = $(id);
	
	if (ob.style.height == '')
	{
		ob.style.height = 1;
	}

	if (parseInt(ob.style.height) < (IsIE ? 170 : 190))
	{
		ob.style.height = parseInt(ob.style.height) + 12;
		
		setTimeout('ShowFormDiv(\'' + id + '\')', 1);
	}
	else
	{
		ProcessRunning = false;
	}
}

function HideFormDiv(id)
{
	ob = $(id);

	if (parseInt(ob.style.height) > 1)
	{
		ob.style.height = parseInt(ob.style.height) - 12;

		setTimeout('HideFormDiv(\''+id+'\')', 1);
	}
	else
	{
		ProcessRunning = false;
		//$('RollDown'+id).style.display = 'none';
	}

}

function VoteComment(CommentId, Vote)
{
	new Ajax.Request
	(
		'/modules/articles_module/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()
{
	
}

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 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");
	}
}