// simple CSS calendar popup
// written by Darren Munk 7/05
// ---

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);
var NS4 = (bName == "Netscape" && bVer >= 4);
var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);

function showCalendar(month,year)
{
	theID = "calendar";
	document.getElementById(theID).innerHTML = calendarHTML(month,year);
	document.getElementById(theID).style.display = '';
	document.getElementById(theID).style.position = 'absolute';
	theButton = document.images['popcalendar'];

	if( NS4 )
	{
		theButton_x = theButton.x;
	}
	else
	{
		xPos = theButton.offsetLeft;
		tempEl = theButton.offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	  theButton_x = xPos;
	}

	if( NS4 )
	{
		theButton_y = theButton.y;
	}
	else
	{
		yPos = theButton.offsetTop;
		tempEl = theButton.offsetParent;
		while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
		theButton_y = yPos;
	}
	  
	document.getElementById(theID).style.left = (theButton_x + theButton.width + 3 ) +"px";
	document.getElementById(theID).style.top = (theButton_y - 2) + "px";
}

function hideCalendar()
{
	theID = "calendar";
	document.getElementById(theID).style.display = 'none';
}

function setDateField(fieldID,theValue)
{
	document.getElementById(fieldID).value = theValue;
	hideCalendar();
}

function calendarHTML(month,year)
{
	months = new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
	month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	if( month == 2 && ( year % 4 ) == 0 )
	  eom = 29;
	else
	  eom = month_days[month-1];
	
	if( month == 1 )
	{
		last_month = 12;
		last_month_year = year - 1;
	}
	else
	{
	  last_month = month - 1;
	  last_month_year = year;
	}
	
	if( month == 12 )
	{
		next_month = 1;
		next_month_year = year + 1;
	}
	else
	{
	  next_month = month + 1;
	  next_month_year = year;
	}
	
	month_date = new Date(year,month-1,1);
	start = month_date.getDay();
	ret_val = '<table><thead><tr><td colspan="7" class="month"><span style="font-size:8px;color:#AAA;padding-right:8px;" class="clickable" onclick="showCalendar('+last_month+','+last_month_year+')">'+months[last_month-1]+'</span>'+months[month-1]+' '+year;
	ret_val += '<span style="font-size:8px;color:#AAA;padding-left:8px;" class="clickable" onclick="showCalendar('+next_month+','+next_month_year+')">'+months[next_month-1]+'</span></td></tr><tr><td>s</td><td>m</td><td>t</td><td>w</td>';
	ret_val += '<td>t</td><td>f</td><td>s</td></tr></thead><tbody><tr>';
	
	today = new Date();
	today_date = (today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear();
	
	for( i=1;i<=eom+start;i++ )
	{
		d = i - start;
		display_day = d;
		calendar_month = month;
		compare_date = month + '/' + d + '/' + year;
		
		if( calendar_month < 10 )
			calendar_month = '0' + calendar_month;
	  if( d < 10 )
	  	d = '0' + d;
		
		calendar_date = calendar_month + '/' + d + '/' + year;
		if( d > 0 )
		{
			ret_val += '<td class="clickable';
		
		 	if( Date.parse(compare_date) == Date.parse(today_date) )
		 	{
		 		ret_val += ' today';
		 	}
		 	else if( Date.parse(compare_date) < Date.parse(today_date) )
		 	{
		 		ret_val += ' past';
		 	}
		 	else
		 	{
		 		ret_val += ' future';
		 	}
		 	
			ret_val += '" onclick="setDateField(\'decision_date_c\',\''+ calendar_date + '\')">'+ display_day +'</td>';
		}
		else
			ret_val += "<td class='empty'>&nbsp;</td>";
		
		if( i == eom+start )
		{
			if( i % 7 > 0 )
			{
				cells_remaining = 7 - (i%7);
				for( j=0;j<cells_remaining;j++ )
					ret_val += "<td>&nbsp;</td>";
			}
		}
		else if( i % 7 == 0 )
			ret_val += "</tr><tr>";
	}
	
	ret_val += '</tr></tbody></table>';
	return ret_val;
}
