function buildCal(m, y, cM, cH, cDW, cD, brdr,FE,FI,F,A,E,directori,site,lang){
    //m = 2;
    //y = 2009;
    var mn 
    if(lang=='cat'){
        mn= ['Gener', 'Febrer', 'Març', 'Abril', 'Maig', 'Juny', 'Juliol', 'Agost', 'Septembre', 'Octubre', 'Novembre', 'Desembre'];
    }else{
        mn = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre'];
    }
    var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
    var oD = new Date(y, m-1, 1); //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 oDmes1 = new Date(y, m+1 , 1);
    var mmes1 = oDmes1.getMonth();
    var ymes1 = oDmes1.getFullYear();
    var oDmenys1 = new Date(y, m-1, 1);
    var mmenys1 = oDmenys1.getMonth();
    var ymenys1 = oDmenys1.getFullYear();
    var todaydate = new Date() //DD added
    var linkcal = '';
    if (directori.toUpperCase() == 'CONSELL') {
        linkcal='<a href="/' + site + '/CommonFiles/calendarLlistat.asp">'
    } else {
        linkcal = '<a href="/' + site + '/fullcalendar/calendar.asp">'
    }
    if(m==0){m=12;y=y-1;}
    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 class="' + cM + '" id="trainingcalendar"><table class="' + cM + '" cols="7" cellpadding="0" border="' + brdr + '" cellspacing="0" align="center"><tr align="center">';
    t += '<td align="center" class="' + cH + '"><a href="#" onclick="javascript:document.getElementById(\'trainingcalendar\').innerHTML=buildCal(' + mmenys1 + ',\'' + ymenys1 + '\',\'main\',\'month\',\'daysofweek\',\'days\',0,\'' + FE + '\',\'' + FI + '\',\'' + F + '\',\'' + A + '\',\'' + E + '\',\'' + directori + '\',\'' + site + '\',\'' + lang + '\');"><</a></td><td colspan="5" nowrap align="center" class="' + cH + '">' + linkcal + '<div class="' + cH + '">' + mn[m - 1].toUpperCase() + '\'' + String(y).substring(2,4) + '</div></A></td><td align="center" class="' + cH + '"><a href="#" onclick="javascript:document.getElementById(\'trainingcalendar\').innerHTML=buildCal(' + mmes1 + ',\'' + ymes1 + '\',\'main\',\'month\',\'daysofweek\',\'days\',' + brdr + ',\'' + FE + '\',\'' + FI + '\',\'' + F + '\',\'' + A + '\',\'' + E + '\',\'' + directori + '\',\'' + site + '\',\'' + lang + '\');">></A></td></tr><tr align="center">';
    for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"LMXJVSD".substr(s,1)+'</td>';
    t+='</tr><tr align="center">';
    var cur = 0;
    for(i=0;i<=42;i++){
	    var x=((i-oD.od+1>=-1)&&(i-oD.od+1<dim[m-1]))? i-oD.od+2 : '&nbsp;';
	    cur++;
	    if (i == 0 && (x==0||x=='&nbsp;')) {
	    }
	    else {
	        if (i == 0 && x == 1) t += '<td class="' + cD + '"></td><td class="' + cD + '"></td><td class="' + cD + '"></td><td class="' + cD + '"></td><td class="' + cD + '"></td><td class="' + cD + '"></td>';
	        if (x == 0) x = '&nbsp;';
	        if (x == scanfortoday) //DD added
	            x = '<span id="today">' + linkcal + x + '</a></span>' //DD added
	        if (FI.indexOf("|" + x + "/" + m + "/" + y + "|") > -1)
	            x = '<span id="FI">' + linkcal + x + '</a></span>' //DD added
	        if (F.indexOf("|" + x + "/" + m + "/" + y + "|") > -1)
	            x = '<span id="F">' + linkcal + x + '</a></span>' //DD added
	        if (FE.indexOf("|" + x + "/" + m + "/" + y + "|") > -1)
	            x = '<span id="FE">' + linkcal + x + '</a></span>' //DD added
	        if (E.indexOf("|" + x + "/" + m + "/" + y + "|") > -1)
	            x = '<span id="E">' + linkcal + x + '</a></span>' //DD added
	        if (A.indexOf("|" + x + "/" + m + "/" + y + "|") > -1)
	            x = '<span id="A">' + linkcal + x + '</a></span>' //DD added
	        t += '<td class="' + cD + '">' + x + '</td>';
	        if (((i) % 7 == 0) && (i < 36)){
	            t += '</tr><tr align="center">';
	            cur = 0;
	        }
	        if ((x == '&nbsp;') && (i > 25)) i = 43;
	    }
	}
	var rem = 7 - cur;
	if (rem < 7) {
        for (i=0;i<rem;i++){
            t += '<td class="' + cD + '"></td>';
        }
	}
    return t += '</tr></table></div>';
    
}

