
cal_days_labels = ['Sn', 'Mo', 'Ti', 'Wo', 'To', 'Fr', 'Sn'];
cal_months_labels = ['jannewaris', 'febrewaris', 'maart', 'april','maaie', 'juny', 'july', 'augustus', 'septimber','oktober', 'novimber', 'desimber'];
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//huidige datum
cal_current_date = new Date();
cal_active_dates = "";

Date.prototype.addMonths = function (n)
{
	var day = this.getDate();
	this.setMonth(this.getMonth() + n);
	if (this.getDate() < day)
	{
		this.setDate(1);
		this.setDate(this.getDate() - 1);
	}

	return this;
}

function nextMonth()
{
	var d = cal_current_date;
	cal_current_date =  d.addMonths(1);

	getActiveDate(cal_current_date.getMonth(), cal_current_date.getFullYear())
}

function previousMonth()
{
	var d = cal_current_date;
	cal_current_date =  d.addMonths(-1);

	getActiveDate(cal_current_date.getMonth(), cal_current_date.getFullYear())
}

function Calendar(month, year) {
  this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
  this.year  = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
  this.html = '';
}

Calendar.prototype.generateHTML = function(){
  // get first day of month
  var firstDay = new Date(this.year, this.month, 1);
  var startingDay = firstDay.getDay();

  // find number of days in month
  var monthLength = cal_days_in_month[this.month];

  // compensate for leap year
  if (this.month == 1) { // February only!
    if((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
      monthLength = 29;
    }
  }

  // do the header
  var monthName = cal_months_labels[this.month]
  var html = '<table border="0" cellspacing="0" cellpadding="0" class="calendar-table">';
  html += '<tr><td align="center" colspan="7" class="calenderHead"><img src="http://www.tryater.nl/images/head_agenda_fries.jpg" border="0"></td></tr>';
  html += '<tr>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '<td bgcolor="#000000" height="1"><img src="http://www.tryater.nl/images/transparant.gif" width="20" height="1"></td>';
  html += '</tr>';
  html += '<tr><td width="20" onClick="previousMonth()" style="cursor:pointer; _cursor:hand" class="cssMaand">&nbsp;<<</td><td align="center" colspan="5" class="cssMaand">';
    html +=  monthName + "&nbsp;" + this.year;
  html += '</td><td width="20" onClick="nextMonth()" style="cursor:pointer; _cursor:hand" class="cssMaand">>></td></tr>';
  html += '<tr class="calendar-header">';
  for(var i = 0; i <= 6; i++ ){
    html += '<td align="center" class="calendar-header-day">';
    html += cal_days_labels[i];
    html += '</td>';
  }
  html += '</tr><tr>';

  // fill in the days
  var day = 1;
  // this loop is for is weeks (rows)

  var arrData = cal_active_dates.split("_");
   var titel = ""
  for (var i = 0; i < 9; i++) {
    // this loop is for weekdays (cells)
    for (var j = 0; j <= 6; j++) {
		titel = "";
		if (day <= monthLength && (i > 0 || j >= startingDay))
		{
		  found = 0
		  for (var a = 0; a < arrData.length; a++)
		  {
			tempStr = "_"+arrData[a];

			if (tempStr.indexOf("_"+day+"|") >= 0)
			{
				tempStr = arrData[a];
				arrTemp = tempStr.split("|")
				if (titel == "")
					titel = arrTemp[1];
				else
					titel = titel + "<br>" + arrTemp[1];
				found = 1;
			}
          }
	      if (found == 1)
		  {
	      	html += '<td class="active-calendar-day" align="center" width="20" height="20" onMouseOver="this.className=\'active-calendar-day-hoover\'; ddrivetip(\''+titel+'\',\'yellow\', 300)" onClick="location.href=\'/content.php?agenda=1&maand='+(this.month+1)+'&jaar='+this.year+'\'" onMouseOut="this.className=\'active-calendar-day\';hideddrivetip()">';
		  }
	      else
		  {
	      	html += '<td class="calendar-day" align="center" width="20" height="20">';
		  }

	        html += day;
	        day++;
	    }
	    else
	    {
	    	html += '<td class="calendar-day" align="center" width="20" height="20">&nbsp;';
	    }
      html += '</td>';
    }
    // stop making rows if we've run out of days
    if (day > monthLength) {
      break;
    } else {
      html += '</tr><tr>';
    }
  }
  html += '</tr></table>';
  this.html = html;

  document.getElementById('calender').innerHTML = this.html;
}

Calendar.prototype.getHTML = function() {
  return this.html;
}


function getActiveDate(month, year)
{
	month = month+1;
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		http = new XMLHttpRequest();
	}

	http.abort();
	http.open("GET", "/getDate.php?month="+month+"&year="+year+"&a="+Math.floor(Math.random()*1100), true);

	http.send(null);

	http.onreadystatechange=function()
	{
		if (http.readyState==4)
		{
			cal_active_dates = URLDecode(http.responseText);
			var cal = new Calendar();
			cal.generateHTML();
		}
	}
}

if (!document.layers&&!document.all&&!document.getElementById)
	event="test"

function showtip(current,e,text)
{
	var ns = (document.layers) ? true : false ;
	var ie = (document.all) ? true : false;


	if (ie)
	{
		var mouseX = 0;
		mouseX=ie? event.clientX : e.clientX;
		var mouseY = 0;
		mouseY=ie? event.clientY : e.clientY;
	}
	else
	{
		mouseX = e.pageX
		mouseY = e.pageY
	}
	document.getElementById('calender_tooltip').innerHTML = text;
	document.getElementById('calender_tooltip').style.left = (mouseX+5) + "px";
	document.getElementById('calender_tooltip').style.top = (mouseY-380) + "px";
	document.getElementById('calender_tooltip').style.visibility="visible"

}

function hidetip()
{
	document.getElementById('calender_tooltip').style.visibility="hidden"
}

function URLDecode(encoded)
{
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
	   var ch = encoded.charAt(i);
	   if (ch == "+") {
		   plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return(plaintext);
}

var offsetxpoint=10 //Customize x offset of tooltip
var offsetypoint=-30 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false


function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth)
{
	if (ie||ns6)
	{
		var tipobj=document.all? document.all["calender_tooltip"] : document.getElementById? document.getElementById("calender_tooltip") : ""
	}

	if (ns6||ie)
	{
		tipobj.innerHTML=thetext
		enabletip=true
		return false
	}
}

function positiontip(e)
{
	if (enabletip)
	{
		if (ie||ns6)
		{
			var tipobj=document.all? document.all["calender_tooltip"] : document.getElementById? document.getElementById("calender_tooltip") : ""
		}

		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
		else if (curX<leftedge)
		tipobj.style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		tipobj.style.left=curX+offsetxpoint+"px"
		
		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
		tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
		else
		tipobj.style.top=curY+offsetypoint+"px"
		tipobj.style.visibility="visible"
	}
}

function hideddrivetip()
{
	if (ns6||ie)
	{
		if (ie||ns6)
		{
			var tipobj=document.all? document.all["calender_tooltip"] : document.getElementById? document.getElementById("calender_tooltip") : ""
		}

		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
	}
}

document.onmousemove=positiontip