var oggi,oggiData,mese,giorni,http_request,caricato,ritardo,tipo,mesi,datiURL,calendario,dataAtt;

if(document.getElementsByTagName)
{
 oggi=new Date();
 oggiData=oggi.getDate()+"/"+(oggi.getMonth()+1)+"/"+oggi.getFullYear();
 mese=oggi.getMonth()+1,anno=oggi.getFullYear();
 giorni=new Array();
 http_request=false;;
 caricato=false;
 ritardo=false;
 dataAtt="";
 tipo="t";
 mesi=["","Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"];
 datiURL="/giorni.php";
}

function trim(s)
{
 var tmp=s.replace(/^[ \n\r\t]+/,"");
 tmp=s.replace(/[ \n\r\t]+$/,"");
 return s;
}

function scriviData(e)
{
 var a;
 if(!e) var e=window.event;
 if(e.target) a=e.target;
 else if(e.srcElement) a=e.srcElement;
 if(a.nodeType==1 && a.nodeName.toLowerCase()=="a")
 {
  var dati=a.getAttribute("href");
  if(document.getElementById(dataAtt))
  {
   document.getElementById(dataAtt).value=dati;
   nascondiCalendario();
  }
 }
 if(e.preventDefault) e.preventDefault();
 if(e.stopPropagation) e.stopPropagation();
 else if(e.cancelBubble) e.cancelBubble();
 return false;
}

function riempi(tr,c)
{
 var td;
 for(var n=0; n<c; n++)
 {
  td=document.createElement("td");
  td.appendChild(document.createTextNode(" "));
  tr.appendChild(td);
 }
}

function prelevaGiorni(mese,anno,tipo)
{
 if(!datiURL) return false;
 var stato=document.getElementById(calendario+"stato");
 if(!stato) return false;
 if(!stato.hasChildren) stato.appendChild(document.createTextNode(""));
 stato=stato.firstChild;
 if(window.XMLHttpRequest)
 {
  http_request=new XMLHttpRequest();
  if(http_request.overrideMimeType) http_request.overrideMimeType("text/xml");
 }
 else if(window.ActiveXObject)
 {
  try { http_request=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e)
  {
   try { http_request=new ActiveXObject("Microsoft.XMLHTTP"); }
   catch(e) {}
  }
 }
 if(http_request)
 {
  http_request.onreadystatechange=function()
  {
   var stato=document.getElementById(calendario+"stato").firstChild;
   if(http_request.readyState==4)
   {
    if(http_request.status==200)
    {
     var xmldoc=http_request.responseXML;
     var root=xmldoc.getElementsByTagName("root").item(0);
     var giorno=root.firstChild;
     var numVoci=0;
     while(giorno)
     {
      var val;
      if(giorno.nodeType==1 && giorno.tagName.toLowerCase()=="giorno")
      {
       if(giorno.firstChild && giorno.firstChild.nodeType==3) val=parseInt(giorno.firstChild.nodeValue,10);
       else val=0;
       if(val)
       {
        giorni[val]=true;
        numVoci++;
       }
      }
      giorno=giorno.nextSibling;
     }
     caricato=true;
     stato.nodeValue="";
     riempiCalendario();
    }
    else
     stato.nodeValue="Errore: "+http_request.status;
   }
   else
    return false;
  };
  delete giorni;
  giorni=new Array();
  caricato=false;
  stato.nodeValue="Caricamento informazioni...";
  http_request.open("GET",datiURL+"?m="+mese+"&a="+anno+"&t="+tipo);
  http_request.send(null);
 }
 else return false
}

function cambiaMese(passo)
{
 mese+=passo;
 if(mese<1) { mese=12; anno--; }
 else if(mese>12) { mese=1; anno++; }
 if(anno<2007) { mese=1; anno=2007; }
 else if(anno>2100) { mese=12; anno=2100; }
 if(prelevaGiorni(mese,anno,tipo))
  ritardo=setTimeout(riempiCalendario,3000);
 else
  riempiCalendario();
}

function vaiA(ms,an)
{
 if(mese==ms && anno==an) return false;
 mese=ms; anno=an;
 if(mese<1) mese=1;
 else if(mese>12) mese=12;
 if(anno<2007) anno=2007;
 else if(anno>2100) anno=2100;
 if(prelevaGiorni(mese,anno,tipo))
  ritardo=setTimeout(riempiCalendario,3000);
 else
  riempiCalendario();
}

