// JavaScript Document

function getPlanner(index) {
	
        var httpRequest;
		var url;
		var monthindex;
		var  yearindex;
		var month;
		var year;
		var date = new Date();
		yearindex = document.getElementById('yearselect').selectedIndex
		monthindex = document.getElementById('monthselect').selectedIndex
		if(index == 'next')
			{
				if(document.getElementById('monthselect').selectedIndex < 11)
					{
						monthindex = document.getElementById('monthselect').selectedIndex+1;
					}
					else
					{
						monthindex = 0
						if(document.getElementById('yearselect').selectedIndex == 2)
							{
								yearindex = 0;
							}
							else
							{
								yearindex = yearindex + 1;
							}	
					}
			}
		if(index == 'previous')
			{
				if(document.getElementById('monthselect').selectedIndex > 0)
					{
						monthindex = document.getElementById('monthselect').selectedIndex-1
					}
					else
					{
						monthindex = 11
						if(document.getElementById('yearselect').selectedIndex == 0)
							{
								yearindex = 0;
								monthindex = 0;
							}
							else
							{
								yearindex = document.getElementById('yearselect').selectedIndex-1;
							}
						
					}
			}
	if(index == 'today')
		{
			yearindex = 0;
			monthindex = date.getMonth();
		}

		url = '/agenda/planner.php?year='+yearindex+'&month='+monthindex;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/html');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }


		httpRequest.onreadystatechange = function() 
			{
				if(httpRequest.readyState <= 3)
					{
						document.getElementById('planner-header').innerHTML = "planner (laden...)";	
					}
				if(httpRequest.readyState == 4)
					{
						if(httpRequest.status == 200)
							{
								document.getElementById('planner').innerHTML = httpRequest.responseText;
								document.getElementById('planner-header').innerHTML = "planner";
								highlightToday(monthindex,yearindex);
							}
							else
							{
								alert('There was a problem with the request.');	
							}
					}
			}
	httpRequest.open('GET', url, true);
    httpRequest.send(null);
}

function highlightToday(month,year) {
	var date = new Date();
	if(month == date.getMonth() && year == 0)
		{
			today = document.getElementById(date.getDate());
			today.style.backgroundColor = 'rgb(48,121,182)';
			today.style.color = '#ffffff';
			//today.style.fontWeight = 'normal';
		}
}

