//version 1.0

	var theDate = new Date(); 
	var theYear = theDate.getYear();
	var theMonth = theDate.getMonth();
	var theDay = theDate.getDate();
	var theSpan;
	var functionAfter;
	var referObj;
	var theType = "span";
	var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var numDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var days = new Array("S","M","T","W","T","F","S");
	function setCalendar(spanID,type,fncAfter,aObj) {
		//alert("spanID=" + spanID +"\ntype=" + type + "\nfncAfter=" + fncAfter + "\naObj=" + aObj);
		theSpan = spanID;
		theType = type;
		functionAfter = fncAfter;
		
		referObj = aObj;
		//referObj = document.getElementById('calendar');
		if(theYear >1900) theYear -= 1900;
		
		var tempYear = theYear + 1900;
		var tempMonth = theMonth + 1;
		var loopDate = new Date(tempMonth + "/1/" + tempYear);
		var i;
		var cal;
		var dayCount=0;
		var calDiv = document.getElementById('calendarDiv');
		cal = "<table><tr><td class='leftTD'><span class='clickable' onclick='SetPrevDate()'>&lt;</span></td><td class='heading'>" + months[theMonth] + " " + eval(theYear + 1900) + "</td><td class='rightTD'><span class='clickable' onclick='SetNextDate()'>&gt;</span></td></tr></table>";
		cal += "<table>";
		cal += "<tr>";
		for(i=0;i<7;i++) {
			cal += "<td class='dayHead'>" + days[i] + "</td>";
		}
		cal += "</tr>";
		//get the last day of the month
		var lastDay = getLastDay(theMonth,theYear);
		//get the day of the first day of the month
		var firstDay = loopDate.getDay();
		
		cal += "<tr>";
		while(dayCount<firstDay) {
			cal += "<td class='noday'>&nbsp;</td>";
			dayCount++;
		}
		for(i=1;i<=lastDay;i++) {
			tempYear = loopDate.getYear();
			if(tempYear > 1900) tempYear -= 1900;
			cal += "<td id='day" + i + "'class='day' onmouseover='calChangeColor(this)' onmouseout='calChangeBack(this)' onclick='inputDate(" + loopDate.getYear() + "," + loopDate.getMonth() + "," + i + ")'>" + i + "</td>";
			dayCount++;
			if(dayCount == 7) {
				dayCount = 0;
				cal += "</tr><tr>";
			}
		}
		if(dayCount >0) {
		while(dayCount < 7) {
			dayCount++;
			cal += "<td class='noday'>&nbsp;</td>";
		}
		}
		cal += "</tr>";
		cal += "</table>";
		cal += "<table><tr><td class='day' onmouseover='calChangeColor(this)' onmouseout='calChangeBack(this)' onclick='resetCalendar()'>This Month</td>";
		cal += "<td class='day' onmouseover='calChangeColor(this)' onmouseout='calChangeBack(this)' onclick='cancelCalendar()'>Cancel</td></tr></table>"; 
		calDiv.innerHTML = cal;
		calDiv.style.display = "block";
		calDiv.style.position = "absolute";
		calDiv.style.zIndex = "10";
		calDiv.style.top = findPosY(referObj) + "px";
		calDiv.style.left = findPosX(referObj) + "px";
	}
	
	function getLastDay(month,year) {
		
		var lastDay = numDays[month];
		if(month == 1 && year%4==0) {
			lastDay++;
		}
		return lastDay;
	}
	
	function inputDate(year,month,day) {
		var calDiv = document.getElementById('calendarDiv');
		if(year < 200) year += 1900;
		month += 1;
		var theDate = month + "/" + day + "/" + year;
		if(theType == "span") {
			var theInput = document.getElementById(theSpan);
			theInput.innerHTML = theDate;
		}
		else if(theType == "input") {
			document.getElementById(theSpan).value=theDate;
		}
		calDiv.style.display = "none";
		if(functionAfter) {
			eval(functionAfter);  //getAssignment();
		}
	}
	function SetNextDate() {
		theMonth = theMonth + 2;
		theYear += 1900;
		if(theMonth == 13) { theYear++; theMonth = 1; }
		theDate = new Date(theMonth + "/1/" + theYear);
		theYear = theDate.getYear();
		theMonth = theDate.getMonth();
		
		setCalendar(theSpan,theType,functionAfter,referObj);
	}
	
	
	function SetPrevDate() {
		theYear += 1900;
		if(theMonth == 0) { theYear--; theMonth = 12; }
		theDate = new Date(theMonth + "/1/" + theYear);
		theYear = theDate.getYear();
		theMonth = theDate.getMonth();
		
		setCalendar(theSpan,theType,functionAfter,referObj);
	}
	
	
	function calChangeColor(theTD) {
		theTD.style.backgroundColor = "#ff0000";
		//theTD.style.fontWeight = "bold";
	}
	function calChangeBack(theTD) {
		theTD.style.backgroundColor = "#ffffff";
		//theTD.style.fontWeight = "default";
	}
	
	function resetCalendar() {
				theDate = new Date();
		theYear = theDate.getYear();
		theMonth = theDate.getMonth();
		setCalendar(theSpan,theType,functionAfter,referObj);
	}
	function cancelCalendar() {
		document.getElementById('calendarDiv').style.display = "none";
	}
	function findPosY(obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
				return curtop;
	}
	function findPosX(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
		
		
	}