function riempiCalendario()
{
 if(ritardo) clearTimeout(ritardo);
 var cal=document.getElementById(calendario);
 var tbody=cal.getElementsByTagName("tbody").item(0);
 if(!tbody) return false;
 var p=cal.getElementsByTagName("p").item(0);
 if(!p) return false;
 var strong=p.getElementsByTagName("strong").item(0);
 if(!strong) return false;
 var primo=new Date(anno,mese-1,1);
 var ultimo=new Date(anno,mese,0);
 var primoGiorno=primo.getDay()-1;
 if(primoGiorno==-1) primoGiorno=6;
 var ultimoGiorno=ultimo.getDate();
 strong.firstChild.nodeValue=mesi[mese]+" "+anno;
 var tr,td,a,g,n;
 tr=tbody.childNodes;
 for(n=tr.length; n>0; n--)
  tbody.removeChild(tr[n-1]);
 tr=document.createElement("tr");
 riempi(tr,primoGiorno);
 for(g=1,n=primoGiorno; g<=ultimoGiorno; g++,n++)
 {
  if(n>=7)
  {
   tbody.appendChild(tr);
   tr=document.createElement("tr");
   n=0;
  }
  td=document.createElement("td");
  if(g+"/"+mese+"/"+anno==oggiData) td.className="oggi";
  if(!caricato || (caricato && giorni[g]))
  {
   a=document.createElement("a");
   a.setAttribute("href",g+"/"+mese+"/"+anno);
   a.appendChild(document.createTextNode(g));
   if(a.addEventListener) { a.addEventListener("click",scriviData,false); }
   else { a.onclick=scriviData; }
  }
  else
  {
   a=document.createElement("span");
   a.appendChild(document.createTextNode(g));
  }
  td.appendChild(a);
  tr.appendChild(td);
 }
 if(n<=7)
 {
  riempi(tr,7-n);
  tbody.appendChild(tr);
 }
}

function mostraCalendario(e)
{
 var a;
 if(!e) var e=window.event;
 if(e.target) i=e.target;
 else if(e.srcElement) i=e.srcElement;
 if(i.nodeType==1 && i.nodeName.toLowerCase()=="img")
 {
  if(dataAtt==i.parentNode.getAttribute("href")) nascondiCalendario();
  else
  {
   dataAtt=i.parentNode.getAttribute("href");
   var val=document.getElementById(dataAtt).value;
   if(val)
   {
    var dt=val.split("/");
    var ms=parseInt(dt[1],10);
    var an=parseInt(dt[2],10);
    if(ms>0 && ms<13 && anno>2006) vaiA(ms,an);
   }
   document.getElementById(calendario).style.display="block";
  }
 }
 if(e.preventDefault) e.preventDefault();
 if(e.stopPropagation) e.stopPropagation();
 else if(e.cancelBubble) e.cancelBubble();
 return false;
}

function nascondiCalendario()
{
 document.getElementById(calendario).style.display="none";
 dataAtt="";
}

function creaCalendario(idCal,tp,idDate)
{
 if(!document.getElementsByTagName) return;
 calendario=idCal;
 tipo=tp;
 var cal=document.getElementById(idCal);
 if(!cal) return;
 try { cal.innerHTML=""; } catch(e) { return; }
 var td,img,a;
 for(var id,n=0; n<idDate.length; n++)
 {
  id=idDate[n];
  td=document.getElementById(idCal+"-"+id);
  if(td)
  {
   img=document.createElement("img");
   img.src="img-stile/calendario.png";
   img.alt="C";
   a=document.createElement("a");
   if(a.addEventListener) { a.addEventListener("click",mostraCalendario,false); }
   else { a.onclick=mostraCalendario; }
   a.title="Calendario";
   a.href=id;
   a.className="calendario";
   a.appendChild(img);
   td.appendChild(a);
  }
 }
 cal.innerHTML='<p><a class="prec" href="#" onclick="return(cambiaMese(-1),false)">«</a><strong>Mese</strong><a class="succ" href="#" onclick="return(cambiaMese(+1),false)">»</a></p><table><colgroup><col span="6" class="feriali" /><col class="domenica" /></colgroup><thead><tr><th><abbr title="Lunedì">Lun</abbr></th><th><abbr title="Martedì">Mar</abbr></th><th><abbr title="Mercoledì">Mer</abbr></th><th><abbr title="Giovedì">Gio</abbr></th><th><abbr title="Venerdì">Ven</abbr></th><th><abbr title="Sabato">Sab</abbr></th><th class="domenica"><abbr title="Domenica">Dom</abbr></th></tr></thead><tbody></tbody></table><p id="calstato" class="stato"></p>';
 cambiaMese(0);
}
