var title="";
//Use brdr for border
function buildCal(m, y, cM, cH, cDW, cD, brdr, datevalues, linktouse){
var mn=['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','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(); //DD replaced line to fix date bug when current day is 31st
if (oD.od == 0) oD.od = 7; //LR fix to handle Sundays on mediterranean
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 class="'+cM+'"><table class="'+cM+'" cols="7" cellpadding="0" border="0" cellspacing="0" width="100%"><tr align="center">';
t+='<td class="'+cH+'" width="10"><a href="' + linktouse + 'Left#Navi" class="Navi" style="margin-left:0px;"><</a></td><td colspan="5" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td><td class="'+cH+'" width="10"><a href="' + linktouse + 'Right#Navi" class="Navi" style="margin-left:0px;">></a></td></tr><tr align="center">';
for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"LMMJVSD".substr(s,1)+'</td>';
t+='</tr><tr align="center">';
for(i=1;i<=42;i++){
	title = "";
	var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
	var range=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
	var version = datefilters(datevalues,m,x,y);
	if (version == "Evento") {
		if (x==scanfortoday)//DD added
			t+='<td class="'+version+'"><span class="today"><a href="#" class="Evento" style="margin-left:0px;" title="' + title + '" alt="' + title + '" onclick=event_desc("'+m+'","'+x+'","'+y+'")>'+x+'</a></span></td>';
		else
			t+='<td class="'+version+'"><a href="#" title="' + title + '" class="Evento" style="margin-left:0px;" alt="' + title + '" onclick=event_desc("'+m+'","'+x+'","'+y+'")>'+x+'</a></td>'; 
	}
	else if (version == "Curso") {
		if (x==scanfortoday)//DD added
			t+='<td class="'+version+'"><span class="today"><a href="#" class="Curso" style="margin-left:0px;" title="' + title + '" alt="' + title + '" onclick=event_desc("'+m+'","'+x+'","'+y+'")>'+x+'</a></span></td>';
		else
			t+='<td class="'+version+'"><a href="#" title="' + title + '" class="Curso" style="margin-left:0px;" alt="' + title + '" onclick=event_desc("'+m+'","'+x+'","'+y+'")>'+x+'</a></td>'; 
	}
	else {
		if (x==scanfortoday)//DD added
			t+='<td class="'+version+'"><span class="today">'+x+'</span></td>';
		else
			t+='<td class="'+version+'">'+x+'</td>'; 
	}
	if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
	}
	return t+='</tr></table></div>';
}

function datefilters(dte,mnth,cdydte,yr){
	
	var tempdatestouse = new Array(50);
	var classreturn = "Libre";
	var tempdatefrom = new Date();
	var tempdateto = new Date();
	var tempstyle = "Libre";
	var currentamountofdates = 0;
	
	for (currentvalue=0;currentvalue<80;currentvalue++) {
		tempdatestouse[currentvalue] = "";
		if (dte.indexOf("-") > 0) {
			tempdatestouse[currentvalue] += dte.substr(1, dte.indexOf("]") - 1);
			currentamountofdates++;
		}
		else {
			break;
		}
		dte = dte.substr(dte.indexOf("]")+1, dte.length);		
	}
	
	for(curdte=0;curdte<currentamountofdates;curdte++){
		tempdatefrom = tempdatestouse[curdte].substr(0, 10);
		tempdateto = tempdatestouse[curdte].substr(11, 10);
		//if (title.indexOf(tempdatestouse[curdte].substr(tempdatestouse[curdte].indexOf("-") + 1, tempdatestouse[curdte].indexOf("@"))) == -1)
			//title = title + tempdatestouse[curdte].substr(tempdatestouse[curdte].indexOf("-") + 1, tempdatestouse[curdte].indexOf("@")) +  "&#13;";
		title = "Pinche aquí para ver los cursos/eventos de este día."
		tempstyle = tempdatestouse[curdte].substr(tempdatestouse[curdte].indexOf("@") + 1,tempdatestouse[curdte].length);
		currentdate = mnth + "/" + cdydte + "/" + yr;
		if(dateWithin(tempdatefrom,tempdateto,currentdate))
		{
			classreturn = tempstyle;
		}
		else 
		{
			classreturn = "Libre";
		}
		
		if (classreturn != "Libre")
			break;
	}
	
	return classreturn;
}

function dateWithin(beginDate,endDate,checkDate) {
        var b,e,c;
        b = Date.parse(beginDate);
        e = Date.parse(endDate);
        c = Date.parse(checkDate);
        if((c <= e && c >= b)) {
                return true;
        }
        return false;
}

function event_desc(mnth,cdydte,yr) {
	var currenturl = 'popup_evento.php?mnth='+mnth+'&cdydte='+cdydte+'&yr='+yr+'&lang=es';
	var win = window.open(currenturl,'Popup','height=350,width=450,left=10,top=10,scrollbars');
}
