function Response2XML(response)
{
	var xml;
	if (typeof response == "string") {
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = false;
		xml.loadXML(response);
	} else {
		xml = response;
	}
	return(xml);
}


function WriteComment(topic_id, item_type)
{
	var new_comment = $('#new-comment').val();
	var new_comment_parent_id = $('#new-comment-parent').val();
	
	if (new_comment.length < 2) {
		alert('Комментарий должен быть более выразительным!');
		return(false);
	}
	
	// sending ajax request
	//ShowLoader();
	
	var url = '/ajax/?section=add-comment';
	if (item_type == 'blog') {
		url += '-blog';
	}
	$.ajax({
		type: 'POST',
		url: url,
		cache: false,
		dataType: ($.browser.msie) ? "text" : "xml",
		data: 'text=' + new_comment + '&parent_id=' + new_comment_parent_id + '&topic_id=' + topic_id,
		async: true,
		success: function(response){
						
			var xml = Response2XML(response);
			var response_type = $('response', xml).attr('type');
			
			var result = $('response', xml).attr('description');
			
			if (response_type == 'error') {
				alert($('response', xml).attr('description'));
			}
			else {
				var cdata = new Array();
				
				$('item', xml).each(function(){
					index = $("name",this).text();										
					cdata[index] = $("value",this).text();			
					//alert(cdata[index]);
	            });
				
				if (new_comment_parent_id=='0') {
					var to_div = 'comments';
					var go_level = 0;
				}
				else {
					var to_div = 'comment-' + new_comment_parent_id;
					var go_level = 1;
				}
				
				if (cdata['avatar']!='') {
					var avatar = '<img src="/files/Image/avatars/' + cdata['user_id'] + cdata['avatar'] + '" alt="" />';
				}
				else {
					$('img').attr('src', '/templates/default/images/px.gif').attr('width','42').attr('height','42').appendTo(div_ava);
					var avatar = '<img src="/templates/default/images/px.gif" alt="" width="42" height="42" />';
				}
				
				var last_id = 'comments';
				$("#comments div.parent_"+new_comment_parent_id).each (
					function() {
						last_id = $(this).attr('id');
					}
				);
				
				$("#" + last_id).after("<div class='fixer'></div><div id='comment-"+cdata['id']+"' class='comment c-level-"+(parseInt(cdata['level'])+1)+"'><div class='comment-avatar'>" + avatar + "</div><div class='comment-text'>"+cdata['text']+"<p>написал <a href='#'>" + cdata['login'] + "</a> - " + cdata['date'] + " &nbsp; <a href='" + $('#new-comment-parent-url').val() + "#comment-" + cdata['id'] + "'>#</a></p></div><div class='fixer'></div></div>");
					
				try {
					$('#no-comments').hide();
				}
				catch (e) {}
				
				$('#new-comment').val('');
				$('#write-comment').hide();
			}
			
			//CloseLoader();
		}
	});
	
	return(true);
}


function AnswerComment(target)
{
	if (target != 'new') {
		$('#comment-' + target).get(0).appendChild($('#write-comment').get(0));
		$('#write-comment').show();
		$('#new-comment-parent').val(target);
	}
	else {
		$('#new-comment-form').get(0).appendChild($('#write-comment').get(0));
		$('#write-comment').show();
		$('#new-comment-parent').val('0');
	}
}

