var ie=document.all
var ns6=document.getElementById&&!document.all
var n=document.layers


if (ie||ns6)    {fShow="visible";fHide="hidden";}
if (n) {fShow="show"; fHide="hide";}

var MonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var nCurrentYear = 0;
var nCurrentMonth = 0;

var nWidth  = 30;
var nHeight = 20;

var leftX;
var rightX
var topY;
var bottomY;



function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}



function Calendar()
{

	var HTMLstr = "";

	HTMLstr += "<table width='170' cellspacing='4'>"
	HTMLstr += "	<tr>"
	HTMLstr += "		<td class='empty'><a href=\"javascript:prevMonth();\" style=\"border:0\"><img style=\"border:0\" src='images/cal_back.gif' onMouseOver=\"this.src='images/cal_back_over.gif'\" onMouseOut=\"this.src='images/cal_back.gif'\" /></a></td>"
	HTMLstr += "		<td style='width:120px;font-weight: bold;' colspan='5' align='center'>"
	
	if (ie||ns6)HTMLstr += "<div id='main2' style='position=relative;text-align:center;font-size:13px;'>";
	if (n) HTMLstr += "<ilayer id='main2'><layer id='idMonth' top='0' left='0'>\n";
	HTMLstr += "\n";
	if (ie||ns6)HTMLstr += "<div>\n";
	if (n) HTMLstr += "</layer></ilayer>\n";

	HTMLstr += "		</td>"
	HTMLstr += "		<td class='empty'><a href=\"javascript:nextMonth();\" style=\"border:0\"><img style=\"border:0\" src='images/cal_next.gif' onMouseOver=\"this.src='images/cal_next_over.gif'\" onMouseOut=\"this.src='images/cal_next.gif'\" /></a></td>"
	HTMLstr += "	</tr>"

	HTMLstr += "	<tr>"
	HTMLstr += "		<th colspan='7'>"
	HTMLstr += "			<table width='170' cellspacing='0'>"
	HTMLstr += "				<tr id='titleRow' style=\"font-size: 11px;text-align:center;\">"
	HTMLstr += "					<th width=\"20\">M</th>"
	HTMLstr += "					<th width=\"20\">T</th>"
	HTMLstr += "					<th width=\"20\">W</th>"
	HTMLstr += "					<th width=\"20\">T</th>"
	HTMLstr += "					<th width=\"20\">F</th>"
	HTMLstr += "					<th width=\"20\">S</th>"
	HTMLstr += "					<th width=\"20\">S</th>"
	HTMLstr += "				</tr>"
	HTMLstr += "			</table>"
	HTMLstr += "		</th>"
	HTMLstr += "	</tr>"
	HTMLstr += "</table>"

	if (ie||ns6) HTMLstr += "<div id='idMenuContainer' style='position: relative;'>";
	if (n) HTMLstr += "<ilayer id='idMenuContainer' height='120px' width='140px'>\n";

	if (ie||ns6)HTMLstr += "</div>";
	if (n) HTMLstr += "</ilayer>\n";



	document.writeln(HTMLstr);
	//buildMonthMenu(HTMLstr);

}

function daysInMonth(iMonth, iYear){
	return 32 - new Date(iYear, iMonth, 32).getDate();
}


function setCurrentMonth()
{
  date = new Date();
  currentyear = date.getYear()
  if (currentyear < 1000)
  currentyear+=1900
  setYearMonth(currentyear, date.getMonth()+1);
}

function setMonth(nMonth)
{
	setYearMonth(nCurrentYear, nMonth);
}

