function oCalendar(div,name) {
	this.div=div;
	this.name=name;
	//div.style.width=150;
	this.dateFunctions=new oDate();
	this.today=true;
	this.exclusions=new Object();
	this.earliestDate=oDate.getDateOnly();
	this.latestDate=oDate.getDateOnly();
	this.latestDate.setMonth(this.latestDate.getMonth()+11);
	this.time=false;}

oCalendar.prototype.show=function(event,control,date) {
	this.date=oDate.clone(date);
	this.setMenuCalendar();
	var position=oCalendar.findPosition(control);
	this.div.style.left=position[0] - this.div.scrollWidth + control.scrollWidth;
	this.div.style.top=position[1] + control.scrollHeight;
	this.div.style.display='block';}

oCalendar.findPosition=function(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop;}}
	return [curleft,curtop];}

oCalendar.prototype.addMonth=function(increment) {
	this.date.setDate(1);
	this.setMonth(this.date.getMonth() + increment);}

oCalendar.prototype.setMonth=function(month) {
	this.date.setMonth(month);
	this.setMenuCalendar();}

oCalendar.prototype.addYear=function(increment) {
	this.setYear(this.date.getFullYear() + increment);}

oCalendar.prototype.setYear=function(year) {
	this.date.setYear(year);
	this.checkMonth();
	this.setMenuCalendar();}

oCalendar.prototype.checkMonth=function() {
	if (this.date.getFullYear()==this.earliestDate.getFullYear() && this.date.getMonth()<this.earliestDate.getMonth()) {
		this.date.setMonth(this.earliestDate.getMonth());}
	if (this.date.getFullYear()==this.latestDate.getFullYear() && this.date.getMonth()>this.latestDate.getMonth()) {
		this.date.setMonth(this.latestDate.getMonth());}}

oCalendar.prototype.setMenuCalendar=function() {
	monthStart=oDate.clone(this.date);
	monthStart.setDate(1);
	var firstMonth=0, lastMonth=11;
	var inEarliest=this.date.getFullYear()==this.earliestDate.getFullYear(), inLatest=this.date.getFullYear()==this.latestDate.getFullYear();
	if (inEarliest) firstMonth=this.earliestDate.getMonth();
	if (inLatest) lastMonth=this.latestDate.getMonth();
	var html='';
	html+='<table><tr><td class="popupHeaderTitle" onmousedown="oDrag.start(event,get(\'' + this.div.id + '\'))">';
	html+='<table style="margin: 0 auto;"><tr><td style="padding:0px;">';
	html+='<input type="button" id="dateLeft" style="width:15px" value="<" onclick="' + this.name + '.addMonth(-1);" ' + (inEarliest && this.date.getMonth()==firstMonth?'disabled':'') + '>';
	html+='</td><td style="padding:0px;">';
	html+='<select name="month" id="dateMonth" onchange="' + this.name + '.date.setTime(this.value);' + this.name + '.setMenuCalendar();" onmousedown="browser.preventBubble(event,true);">';
	var current=new Date()
	current.setTime(this.earliestDate.valueOf());
	current.setDate(1);
	while (current.getFullYear()<this.latestDate.getFullYear() || 
		(current.getFullYear()==this.latestDate.getFullYear() && current.getMonth()<=this.latestDate.getMonth())) {
		html+='<option ' + (this.date.getFullYear()==current.getFullYear() && this.date.getMonth()==current.getMonth()?'selected ':'') + ' value="' + current.valueOf() + '">' + this.dateFunctions.monthName[current.getMonth()].substring(0,3) + ' ' + current.getFullYear() + '</option>';
		current.setMonth(current.getMonth() + 1);}
	html+='</select>';
	html+='</td><td style="padding:0px;">';
	html+='<input  id="dateRight" type="button" style="width:15px" value=">" onclick="' + this.name + '.addMonth(1);" ' + (inLatest && this.date.getMonth()==lastMonth?'disabled':'') + '>';
	html+='</td></tr></table>';
	html+='</td><td class="popupHeaderCross"> '
	html+='<input id="findCancel" type="button" value=" x " onClick="' + this.name + '.select()">';	
	html+='</td></tr><tr><td class="popupContent" colspan="2" style="padding:0px;"><table><tr>';
	for (i=1;i<8;i++) html+='<td class="calendarDay">' + this.dateFunctions.dayName[i==7?0:i].substring(0,1) + '</td>';	
	html+='</tr><tr>';
	var blanks=monthStart.getDay()-1;
	if (blanks==-1) blanks=6;
	if (blanks>0) html+='<td class="calendarDate" colspan="' + blanks + '"></td>';
	var i=blanks,row=0, daysInMonth=oDate.daysInMonth(monthStart);
	while (row==0 || monthStart.getDate()>1) {
		var lastRow=monthStart.getDate();
		if (row>0 && i==0) html+='<tr>'
		var include=this.exclusions[monthStart.valueOf()]==undefined;
		if (include) include=monthStart>=this.earliestDate && monthStart<=this.latestDate;
		
		//if (include) html+='<a class="calendarDate" href="javascript:' + this.name + '.select(' + monthStart.getTime() + ')">'
		if (include) {
			html+='<td class="calendarDate">';
			html+='<a class="calendarDate"';
			if (include) html+='href="javascript:' + this.name + '.select(' + monthStart.getTime() + ')"';
			html+='>';}
		else html+='<td class="calendarDateExclude">';

		html+=monthStart.getDate()
		if (include) html+='</a>';
		//html+='</a>';
		html+='</td>';
		monthStart.setDate(monthStart.getDate() + 1);
		i++;
		if (i==7 ) {
			i=0;
			row++;
			html+='</tr>';}
		else if (row>0 && monthStart.getDate()==1) html+='<td class="calendarDate" colspan="' + (7-i) + '"></td></tr>';}
	if (this.today || this.time) {
		html+='<tr><td colspan="7" class="calendarFooter">';
		if (this.today) html+='<input type="button" value="Today" onclick="javascript:' + this.name + '.select(-1)">';
		else if (this.time) {
			html+='Time: <select id="oCalendarHour">';
			for (var i=9;i<22;i++) html+='<option value="">' + (i<10?'0':'') + i + '</option>';
			html+='</select><select id="oCalendarMinute">';
			for (var i=0;i<60;i++) html+='<option value="' + i + '">' + (i<10?'0':'') + i + '</option>';
			html+='</select>';}
		html+='</td></tr>';}
	//html+='<input type="button" value="Close" onclick="javascript:' + this.name + '.select()">';
	html+='</table></td></tr></table>';
	//get('temp').innerHTML=html.replace(/</g,'&lt;').replace(/>/g,'&gt;');
	this.div.innerHTML=html;}

oCalendar.prototype.select=function(date) {
	this.div.style.display='none';
	if (date!=undefined) {
		if (date==-1) this.date.setTime(new Date().getTime());
		else this.date.setTime(date);
		if (this.time) {
			this.date.setHours(get('oCalendarHour').value);
			this.date.setMinutes(get('oCalendarMinute').value);}
		this.selectFunction(this.date);}
	else this.closeFunction();}
	
oCalendar.prototype.selectFunction=function(date) {
	alert('Insert your own date selection function instead of this.\n\nNew Date=' + date);}

//Needed to clear stuff up if closed
oCalendar.prototype.closeFunction=function() {}
