function askDelNews(newsId,infoPage){
	if(confirm("Sei sicuro di voler cancellare la notizia?")){
		delNews(newsId,infoPage);
	}
}
function delNews(newsId,infoPage){
	ajaxGetRand('/ajax/news_del.php?newsId='+newsId,myHandler,newsId);
	function myHandler(aa,newsId){
		if(aa!='ERROR'){
			if(infoPage==true){location.href='/';}
			document.getElementById('news'+newsId).innerHTML="";
			document.getElementById('news'+newsId).style.display='none';
			document.getElementById('news'+newsId).style.visibility='hidden';
			return true;
		}else{
			alert("Non e' possibile cancellare la notizia");
			return false;
		}
	}

}


function appendTooltipTextToObj(text,target,xoff,yoff){
    if($(target).attr("novoteActive")) return;
    var x;
    var y;
    if(typeof(xoff) == "undefined" || typeof(yoff) == "undefined") {
        x = $(target).offset().left+($(target).width()*1.5);
        y = $(target).offset().top;
    } else {
        x = $(target).offset().left+$(target).width()+xoff;
        y = $(target).offset().top+yoff;
    }

    var div = document.createElement('div');
    div.className = 'clicktip';
    div.style.position = 'absolute';
    div.style.left = x+"px";
    div.style.top = y+"px";
    div.innerHTML = "<img src='/images/tooltiparrow.png'>"+text; 
    document.body.appendChild(div); 
    $(target).attr("novoteActive",true);

    $(div).click(function(){
       // $(this).remove();
        removetooltip(this,target);
    });

    /* Compute how long the clicktip should be shown */
    var milliseconds = 2000; /* base time */
    var textlen = text.length;

    /* Add one second for every 50 characters */
    while(textlen > 30) {
        milliseconds += 900;
        textlen -= 30;
    }

    setTimeout(function() {
        try {
            removetooltip(div,target);
        } catch(e) {};
    }, milliseconds);
}

function removetooltip(obj,target) {
    document.body.removeChild(obj);
    $(obj).remove();
    $(target).removeAttr("novoteActive");
}

function vote(vote,newsId,vs,inFrame){
	var infoPage=false;
	vote2html(vote,newsId,vs,inFrame);
	voteLoading(newsId);
	if(document.getElementById('voteReport')){
		infoPage=1;
	}
	ajaxGetRand('/ajax/vote.php?vote='+vote+'&newsid='+newsId+'&vs='+vs+'&infoPage='+infoPage+'&inframe='+inFrame+'&c=1',myHandler,newsId,inFrame);
	function myHandler(aa,newsId,inFrame){
		callRS();
		if(aa.substr(0,5)!='ERROR'){
			document.getElementById('news'+newsId).innerHTML=aa;
			if(inFrame){
        			if(typeof(noreload) == 'undefined' &&
                                   parent.opener.location)
	        			parent.opener.location.reload();
			}
			if(infoPage){
				updateReportVote(newsId);
			}
			registerVoteExpiredTip();
			return true;
		}else{
			newsUnloading(newsId);
			return false;
		}
	}

}
function updateReportVote(newsId){
	ajaxGetRand('/ajax/update_report_vote.php?newsid='+newsId,myHandler);
	function myHandler(aa){
		if(aa!='ERROR'){
			document.getElementById('voteReport').innerHTML=aa;
			return true;
		}else return false;
	}

}
function vote2html(vote,newsId,vs,inFrame){
	var OK;
	var NO;
	var styleAdd="";
	if(inFrame==true){
		styleAdd=" style=\"float:left;\" ";
	}
	if(!isLoggedIn()){
		vs=ipSecret;
	}
	if(vote == 1){
		OK="<a href='javascript:vote(0,"+newsId+",\""+vs+"\","+inFrame+");' onmouseover='window.status=\"Cancella Voto\";return true;' onmouseout='window.status=\"\";return true;' title='Cancella Voto' "+styleAdd+"><strong>OK</strong></a>";
		NO="<a href='javascript:vote(-1,"+newsId+",\""+vs+"\","+inFrame+");' onmouseover='window.status=\"Vota NO\";return true;' onmouseout='window.status=\"\";return true;' title='Vota NO' "+styleAdd+">NO</a>";
	}else if(vote == -1){
		OK="<a href='javascript:vote(1,"+newsId+",\""+vs+"\","+inFrame+");' onmouseover='window.status=\"Vota OK\";return true;' onmouseout='window.status=\"\";return true;' title='Vota OK' "+styleAdd+">OK</a>";
		NO="<a href='javascript:vote(0,"+newsId+",\""+vs+"\","+inFrame+");' onmouseover='window.status=\"Cancella Voto\";return true;' onmouseout='window.status=\"\";return true;' title='Cancella Voto' "+styleAdd+"><strong>NO</strong></a>";
	}else{
		OK="<a href='javascript:vote(1,"+newsId+",\""+vs+"\","+inFrame+");' onmouseover='window.status=\"Vota OK\";return true;' onmouseout='window.status=\"\";return true;' title='Vota OK' "+styleAdd+">OK</a>";
		NO="<a href='javascript:vote(-1,"+newsId+",\""+vs+"\","+inFrame+");' onmouseover='window.status=\"Vota NO\";return true;' onmouseout='window.status=\"\";return true;' title='Vota NO' "+styleAdd+">NO</a>";
	}
	var html='<div class="newsVoteOk">'+OK+'</div><div class="newsVoteNo">'+NO+'</div>';
	document.getElementById('newsVote'+newsId).innerHTML=html;
	return false;
}

