/* returned errorcodes:

1 = [from-] date beyond normal date range (month or date do does not exist)
2 = [dateranges] to-date beyond normal date range
3 = date is in the future
4 = date is in the past
5 = date is today
6 = [dateranges] from-date is same as to-date
7 = [dateranges] dates are reversed (enddate comes before startdate)

Re-use of this code is allowed only for non-profit organisations and individuals, comment must be retained (including this sentance and the author's name below).

(c) Martin Ekblom 2006
*/

var monthNames = new Array("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");

function validateDate(day,month,year) {
	if(typeof day==undefined) { alert("Debug: Day is not defined."); return null; }
	if(typeof month==undefined) { alert("Debug: Month is not defined."); return null; }
	if(typeof year==undefined) { alert("Debug: Year is not defined."); return null; }
	day = parseInt(day,10);
	if(isNaN(day)) { alert("Debug: Day is not a number."); return null; }
	month = parseInt(month,10);
	if(isNaN(month)) { alert("Debug: Month is not a number."); return null; }
	year = parseInt(year,10);
	if(isNaN(year)) { alert("Debug: Year is not a number."); return null; }
	month--;
	if(month<0 || month>11) return false;
	if(day<1 || day>31) return false;
	var createdDate = new Date(year,month,1);
	createdDate.setDate(day);
	if(createdDate.getMonth()!=month) return false; else return createdDate;
}

function dateIsToday(day,month,year) {
	if(today.getFullYear()==year && today.getMonth()==month-1 && today.getDate()==day) return true; else return false;
}

function dateInFuture(day,month,year) {
	var testdate = validateDate(day,month,year);
	if(testdate) {
		var today = new Date();
		if(today.valueOf() < testdate.valueOf()) return true; else return false;
	} else return null;
}

function dateInPast(day,month,year) {
	var testdate = validateDate(day,month,year);
	if(testdate) {
		var today = new Date();
		if(today.valueOf() > testdate.valueOf()) return true; else return false;
	} else return null;
}

function dateTomorrow(day,month,year) {
	var testdate = initDate(day,month,year);
	if(testdate==null) return null;

}

function lastDateOfTheMonth(month,year) {
	var testdate = initDate(26,month,year);
	if(testdate==null) return null;
	month = testdate.getMonth();
	var d=26;
	do {
		testdate.setDate(++d);	
	} while (testdate.getMonth()==month);
	return (d-1);
//	for(var d=26; d<32; d++) {
//		testdate.setDate();
//	}
}

// Days: 1 = Monday, ... , 7 = Sunday
function firstDayOfTheMonth(month,year) {
	var testdate = initDate(1,month,year);
	if(testdate==null) return null;
	var firstDay = testdate.getDay();
	if(firstDay===0) firstDay=7;
	return firstDay;
}

function firstMondayInMonth(month,year) {
	var testdate = initDate(1,month,year);
	if(testdate==null) return null;

}

function nextXday(xday,day,month,year) {
	var testdate = initDate(day,month,year);
	if(testdate==null) return null;
// test xday 0->6
// +1, +1 tills day x igen
}

function registeredClickOn(day,month,year) {
	document.getElementById('arrivalday').selectedIndex = day-1;
	var my = document.getElementById('arrivalmonthyear').options;
	for(var i=0; i<my.length; i++) {
		var optYear = parseInt(my[i].value);
		var optMonth = my[i].value.slice(5);
		if(optYear==year && optMonth==month) {
			my[i].selected = true;
		}
	}
	document.getElementById('cal').style.display = 'none';
}

var currentmonth, currentYear;
function printCalendar(targetElem,month,year,idBaseName) {
	if(idBaseName==null) idBaseName = "day";
	var eachDay = new Date(year,month-1,1);
	if(eachDay==null) return null;
	var nextYear = eachDay.getFullYear();
	var nextMonth = eachDay.getMonth()+2;
	if(nextMonth>12) {
		nextMonth = 1;
		nextYear++;
	}	
	var lastYear = eachDay.getFullYear();
	var lastMonth = eachDay.getMonth();
	if(lastMonth<1) {
		lastMonth = 12;
		lastYear--;
	}	
	var now = new Date();
	var now = new Date(now.getFullYear(),now.getMonth(),1);
	var earlier = new Date(lastYear,lastMonth-1,1);
	var calendar = "<table><caption>"
	if(now.valueOf()<=earlier.valueOf()) calendar += "<img src='gfx/Left.gif' onclick=\"printCalendar('"+targetElem+"','"+lastMonth+"','"+lastYear+"','"+idBaseName+"')\" title='Verheriger Monat ("+monthNames[lastMonth-1]+" "+lastYear+")' alt='Verheriger Monat'/>";
	calendar += " "+monthNames[eachDay.getMonth()]+" "+eachDay.getFullYear()+" ";
	calendar += "<img src='gfx/Right.gif' onclick=\"printCalendar('"+targetElem+"','"+nextMonth+"','"+nextYear+"','"+idBaseName+"')\" title='Nächster Monat ("+monthNames[nextMonth-1]+" "+nextYear+")' alt='Nächster Monat'/>";
	calendar += "</caption>";
	calendar += "<thead><tr><th title='Montag'>Mo</th><th title='Dienstag'>Di</th><th title='Mittwoch'>Mi</th><th title='Donnerstag'>Do</th><th title='Freitag'>Fr</th><th class=\"saturday\" title='Samstag'>Sa</th><th class=\"sunday\" title='Sonntag'>So</th></tr></thead><tbody>";
	var starton = eachDay.getDay();
	if(starton===0) starton=7;
	var y = eachDay.getFullYear();
	var m = eachDay.getMonth()+1;
	var d = 1;
	calendar += "<tr>";
	for(var i=1; i<=7; i++) {
		if(i<starton) calendar += "<td class=\"emptyday\"> </td>"; else {
			if(i==6) calendar += "<td class=\"saturday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>"; 
			else if(i==7) calendar += "<td class=\"sunday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>"; 
			else calendar += "<td class=\"weekday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>";
		}
	}
	calendar += "</tr>";
	for(var week=1; week<=3; week++) {
		calendar += "<tr>";
		for(var i=1; i<=7; i++) {
			if(i==6) calendar += "<td class=\"saturday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>"; else if(i==7) calendar += "<td class=\"sunday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>"; else calendar += "<td class=\"weekday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>";
		}
		calendar += "</tr>";
	}
	eachDay.setDate(d);
	month--;
	for(week=4; week<=5; week++) {
		if(eachDay.getMonth()==month) {
			calendar += "<tr>";
			for(var i=1; i<=7; i++) {
				if(eachDay.getMonth()==month) {
					if(i==6) calendar += "<td class=\"saturday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>"; else if(i==7) calendar += "<td class=\"sunday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>"; else calendar += "<td class=\"weekday\" id=\""+idBaseName+d+"\" onclick=\"registeredClickOn("+d+","+m+","+y+");\">"+(d++)+"</td>";
				} else calendar += "<td class=\"emptydayend\"> </td>";
				eachDay.setDate(d);
			}
			calendar += "</tr>";
		} else break;
	}
	calendar += "</tbody></table>";
	document.getElementById(targetElem).innerHTML = calendar;
}

// (c) Martin Ekblom 2006
