function createMonths(cottage,display) {
  currentDate = new Date()
  with (currentDate) {
    var currentMonth = getMonth();
    var currentYear = getFullYear();
    // increment months and create tables
    for(i=0; i<12; i++) {
      if(i+currentMonth < 12){
        var myMonth = currentMonth + i          
        var myYear = currentYear
        }
        else
        {
          var myMonth = currentMonth + i -12
          var myYear = currentYear+1
        }
      setCal(myMonth, myYear, cottage, display)
    }    
  }
}

function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
/* else */ // else not needed when statement is "return"
return false // is not leap year
}

function getDays(month, year) {
// create array to hold number of days in each month
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December

// return number of days in the specified month (parameter)
return ar[month]
}

function getMonthName(month) {
// create array to hold name of each month
var ar = new Array(12)
ar[0] = "January"
ar[1] = "February"
ar[2] = "March"
ar[3] = "April"
ar[4] = "May"
ar[5] = "June"
ar[6] = "July"
ar[7] = "August"
ar[8] = "September"
ar[9] = "October"
ar[10] = "November"
ar[11] = "December"

// return name of specified month (parameter)
return ar[month]
}

function setCal(month, year, cottage, display) {
// standard time attributes
if (year < 1000)
year+=1900
var monthName = getMonthName(month)

// create instance of first day of month, and extract the day on which it occurs
var firstDayInstance = new Date(year, month, 1)
var firstDay = firstDayInstance.getDay()
firstDayInstance = null

// number of days in current month
var days = getDays(month, year)

// call function to draw calendar
drawCal(firstDay + 2, days, Date(year, month), monthName, month, year, cottage, display)
}

//function to get myDate
function getMyDate(month, digit, year) {
  if(eval(month+1) < 10){
   var iMonth = '0'+eval(month+1)
  }
  else{
   var iMonth = eval(month+1)
  };
  
  if(digit < 10){
   var iDigit = '0'+digit
  }
  else{
   var iDigit = digit
  };
  var myDate = iDigit+''+iMonth+''+year;
  return myDate;
};



function drawCal(firstDay, lastDate, date, monthName, month, year, cottage, display) {
// constant table settings
var myClass = "calendar" // css class of table
var cellspacing = 4 // width of table's border
var colWidth = 30 // width of columns in table
var dayCellHeight = 25 // height of cells containing days of the week
var cellHeight = 25 // height of cells representing dates in the calendar

// create basic table structure
var text = "" // initialize accumulative variable to empty string

text += '<TABLE CLASS=' + myClass + '>' // table settings
text += '<TH COLSPAN=8>' // create table header cell
text += monthName + ' ' + year 
text += '</TH>' // close header cell

// variables to hold constant settings
var openCol = '<TD WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '>'
var closeCol = '</TD>'

// create array of abbreviated day names
var weekDay = new Array(7)
weekDay[0] = "Sat"
weekDay[1] = "Sun"
weekDay[2] = "Mon"
weekDay[3] = "Tues"
weekDay[4] = "Wed"
weekDay[5] = "Thu"
weekDay[6] = "Fri"


// create first row of table to set column width and specify week day
text += '<TR>'
text += openCol + '&pound; wk' + closeCol 
for (var dayNum = 0; dayNum < 7; ++dayNum) {
text += openCol + weekDay[dayNum] + closeCol 
}
text += '</TR>'

// declaration and initialization of two variables to help with tables
var digit = 1
var curCell = 1

for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
text += '<TR>';

// find date for begining of week
var firstDateInWeek = getMyDate(month, digit, year);


if(firstDay == 8) {
 var firstDay = 1
};

function isDefined(variable) {
  return (typeof(window[variable]) == "undefined")?  false: true;
}

// check if the price has been defined
if(isDefined('PRICE'+cottage+firstDateInWeek) && eval('PRICE'+cottage+firstDateInWeek) != ""){
  var myPrice = eval('PRICE'+cottage+firstDateInWeek) 
  }
  else {
  var myPrice = '';
}

// set the id and form name of the cell
var myCellID = 'PRICE'+cottage+firstDateInWeek

// if we're displaying in a form, display the price in a form field
if(display == 'form') {
  var myPrice = '<input type="text" size="2" name="'+myCellID+'" value="'+myPrice+'">'
}

text += '<td id="'+myCellID+'">'+myPrice+'</td>';

for (var col = 1; col <= 7; ++col) {
if (digit > lastDate)
break
if (curCell < firstDay) {
text += '<TD></TD>';


curCell++
} else {

//set id and create cell with data in it
var myDate = getMyDate(month, digit, year);

// check if this day has been booked
if(isDefined('b'+cottage+myDate) && eval('b'+cottage+myDate)!=""){
  myClass = 'dataBooked';
  isChecked = 'checked';
  }
  else {
  myClass = 'data';
  isChecked = '';
}

// are we displaying form fields
if(display == 'form') {
  myCheckbox = '<input type="Checkbox" value="1" name="b'+cottage+myDate+'"'+ isChecked +'>'
  }
else {
  myCheckbox = '';
}

text += '<TD class="'+ myClass +'" id="'+cottage+''+myDate+'" HEIGHT=' + cellHeight + '>' + digit + myCheckbox +'</TD>'
digit++
}
}
text += '</TR>'
}

// close all basic table tags
text += '</TABLE>'

// print accumulative HTML string
document.write(text)
  
}
