function showhiddencomment(commentid){
      var hiddencomment=document.getElementById("hiddencomment"+commentid);
      hiddencomment.style.display="none";
      var commenttxt=document.getElementById("commenttxt"+commentid);
      commenttxt.style.display="inline";
      var commentcontainer=document.getElementById("commentcontainer"+commentid);
      var contclass=commentcontainer.className;
      var contclassnew = contclass.replace(/ hiddencommentmain/gi, '');
      commentcontainer.className=contclassnew;
      hiddencomments[commentid]=0;
      return false;
}
var lastcommentformcontainer='commentform';
var tmpcommentformcontainer;
var undercommformshown=0;
var commentstermsshown=0;
var commentatorsshown=0;
var showncommentreplies=new Array();
var hiddencomments=new Array();
var shownallreplies=0;
var domcid='';
function showcommentform(commentid){
    if(undercommformshown==1 && lastcommentformcontainer=="commentformafter"+commentid){
        var oldcontainer=document.getElementById(lastcommentformcontainer);
        oldcontainer.style.display='none';
        undercommformshown=0; 
    }
    else{
        resetcommentform();
        if(cformshow==1) showcommform();
        var commentformholder=document.getElementById("commentformholder");
        var newcontainer=document.getElementById("commentformafter"+commentid);
        var oldcontainer=document.getElementById(lastcommentformcontainer);
        oldcontainer.style.display='none';
        newcontainer.appendChild(commentformholder); 
        newcontainer.style.display='inline';
        lastcommentformcontainer="commentformafter"+commentid;
        undercommformshown=1;
        
        checkifvisibleandscroll(newcontainer);
        if(document.commentpostform!=undefined){
        	if(document.commentpostform.replyto!=undefined) document.commentpostform.replyto.value=commentid;
        	if(document.commentpostform.actid!=undefined) document.commentpostform.actid.value=commentid;
			document.commentpostform.comment.focus();
		}
    }
    
    return false;
}

