
var date = new Date();
var hilited = null;

function eventCal() {
	var arrayMonths = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	today = new Date();
	
	var text ='<p class="current"><strong>Click the links to view<br/>events for the month<strong><br />';

	var mm = today.getMonth();
	var yy = today.getFullYear();
	//showEvents2(yy,mm,1);
	showAll2();
	
	var tm = mm;
	var ty = yy;
	
	//today.setMonth(today.getMonth() - 6);
	today.setMonth(today.getMonth() -1);
	
	for (i = 0; i < 18; i++) {
		today.setMonth(today.getMonth() + 1);
		mm = today.getMonth();
		yy = today.getFullYear();
		
		if ((mm == tm) && (mm == ty)) {
			text += '<a href="#" class="hilited" onclick="showEvents2('+ yy +',' + mm + ',1); hilite(this);">' + arrayMonths[mm] + ' ' + yy + '</a><br />';
		}
		else {
			text += '<a href="#" onclick="showEvents2('+ yy +',' + mm + ',1); hilite(this);">' + arrayMonths[mm] + ' ' + yy + '</a>';
			text += '<br />';
		}
	}
	
	text += '<br /><a href="#" onclick="showAll2(); hilite(this)">Show all events</a></p>';
	
	document.getElementById('monthList').innerHTML = text;
}

function up()
{
	t = document.getElementById("eventlist").style.top;
	t -=10;
	if (t < 0) t = 0;
		document.getElementById("eventlist").style.top = t;
}

function down()
{
	document.getElementById("eventlist").style.top = "100px";
}


function hilite(obj) {
	if (hilited != null) {
		hilited.className = "";
	}
	
	obj.className = "hilited";
	hilited = obj;
}

function resetMonth() {
	date = new Date();
	showCalendar();
}

function prevMonth() {
	date.setMonth(date.getMonth() - 1);
	showCalendar();
}

function nextMonth() {
	date.setMonth(date.getMonth() + 1);
	showCalendar();
}

function mmov() {
	document.getElementById('mmdrop').innerHTML = '<select name="select"><option>January</option><option>February</option><option>March</option><option>April</option><option>May</option><option>June</option><option>July</option><option>August</option><option>September</option><option>October</option><option>November</option><option>December</option></select>'	
}

function mmot() {
	var arrayMonths = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	document.getElementById('mmdrop').innerHTML = arrayMonths[date.getMonth()];
	
}

function yyov() {
	document.getElementById('yydrop').innerHTML = '<select name="select2"><option>2006</option><option>2007</option><option>2008</option></select>';
}

function yyot() {
	document.getElementById('yydrop').innerHTML = date.getFullYear();
}

	

function showCalendar() {
	/* variables that we need */
	var arrayDays = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	var tmpDate = new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0);
	var dayStart = tmpDate.getDay();
	tmpDate = new Date(date.getFullYear(), date.getMonth() + 1, 0, 0, 0, 0);
	var daysInMonth = tmpDate.getDate();
	var daysWritten = 0;
	
	/* start the table */
	var text = '<table align="center" id="caltable" cellpadding="0" cellspacing="0"><tr>'
	//text += '<td id="prev" align="left" onclick="prevMonth()"><h6>&lt;</h6></td>'
	//text += '<td colspan="4" bgcolor="#FFFFFF">&nbsp;</td>'
	text += '<td colspan="7" class="headings" id="head" align="center"><span id="mmdrop"></span>&nbsp;<span id="yydrop"></span></td>'
	//text += '<td id="next" align="right" onclick="nextMonth()"><h6>&gt;</h6></td>'
	text += '</tr>'
	
	/* write the day header */		
	text += '<tr>';
	for (i = 0; i < 7; i++) {
		text += '<td align="center" class="dayhead">' + arrayDays[i] + '</td>';
	}
	text += '</tr><tr>';
	
	if ((dayStart + daysInMonth) <= 35)
		var numcells = 35;
	else
		var numcells = 42;
	
	/* now draw the 42 cells */
	for (i = 0; i < numcells; i++) {	
		if ((dayStart > i) || (daysWritten >= daysInMonth) ){
			text += '<td class="nonmonthdays">&nbsp;</td>';
		}
		else {
			daysWritten++;
			text += '<td align="right" class="monthdays">' + daysWritten + '</td>';
		}
		if ( ((i+1) % 7) == 0 ) {
			text += '</tr><tr>';
		}
	}
	
	//text += '</tr><tr><td colspan="7" onclick="resetMonth()" id="todaybox"><h6>Jump to Current Month</h6></td></tr>'
	text += '</tr></table>';
	
	text += '<table align="center" id="calnav"><tr><td width="5">&nbsp;</td>';
	text += '<td id="prev" onclick="prevMonth()">&lt;&lt;</td>';
	text += '<td onclick="resetMonth()" id="todaybox">Current Month</td>';
	text += '<td id="next" onclick="nextMonth()">&gt;&gt;</td>';
	text += '<td width="5">&nbsp;</td></tr></table>';
	
