
var originalMonth = null;
var currentMonth = null;
var calendarMonth = null;
var chosen_date;
var today_date;

function addCalendar() {
	var date_select = document.getElementById('date_select');
	
	if(date_select.value != 'later') return; //-----------------------------------
	
	var calendar_date = document.getElementById('calendar_date');
	
	date_select.style.display = 'none';
	date_select.options[0].selected = true;
	date_select.name = 'select_date';
	calendar_date.name = 'date';
	calendar_date.value = date_select.value;
	
	var calendar = document.getElementById('calendar');
	
	calendar.style.display = '';
	
	var timestamp = today_timestamp;
	today_date = new Date();
	today_date.setTime(timestamp * 1000);
	

	chosen_date = new Date();
	chosen_date.setTime(timestamp * 1000);
	
	updateLabel();
	updateDays();
	chooseDay(today_date.getDate());
}




function monthPlus() {
	var temp = new Date(chosen_date.getFullYear(), chosen_date.getMonth() + 1, 1, 0, 0, 0);
	chosen_date = temp;
	
	if(chosen_date.getFullYear() - today_date.getFullYear() == 1 && chosen_date.getMonth() - today_date.getMonth() == 0) {
		document.getElementById('right_arrow').style.display = 'none';
	}
	
	updateLabel();
	updateDays();
	chooseDay(1);	

	document.getElementById('left_arrow').style.display = '';
}


function monthMinus() {	
	var temp = new Date(chosen_date.getFullYear(), chosen_date.getMonth() - 1, 1, 0, 0, 0);
	chosen_date = temp;
	
	if(chosen_date.getMonth() == today_date.getMonth() && chosen_date.getFullYear() == today_date.getFullYear()) {
		document.getElementById('left_arrow').style.display = 'none';
	}
	
	updateLabel();
	updateDays();
	chooseDay(1);		

	document.getElementById('right_arrow').style.display = '';	
}


function chooseDay(date) {
	if(chosen_color == null) chosen_color = '#cc9999'
	
	var temp = new Date(chosen_date.getFullYear(), chosen_date.getMonth(), date, 0, 0, 0);
	while(temp.getDay() == 2 || temp.getDay() == 3 || (temp.getDate() < today_date.getDate() && temp.getMonth() == today_date.getMonth())) {				
		temp.setDate(temp.getDate() + 1);				
		date = temp.getDate();
	}

	var elem = null;
	for(i = 0; i <= 31; i++) {
		elem = document.getElementById('date' + i);
		if(elem != null) {
			elem.style.backgroundColor = '';
			elem.style.color = '';
		}
	}


	for(i = 0; i <= 31; i++) {
		elem = document.getElementById('date' + (date+i));
		if(elem != null) break;
	}
	if(elem != null) {
		elem.style.backgroundColor = '#4d4d4d';	
		elem.style.color = '#999999';
		chosen_date = new Date(chosen_date.getFullYear(), chosen_date.getMonth(), date, 0, 0, 0);
		document.getElementById('calendar_date').value = chosen_date.getTime() / 1000;
	}
	
	if(chosen_date.getFullYear() - today_date.getFullYear() == 1 && chosen_date.getMonth() - today_date.getMonth() == 0) {
		document.getElementById('right_arrow').style.display = 'none';
	}
	if(chosen_date.getMonth() == today_date.getMonth() && chosen_date.getFullYear() == today_date.getFullYear()) {
		document.getElementById('left_arrow').style.display = 'none';
	}
}

function updateDays() {
	var temp = new Date(chosen_date.getFullYear(), chosen_date.getMonth(), 1, 0, 0, 0);
	var temp2 = new Date(chosen_date.getFullYear(), chosen_date.getMonth(), 1, 0, 0, 0);
	
	stop_writing_numbers = false;
	for(i = 1; i<=5; i++) {
		for(j = 1; j<=5; j++) {
			document.getElementById('' + i + j).innerHTML = '';
			temp2.setDate(temp.getDate());
			
			while(temp.getDay() == 2 || temp.getDay() == 3) {				
				temp.setDate(temp.getDate() + 1);				
				if(temp2.getDate() - temp.getDate() > 0) stop_writing_numbers = true;
			}
			
			if(temp.getDay() == getWeekday(j) && stop_writing_numbers == false) {
				if(today_date.getDate() <= temp.getDate() && temp.getMonth() == today_date.getMonth() && temp.getFullYear() == today_date.getFullYear()) {
					document.getElementById('' + i + j).innerHTML = '<a id=\"date' + temp.getDate() +'\" href=\"javascript: chooseDay(' + temp.getDate() + ', ' + i + j + ');\" class=\"calendar\">' + temp.getDate() + '</a>';
				} 
				if(temp.getMonth() != today_date.getMonth() || temp.getFullYear() != today_date.getFullYear()) {
					document.getElementById('' + i + j).innerHTML = '<a id=\"date' + temp.getDate() +'\" href=\"javascript: chooseDay(' + temp.getDate() + ', ' + i + j + ');\" class=\"calendar\">' + temp.getDate() + '</a>';
				}
				temp.setDate(temp.getDate() + 1);
				if(temp2.getDate() - temp.getDate() > 0) stop_writing_numbers = true;
			}

			
		}
	}
}


function updateLabel() {
	document.getElementById('calendar_month').innerHTML = getGermanMonth(chosen_date.getMonth()) + ' ' + chosen_date.getFullYear();
}


function getWeekday(param) {
	if(param == 1) return 1;
	if(param == 2) return 4;
	if(param == 3) return 5;
	if(param == 4) return 6;
	if(param == 5) return 0;
}


function getGermanMonth(param) {
	if(param == 0) return 'Januar';
	if(param == 1) return 'Februar';
	if(param == 2) return 'März';
	if(param == 3) return 'April';
	if(param == 4) return 'Mai';
	if(param == 5) return 'Juni';
	if(param == 6) return 'Juli';
	if(param == 7) return 'August';
	if(param == 8) return 'September';
	if(param == 9) return 'Oktober';
	if(param == 10) return 'November';
	if(param == 11) return 'Dezember';
}