function setYearMonth(nYear, nMonth)	{
	if (ie||ns6)	{

		var newHTML = ""

		nCurrentYear = nYear;
		nCurrentMonth = nMonth;

		var cross_obj2=ns6? document.getElementById("main2"): document.all["main2"]
		cross_obj2.innerHTML = MonthNames[nCurrentMonth-1] + ' ' + nCurrentYear +"\n";

		var date   = new Date(nCurrentYear, nCurrentMonth-1, 1);
		var nWeek  = 1;
		var nDate;


		// Verify parameters
		if (nMonth < 1 || nMonth > 12) {alert("Function: setYearMonth()\nERROR: Incorrect month "+nMonth);}

		nCurrentYear = nYear;
		nCurrentMonth = nMonth;

		//var date   = new Date(nYear, nMonth-1, 1);
		var nWeek  = 1;
		var nDate;

		// C2 - had to do some hacking to make this work with a table
		date = new Date();
		date.setFullYear(nCurrentYear, nCurrentMonth -1, 01);
		//alert(date)

		var posDay = date.getDay()-1;
		if (posDay == -1) posDay=6;

		newHTML += "<table width='177' cellspacing='4' style=\"	font-family: 'Trebuchet MS', Verdana, Helvetica, Arial, sans-serif;	font-size: 11px;text-align: center;padding-left:5px;\">"

		// Add empty cells
		for (i=0; i < posDay; i++)	{
			newHTML += "<td class=\"empty\" style=\"background: #fff;height: 17px;width:17px;\" ></td>"
		}

		for (var date=1; date <= daysInMonth(nCurrentMonth -1, nCurrentYear); date++)	{

			if (posDay == 7)		{
				posDay = 0
				newHTML += "</tr><tr>"
			}

			newHTML += "<td id=\"idDate"+date+"\" style=\"background: #e5e5e5;height: 17px;width:17px;\"  ><a href='default.asp?page=s6_1&txt_Date="+ date + "/" + (nCurrentMonth) + "/" + nCurrentYear + "' style=\"text-decoration: none;color:black;display:block;border:0;\">" + date + "</a></td>"

			posDay++ 
		}

		newHTML += "</table>"

	}

	//alert(newHTML)

	var cross_objContents=ns6? document.getElementById("idMenuContainer"): document.all["idMenuContainer"]
	cross_objContents.innerHTML = newHTML;

}



function nextMonth()
{
  nCurrentMonth++;
  if (nCurrentMonth > 12)
  {
	nCurrentMonth -= 12;
	nextYear();
  }
  
  setYearMonth(nCurrentYear, nCurrentMonth);
}


function prevMonth()
{
  nCurrentMonth--;
  if (nCurrentMonth < 1)
  {
	nCurrentMonth += 12;
	prevYear();
  }
  setYearMonth(nCurrentYear, nCurrentMonth);
}

function prevYear()
{
  nCurrentYear--;
  setYearMonth(nCurrentYear, nCurrentMonth);
}

function nextYear()
{
  nCurrentYear++;
  setYearMonth(nCurrentYear, nCurrentMonth);
}

////////////////////////////////////////////////////

function updateIt(e)
{
//  var x = e.pageX;
//  var y = e.pageY;

//  if (x > rightX || x < leftX) hideMonthMenu();
//  else if (y > bottomY || y < topY) hideMonthMenu();
}

function hideMonthMenu()
{
  document.layers["idMonthMenu"].visibility="hide";
}

function showMonthMenu()
{
if (!n) return;
  topY    = document.layers["main2"].pageY-50;
  bottomY = document.layers["main2"].pageY + document.layers["idMonthMenu"].clip.height+50;
  leftX   = document.layers["main2"].pageX-50;
  rightX  = document.layers["main2"].pageX + document.layers["idMonthMenu"].clip.width+50;

  document.layers["idMonthMenu"].top = document.layers["main2"].pageY+document.layers["main2"].clip.height;
  document.layers["idMonthMenu"].left = document.layers["main2"].pageX;
  document.layers["idMonthMenu"].visibility="show";
}

function buildMonthMenu()
{
if (!n) return;
  var HTML = "";
  HTML += "<layer id=\"idMonthMenu\" visibility=\"hide\" width=\"120\" bgcolor=\"darkblue\">\n";
  HTML += "<table border=\"1\" cellspacing=\"0\" cellpading=\"0\">\n";
  for (var month=0; month<12;)
  {
	HTML += "<tr>\n";
	for (var i = 0; i < 3; i++)
	{
		var nMonth = month+1;
		HTML += "<td><a href=\"javascript:hideMonthMenu();setMonth("+nMonth+");\"><font color=\"white\" size=-1><b>"+MonthNames[month]+"</b></font></a></td>\n";
		month++;
	}
	HTML += "</tr>\n";
  }
  HTML += "</table>";
  HTML += "</layer>";
  document.writeln(HTML);
}