//	text += '<h6 onclick="up()">up</h6>';
//	text += '<h6 onclick="down()">down</h6>';
	//text += '<div style="top:0px" id="eventlist"></div>';
	text += '<div id="eventlist"></div>';
	
	/* sent it to the innerHTML */	
	document.getElementById('calendarCal').innerHTML = text;

	/* add the fun DOM stuff */
	document.getElementById('head').style.textAlign = "center";
	document.getElementById('todaybox').style.textAlign = "center";
//	document.getElementById('prev').onClick = "prevMonth";
//	document.getElementById('next').onClick = "nextMonth";
//	document.getElementById('mmdrop').innerHTML = arrayMonths[date.getMonth()];
//	document.getElementById('yydrop').innerHTML = date.getFullYear();

	mmot();
	yyot();

	document.getElementById('mmdrop').style.display = "inline";
	document.getElementById('yydrop').style.display = "inline";

	document.getElementById('mmdrop').onMouseOver = "mmov()";
	document.getElementById('yydrop').onMouseOver = "yyov()";
	document.getElementById('mmdrop').onMouseOut = "mmot()";
	document.getElementById('yydrop').onMouseOut = "yyot()";

	document.getElementById('mmdrop').width = "50%";
	document.getElementById('yydrop').width = "50%";
	
	showEvents(date.getFullYear(), date.getMonth(), 1)
}

// AJAXY

	  /**** Reads Events from XML ****/	
	  function showEvents(yy,mm, durm) {
		var url="http://kendev1/JB/calendar/getevents.php?yy=" + yy + "&mm=" + mm + "&durm=" + durm + "&sh=1";
		createRequest()
		xmlHttp.open("GET", url , true)

		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					document.getElementById("eventlist").innerHTML = xmlHttp.responseText 
				} else {
					alert("Error. Status is " + xmlHttp.status);
				}
			}
		};
		xmlHttp.send(null)
	  }

	  function showEvents2(yy,mm, durm) {
		var url="http://kendev1/JB/calendar/getevents.php?yy=" + yy + "&mm=" + mm + "&durm=" + durm + "&sh=0";
		createRequest()
		xmlHttp.open("GET", url , true)

		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					document.getElementById("eventlist2").innerHTML = xmlHttp.responseText 
				} else {
					alert("Error. Status is " + xmlHttp.status);
				}
			}
		};
		xmlHttp.send(null)
	  }


	  function showAll() {
		  window.location = "kendev1/JB/index.php?option=com_content&task=category&sectionid=12&id=110&Itemid=39"
/*	  	  today = new Date();
		  yy = today.getFullYear();
		  mm = today.getMonth();
  		  showEvents(yy, mm-12, 36);*/
	  }

	  function showAll2() {
	  	  today = new Date();
		  yy = today.getFullYear();
		  mm = today.getMonth();
  		  showEvents2(yy, mm-12, 36);
	  }


function createRequest() {
  try {
    xmlHttp = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        xmlHttp = false;
      }
    }
  }

  if (!xmlHttp)
    alert("Error. Your browser does not support this page.");
}
