function show_comments(id_article,id_page){
	if(!id_article)
		return '';
	$.ajax({
		type: "GET",
		data: "mode=get_comments&id_article="+id_article+"&page="+id_page,
		url: "/ajax.php",
		success: function(msg){
			document.getElementById("comments").innerHTML = msg;
		}
	});
}

function add_comment(id_article,id_textarea,parent_id,captcha){
	if(!id_article)
		return 0;
		
	if(parent_id>0)
		id_error = parent_id;
	else
		id_error = "";
	delete_error(id_error);
		
	if(!document.getElementById(id_textarea).value)
		show_error(id_error,"Не введено сообщение");
	if(document.getElementById(id_textarea).value.length>500)
		show_error(id_error,"Комментарий должен быть не длиннее 500 символов");

	$.ajax({
		type: "GET",
		data: "mode=check_captcha&captcha="+captcha,
		url: "/ajax.php",
		success: function(msg7){
			if(!(msg7 == "OK")){
				show_error(id_error,"Неверный код подтверждения");
			}
		}
	});

	if(document.getElementById('errrrr'+id_error).innerHTML==''){
		$.ajax({
			type: "GET",
			data: "mode=add_comment&id_article="+id_article+"&parent_id="+parent_id+"&text="+document.getElementById(id_textarea).value+"&captcha="+captcha,
			url: "/ajax.php",
			success: function(msg){
				if(msg == 'OK'){
					show_comments(id_article,1);
					document.getElementById(id_textarea).value='';
					$('#new_com_'+parent_id).slideUp('slow');
					if(document.getElementById('td_for_captcha_'+parent_id+'_new')!=undefined)
						document.getElementById('td_for_captcha_'+parent_id+'_new').innerHTML = '...';
					if(document.getElementById('td_for_captcha_'+parent_id+'_edit')!=undefined)
						document.getElementById('td_for_captcha_'+parent_id+'_edit').innerHTML = '...';
				}
			}
		});
	}
}

function update_comment(id_article,id_textarea,comment_id){
	if(!id_article || !id_textarea || !comment_id)
		return 0;
	$.ajax({
		type: "GET",
		data: "mode=update_comment&id_comment="+comment_id+"&text="+document.getElementById(id_textarea).value,
		url: "/ajax.php",
		success: function(msg){
			if(msg = 'OK')
				show_comments(id_article,1);
		}
	});
}

function delete_com(id_article,id_comm){
	if(!id_article || !id_comm)
		return 0;
	$.ajax({
		type: "GET",
		data: "mode=delete_comment&id_comment="+id_comm,
		url: "/ajax.php",
		success: function(msg){
			if(msg == 'OK')
				show_comments(id_article,1);
		}
	});
}

function show_add_com(id_comment,mode,mode2){
	if(mode2 == 'add'){
		$('#edit_com_'+id_comment).slideUp('slow');
		if(document.getElementById('td_for_captcha_'+orkritiy_comment+'_new')!=undefined)
			document.getElementById('td_for_captcha_'+orkritiy_comment+'_new').innerHTML = '<img src="/img/loading.gif">';
		if(orkritiy_comment != id_comment){
			$('#new_com_'+orkritiy_comment).slideUp('slow');
			if(document.getElementById('td_for_captcha_'+orkritiy_comment+'_edit')!=undefined)
				document.getElementById('td_for_captcha_'+orkritiy_comment+'_edit').innerHTML = '<img src="/img/loading.gif">';
		}
		orkritiy_comment = id_comment;
	
		refresh_captcha('src_captcha');
		document.getElementById('td_for_captcha_'+id_comment+'_new').innerHTML = document.getElementById('img_captcha').innerHTML;
		if(document.getElementById('td_for_captcha_'+id_comment+'_edit')!=undefined)
			document.getElementById('td_for_captcha_'+id_comment+'_edit').innerHTML = document.getElementById('img_captcha').innerHTML;
	
		if(mode==1)
			document.getElementById("comment_"+id_comment).value='[quote]'+document.getElementById("text_comment_"+id_comment).innerHTML+'[/quote]\n';
		else
			document.getElementById("comment_"+id_comment).value='';

		$('#new_com_'+id_comment).slideDown('slow');
	}
	if(mode2 == 'update'){
		$('#new_com_'+id_comment).slideUp('slow');
		$('#edit_com_'+id_comment).slideDown('slow');
	}
}

function refresh_captcha(idd){
	document.getElementById(idd).src='/captcha.php?rand='+Math.random();
}

function change_captcha(captcha){
//	document.getElementById(captcha).src='/captcha.php?rand='+Math.random();
}

function show_error(idd,txt){
	document.getElementById("errrrr"+idd).innerHTML += txt+'<br>';
	//document.getElementById('errrrr').innerHTML = '';
	//for(i=0;i<errs.length;i++)
		//document.getElementById('errrrr').innerHTML += errs[i]+'<br>';
	//errs = new Array();
}

function delete_error(idd){
	document.getElementById("errrrr"+idd).innerHTML = '';
}