function showcomments(){
    if(commentatorsshown==1) showcommentators();
    if(undercommformshown==1){
        var commid=lastcommentformcontainer.match(/commentformafter(.*?)$/i);
        showcommentform(commid[1]);
    }
    else{
        if(cformshow==1) showcommform();
    } 
    return false;
}
function showcommentators(){
    var combox=document.getElementById('commentsrows');
    var commentatorsbox=document.getElementById('commentators');
    var commentstab=document.getElementById('commentstab');
    var commentatorstab=document.getElementById('commentatorstab');
    if(cformshow==1) showcommform();
    if(commentatorsshown==0){
        commentstab.className='v4_comtop22';
        commentatorstab.className='v4_comtop1';
        combox.style.display='none';
        commentatorsbox.style.display='inline';
        commentatorsshown=1;
    }
    else{
        commentstab.className='v4_comtop1';
        commentatorstab.className='v4_comtop2';
        commentatorsbox.style.display='none';
        combox.style.display='inline';
        commentatorsshown=0;
    } 
    return false;
}
function showcommform(){
    if(commentatorsshown==1) showcommentators();
    if(cformshow==0){
      resetcommentform();
      if(lastcommentformcontainer!='commentform'){
        var commentformholder=document.getElementById("commentformholder");
        var newcontainer=document.getElementById("commentform");
        var oldcontainer=document.getElementById(lastcommentformcontainer);
        oldcontainer.style.display='none';
        undercommformshown=0;
        newcontainer.appendChild(commentformholder);
      }
      var comtab=document.getElementById('commentstab');
      if(comtab!=undefined){
		  comtab.className='v4_comtop22';
	      var pushtab=document.getElementById('publishcomtab');
		  pushtab.className='v4_comtop12';
	  }
      var combox=document.getElementById('commentform');
	  combox.style.display='block';
      cformshow=1;
      lastcommentformcontainer='commentform';
      if(comtab!=undefined){
      	checkifvisibleandscroll(combox);
      }	
      if(document.commentpostform!=undefined){
		  document.commentpostform.replyto.value='';
	      document.commentpostform.comment.focus();
   	  }
    }
    else{
      var comtab=document.getElementById('commentstab');
      if(comtab!=undefined){
		  comtab.className='v4_comtop1';
	      var pushtab=document.getElementById('publishcomtab');
		  pushtab.className='v4_comtop3';
	  }
	  var combox=document.getElementById('commentform');
	  combox.style.display='none';
      cformshow=0;
    }
    return false;
}
function showcommentstermsofuse(){
    if(commentstermsshown==1){
        var termstxt=document.getElementById('commentstermsofuse');
	    termstxt.style.display='none';
        commentstermsshown=0;
    }
    else{
        var termstxt=document.getElementById('commentstermsofuse');
	    termstxt.style.display='block';
        commentstermsshown=1;
    }
    return false;
}
function showcommentreplies(commentid){
    var termstxt=document.getElementById('commentreplies'+commentid);
    var linkchange=document.getElementById('replieslink'+commentid);
    if(showncommentreplies[commentid]==1){        
	    if(termstxt!=undefined) termstxt.style.display='none';        
        if(linkchange!=undefined) linkchange.className='v4_kom_rep1';
        showncommentreplies[commentid]=0;
    }
    else{
	    if(termstxt!=undefined) termstxt.style.display='block';
        if(linkchange!=undefined) linkchange.className='v4_kom_rep2';
        showncommentreplies[commentid]=1;
    }
    return false;
}
function showallreplies(){
    var linkid=document.getElementById('showreplieslink');
    if(shownallreplies==0){
        linkid.innerHTML='Скрий отговорите';
    }
    else{
        linkid.innerHTML='Покажи и всички отговори';
    }
    for ( var i in showncommentreplies )
    {
        if(showncommentreplies[i]==shownallreplies && hiddencomments[i]!=1) showcommentreplies(i);
    }
    if(shownallreplies==0){
        shownallreplies=1;
    }
    else{
        shownallreplies=0;
    }
    return false;
}
function resetcaptcha(){
    var captchaimg=document.getElementById('captchaimg');
    var rand_no = Math.random();
    rand_no = rand_no * 100;
    rand_no = Math.ceil(rand_no);
    captchaimg.src='/includes/CaptchaSecurityImages.php?width=140&height=40&characters=4&rand='+rand_no;

}
function resetcommentform(){
    var captchaimg=document.getElementById('captchaimg');
    if(captchaimg!=undefined){
		var captchaimgsrc=captchaimg.src;
	    if(captchaimgsrc='' || !captchaimgsrc.match(/CaptchaSecurityImages/)){
	        resetcaptcha();
	    }
    }
	if(document.commentpostform!=undefined){
	    document.commentpostform.comment.value='';
	    if(document.commentpostform.security_code != undefined) document.commentpostform.security_code.value='';
	    if(document.commentpostform.uname != undefined) document.commentpostform.uname.value='';
	    if(document.commentpostform.submitbut!= undefined)document.commentpostform.submitbut.disabled=false;
	    var commform=document.getElementById('commform');
	    var commloader=document.getElementById('commloader');     
	    commloader.style.display='none';
	    commform.style.display='block';
	    var errorcodemsg=document.getElementById('errorcodemsg');
	    errorcodemsg.innerHTML='';
 	}
}
function submitcommformajax(formid, commid){
	 if(commid==undefined) commid='';
	 if(commid==''){
	     var commform=document.getElementById('commform');
	     var commloader=document.getElementById('commloader');
  	 }
     else{
     	 var commform=document.getElementById('commform'+commid);
	     var commloader=document.getElementById('commloader'+commid);
     }
     commform.style.display='none'; 
     commloader.style.display='block';
     var poststr='s=1';
     if(formid.submitbut!=undefined) formid.submitbut.disabled=true;
     if(formid.nid!=undefined){
     	var nid=formid.nid.value;
     	poststr = poststr+"&nid="+nid;
	 }
	 if(formid.comtype!=undefined){
     	var comtype=formid.comtype.value;
     	if(comtype!='') poststr = poststr+"&comtype="+comtype;
	 }
	 if(formid.actid!=undefined){
     	var actid=formid.actid.value;
     	if(actid!='') poststr = poststr+"&actid="+actid;
	 }
     if(formid.picid!=undefined){
     	var picid=formid.picid.value;
		if(picid!='') poststr=poststr+"&picid="+picid;
	 }
     if(formid.groupid!=undefined){
     	var groupid=formid.groupid.value;
		if(groupid!='') poststr=poststr+"&groupid="+groupid;
	 }
     if(formid.topicid!=undefined){
     	var topicid=formid.topicid.value;
		if(topicid!='') poststr=poststr+"&topicid="+topicid;
	 }
	 if(formid.tpl!=undefined){
     	var tpl=formid.tpl.value;
		if(tpl!='') poststr=poststr+"&tpl="+tpl;
	 }
     if(formid.replyto!=undefined){
     	var replyto=formid.replyto.value;
		if(replyto!='') poststr=poststr+"&replyto="+replyto;
	 }
     if(formid.commentid!=undefined){
     	var commentid=formid.commentid.value;
		if(commentid!='') poststr=poststr+"&commentid="+commentid;
	 }
     if(formid.uname!=undefined){
     	var uname=formid.uname.value;
		if(uname!=''){
	 		uname=htmlspecialchars(uname);
			poststr=poststr+"&uname="+uname;
		}
	 }
     if(formid.ac!=undefined){
     	var ac=formid.ac.value;
		if(ac!='') poststr=poststr+"&ac="+ac;
	 }
     if(formid.ac2!=undefined){
     	var ac2=formid.ac2.value;
		if(ac2!='') poststr=poststr+"&ac2="+ac2;
	 }
     if(formid.attpics!=undefined){
     	var attpics=formid.attpics.value;
		if(attpics!='') poststr=poststr+"&attpics="+attpics;
	 }
     if(formid.attvid!=undefined){
     	var attvid=formid.attvid.value;
		if(attvid!='') poststr=poststr+"&attvid="+attvid;
	 }
     if(formid.attlink!=undefined){
     	var attlink=formid.attlink.value;
		if(attlink!='') poststr=poststr+"&attlink="+attlink;
	 }
	 if(domcid!=''){
	 	poststr=poststr+"&domcid="+domcid;
	 }
	 if(document.forms.pollform!=undefined){
	 	
	 	if(document.forms.pollform.pollvote!=undefined){
	     	var pollvote=document.forms.pollform.pollvote.options[document.forms.pollform.pollvote.options.selectedIndex].value;
			if(pollvote!='' && pollvote!='0') poststr=poststr+"&pollvote="+pollvote;
		}
		else if(document.forms.pollform.hasmultivote!=undefined && document.forms.pollform.hasmultivote.value!=''){
			myString=document.forms.pollform.hasmultivote.value;
			var mySplitResult = myString.split(",");
			for(i = 0; i < mySplitResult.length; i++){
				var thisselpol=eval('document.forms.pollform.pollvote'+mySplitResult[i]);
				if(thisselpol!=undefined){
			     	var pollvote=thisselpol.options[thisselpol.options.selectedIndex].value;
					if(pollvote!='' && pollvote!='0') poststr=poststr+"&pollvote"+mySplitResult[i]+"="+pollvote;
				}
			}
		}
		
	 }
     if(formid.topicname!=undefined){
     	var topicname=formid.topicname.value;
		if(topicname!=''){
	 		topicname=htmlspecialchars(topicname);
			poststr=poststr+"&topicname="+topicname;
		}
	 }
	 var commenttxt=formid.comment.value;
	 commenttxt=htmlspecialchars(commenttxt);
     poststr = poststr+"&comment="+commenttxt;
	 if(formid.security_code!=undefined){
	 	var securitycode=formid.security_code.value;
	    if(securitycode!='') poststr=poststr+"&security_code="+securitycode;
	 }
     
     //if(domcid!=undefined) alert(poststr); 
     makePOSTRequest('/js/savecommentajax.php', poststr);
     return false;    
}
function submitcommeditajax(formid, commid){
     var commform=document.getElementById('commform'+commid);
     var commloader=document.getElementById('commloader'+commid);
     commform.style.display='none'; 
     commloader.style.display='block';     

     var nid=formid.nid.value;
     var commentid=formid.commentid.value;
     if(commentid!=commid) return false;
     var commenttxt=formid.comment.value;
     commenttxt=htmlspecialchars(commenttxt);
     var poststr = "nid="+nid+"&commentid="+commentid+"&comment="+commenttxt;
     makePOSTRequest('/js/savecommentajax.php', poststr);
     return false;    
}

