browser = navigator.appName;

function showPopup(event,popupId) {
	var popup=document.getElementById(popupId)
	var popupWidth=parseInt(popup.style.width);
	var popupHeight=parseInt(popup.style.height);
	
	// ricava le coordinate del puntatore nella pagina
	var left=event.clientX;
	var top=event.clientY;
	
    // calcola coordinate left di apertura della popup
    if((left+popupWidth)>document.documentElement.offsetWidth) {
     var diff_left=(left+popupWidth)-document.documentElement.offsetWidth;
     popupLeft=(left+document.body.scrollLeft)-(diff_left+20);
    }
    else popupLeft=left+document.body.scrollLeft;
	
	// calcola coordinate top di apertura della popup
    var h_confront=null;
    if (browser == "Microsoft Internet Explorer")
     h_confront=document.documentElement.offsetHeight;
    else h_confront=window.innerHeight;
    if((top+popupHeight)>=h_confront) {
     var diff_top=(top+popupHeight)-h_confront;
     popupTop=(top+getScrollTop())-(diff_top+10);
    }
	else popupTop=top+getScrollTop();

	popup.style.left=popupLeft+"px";		
	popup.style.top=popupTop+"px";
	popup.style.display="block";	
}

function hidePopup(popupId) {
	var popup=document.getElementById(popupId)
	popup.style.display="none";
}

function getScrollTop(){
	var de = document.documentElement||{}, db = document.body||{};
	return window.pageYOffset||de.scrollTop||db.scrollTop||0;
}

function replaceAll(string,needle,replace) {
	while(string.indexOf(needle)!=-1) {
		string = string.replace(needle,replace);
	}
	return string;
}

function setCookie(cName, cValue, cDays) {
	var todayDate = new Date();
	var expireDate = new Date();
	expireDate.setTime(todayDate.getTime() + 24 * cDays * 3600000);
	document.cookie = cName + "=" + escape(cValue) +"; expires=" + expireDate.toGMTString();
}

function getCookie(cName) {
  	var allCookies = document.cookie.split("; ");
  	// ciclo su tutti i cookies
  	for (var iCks = 0; iCks < allCookies.length; iCks++) {
    	var aCookie = allCookies[iCks].split("=");  
    	if (cName == aCookie[0]) { 
      		return (unescape(aCookie[1]));
   		}
	}
  	return("");
}

function deleteCookie(cName) {
	var todayDate = new Date();
	var expireDate = new Date();
	expireDate.setTime(todayDate.getTime() - (1000*60*60*24));
	document.cookie=cName + "=; expires=" + expireDate.toGMTString();
}

function createRequest() {
	ieDom = false;
	var richiesta;	
	if (browser == "Microsoft Internet Explorer") {
		richiesta = new ActiveXObject("Microsoft.XMLHTTP");
		ieDom = true;
	} else {
		richiesta = new XMLHttpRequest();
	}
	return richiesta;
}

var req=createRequest();

function TargetBlank() {
	var i = 0;
	var collegamenti = document.links;
	var tot_links = collegamenti.length;
	for (i=0; i<tot_links; i++) {
		if ((collegamenti[i].className).indexOf("blank_")>=0) {
			collegamenti[i].target = "_blank";
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}

}

function addUnloadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function'){
    	window.onunload = func;
	} else {
		window.onunload = function(){
			oldonunload();
			func();
		}
	}

}