//Constructor
function calendar(id,d,l){
	this.id = id;
	this.dateObject = d;
	this.language = l;
	this.write = writeCalendar;
	this.length = getLength;
	this.month = d.getMonth();
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	this.getFormattedDate = getFormattedDate;
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);

	this.onChangeDate = function () {};
}

var days = new Array();
var months = new Array();
days['de'] = new Array('Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag');
months['de'] = new Array('Januar','Februar','Maerz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
days['en'] = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
months['en'] = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

days['it'] = new Array('Domenica','Lunedi','Martedi','Mercoledi','Giovedi','Venerdi','Sabato');
months['it'] = new Array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');

function getFormattedDate(){
	var varDate = '';
	switch(this.language) {
		case "en":
			varDate = months['en'][this.month]+' '+this.date+', '+this.year;
			break;
		case "de":
			varDate = this.date+'. '+months['de'][this.month]+' '+this.year;
			break;
		case "it":
			varDate = this.date+'. '+months['it'][this.month]+' '+this.year;
			break;
	}
	return varDate;
}

function writeCalendar(){
	var calString = '<div class="calendar" id="calendar'+this.id+'">';
	//write month and year at top of table
	calString += '<table id="cal' + this.id + '" cellspacing="0" cellpadding="0" style="border:0px;">';
	//write the month
	calString += '<tr>';
//	calString += '<td class="nav" style="text-decoration:underline;" onClick="changeMonth(-12,\'' + this.id + '\')">&lt;</td>';
	calString += '<th class="nav" onClick="changeMonth(-1,\'' + this.id + '\')">&laquo;</th>';
	calString += '<th colspan="5" class="month">' + months[this.language][this.month] + ', ' + this.year + '</th>';
	calString += '<th class="nav" onClick="changeMonth(1,\'' + this.id + '\')">&raquo;</th>';
//	calString += '<td class="nav" style="text-decoration:underline;text-align:right;" onClick="changeMonth(12,\'' + this.id + '\')">&gt;</td>';
	calString += '</tr>';

	//write a row containing days of the week
	calString += '<tr>';
	
	for(i=0;i<days[this.language].length;i++){
		var varDayName = '';
		switch(this.language) {
			case "en":
				varDayName = days['en'][i].substring(0,3);
				break;
			case "de":
				varDayName = days['de'][i].substring(0,2);
				break;
			case "it":
				varDayName = days['it'][i].substring(0,2);
				break;
		}
		calString += '<th class="header">' + varDayName + '</th>';
	}
	
	//write the body of the calendar
	calString += '<tr>';
	//create 6 rows so that the calendar doesn't resize
	for(j=0;j<42;j++){
		var displayNum = (j-this.firstDay+1);
		if(j<this.firstDay){
			//write the leading empty cells
			calString += '<td class="empty">&nbsp;</td>';
		}else if(displayNum==this.date){
			calString += '<td id="' + this.id +'selected" class="date" onClick="javascript:changeDate(this,\'' + this.id + '\')">' + displayNum + '</td>';
		}else if(displayNum > this.length()){
			//Empty cells at bottom of calendar
			calString += '<td class="empty">&nbsp;</td>';
		}else{
			//the rest of the numbered cells
			calString += '<td id="" class="days" onClick="javascript:changeDate(this,\'' + this.id + '\')">' + displayNum + '</td>';
		}
		if(j%7==6){
			calString += '</tr><tr>';
		}
	}
	//close the last number row
	calString += '</tr>';

	calString += '</div>';
	calString += '</table>';
	return calString;
}

function getLength(){
	//thirty days has September...
	switch(this.month){
		case 1:
			if((this.dateObject.getFullYear()%4==0&&this.dateObject.getFullYear()%100!=0)||this.dateObject.getFullYear()%400==0)
				return 29; //leap year
			else
				return 28;
		case 3:
		case 5:
		case 8:
		case 10:
			return 30;
		default:
			return 31;
	}
}
function changeDate(td,cal){
	//Some JavaScript trickery
	//Change the cal argument to the existing calendar object
	//This is why the first argument in the constructor must match the variable name
	//The cal reference also allows for multiple calendars on a page
	cal = eval(cal);
	document.getElementById(cal.id + "selected").className = "days";
	document.getElementById(cal.id + "selected").id = "";
	td.className = "date";
	td.id = cal.id + "selected";

	//set the calendar object to the new date
	cal.dateObject.setDate(td.firstChild.nodeValue);
	cal = new calendar(cal.id,cal.dateObject,cal.language);
	var elparts = cal.id.split('Container');
	document.getElementById(elparts[0]).value = cal.getFormattedDate();
}

function changeMonth(mo,cal){
	//more trickery!
	cal = eval(cal);
	//The Date object is smart enough to know that it should roll over in December
	//when going forward and in January when going back
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar(cal.id,cal.dateObject,cal.language);
	cal.formattedDate = cal.getFormattedDate();
	document.getElementById('calendar'+cal.id).innerHTML = cal.write();
}

