/*script per i tooltip*/

function enableTooltips(){
var links=document.getElementsByTagName("a");
for(i=0;i<links.length;i++){
    t=links[i].getAttribute("title");
    if (t!="" && t!=null) {
        links[i].removeAttribute("title");
        links[i].style.position="relative";
        tooltip=document.createElement("em");
        tooltip.className="tooltip";
        tooltip.style.display="none";
        tooltip.appendChild(document.createTextNode(t));
        links[i].appendChild(tooltip);
        links[i].onmouseover=showTooltip;
        links[i].onmouseout=hideTooltip;
        }
    }
}

function showTooltip(event){
    this.style.zIndex="25";
    this.getElementsByTagName("em")[0].style.display="block";
	sorgente = this.getElementsByTagName("img")[0].src;
	x = sorgente.lastIndexOf(".")
	ext = sorgente.slice(x);
	pre = sorgente.substring(x,-1)
	//alert (ext)
	//alert (pre)
	this.getElementsByTagName("img")[0].src=pre+"_f2"+ext;
}

function hideTooltip(event){
    this.style.zIndex="24";
    this.getElementsByTagName("em")[0].style.display="none";
	sorgente = this.getElementsByTagName("img")[0].src;
	x = sorgente.lastIndexOf(".")
	ext = sorgente.slice(x);
	pre = sorgente.substring(x,-1)
	pre = pre.replace("_f2", "")
	this.getElementsByTagName("img")[0].src=pre+ext;
	//alert(x[0]+".gif")
}

function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function linksCal() {
  var el = getElementsByClass(document,'calendar_link','td');
  for(z=0;z<el.length;z++){ 
    var links = el[z].getElementsByTagName("a");
    for(i=0;i<links.length;i++){
      if (links[i].innerHTML !="link") {
        u=links[i].getAttribute("href");
        vars = u.split("?");
        links[i].href="#";
        mioid = "day"+links[i].innerHTML
        //el[z].onfocus=showPopup;
        //el[z].onblur=hidePopup;
        el[z].onclick=togglePopup;
        caricaEvento(path + "/inc_calevent.php?"+vars[1], mioid)
      }
    }
  }
  var el = getElementsByClass(document,'calendar_todayLinked','td');
  for(z=0;z<el.length;z++){ 
    var links = el[z].getElementsByTagName("a");
    for(i=0;i<links.length;i++){
      if (links[i].innerHTML !="link") {
        u=links[i].getAttribute("href");
        vars = u.split("?");
        links[i].href="#";
        mioid = "day"+links[i].innerHTML
        //el[z].onfocus=showPopup;
        //el[z].onblur=hidePopup;
        el[z].onclick=togglePopup;
        caricaEvento(path + "/inc_calevent.php?"+vars[1], mioid)
      }
    }
  }
}

function togglePopup(event){
	if (this.getElementsByTagName("div")[0].style.display=="block") {
	  this.style.zIndex="24";
      this.getElementsByTagName("div")[0].style.display="none";
	} else {
	  this.style.zIndex="25";
	  this.getElementsByTagName("div")[0].style.display="block";
	}
}
function showPopup(event){
	  this.style.zIndex="25";
	  this.getElementsByTagName("div")[0].style.display="block";
}
function hidePopup(event){
	  this.style.zIndex="24";
      this.getElementsByTagName("div")[0].style.display="none";
}

function caricaEvento(nomeFile, target) {
    //alert(target)
    var ajax = assegnaXMLHttpRequest()
    var elemento = prendiElementoDaId(target)
  if(ajax) {
    ajax.open("get", nomeFile, true);
    ajax.setRequestHeader("connection", "close");
    ajax.onreadystatechange = function() {
	  if(ajax.readyState === 1) {
	  }
      if(ajax.readyState === readyState.COMPLETATO) {
		  //pausecomp(1200);
        if(statusText[ajax.status] === "OK") {
          elemento.innerHTML = ajax.responseText;
		}
        else {
          elemento.innerHTML = "<div class=\"ajax_errore\">Impossibile effettuare l'operazione richiesta.<br />";
          elemento.innerHTML += "Errore riscontrato: " + statusText[ajax.status] + "</div>";
        }
      } 
    }
    ajax.send(null);
  }
}
// funzione per prendere un elemento con id univoco
		function prendiElementoDaId(id_elemento) {
			var elemento;
			if(document.getElementById)
				elemento = document.getElementById(id_elemento);
			else
				elemento = document.all[id_elemento];
			return elemento;
		};
	
// funzione per assegnare un oggetto XMLHttpRequest
		function assegnaXMLHttpRequest() {
			var
				XHR = null,
				browserUtente = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};



/** OGGETTI / ARRAY */

	// oggetto di verifica stato
		var readyState = {
			INATTIVO:	0,
			INIZIALIZZATO:	1,
			RICHIESTA:	2,
			RISPOSTA:	3,
			COMPLETATO:	4
		};

	// array descrittivo dei codici restituiti dal server
	// [la scelta dell' array è per evitare problemi con vecchi browsers]
		var statusText = new Array();
		statusText[100] = "Continue";
		statusText[101] = "Switching Protocols";
		statusText[200] = "OK";
		statusText[201] = "Created";
		statusText[202] = "Accepted";
		statusText[203] = "Non-Authoritative Information";
		statusText[204] = "No Content";
		statusText[205] = "Reset Content";
		statusText[206] = "Partial Content";
		statusText[300] = "Multiple Choices";
		statusText[301] = "Moved Permanently";
		statusText[302] = "Found";
		statusText[303] = "See Other";
		statusText[304] = "Not Modified";
		statusText[305] = "Use Proxy";
		statusText[306] = "(unused, but reserved)";
		statusText[307] = "Temporary Redirect";
		statusText[400] = "Bad Request";
		statusText[401] = "Unauthorized";
		statusText[402] = "Payment Required";
		statusText[403] = "Forbidden";
		statusText[404] = "Not Found";
		statusText[405] = "Method Not Allowed";
		statusText[406] = "Not Acceptable";
		statusText[407] = "Proxy Authentication Required";
		statusText[408] = "Request Timeout";
		statusText[409] = "Conflict";
		statusText[410] = "Gone";
		statusText[411] = "Length Required";
		statusText[412] = "Precondition Failed";
		statusText[413] = "Request Entity Too Large";
		statusText[414] = "Request-URI Too Long";
		statusText[415] = "Unsupported Media Type";
		statusText[416] = "Requested Range Not Satisfiable";
		statusText[417] = "Expectation Failed";
		statusText[500] = "Internal Server Error";
		statusText[501] = "Not Implemented";
		statusText[502] = "Bad Gateway";
		statusText[503] = "Service Unavailable";
		statusText[504] = "Gateway Timeout";
		statusText[505] = "HTTP Version Not Supported";
		statusText[509] = "Bandwidth Limit Exceeded";


//------------------------------------
// addEvent
function addEvent(obj,type,fn){
    try{
        if(obj.addEventListener) obj.addEventListener(type,fn,false);
        else if(obj.attachEvent){
            obj["e"+type+fn]=fn
            obj[type+fn]=function(){ obj["e"+type+fn](window.event)}

            obj.attachEvent("on"+type,obj[type+fn]);
        }
    } catch(e){/*alert(e)*/}
}

//------------------------------------
// removeEvent
function removeEvent(obj,type,fn){
    if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
    else if(obj.detachEvent){
        obj.detachEvent("on"+type,obj[type+fn])
        obj[type+fn]=null
        obj["e"+type+fn]=null;
    }
}
window.onload=function(){
  linksCal();
}

