//Constructor
//Gets the browser specific XmlHttpRequest Object
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		//
	}
}

function loadEvents(month, year){
	//Gets the current email list
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//alert('getEvents.php?month=' + month + '&year=' + year);´

	     receiveReq.open("GET", 'getEvents.php?month=' + month + '&year=' + year, true);

		receiveReq.onreadystatechange = handleLoadEvents; 
		receiveReq.send(null);
		document.getElementById('calendar_status').innerHTML = 'Cargando Eventos...';
		
		
		
	}		
}

function handleLoadEvents(){
	if (receiveReq.readyState == 4) {		
		var xmldoc = receiveReq.responseXML;
		event_node = xmldoc.getElementsByTagName("event");
		var n_events = event_node.length
	
		for (i = 0; i < n_events; i++){
			myDay = event_node[i].getAttribute('day');
			myMonth = event_node[i].getAttribute('month');
			myYear = event_node[i].getAttribute('year');
			myName = event_node[i].getAttribute('name');

document.getElementById('calendar_day' + myDay).className = "calendar_event";

document.getElementById('calendar_day' + myDay).setAttribute("onclick", 
     "window.location='contenido.php?pag=actividades&d="     + myDay+    "&m="      +myMonth+    "&y="     +myYear+     "&name="    +myName+      "'");

	



}
		document.getElementById('calendar_status').innerHTML = '';
	}
}

function calendar(id,d,p){
	this.id = id;
	this.dateObject = d;
	this.pix = p;
	this.write = writeCalendar;
	this.length = getLength;
	this.month = d.getMonth();
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	this.getFormattedDate = getFormattedDate;
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);
}

var days = new Array('Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado');
var months = new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');

function getFormattedDate(){
	return days[this.day] + ', ' + months[this.month] + ' ' + this.date + ', ' + this.year;
	//return this.month + '/' + this.date + '/' + this.year;
}

function writeCalendar(){
	var calString = '<div id="calContainer">';
	//write calendar table
	calString += '<table id="cal' + this.id + '" class="calendar_container" cellspacing="0" width="235" height="282" style="border:0px;">';
	
	//write Header
	calString += '<tr><td width="100%"><a href="contenido.php?pag=actividades" class="tituloEvento" ><img src="images/calendario_header.jpg" border="0"/></a></td></tr>';
	
	//write nav bar
	calString += '<tr><td width="100%"><table cellpadding="0" cellspacing="0"><tr>';
	calString += '<td width="55"><img src="images/calendario_boton_izq.jpg" style="cursor:pointer;" border="0" onClick="changeMonth(-1,\'' + this.id + '\')" /></td>';
	calString += '<td class="calendar_month" width="125" align="center">' + months[this.month] + ' ' + this.year + '</td>';
	calString += '<td width="55"><img src="images/calendario_boton_der.jpg" style="cursor:pointer;" border="0" onClick="changeMonth(1,\'' + this.id + '\')" /></td>';
	calString += '</tr></table></td></tr>';
	
	//write main content table
	calString += '<tr><td width="100%"><table width="100%" cellpadding="0" cellspacing="0">';
	
	//write left margin
	calString += '<tr><td width="22">&nbsp;</td>';
	
	//write content cell
	calString += '<td><table width="191" cellpadding="0" cellspacing="0">';
	
	// write a row containing days of the week
	calString += '<tr>';  
	for(i=0;i<days.length;i++){
	   calString += '<td height="15" class="calendar_dayHeader">' + days[i].substring(0,1) + '</td>';
	}
	calString += '</tr>'; 
	
	// write the body of the calendar
	calString += '<tr>';
	// create 6 rows so the calendar doesn't resize
	for(j=0;j<42;j++){
	   var displayNum = (j-this.firstDay+1);
	   if(j<this.firstDay){
		  // write the leading empty cells
		  calString += '<td width="24" height="24" class="calendar_empty">&nbsp;</td>';
	   }else{
		   	if(displayNum==this.date){
		  		calString += '<td width="24" height="24" id="calendar_day' + displayNum + '" class="calendar_days" ' + 'onClick="javascript:window.location=\'eventos.php?fecha=' + this.date + '-' + this.month + '-' + this.year + '\'">' + displayNum + '</td>';
	   		}else{
				if(displayNum > this.length()){
		  			// Empty cells at bottom of calendar
		  			calString += '<td width="24" height="24">&nbsp;</td>';
	   			}else{
		  			// the rest of the numbered cells
		 			// calString += '<td width="24" height="24" id="" class="calendar_days" onClick="javascript:changeDate(this,\'' + this.id + '\')">' + displayNum + '</td>';
		 			calString += '<td width="24" height="24" id="calendar_day' + displayNum + '" class="calendar_days">' + displayNum + '</td>';
				}
			}
	   }
	   if(j%7==6){
		  calString += '</tr><tr><td colspan="7" height="1"></td></tr><tr>';
	   }
	}
	
	// close the last number row
	calString += '</tr>';
	
	//write bottom margin
	calString += '<tr><td width="100%" colspan="7" align="center" height="9"><div id="calendar_status" style="font-size:10px; font-weight:normal; color:#666;"></div></td></tr>';
	
	//close content cell
	calString += '</table></td>';
	
	//write right margin and close row
	calString += '<td width="22">&nbsp;</td></tr>';
	
	//close main content table
	calString += '</table>';
	
	//write bottom margin
	//calString += '<tr><td width="100%" height="9"><div id="calendar_status" style="font-size:10px; font-weight:normal; color:#666;"></div></td></tr>';
	
	//close calendar
	calString += '</table>';
	calString += '</div>';
	return calString;
} 

function getLength(){
	//thirty days has September...
	switch(this.month){
		case 1:
			if((this.dateObject.getFullYear()%4==0&&this.dateObject.getFullYear()%100!=0)||this.dateObject.getFullYear()%400==0)
				return 29; //leap year
			else
				return 28;
		case 3:
			return 30;
		case 5:
			return 30;
		case 8:
			return 30;
		case 10:
			return 30
		default:
			return 31;
	}
}
function changeDate(td,cal){
	//Some JavaScript trickery
	//Change the cal argument to the existing calendar object
	//This is why the first argument in the constructor must match the variable name
	//The cal reference also allows for multiple calendars on a page
	cal = eval(cal);
	document.getElementById(cal.id + "selected").className = "calendar_days";
	document.getElementById(cal.id + "selected").id = "";
	td.className = "calendar_today";
	td.id = cal.id + "selected";
	//set the calendar object to the new date
	cal.dateObject.setDate(td.firstChild.nodeValue);
	cal = new calendar(cal.id,cal.dateObject,cal.pix);
	//here is where you could react to a date change - I'll just display the formatted date
	//alert(cal.getFormattedDate());
}

function changeMonth(mo,cal){
	//more trickery!
	cal = eval(cal);
	//The Date object is smart enough to know that it should roll over in December
	//when going forward and in January when going back
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar(cal.id,cal.dateObject,cal.pix);
	cal.formattedDate = cal.getFormattedDate();
	document.getElementById('calContainer').innerHTML = cal.write();
	loadEvents(cal.month, cal.year);
	
}

