var currentField;		// the current field into which the date will be placed
var currentImg;			// the calendar icon associated with the current field
var	arrowImg = new Image(); arrowImg.src = "/ico/arrow.gif";
var globalTime = new Date();

function GetCoords(el) {for (var lx=0,ly=0;el!=null;lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);return {x:lx,y:ly}}

function showCalendar(img,fieldName) {
	event.cancelBubble = true;
	CalDiv = document.getElementById("Calendar");
	resetFieldAttributes();

	nextMonth(); prevMonth(); // set to the beginning of the month; is this really desired?

	setDivHTML(globalTime.getTime());

	currentField = document.getElementById(fieldName);
	currentField.style.background = "#FFFF99";

	currentImg = img;
	currentImg.src = arrowImg.src;

	coords = GetCoords(img);
	CalDiv.style.left = coords.x+17;
	CalDiv.style.top = coords.y-35;
	CalDiv.style.visibility = "visible"; // show the Calendar div
	Hide('dateIns');
}

function hideCalendar() {
	if (document.getElementById("Calendar")) {
		document.getElementById("Calendar").style.visibility = "hidden";
	}
	resetFieldAttributes();
}

function resetFieldAttributes() {
	if (currentField) {currentField.style.background = "#FFFFFF";}
	currentField = null;
	if (currentImg) {currentImg.src = '/ico/calendar.gif';}
	currentImg = null;
}

function setDate(timestamp) {
	var dateObj = new Date(timestamp);
	var year = dateObj.getFullYear()+"";
	var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	year = year.substr(3,2);
	if ((year/1) < 10) {year = "0"+year;}
	var month = months[dateObj.getMonth()];
	var day = dateObj.getDate();
	currentField.value = day+"-"+month+"-"+year;
	currentField.style.color="#000000";
	currentField.style.fontWeight="normal";
	globalTime.setTime(timestamp);
	hideCalendar();
}

function checkDate(fieldObj) {
	// to be called onChange of field; used for validating manual date entry
}

function getMonthFromField(fieldObj) {
}

function prevMonth() {
	do {
		globalTime.setTime(globalTime.getTime()-86400000);
	} while (globalTime.getDate() != 1)
	setDivHTML(globalTime.getTime());
}

function nextMonth() {
	do {
		globalTime.setTime(globalTime.getTime()+86400000);
	} while (globalTime.getDate() != 1);
	setDivHTML(globalTime.getTime());
}

function setDivHTML(timestamp) {
	CalDiv = document.getElementById("Calendar");
	var months = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
	var year;
	var week;	// calendar rows
	var day;	// calendar columns
	var thisTime = new Date(timestamp);
	var monthName = months[thisTime.getMonth()];
	var fullYear = thisTime.getFullYear();

	while (thisTime.getDay()) { // loop until the day is zero (Sunday)
		thisTime.setTime(thisTime.getTime()-86400000);
	} // start today or previous Sunday; assumes users will enter dates in advance of actual event

	var html = "";
	html += "<table cellspacing='1'>";
	html += "<tr>";
	html += "<th class='cal' style='cursor:hand' onMouseOver='this.style.background=\"#FFFF00\";' onMouseOut='this.style.background=\"#E0F0E0\"' onClick='prevMonth()'>&lt;</th>";
	html += "<th class='cal' colspan='5'><nobr>" + monthName + " " + fullYear + "</nobr></td>";
	html += "<th class='cal' style='cursor:hand' onMouseOver='this.style.background=\"#FFFF00\";' onMouseOut='this.style.background=\"#E0F0E0\"' onClick='nextMonth()'>&gt;</th>";
	html += "</tr>";
	html += "<tr>";
	html += "<th class='cal'>S</th>";
	html += "<th class='cal'>M</th>";
	html += "<th class='cal'>T</th>";
	html += "<th class='cal'>W</th>";
	html += "<th class='cal'>T</th>";
	html += "<th class='cal'>F</th>";
	html += "<th class='cal'>S</th>";
	html += "</tr>";

	var inMonth = false; // indicates whether we're in the current month yet
	var endMonth = false; // indicates whether the end of the month has been reached
	var cellClass = "cal_fill";

	while (endMonth != true) {
		html += "<tr>";
		for (day = 0; day < 7; day++) {
			this_stamp = thisTime.getTime();
			this_date  = thisTime.getDate();
			// ADD THE MONTH IF NEW:
			if (this_date == 1) {
				if (inMonth) {
					endMonth = true;
					inMonth = false;
				} else {
					inMonth = true;
				}
			}
			// PRINT THE DATE CELL:
			if (inMonth) {
				html += "<td class='cal' onMouseOver='this.style.background=\"#FFFF00\";' onMouseOut='this.style.background=\"#FFFFFF\"' onClick='setDate("+this_stamp+")'>"+this_date+"</td>";
			} else {
				html += "<td class='cal_fill' onMouseOver='this.style.background=\"#FFFF00\";' onMouseOut='this.style.background=\"#D8D8D8\"' onClick='setDate("+this_stamp+")'>"+this_date+"</td>";
			}
			thisTime.setTime(thisTime.getTime()+86400000);
		}
		html += "</tr>";
		if (thisTime.getDate() == 1){break;}
	}

	html += "</table>";
	CalDiv.innerHTML = html;
}
