

var fcbooked='red' // colour of font if night is booked
var fcdate='olive' // colour of font if night is available

function numDaysIn(mth,yr) {
 if (mth==3 || mth==5 || mth==8 || mth==10) return 30;
 else if ((mth==1) && leapYear(yr)) return 29;
 else if (mth==1) return 28;
 else return 31;
}
function leapYear(yr) {
 if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0)
  return true;
 else
  return false;
}
// fixes a Netscape 2 and 3 bug
function getFullYear(d) { // d is a date object
 yr = d.getYear(); 
 yrNow = d.getYear();
 if (yr < 1000)
  yr+=1900;
 return yr;
}
function arr() {
 for (n=0;n<arr.arguments.length;n++) {
  this[n] = arr.arguments[n];
 }
}

// set up day and month arrays
weekdays = new arr("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
months = new arr("January","February","March","April","May", "June","July","August","September","October","November","December");
var filename="availability.html"; // target file
var flg=0;
var today = new Date();
var fs=2


hard_code_year = false; // change to true if desired
hard_yr = 2001; // specify your hard coded year if desired
hard_code_date = false; // change to true if desired
hard_mo = 11; // 0=Jan, 1=Feb, etc.

//Determine the selected month
function getMonth()
{
   if( hard_code_date )
      return( hard_mo );
   
   month=today.getMonth();
   moNow=today.getMonth();//Required for previous month test later
   endIndex=location.search.length;

   if( location.search.length > 1 )
   {
      index = location.search.indexOf(",");
      if( index != -1 )
      {
         endIndex=index;
      }

      mon = parseInt(location.search.substring(1,endIndex) );
      if ( ((""+mon).length<=2) && (mon >= 0) && (mon <= 11)) {
         month=mon;
      }
   }
   return( month );
}

//Determine the selected year
function getYear() 
{
   if (hard_code_year) {
      return( hard_yr );
   }

   year=getFullYear(today);

   if( location.search.length > 1 )
   {
      index = location.search.indexOf(",");
      if( index != -1 )
      {
         yr = parseInt(location.search.substring(index+1) );
      }
      if ( ((""+yr).length<=4) && (yr >= 1900) && (yr <= 2010)) {
         year=yr;
      }
   }
   return( year );
}

//Output HTML to provide a month selection choice.
function addMonthSelect()
{
   document.write( "<SELECT NAME='monthSelect' SIZE=1>" );
   for( i=0; i<12; i++ )
   {
      //alert("mo = "+mo);
	  (i == mo) ? document.write("<OPTION SELECTED>"+months[mo]) : document.write( "<OPTION>"+months[i]);
   }
   document.write( "</SELECT>" );
}


//Output HTML to provide a year selection choice.
function addYearSelect()
{
    document.write( "<SELECT NAME='yearSelect' SIZE=1>" );
	(today.getFullYear)?getY=today.getFullYear():getY=today.getYear(); // fixes Nutscape date bug
  
    for( i=0; i<3; i++ )
	{
    ((getY+i) == yr) ? document.write("<OPTION SELECTED>"+yr) : document.write( "<OPTION>"+(getY+i) );
	}
	document.write( "</SELECT>" );
}

//Check chosen date is not in the past
function checkDate(moSel,yrSel)
{
	if (yrSel.options[yrSel.selectedIndex].text < today.getYear() )
	{
	alert("The year is in the past - please select again "+ today.getYear() );
	return false;
	}
	else if (yrSel.options[yrSel.selectedIndex].text == today.getYear()  && moSel.options[moSel.selectedIndex].index < today.getMonth() )
	{
	alert("The date selected is in the past - please select again");
	return false;
	}
	else
	{
	changeDate(document.selectForm.monthSelect,document.selectForm.yearSelect);
	}
}

//Handle changing the date from month and year selects.
function changeDate(monthObj, yearObj)
{
   newMonth=monthObj.selectedIndex;
   newYear=yearObj.options[yearObj.selectedIndex].text;
   location.href=filename+"?"+newMonth+","+newYear;
}

//Main calendar generator.
function Calendar()
{
 todayYr=getFullYear(today);
 mo=getMonth();
 yr=getYear();

 bgn=new Date(months[mo]+" 1,"+yr);
 dy=bgn.getDay();
 ld=numDaysIn(mo,yr);

 // Start table
 document.write("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 "
 +" BGCOLOR='White'><TR><TD ALIGN=CENTER COLSPAN=7><SPAN  CLASS='hd'>"+months[mo]+" "+yr+"</TD></TR><TR>");

 for (var i=0;i<7;i++){
  document.write("<TD ALIGN=CENTER WIDTH=14% BGCOLOR=#C0A050><SPAN  CLASS='hd'>"+weekdays[i]+"</SPAN></TD>");
 }
 document.write("</TR><TR>");
 ctr=0;
 for (var i=0;i<7;i++) {

  if (i<dy){
   document.write("<TD ALIGN=CENTER>&nbsp;</TD>"); // write a space if a non-day
  }
  else {
   ctr++;
   di=i; // carry var i outside this routine
   dupWrt();
  }
 }

 document.write("</TR><TR>");
 while (ctr<ld){
  for (var i=0;i<7;i++){
   ctr++;
   /* If the ctr is greater than the last
      day of the month, print a space in
      this cell of the table. */
   if (ctr>ld){
    document.write("<TD ALIGN=CENTER>&nbsp;</TD>");
   }
   /* Otherwise, write date and the event,
      if any, in this cell of the table. */
   else{
    di=i; // carry var i outside this routine
    dupWrt();
   }
  }
  document.write("</TR><TR>");
 }

// Create prev/next jump links
 jump = "";
 if( ! hard_code_date )
 {
   document.write("<TD colspan=7 align=center height='30'>");

   // Prev link
   prevMonth=(mo - 1);
   prevYr=yr;
   if( mo == 0 )
   {
      prevMonth=11;
      prevYr=yr - 1;
   }
   
   if ((prevMonth>=moNow) || (prevYr>yrNow)) { //TEST if link to previous month is required
   //alert(prevMonth+" - "+moNow+" - "+ prevYr+" - "+yrNow);
   document.write('<a href="'+filename+'?'+prevMonth+','+prevYr+'"><IMG BORDER=0 SRC=images/left_arrow.gif> '+months[prevMonth]+' </a> &nbsp; | &nbsp; '); }

   // Middle
   //document.write(" &nbsp; | &nbsp; ");

   // Next link
   nextMonth=(mo + 1);
   nextYr=yr;
   if( mo == 11 )
   {
      nextMonth=0;
      nextYr=yr + 1;
   }
   document.write('<a href="'+filename+'?'+nextMonth+','+nextYr+'"> '+months[nextMonth]+' <IMG BORDER=0 SRC=images/right_arrow.gif></a>');
   document.write("</TD>");
   document.write("</TR>");
   document.write("<TR><TD colspan=7 align=center valign='bottom'>");
   document.write( "<FORM Name='selectForm'>" );
   addMonthSelect();
   addYearSelect();
   document.write( "<INPUT TYPE=BUTTON VALUE='Go' onClick='checkDate(document.selectForm.monthSelect,document.selectForm.yearSelect)'>");
   document.write( "</FORM>" );
   document.write("</TR></TABLE>");
  }
}

function dupWrt() {
 document.write("<TD ALIGN=CENTER");  // write a cell
 colorSet=0;
 if ( (mo==today.getMonth()) && (yr==todayYr) && (ctr==today.getDate()) )
 {
  /* Change color to whatever color you wish
     for the current date to be differentiated on
     screen.  I used green here.  */
  document.write(" BGCOLOR=#E1FAAF");
  colorSet=1;
 }
  /*  else if ( (di==0) )
 {
     Change color to whatever color you wish
     for Sundays to be differentiated on
     screen.  I used red here. 
  //document.write(" COLOR='gray'");
  //colorSet=1;
 }  */
 /* For dates without an event, an HTML padder
    that will align dates properly with dates
    having an event. Here, space break space. */
 tmp="";
 /* Then check for event in this month's events
    array for this date. If present, rewrite the
    tmp variable to contain the event. */
 if( B[yr] )
 {
  pos=B[yr][mo].indexOf("|"+ctr+" ");
  if (pos>-1){
   tmp=B[yr][mo].substring(pos+3,B[yr][mo].length);
   pos=tmp.indexOf("|");
   tmp=tmp.substring(0,pos);
   if (colorSet!=1){
    /* Change color to whatever color you wish
       for the events to be differentiated on
       screen.  I used blue here.  */
   document.write("  BGCOLOR='#FFFFCC'><S><FONT  COLOR="+fcbooked);
   }
  }
 }
 document.write(">"+ctr+tmp+"</TD>");
 }