function updategrcomments(){
    var commentstab=document.getElementById('cntcomments');
    if(commentstab!=undefined){
	    var commentstabtxt=commentstab.innerHTML;
	    cntallcomments=Math.ceil(commentstabtxt)+1;
	    if(cntallcomments==1){
	        var nocommentsrow=document.getElementById('nocomments');
	        nocommentsrow.style.display='none';
	    }
	    commentstab.innerHTML=cntallcomments;
    }
}
function updategrcommentsdelete(){
    var commentstab=document.getElementById('cntcomments');
    var commentstabtxt=commentstab.innerHTML;
    cntallcomments=Math.ceil(commentstabtxt)-1;
    if(cntallcomments<0){
        cntallcomments=0;
    }
    commentstab.innerHTML=cntallcomments;
}

function updatecommentsnow(origreplyto){
    if(origreplyto!=''){
        var commscontainer=document.getElementById('commentreplies'+origreplyto);
        if(commscontainer.style.display=='none') commscontainer.style.display='block';
        var replieslink=document.getElementById('replieslink'+origreplyto);
        if(showncommentreplies[origreplyto]===undefined){
            showncommentreplies[origreplyto]=0;    
            replieslink.style.display='block';
        }
        if(showncommentreplies[origreplyto]==0) showcommentreplies(origreplyto);    
        var replieslinkcont=replieslink.innerHTML;
        var cntreplies=replieslinkcont.match(/\((.*?)\)/i);
        cntreplies=Math.ceil(cntreplies[1])+1;
        var replieslinknewtxt = replieslinkcont.replace(/\((.*?)\)/gi, '('+cntreplies+')');
        replieslink.innerHTML=replieslinknewtxt;
    }
    var commentstab=document.getElementById('commentstab');
    var commentstabtxt=commentstab.innerHTML;
    var cntallcomments=commentstabtxt.match(/\((\d+)\)/);    
    cntallcomments=Math.ceil(cntallcomments[1])+1;
    if(cntallcomments==1){
        var nocommentsrow=document.getElementById('nocomments');
        nocommentsrow.style.display='none';
        var commentsarefromusers=document.getElementById('commentsarefromusers');
        commentsarefromusers.style.display='block';
    }
    else{
        if(cntallcomments>10){
            var showallcommslink=document.getElementById('showallcommslink');
            if(showallcommslink!=undefined){
                var showallcommslinktxt=showallcommslink.innerHTML;
                showallcommslinktxt=showallcommslinktxt.replace(/\((\d+)\)/, '('+cntallcomments+')');
                showallcommslink.innerHTML=showallcommslinktxt;
            }
        }
    }
    var cntcommentsnewtxt = commentstabtxt.replace(/\((\d+)\)/, '('+cntallcomments+')');
    commentstab.innerHTML=cntcommentsnewtxt;
}
function hasnewuserintab(turononlyreglink){
    var commentatorstab=document.getElementById('commentatorstab');
    var commentatorstabtxt=commentatorstab.innerHTML;
    var cntallcommentators=commentatorstabtxt.match(/\((\d+)\)/);    
    cntallcommentators=Math.ceil(cntallcommentators[1])+1;
    if(cntallcommentators==1){
        commentatorstab.style.display='block';
    }
    var commentatorstabnewtxt = commentatorstabtxt.replace(/\((\d+)\)/, '('+cntallcommentators+')');
    commentatorstab.innerHTML=commentatorstabnewtxt;
    
    var filtersrow=document.getElementById('filtersrow');
    var filterslinks=document.getElementById('filterslinks');
    var filtersmine=document.getElementById('filtersmine');
    if(filtersrow.style.display=='none') filtersrow.style.display='block';
    if(filterslinks.style.display=='none') filterslinks.style.display='block';
    filtersmine.style.display='inline'; 
    if(turononlyreglink==1){
        var filtersonlyreg=document.getElementById('filtersonlyreg');
        if(filtersonlyreg!==undefined && filtersonlyreg.style.display=='none') filtersonlyreg.style.display='inline';
    }
    
}
function removecaptcha(){
	var captchahtml1=document.getElementById('captchahtml1');
	if(captchahtml1==undefined) return;
	captchahtml1.style.visibility='hidden';
	var captchahtml2=document.getElementById('captchahtml2');
	if(captchahtml2==undefined) return;
	captchahtml2.style.visibility='hidden';
	var captchahtml3=document.getElementById('captchahtml3');
	if(captchahtml3==undefined) return;
	captchahtml3.style.visibility='hidden';
}
function showcaptchata(){
	var captchahtml1=document.getElementById('captchahtml1');
	if(captchahtml1==undefined) return;
	captchahtml1.style.visibility='visible';
	var captchahtml2=document.getElementById('captchahtml2');
	if(captchahtml2==undefined) return;
	captchahtml2.style.visibility='visible';
	var captchahtml3=document.getElementById('captchahtml3');
	if(captchahtml3==undefined) return;
	captchahtml3.style.visibility='visible';
}
function editcommentform(commentid){
    var commentcontainer=document.getElementById('commentcontainer'+commentid);
    var commentedit=document.getElementById('commentedit'+commentid);
    commentcontainer.style.display='none';
    commentedit.style.display='block';
    return false;
}
function canceleditcomment(commentid){
    var commentcontainer=document.getElementById('commentcontainer'+commentid);
    var commentedit=document.getElementById('commentedit'+commentid);    
    var commform=document.getElementById('commform'+commentid);
    var commloader=document.getElementById('commloader'+commentid);   
    commentedit.style.display='none';
    commentcontainer.style.display='block';
    commform.style.display='block'; 
    commloader.style.display='none';    
    return false;
}
function deletecomment(commid, comtype){
	if(comtype==undefined) comtype='';
    var confirmed=confirm('Сигурни ли сте, че желаете да изтриете коментара?');
    if(confirmed==true){
         var commform=document.getElementById('commform'+commid);
         if(commform!=undefined){
         	var commloader=document.getElementById('commloader'+commid);
	         commform.style.display='none'; 
	         commloader.style.display='block';
		 }     

         var poststr = "comtype="+comtype+"&ac=delete&commentid="+commid;
         makePOSTRequest('/js/savecommentajax.php', poststr);
    }
    return false;
}
function updatecommentsondelete(origreplyto){
    if(origreplyto!=''){
        var commscontainer=document.getElementById('commentreplies'+origreplyto);
        var replieslink=document.getElementById('replieslink'+origreplyto);
        if(showncommentreplies[origreplyto]===undefined){
            showncommentreplies[origreplyto]=0;    
            
        }    
        var replieslinkcont=replieslink.innerHTML;
        var cntreplies=replieslinkcont.match(/\((.*?)\)/i);
        cntreplies=Math.ceil(cntreplies[1])-1;
        if(cntreplies==0){
            replieslink.style.display='none';
        }
        else{
            var replieslinknewtxt = replieslinkcont.replace(/\((.*?)\)/gi, '('+cntreplies+')');
            replieslink.innerHTML=replieslinknewtxt;
        }
    }
    var commentstab=document.getElementById('commentstab');
    var commentstabtxt=commentstab.innerHTML;
    var cntallcomments=commentstabtxt.match(/\((\d+)\)/);    
    cntallcomments=Math.ceil(cntallcomments[1])-1;
    var showallcommslink=document.getElementById('showallcommslink');
    var cntcommentsnewtxt = commentstabtxt.replace(/\((\d+)\)/, '('+cntallcomments+')');
    commentstab.innerHTML=cntcommentsnewtxt;
    if(showallcommslink!=undefined){
        var showallcommslinktxt=showallcommslink.innerHTML;
        showallcommslinktxt=showallcommslinktxt.replace(/\((\d+)\)/, '('+cntallcomments+')');
        showallcommslink.innerHTML=showallcommslinktxt;
    }
    
}

