// **********************************************
// Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
// Script featured on Dynamic Drive (http://www.dynamicdrive.com)
// This notice must stay intact for use
// Visit http://www.dynamicdrive.com/ for full source code
// ********************************************** 

// Funzione buildCal con seguenti parametri :
//          1° parametro   mese da visualizzare 1=Gen 6=Giu 12=Dic
//          2° parametro   anno del mese da visualizzare
//          3° parametro   nome stile CSS della tabella esterna del calendario
//          4° parametro   nome stile CSS della riga mese/anno del calendario
//          5° parametro   nome stile CSS della riga nomi giorni della settimana
//          6° parametro   nome stile CSS della riga giorni in dettaglio
//          7° parametro   spessore di separazione di tutte le celle
//		                  (equivalente parametro border delle tabelle)

function buildCal(m, y, cM, cH, cDW, cD, brdr)
{
var mn=['GENNAIO','FEBBRAIO','MARZO','APRILE','MAGGIO','GIUGNO','LUGLIO','AGOSTO','SETTEMBRE','OTTOBRE','NOVEMBRE','DICEMBRE'];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

// Italian fixed holidays (day+month)
var hdf=[1,6,25,1,2,15,1,8,25,26];
var hmf=[1,1,4,5,6,8,11,12,12,12];

// Italian variable holidays (next day of Easter) until 2050  (day+month+year)
var hdv=[5,25,9,1,21,6,28,17,2,22,13,5,18,10,1,21,6,29,17,2,22,14,29,18,10,26,14,6,26,11,2,22,7,30,18,10,26,15,6,19,11];
var hmv=[4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,3,4,4,4,4,3,4,4,3,4,4,4,4,4,4,4,3,4,4,3,4,4,4,4];
var hyv=[2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030,2031,2032,2033,2034,2035,2036,2037,2038,2039,2040,2041,2042,2043,2044,2045,2046,2047,2048,2049,2050];

var oD = new Date(y, m-1, 0); //DD replaced line to fix date bug when current day is 31st
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

var todaydate=new Date() //DD added
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
var t='<div><table class="'+cM+'" cols="7" border="'+brdr+'"><tr >';
t+='<td colspan="7" class="'+cH+'">'+mn[m-1]+' - '+y+'</td></tr><tr >';
for(s=0;s<12;s=s+2)t+='<td class="'+cDW+'">'+"LuMaMeGiVeSa".substr(s,2)+'</td>';
var sndyw='<span id="sundayw">Do</span>' 
t+='<td class="'+cDW+'">'+sndyw+'</td>';   // cDW sunday with other color 
t+='</tr><tr >';

for(i=1;i<=42;i++)
{
var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';

if((i)%7==0&&(x!='&nbsp;'))   // Verify sunday 
  if (x!=scanfortoday)
    {  x='<span id="sunday">'+x+'</span>'  }  //DD added for sunday
  else
    {  x='<span id="todayspec">'+x+'</span>'  }

var scanforhdf=0
for(j=0;j<=9;j++)  // Verify fixed holiday midweek
{
scanforhdf=(hmf[j]==m&&hdf[j]==x&&(x!='&nbsp;')&&((i)%7!=0))?x:0
if (x==scanforhdf)
   if(x!=scanfortoday)
     { x='<span id="holiday">'+x+'</span>' } 
   else
     { x='<span id="todayspec">'+x+'</span>'  }
}

var scanforhdv=0
for(k=0;k<=40;k++)  // Verify variable holiday midweek
{
scanforhdv=(hyv[k]==y&&hmv[k]==m&&hdv[k]==x&&(x!='&nbsp;')&&((i)%7!=0))?x:0
if (x==scanforhdv) 
   if(x!=scanfortoday)
     { x='<span id="holiday">'+x+'</span>' } 
   else
     { x='<span id="todayspec">'+x+'</span>'  }
}

//   Verify if normal day is today  ( "x" is changed with "span" for special or sunday day ) 
if (x==scanfortoday)
{
x='<span id="today">'+x+'</span>'   //DD added for today
}

t+='<td class="'+cD+'">'+x+'</td>';
if(((i)%7==0)&&(i<36))t+='</tr><tr >';
}
return t+='</tr></table></div>';
}
