/* mon1-12 arrays have "1" if theres a picture to display
curmon is the current month array to which this data is copied
V1.1  Traps an error if the date is outside the range of images
V1.2  Allow rollover to next and previous years
*/

var curmon = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

 var mon1 = new Array(0,1,1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,1,0,1,1,1);
 var mon2 = new Array(0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0);
 var mon3 = new Array(0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1);
 var mon4 = new Array(0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0);
 var mon5 = new Array(0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0);
 var mon6 = new Array(0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
 var mon7 = new Array(0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,1);
 var mon8 = new Array(0,0,0,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,1);
 var mon9 = new Array(0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,1,1,0,0,0);
var mon10 = new Array(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0);
var mon11 = new Array(0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0);
var mon12 = new Array(1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0);


var valid=false;
var current = 2010;
var heading = "#000000";
var bgcolor1 = "#ffffcc";
var bgcolor2 = "#808080";
var bgcolor3 = "yellow"
var bgcolor4 ="red"
var oldcol="#000000"
var font1 = "white";
var font2 = "black";
var height = "180";
var width = "250";

/* Used to set the calendar */
function DaysInMonth(Y, M) {
    with (new Date(Y, M, 1, 12)) {
        setDate(0);
        return getDate();
    }
}

/* Move the relevant month data in curmon(array) */
function setcal(mon,yea)
{
	mon=mon+1;
	switch(mon)
	{
		case 1:
		for (x in curmon)
		{
			curmon[x]=mon1[x]
		}
		break;
		case 2:
		for (x in curmon)
		{
			curmon[x]=mon2[x]
		}
		break;
		case 3:
		for (x in curmon)
		{
			curmon[x]=mon3[x]
		}
		break;
		case 4:
		for (x in curmon)
		{
			curmon[x]=mon4[x]
		}
		break;
		case 5:
		for (x in curmon)
		{
			curmon[x]=mon5[x]
		}
		break;
		case 6:
		for (x in curmon)
		{
			curmon[x]=mon6[x]
		}
		break;
		case 7:
		for (x in curmon)
		{
			curmon[x]=mon7[x]
		}
		break;
		case 8:
		for (x in curmon)
		{
			curmon[x]=mon8[x]
		}
		break;
		case 9:
		for (x in curmon)
		{
			curmon[x]=mon9[x]
		}
		break;
		case 10:
		for (x in curmon)
		{
			curmon[x]=mon10[x]
		}
		break;
		case 11:
		for (x in curmon)
		{
			curmon[x]=mon11[x]
		}
		break;
		case 12:
		for (x in curmon)
		{
			curmon[x]=mon12[x]
		}
		break;
	}
	
	var days = DaysInMonth(yea,mon);
	var D = new Date(mon+"/01/"+yea);
	var day = D.getDay();
		
	var ar = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var df = ar[mon-1];
	df = df+", "+D.getFullYear();
	document.xxx.ddd.value = df;

	var ddf = document.calen.length;
	/* Clear the calendar image and background colour*/
  /* Check if its the year for the dataset */
   
	if (yeare != current)
  {
  valid=false
  }
  else
  (
  valid=true
  )

	for(var xx=0; xx<ddf; xx++)
	{
		document.calen[xx].value = ""
		document.calen[xx].style.background=bgcolor1
	}
	/* Add the date to the calendar */
	var ss = 1;
	for(var xx=day; xx<day+days; xx++){
		document.calen[xx].value = ss++;
	/* If there is data, make the background yellow*/	
		if (curmon[ss-2]==1 && valid == true) 
			{
			document.calen[xx].style.background=bgcolor3
			}
	}
}

/*Get the current Month and day */
var exd = new Date();
var monthe = exd.getMonth();
var yeare = exd.getFullYear();


/* When you click on a date, show the relevant image.gif and text.html */
/* If its got an event, show that, or else the help image and text */
/* If the year is not as expected, show an error page */
function mocl(Y)
{
	if (yeare != current)
  {
  valid=false
  }
  else
  (
  valid=true
  )
	selday=document.calen[Y].value
	imgok=curmon[selday-1]
	
	if (valid == false) 
	{ 
    document.getElementById("textarea").src="text/"+"error.html"
		document.getElementById("imagearea").src="images/"+"help.gif"
	}
	else if ((selday !="") && (imgok=="1"))
	{ 
 		imgday=selday
		imgmon=(monthe+1)
		img=imgday + "-" + imgmon + "-" + yeare
		document.getElementById("imagearea").src="images/"+imgmon+"/"+img+".gif"
		document.getElementById("textarea").src="text/"+imgmon+"/"+img+".html"
  }
	else
	{
    document.getElementById("textarea").src="text/"+"help.html"
		document.getElementById("imagearea").src="images/"+"help.gif"
  }

  return
  }
/* Move over the date, turn it red */
function movr(Y)
	{
	if (document.calen[Y].value != "")
		{
		oldcol=document.calen[Y].style.background
    	document.calen[Y].style.background=bgcolor4
		}
  return
  	}
/* Move away from the date, return the original background */  
function moot(Y)
	{
	if (document.calen[Y].value != "")
		{
		document.calen[Y].style.background=oldcol
		}
  return
  }
  
/* Go back a month, roll over to a previous year if <January */
function prev()
{
	monthe = monthe-1;
	if(monthe < 0)
	{
		monthe=11
		yeare=yeare-1
	}
	setcal(monthe, yeare);
	return false;
}

/* Go forward a month, roll over to next year if December */
function next()
{
	monthe = monthe+1;
	if(monthe > 11)
	{
		monthe=0
		yeare=yeare+1
	}
	setcal(monthe, yeare);
	return false;
}

/* Set the calendar Month and Year */
function thismon()
{
	setcal(monthe, yeare);
}

/* This writes the calendar form to the top left corner */

document.write("<table width="+width+" height="+height+" cellpadding=1 cellspacing=0 \
		style=\"font-family: arial, verdana; color: "+font1+"; font-size: 12px;\" bgcolor="+bgcolor1+" border=1>\
			<tr bgcolor="+heading+" align=center><td colspan=7>\
				<table width=100% align=center style=\"color: "+font1+"; font-size: 12px;\" align=center>\
				<tr align=center><td><a style=\"cursor: pointer;\" onclick=\"return prev()\">Prev</a></td>\
				<td><form name=xxx style=\"margin: 0px; padding: 0px;\"><input size=12 style=\"font-size: 12px; \
				font-weight: bold; text-align: center; font-family: san-serif, verdana, arial;\" type=text name=ddd></form></td>\
				<td><a style=\"cursor: pointer;\" onclick=\"return next()\">Next</a></td></tr></table>\
			</td></tr>\
			<tr bgcolor="+bgcolor2+" align=center><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thur</td><td>Fri</td><td>Sat</td></tr>");
document.write("<form name=calen style=\"border: 0px; padding:0px;\">");
for(var xx=0; xx<6; xx++)
{
	document.write("<tr>");
	for(var cc=0; cc<7; cc++)
  {
		var dd = xx*7+cc;
    document.write("<td align=center><input style=\"background-color: "+bgcolor1+"; color: "+font2+"; border: 0px; \" name=x"+dd+" onclick=\"mocl("+dd+")\"  onmouseover=\"movr("+dd+")\"  onmouseout=\"moot("+dd+")\" readonly size=2> </td>")
  }
document.write("</tr>");
}
document.write("</form></table>");
thismon();
