function initializeCalendar() {
	var dateField = document.getElementById("d");
	var container = document.getElementById("CalendarPlaceHolder");
	calendar = new Calendar(container, dateField);
	CalendarPlaceHolder.calendar = calendar;
	calendar.ShowCalendar();
}

function Calendar(container, dateField) {
	this.months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
	this.days = ["S", "M", "T", "W", "T", "F", "S"];
	this.container = container;
	this.dateField = dateField;
	this.Form = this.dateField.form;
	this.frameClassName = "calFrame";
	this.headClassName = "calhead";
	this.tableClassName = "caltable";
	this.weekdayClassName = "weekday";
	this.dayClassName = "dayCell";
	this.otherDayClassName = "otherCell";
	this.selectedDayClassName ="selectedCell";
	this.getSelectedDate = getSelectedDate;
	this.setSelectedDate = setSelectedDate;
	this.ShowCalendar = showCalendar;
	this.RenderCalendar = renderCalendar;
	this.GetCellClassName = getCellClassName;

	this.selectedDate = this.getSelectedDate();

function showCalendar(){
	this.currentDate = this.getSelectedDate();
	this.RenderCalendar();
}

function renderCalendar() {
	fr = document.createElement("div");
	fr.className = this.frameClassName;

	lhd = document.createElement("div");
	lhd.className = this.headClassName;
	lhd.style.width = "15px";
	lhda = document.createElement("a");
	lhda.href = "#";
	lhda.date = new Date(this.currentDate);
	lhda.date.setMonth(this.currentDate.getMonth() - 1);
	lhda.cal = this;
	lhda.innerHTML = "&lt;";
	lhda.onclick = prevNextOnClick;
	lhd.appendChild(lhda);
	fr.appendChild(lhd);

	hd = document.createElement("div");
	hd.className = this.headClassName;
	hd.innerHTML = this.months[this.currentDate.getMonth()] + " " + this.currentDate.getFullYear();
	//hd.style.width = "146px";
	fr.appendChild(hd);
	
	rhd = document.createElement("div");
	rhd.className = this.headClassName;
	rhd.style.width = "15px";
	rhda = document.createElement("a");
	rhda.href = "#";
	rhda.date = new Date(this.currentDate);
	rhda.date.setMonth(this.currentDate.getMonth() + 1);
	rhda.cal = this;
	rhda.onclick = prevNextOnClick;
	rhda.innerHTML = "&gt;";
	rhd.appendChild(rhda);
	fr.appendChild(rhd);

	t = document.createElement("Table");
	t.className = this.tableClassName;
	t.style.clear = "both";
	fr.appendChild(t);

	tb = document.createElement("TBody");
	t.appendChild(tb);

	tr = document.createElement("tr");
	for( i=0; i<7; i++) {
		td = document.createElement("td");
		td.className = this.weekdayClassName;
		td.innerHTML = this.days[i];
		tb.appendChild(td);
	}
	tb.appendChild(tr);

	var d = this.currentDate;
	d = new Date(d.getFullYear(), d.getMonth(), 1);
	d.setDate(d.getDate() - d.getDay());
	row = 0; 
	while( row < 5 ) {
		tr = document.createElement("tr");
		var column = 0;
		while( column < 7 ) {
			td = document.createElement("td");
			td.className = this.GetCellClassName(d);
			a = document.createElement("a");
			a.href = "#";
			a.date = new Date(d);
			a.innerHTML = d.getDate();
			a.cal = this;
			a.onclick = dayOnClick;
			td.appendChild(a);
			d.setDate(d.getDate()+1);
			column ++;
			tr.appendChild(td);
		}
		tb.appendChild(tr);
		row ++;
	}

	if(container.childNodes.length > 0) {
		container.innerHTML = "";
	}
	container.appendChild(fr);
}

function getCellClassName(date) {
	if(date.getMonth() != this.currentDate.getMonth())
		return this.otherDayClassName;

	if(date.getDate() != this.selectedDate.getDate())
		return this.dayClassName;

	if(date.getFullYear() != this.selectedDate.getFullYear())
		return this.dayClassName;

	if(date.getMonth() != this.selectedDate.getMonth())
		return this.dayClassName;

		return this.selectedDayClassName;
}

function getSelectedDate() {
	try {
		return new Date(this.dateField.value);
	} catch(e) {}

	return new Date();
}

function setSelectedDate(date) {
	this.dateField.value = formatDate(date);
	this.selectedDate = date;
	this.Form.submit();
}


function formatDate(date) {
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var year = date.getFullYear();

	return month + "/" + day + "/" + year;
}

function dayOnClick() {
	this.cal.setSelectedDate(this.date);
}

function prevNextOnClick() {
	this.cal.currentDate = this.date;
	this.cal.RenderCalendar();
}

} // End Calendar


