var onclickAction;

function onDayClick(window_id, txt_id, m_val, d_val, y_val)
{
	document.getElementById(txt_id).value = m_val + "/" + d_val + "/" + y_val;
	window_close(window_id);
	eval(onclickAction);
}

function window_close(id)
{
	document.getElementById(id).style.display="none";
}

function calendar(id, txt_id, min, max, init_m, init_y, onclickEvt)
{
	var init_date;
	var month_names = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
	var p_m, p_y, n_m, n_y;
	
	onclickAction = onclickEvt;
	
	// If the initial month and year arguments exist, set the initial date to
	// the first day in that month.
	// Otherwise, default to the current month.
	if (arguments.length == 4)
	{
		init_date = new Date();
		init_m = init_date.getMonth();
		init_y = init_date.getFullYear();
	}
	else
	{
	    init_date = new Date(init_y, init_m, 1, 0, 0, 0, 0);
	}

	// If the initial date is out-of-bounds, set it to the maximum date.
	if (init_date > new Date(max))
	{
	    init_m = new Date(max).getMonth();
	    init_y = new Date(max).getFullYear();
	}
	else if (init_date < new Date(min))
	{
	    init_m = new Date(min).getMonth();
	    init_y = new Date(min).getFullYear();
	}

	// Adjust to the first day of the month.
	init_date = new Date(init_y, init_m, 1, 0, 0, 0, 0);
	// Javascript months are base-0, adjust the month so that it is base-1.
	init_m = parseInt(init_m);
	var days_in_month = getDaysInMonth(init_m + 1, init_y);
	var init_d = init_date.getDay();
	
	// Previous month and year.
	if (init_m == 0)
	{
		p_m = 11;
		p_y = init_y - 1;
	}
	else
	{
		p_m = init_m - 1;
		p_y = init_y;
	}
	
	// Next month and year.
	if (init_m == 11)
	{
		n_m = 0;
		n_y = init_y + 1;
	}
	else
	{
		n_m = init_m + 1;
		n_y = init_y;
	}
	
	var min_dt = new Date(min);
	var max_dt = new Date(max);
	var prev_cal = new Date(p_y, p_m, min_dt.getDate(), 0, 0, 0, 0);
	var next_cal = new Date(n_y, n_m, max_dt.getDate(), 0, 0, 0, 0);
	var output = "";
	
	output += "<table cellpadding=\"0\" cellspacing=\"0\"  class=\"mihLayerCalendar\">\n";
	output += "<tr>\n";
	output += "	<td colspan=\"7\" class=\"mihLayerCalendarMonth\" style=\"text-align:right;border-bottom:none;padding-right:2px;font-size:90%;\"><a href=\"javascript:void(0);\" title=\"Close calendar\" onclick=\"window_close('" + id + "')\">CLOSE</a></td>\n";
	output += "</tr>\n";
	output += "<tr>\n";
	output += "	<td class=\"mihLayerCalendarMonth\" colspan=\"2\" style=\"padding-left:2px;width:26%;\">" + ((prev_cal >= min_dt) ? "<a href=\"javascript:\" style=\"font-weight:bold;\" onclick=\"calendar('" + id + "','" + txt_id + "','" + min + "','" + max + "'," + p_m + "," + p_y + ")\">&#8249;&#8249;&nbsp;</a>" : "&nbsp;") + "</td>\n";
	output += "	<td class=\"mihLayerCalendarMonth\" colspan=\"3\" style=\"text-align:center;width:40%;\">" + month_names[init_m] + " " + init_y + "</td>\n";
	output += "	<td class=\"mihLayerCalendarMonth\" colspan=\"2\" style=\"text-align:right;padding-right:2px;width:26%;\">" + ((next_cal <= max_dt) ? "<a href=\"javascript:\" style=\"font-weight:bold;\" onclick=\"calendar('" + id + "','" + txt_id + "','" + min + "','" + max + "'," + n_m + "," + n_y + ")\">&nbsp;&#8250;&#8250;</a>" : "&nbsp;") + "</td>\n";
	output += "</tr>\n";
	output += "<tr>\n";
	output += "	<td class=\"weekDay\" style=\"border-left:none;\">S</td>\n";
	output += "	<td class=\"weekDay\">M</td>\n";
	output += "	<td class=\"weekDay\">T</td>\n";
	output += "	<td class=\"weekDay\">W</td>\n";
	output += "	<td class=\"weekDay\">T</td>\n";
	output += "	<td class=\"weekDay\">F</td>\n";
	output += "	<td class=\"weekDay\">S</td>\n";
	output += "</tr>\n";

	output += "<tr>\n";
	var day;
	for (var i=1; i <= 7; i++)
	{
		day = i - init_d;
		if (day > 0)
		{
			if (((init_m == min_dt.getMonth()) && (day < min_dt.getDate()) && (init_y == min_dt.getFullYear())) || 
				((init_m == max_dt.getMonth()) && (day > max_dt.getDate()) && (init_y == max_dt.getFullYear())))
			{
				output += getDisabledCell(day, (i==1), false);
			}
			else
			{
				output += getDayCell(init_m + 1, day, init_y, id, txt_id, (i==1), false);
			}
		}
		else
		{
			output += getEmptyCell((i==1), false);
		}
	}
	output += "</tr>\n";
	
	for (var i=0; i < 5; i++)
	{
		output += "<tr>\n";
		for (var j=0; j < 7; j++)
		{
			day = ((8 + j) + (i * 7)) - init_d;
			
			if (day <= days_in_month)
			{
				if (((init_m == min_dt.getMonth()) && (day < min_dt.getDate()) && (init_y == min_dt.getFullYear())) || 
					((init_m == max_dt.getMonth()) && (day > max_dt.getDate()) && (init_y == max_dt.getFullYear())))
				{
					output += getDisabledCell(day, (j==0), (i==4));
				}
				else
				{
					output += getDayCell(init_m + 1, day, init_y, id, txt_id, (j==0), (i==4));
				}
			}
			else
			{
				output += getEmptyCell((j==0), (i==4));
			}
		}
		output += "</tr>\n";
	}
	output += "</table>\n";
	
	var container = document.getElementById(id);
	container.innerHTML = output;
}

function getDayCell(m, d, y, id, txt_id, is_first_day, is_last_row)
{
	return "	<td class=\"calDay\" style=\"" + getStyle(is_first_day, is_last_row) + "\"><a class=\"calDay\" href=\"javascript:void(0)\" onclick=\"onDayClick('" + id + "','" + txt_id + "'," + m + "," + d + "," + y + ")\">" + d + "</a></td>\n";
}

function getEmptyCell(is_first_day, is_last_row)
{
	return "	<td class=\"emptyDay\" style=\"" + getStyle(is_first_day, is_last_row) + "\">&nbsp;</td>\n";
}

function getDisabledCell(d, is_first_day, is_last_row)
{
	return "	<td class=\"mihLayerCalendarDisabledDay\" style=\"" + getStyle(is_first_day, is_last_row) + "\">" + d + "</td>\n";
}

function getStyle(is_first_day, is_last_row)
{
    return ((is_first_day)?"border-left:none;":"") + ((is_last_row)?"border-bottom:none;":"");
}

// CHECK TO SEE IF YEAR IS A LEAP YEAR
function isLeapYear(Year)
{
	if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}

// GET NUMBER OF DAYS IN MONTH
function getDaysInMonth(month, year)
{
	var days;

	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		days = 31;
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		days = 30;
	}
	else if (month == 2)
	{
		if (isLeapYear(year)) 
		{
			days = 29;
		}
		else 
		{
			days = 28;
		}
	}
	return days;
}