function voteComment2html(vote,commentId){
	var OK;
	var NO;
	if(vote == 1){
		OK="<a href='javascript:voteComment(0,"+commentId+");'  onmouseover='window.status=\"Cancella Voto\";return true;' onmouseout='window.status=\"\";return true;' title='Cancella Voto'><strong>OK</strong></a>";
		NO="<a href='javascript:voteComment(-1,"+commentId+");' onmouseover='window.status=\"Vota NO\";return true;' onmouseout='window.status=\"\";return true;' title='Vota NO'>NO</a>";
	}else if(vote == -1){
		OK="<a href='javascript:voteComment(1,"+commentId+");' onmouseover='window.status=\"Vota OK\";return true;' onmouseout='window.status=\"\";return true;' title='Vota OK'>OK</a>";
		NO="<a href='javascript:voteComment(0,"+commentId+");' onmouseover='window.status=\"Cancella Voto\";return true;' onmouseout='window.status=\"\";return true;' title='Cancella Voto'><strong>NO</strong></a>";
	}else{
		OK="<a href='javascript:voteComment(1,"+commentId+");'onmouseover='window.status=\"Vota OK\";return true;' onmouseout='window.status=\"\";return true;' title='Vota OK'>OK</a>";
		NO="<a href='javascript:voteComment(-1,"+commentId+");' onmouseover='window.status=\"Vota NO\";return true;' onmouseout='window.status=\"\";return true;' title='Vota NO'>NO</a>";
	}
	var html='<div class="commentVoteOk">'+OK+'</div><div class="commentVoteNo">'+NO+'</div>';
	document.getElementById('commentVote'+commentId).innerHTML=html;
	return false;
}
function voteLoading(newsId){
	newsLoading(newsId,'Stiamo contabilizzando il voto.');
}
function newsLoading(newsId,txt){
	document.getElementById('loading'+newsId).innerHTML='&nbsp;&nbsp;<img src="/images/indicatore2.gif" alt="Attendere prego" width="16" height="9">&nbsp;'+txt;
}
function newsUnloading(newsId){
	document.getElementById('loading'+newsId).innerHTML='';
}
function voteCommentLoading(commentId){
	document.getElementById('loadingComment'+commentId).innerHTML='&nbsp;&nbsp;<img src="/images/indicatore2.gif" alt="Attendere prego" width="16" height="9">&nbsp;Attendere...&nbsp;&nbsp;'

}
function voteCommentUnloading(commentId){
	document.getElementById('loadingComment'+commentId).innerHTML=''

}
function voteComment(vote,commentId,vs){


	voteCommentLoading(commentId);
	voteComment2html(vote,commentId);
	ajaxGetRand('/ajax/comment_vote.php?vote='+vote+'&commentId='+commentId+'&vs='+vs,myHandler,commentId);
	function myHandler(aa,commentId){
		if(aa!='ERROR'){
			document.getElementById('commentVoteInfo'+commentId).innerHTML=aa;
			voteCommentBackground(commentId);
			voteCommentUnloading(commentId);
		}else return false;


	}

}
function voteCommentBackground(commentId){
	ajaxGetRand('/ajax/comment_vote_background.php?commentId='+commentId,myHandler,commentId);
	function myHandler(aa,commentId){
		if(aa!='ERROR'){
			document.getElementById('comment'+commentId).style.background=aa;
			return true;
		}else return false;


	}

}
function saveNews(newsId){
	newsLoading(newsId,'Attendere...');
	ajaxGetRand('/ajax/save_news.php?newsid='+newsId,myHandler,newsId);
	function myHandler(aa,newsId){
		if(aa=='ERROR'){
			document.getElementById('saveNews'+newsId).innerHTML='';
			newsUnloading(newsId);
			return false;
		}else if(aa=='SAVED'){
			document.getElementById('saveNews'+newsId).innerHTML='<a href="javascript:void(0);" onclick="saveNews('+newsId+');" title="Elimina dalle notizie salvate"><img src="/images/star_on.png" border="0" alt="Salva" width="16" height="16"></a>';
			newsUnloading(newsId);
			return true;
		}else if(aa=='UNSAVED'){
			document.getElementById('saveNews'+newsId).innerHTML='<a href="javascript:void(0);" onclick="saveNews('+newsId+');" title="Salva questa notizia"><img src="/images/star_off.png" border="0" alt="Non salvare" width="16" height="16"></a>';
			newsUnloading(newsId);
			return true;
		}
	}

}
function chref(el,v) {
    el.href = v;
    return true;
}