function chcommpage(sid, urlinfo){
	//if(comtype==undefined) comtype='';
	var commentsrows=document.getElementById('commentsrows');
	var commentsrowsloader=document.getElementById('commentsrowsloader');
	commentsrows.style.display='none'; 
	commentsrowsloader.style.display='block';
	
	urlinfo =  urlinfo.replace(/\?/gi, '');
	
    var poststr = urlinfo+"&shownallreplies="+shownallreplies+"&jsac=chcommpage&sid="+sid;
    //alert(poststr);
    makePOSTRequest('/js/getcontentent.php', poststr);

    return false;
}

function showusercomms(sid, userid){
	showcomments();
	chcommpage(sid, 'shuser='+userid);
	return false;
}

function showuserdets(commid){
	var userdetsdiv=document.getElementById('userdets'+commid);
	userdetsdiv.style.display='block';	
}

function closeuserdets(commid){
	var userdetsdiv=document.getElementById('userdets'+commid);
	userdetsdiv.style.display='none';	
	return false;
}

var EditTimersStarted=0;
function startnewedittimer(comid){
        editablecomments[comid]=300;
        if(EditTimersStarted==0) startEditTimers();
}
function startEditTimers(){setInterval(function(){for(i in editablecomments){editablecomments[i]--;updateTimer(i);if(editablecomments[i]<=0){delete editablecomments[i]; var edittool=document.getElementById("edittool"+i);edittool.style.display='none';}}},1000);EditTimersStarted=1;}
function updateTimer(ci){if(!ci.match(/^\d+$/))return''; var timercont=document.getElementById("edittimer"+ci);var timercont2=document.getElementById("editintimer"+ci);var A=timeLeft(ci);timercont2.innerHTML=timercont.innerHTML=("("+A.minutes+" мин. "+A.seconds+" сек.)");}
function secondsLeft(ci){return(editablecomments!=undefined&&editablecomments[ci]!=undefined)?editablecomments[ci]:0}
function timeLeft(ci){var A=secondsLeft(ci);return {minutes:Math.floor(A/60),seconds:Math.floor(A%60)}; }