CALENDAR = function() {
	this.daysOfWeek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
	this.monthString = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	this.defaultDate = new Date();
	this.defaultDay = this.defaultDate.getDay();
	this.defaultMonth = this.defaultDate.getMonth()+1;
	this.defaultYear = this.defaultDate.getFullYear();
	this.opened = '';
	this.clicked = false;
	this.calendarForm = '';
	this.action = function() {}
	this.getItem = function(o) {
		var obj = document.getElementById(o);
		if (!obj.style) obj.style = obj;
		return obj;
	}
	this.getObjectX = function(o) {
		return (o.x) ? o.x : this.getObjectPosition(o, 'Left');
	}
	this.getObjectY = function(o) { 
		return (o.y) ? o.y : this.getObjectPosition(o, 'Top');
	}
	this.getObjectPosition = function(o, t) {
		var iPos = 0; 
		while (o != null) { 
			iPos += o["offset"+t]; 
			o = o.offsetParent; 
		} 
		return iPos;
	}
	this.openCalendar = function(o, f) {
		if(document.getElementById) {
			if(this.opened != '') this.closeCalendar(this.opened);
			this.clicked = false;
			this.calendarForm = this.getItem(f);
			this.opened = o.name;
			if(this.validateDate(o.value)) {
				var tempDate = o.value.split('/');
				useDay = tempDate[1];
				useMonth = tempDate[0];
				useYear = tempDate[2];
			} else {
				useDay = this.defaultDay;
				useMonth = this.defaultMonth;
				useYear = this.defaultYear;
			}
			var calendarID = this.opened+'Cal';
			var calendarObject = this.getItem(calendarID);
			var x = this.getObjectX(this.getItem(this.opened+'CalImg'));
			var y = this.getObjectY(this.getItem(this.opened+'CalImg'));
			this.createCalendar(o.name, useDay, useMonth, useYear);
			calendarObject.style.left = x + 'px';
			calendarObject.style.top = y + 'px';
			calendarObject.style.visibility = 'visible';
			document.onmouseup = this.autoClose;
		}
	}
	this.closeCalendar = function(n) {
		if(document.getElementById) {
			this.getItem(n+'Cal').style.visibility = 'hidden';
			this.opened = '';
			this.calendarForm = '';
			document.onmouseup = function () {}
		}
	}
	this.closeCalendarObject = function(o) {
		this.closeCalendar(o.name);
	}
	this.autoClose = function() {
		document.onmouseup = function () {
			if(!calendar.clicked) calendar.closeCalendar(calendar.opened);
			document.onmouseup = function () {}
		}
	}
	this.pressItem = function() {
		this.clicked = true;
	}
	this.createCalendar = function(n, d, m, y) {
		var calText = '<table class="pickcalendar">';
		var prevyear = (m-1 == 0) ? Number(y)-1 : y;
		var nextyear = (m+1 == 13) ? Number(y)+1 : y;
		var prevmonth = (m-1 == 0) ? 12 : Number(m)-1;
		var nextmonth = (m+1 == 13) ? 1 : Number(m)+1;
		var nyear = Number(y)+1;
		var pyear = Number(y)-1;
		var prevText = '<a href="javascript:calendar.action();" onclick="calendar.createCalendar(\''+n+'\', '+d+', '+prevmonth+', '+prevyear+')" onmousedown="calendar.pressItem();" title="Previous Month">&laquo;</a>';
		var nextText = '<a href="javascript:calendar.action();" onclick="calendar.createCalendar(\''+n+'\', '+d+', '+nextmonth+', '+nextyear+')" onmousedown="calendar.pressItem();" title="Next Month">&raquo;</a>';
		var prevYearText = '<a href="javascript:calendar.action();" onclick="calendar.createCalendar(\''+n+'\', '+d+', '+m+', '+pyear+')" onmousedown="calendar.pressItem();" title="Previous Year"><strong>&laquo;</strong></a>';
		var nextYearText = '<a href="javascript:calendar.action();" onclick="calendar.createCalendar(\''+n+'\', '+d+', '+m+', '+nyear+')" onmousedown="calendar.pressItem();" title="Next Year"><strong>&raquo;</strong></a>';
		var datestring = this.monthString[m-1]+' '+y;
		var dim = this.daysInMonth(m,y);
		var dow = this.dayOfWeek(1,Number(m),Number(y));
		calText += '<thead><tr><td colspan="7" class="caldate">'+datestring+'</td></tr></thead><tbody>';
		var t = 0;
		var da = 0;
		var ctext = '';
		for(var r = 0; r<6; r++){
			ctext += '<tr>';
			for(var c = 0; c<7; c++){
				t += 1;
				if(t > dow && da < dim) {
					da += 1;
					newdate = "'"+n+"',"+da+","+m+","+y;
					ctext += '<td class="calbox"><a href="javascript:calendar.action();" onclick="calendar.setInputDate('+newdate+');" onmousedown="calendar.pressItem();" title="'+this.daysOfWeek[c]+', '+this.monthString[m-1]+' '+da+', '+y+'">'+da+'</a></td>';
				} else {
					ctext += '<td class="calboxblank">&nbsp;</td>';
				}
			}
			ctext += '</tr>';
		}
		calText += ctext;
		calText += '<tr><td class="pcfooter">'+prevText+'</td><td class="pcfooter">'+prevYearText+'</td><td colspan="3" class="pcfooter"><a href="javascript:calendar.action();" onclick="calendar.closeCalendar(\''+n+'\');" onmousedown="calendar.pressItem();" title="Close Calendar">Close</a></td><td class="pcfooter">'+nextYearText+'</td><td class="pcfooter">'+nextText+'</td></tr>';
		calText += '</tbody></table>';
		document.getElementById(n+'Cal').innerHTML = calText;
	}
	this.setInputDate = function(n, d, m, y) {
		var uD = ((d<10) ? '0' : '')+d;
		var uM = ((m<10) ? '0' : '')+m;
		var uY = y;
		var dStr = uM+'/'+uD+'/'+uY;
		this.calendarForm[n].value = dStr;
		this.closeCalendar(n);
	}
	this.daysInMonth = function(m, y) {
		if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
			return 31;
		} else if (m == 4 || m == 6 || m == 9 || m == 11) { 
			return 30;
		} else if (m == 2) { 
			return ((y%4 == 0 && y%100 !=0) || y%1000 == 0) ? 29 : 28;
		}
	}
	this.dayOfWeek = function(d, m, y) {
		var va = Math.floor((14-m)/12);
		var vy = y-va;
		var vm = m+12*va-2;
		return (d + vy + Math.floor(vy/4) - Math.floor(vy/100) + Math.floor(vy/400) + Math.floor((31*vm)/12)) % 7;
	}
	this.validateDate = function(d) {
		var tempDate = d.split('/');
		if(tempDate.length == 3) {
			var m = tempDate[0];
			var d = tempDate[1];
			var y = tempDate[2];
			if((!isNaN(m) && !isNaN(d) && !isNaN(y)) && (m >= 1 && m <= 12) && (y >= 1980 && y <= 2050) && (d >= 1 && d <= this.daysInMonth(m, y))) return true;
		}
		return false;
	}
}
calendar = new CALENDAR();