var d = document;
var frm = null;
var inProcess = false;


function showLoading(param)
{
	inProcess = param;
	if(param)
	{
		hide("commentsContent_2_inner");
		show("commentsContent_loading");
	}
	else
	{
		hide("commentsContent_loading");
		show("commentsContent_2_inner");
	}
	return true;
}

function submitComment()
{
	if(inProcess) return false;
	showLoading(true);
	frm = d.getElementById(settings["form"]);
	var params = requestComment();
	if(params != "")
	{
		var url = "/fitch/index.php";
		sndReq(url, params, "POST", handleResponseComment);
	}
	return false;
}

function requestComment()
{
	var res = "cmd=" + settings["cmd"];
	res += "&action=" + settings["action"];
	if(frm)
	{
		for(var i = 0; i < frm.elements.length; i++)
		{
			res += "&" + frm.elements[i].name + "=" + escape(frm.elements[i].value);
		}
	}
	//res = encodeURI(res);
	return res;
} //end function requestPostComment

function handleResponseComment()
{
	if(http.readyState == 4)
	{
		var t = http.responseText;
		if(t.substring(0, 4) == "OkOk")
		{
			res = t.fetchAssocArray(":|:");
			updateComments(res["OkOk"]);
		}
		else
		{
			alert("Error. Please, check the required fields and try again.");
			//alert("Error. Please try again later.");
			//alert(t);
		}
		showLoading(false);
		showDivTab('1', 'comments', '2');
	}
	return true;
} //end function handleResponsePostComment


function updateComments(comments)
{
	if(comments)
	{
		var comment = null;
		comments = comments.fetchAssocArray(":|0|:");
		comments_count = comments.length;
		
		if (frm.elements.last_comment_id.value == 0)
		{
			if (no_comments = d.getElementById(settings["no_comments"]))
			{
				no_comments.style.display = "none";
			}
		}
		
		for (var i = comments_count - 1; i >= 0; i--)
		{
			comment = comments[i].fetchAssocArray(":|1|:");
			writeComment(comment);
		}
		
		if (comment)
		{
			var count = comments.length;
			if (count > 0)
			{
				if (comments_count = d.getElementById(settings["comments_count"]))
				{
					if(comments_count.innerHTML == "NO")
					{
						comments_count.innerHTML = 0;
					}
					comments_count.innerHTML = parseInt(comments_count.innerHTML) + count;
					if (parseInt(comments_count.innerHTML) > 1)
					{
						d.getElementById(settings["comments_s"]).innerHTML = "S";
					}
				}
			}
			frm.elements.last_comment_id.value = comment["id"];
			frm.elements.comment.value = "";
		}
	}
	return true;
} //end function updateComments

function writeComment(comment)
{
	if(comment)
	{
		var commentHTML = '';
		commentHTML += '<table class="comments_table">';
			commentHTML += '<tr>';
				commentHTML += '<td valign="top" class="td_image">';
				if (comment["photo"] != "")
				{
					commentHTML += '<img src="' + imagesDomain + '/images/users/s/' + comment["photo"] + '" class="user_photo_small" alt="' + (comment["display_full_name"] == 1 ? comment["full_name"] : comment["username"]) + '" title="' + (comment["display_full_name"] == 1 ? comment["full_name"] : comment["username"]) + '" />';
				}
				else
				{
					commentHTML += '<img src="' + imagesDomain + '/images/users/nophoto_s.gif" class="user_photo_small" alt="No Photo" title="No Photo" />';
				}
				commentHTML += '</td>';
				commentHTML += '<td valign="top" class="td_user">';
				if (comment["username"] != "")
				{
					commentHTML += '<span class="username"><a href="/u/' + comment["username"] + '">' + (comment["display_full_name"] == 1 ? comment["full_name"] : comment["username"]) + '</a></span>';
				}
				else
				{
					commentHTML += '<span class="username">' + comment["user_full_name"] + '</span>';
				}
					commentHTML += '<br />';
					commentHTML += '<span class="date">' + comment["date_added"] + '</span>';
				commentHTML += '</td>';
				commentHTML += '<td valign="top" class="td_content">';
					commentHTML += '<div class="content_inner">';
						commentHTML += '' + comment["comment"];
					commentHTML += '</div>';
				commentHTML += '</td>';
			commentHTML += '</tr>';
		commentHTML += '</table>';
		
		var commentsDiv = d.getElementById(settings["comments"]);
		commentsDiv.innerHTML = commentHTML + commentsDiv.innerHTML;
	}
	return true;
} //end function writeComment
