var comment_backup = new Array();
var comment_editmode = new Array();


var CAPTCHA_SHOWN = false;
function showCaptcha(){
	if(CAPTCHA_SHOWN) return false;
	var cont = document.getElementById("captcha_cont");
	doLoadParser(true, 
		null, 
		'/bgscript/comment_captcha.html',
		function(debug,res){ 
			cont.innerHTML = res.html;
		}
	);
	CAPTCHA_SHOWN = true;
	document.getElementById("comment_btn").removeAttribute("disabled");
}

function addComment(form){
	if(document.MF.text.value == ''){
		alert('Вы не можете добавить пустой комментарий!');
		return false;
	}
	document.getElementById("comment_btn").setAttribute("disabled", "true");
	setWaitDialog(true, "Добавляем комментарий.");
	return true;
}

function commentTools(){
	try{
	    var form = document.getElementById("comment_tools");
	    if(form._action.value == "") return false;
	    var comments_select = $('form#comment_tools select[name=cid]');
    	    comments_select.children().remove();
	    
	    var comment_cbs = $('input[name=cid]:checked');
	    for(var i = 0; i < comment_cbs.size(); i++){
			var comment_id = $(comment_cbs.get(i)).val();
			var opt = $('<option>');
			opt.attr('value', comment_id);
			opt.appendTo($('form#comment_tools select[name=cid]'));
	    }
	    comments_select.children().attr('selected', 'true');
		
	    var act_title = form._action[form._action.selectedIndex].getAttribute("warn_action");
	    if(form.cid.length > 0){
			return confirm("Вы действительно хотите "+act_title+" "+form.cid.length+" комментариев?");
	    }else{
			alert("Не выбрано ни одного комментария.");
		return false;
	    }
	}catch(e){
	    alert("Error:"+e);
	    return false;
	}
}

function markAllComments(){
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].getAttribute("type") == "checkbox" && inputs[i].getAttribute("name") == "cid"){
			inputs[i].checked=!inputs[i].checked;
		}
	}
}

function editTimeOut(cid){
	var c = document.getElementById("comment_"+cid+"_edit_container");
	c.parentNode.removeChild(c);
	if(comment_editmode[cid]){
		editCommentCancel(cid);
	}
}

function editCommentCancel(cid){
	var container = document.getElementById("comment_"+cid+"_text_container");
	container.innerHTML = comment_backup[cid];
	comment_editmode[cid] = false;
}

function editCommentLoad(cid){
	if(comment_editmode[cid]) return;
	
	var container = document.getElementById("comment_"+cid+"_text_container");
	comment_backup[cid] = container.innerHTML;
	comment_editmode[cid] = true;
	while(container.childNodes.length>0){
		container.removeChild(container.childNodes[0]);
	}
	doLoadParser(true, 
		{act:"read", cid:cid}, 
		'/bgscript/comment_edit.html',
		function(debug,res){ 
			if(res.ok == 1){
				container.innerHTML = res.html;
			}else{
				alert(res.error);
				editCommentCancel(cid);
			}
		}
	);
}

function editCommentWrite(cid, sk, button){
	var container = document.getElementById("comment_"+cid+"_text_container");
	var textarea = document.getElementById("comment_"+cid+"_textarea");
	button.setAttribute('disabled', 'true');
	button.setAttribute('value', 'Идет редактирование...');
	doLoadParser(true, 
		{act:"write", cid:cid, text:textarea.value, sk:sk}, 
		'/bgscript/comment_edit.html',
		function(debug,res){ 
			if(res.ok == 1){
				container.innerHTML = res.html;
				comment_editmode[cid] = false;
			}else{
				alert(res.error);
				editCommentCancel(cid);
			}
		}
	);
}

function deleteComment(cid, sk, login, eid){
	if(!confirm("Вы действительно хотите удалить свой комментарий?")) return;
	setWaitDialog(true, "Удаление комментария");
	doLoadParser(true, 
		{cid:cid, sk:sk, eid:eid}, 
		'/bgscript/comment_del.html',
		function(debug,res){
			setWaitDialog(false);
			if(res.ok == 1){
				if(confirm("Комментарий удален. Перегрузить страницу?")){
					window.location.href="/users/"+login+"/"+eid+"/comment/?lastcomment="+res.comment_cnt;
				}else{
					document.getElementById('comment_'+cid+'_container').style.display='none';
				}
			}else{
				alert(res.error);
			}
			
		}
	);
	
}

