
/*
 * In the head:
 * function calPopUp()  {window.open('calendar.jsp','Wincalendar','width=260,height=320,menubar=no,scrollbars=no,status=no,locationbar=no');}
*/
 

dayName = new Array ("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
monName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");


//list of date variables to be called throughout script.
//excluding now and monthName, these are integers showing the item's place in an array of months or days.
now = new Date();
today = now.getDay();
todate = now.getDate();
//thisIsThisYear is factually THIS year. thisYear can be changed by setPreviousMonth() and setNextMonth()
thisIsThisYear = now.getFullYear();
thisYear = thisIsThisYear;
//thisIsThisMonth is factually THIS month. thisMonth can be changed by setPreviousMonth() and setNextMonth()
thisIsThisMonth = now.getMonth();
thisMonth = thisIsThisMonth;
thisIsNineMonths = thisIsThisMonth + 9;
monthName = monName[thisMonth];
currentTime = now.getHours();
warning = "";

function clearWarning(){
	warning = "";
}

function setPreviousMonth() {
var prevMonth;
	clearWarning();
	if (thisMonth == 0){
		//if Jan, previous month is Dec, and 1 is subtracted from thisYear
		prevMonth = 11;
		thisYear--;
	} else {
		prevMonth = thisMonth - 1 ;
	}
thisMonth = prevMonth;
//renews monthName, then gets new firstDay and re-draws the table
monthName = monName[thisMonth];
getFirstDay();
setUp();
}

function setNextMonth(){
var nextMonth;
	if(thisYear == thisIsThisYear){
		NineMonths = thisIsNineMonths;
//		if(thisMonth < NineMonths){
			clearWarning();
			if (thisMonth == 11) {
				//if Dec, next month is Jan, and 1 is added to thisYear
				nextMonth = 0;
				thisYear++;
				thisMonth =  nextMonth;
			} else {
				nextMonth = thisMonth + 1 ;
				thisMonth =  nextMonth;
			}
//		} else {
//			warning = "Sorry, we do not accept bookings more than nine months in advance.";
//		}
	} else if(thisYear > thisIsThisYear){
		NineMonths = thisIsNineMonths - 12;
//		if(thisMonth < NineMonths) {
			clearWarning();
			if (thisMonth == 11) {
				//if Dec, next month is Jan, and 1 is added to thisYear
				nextMonth = 0;
				thisYear++;
				thisMonth =  nextMonth;
			} else {
				nextMonth = thisMonth + 1 ;
				thisMonth =  nextMonth;
			}
//		} else {
//			warning = "Sorry, we do not accept bookings more than nine months in advance.";
//		}
		}


//renews monthName, then gets new firstDay and re-draws the table
monthName = monName[thisMonth];
getFirstDay();
setUp();
}


/*****************
function setLastYear(){
lastYear = thisYear - 1;
return lastYear;
}

function setNextYear(){
nextYear = thisYear + 1;
alert("Next year: " + nextYear);
return nextYear;
}
*****************/

function getDaysInMonth(){
var days;
var month = thisMonth;
	if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
		days = 31;
	} else if (month == 3 || month == 5 || month == 8 || month == 10) {
		days = 30;
	} else if (month == 1){
		if (isLeapYear(thisYear)){
			days = 29;
		} else {
			days = 28;
		}
	}
return days;
}

function isLeapYear(which){
if (((which % 4)==0) && ((which % 100)!=0) || ((which % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}

function getFirstDay() {
var year = thisYear;
var month = thisMonth;
var first = new Date(year,month,1);
first = first.getDay();
return first;
}

//noarrow, leftarrow and rightarrow, is images defined on the page that uses this script.
//noarrow (a blank img), used to be '&nbsp;', but NS4 on WinNT, wants an image else the month name doesn't display. WIERD!
function writeMonthHeader(){
	page = "<tr>";
	if (thisMonth != thisIsThisMonth || thisYear!=thisIsThisYear) {
		page += "<td class='TD_CalMonth'><a href='javascript:setPreviousMonth();'>" + leftarrow + "</a></td>";
	} else {
		page += "<td class='TD_CalMonth'>" + noarrow + "</td>";	
	}
	page += "<td class='TD_CalMonth' colspan=" + (dayName.length-2) + ">" + monthName + " " + thisYear + "</td>";
	page += "<td class='TD_CalMonth' align='right'><a href='javascript:setNextMonth();'>" + rightarrow + "</a></td>";
	page += "</tr>"
	return page;
}

function writeDayHeaders(){
start = "<tr>";
end = "</tr>";
middle = "";
//i starts as 1, because 0 = Sun, and we want the calendar to start with Mon,
//therefore has to continue until dayName.length+1
	for (i=1; i<dayName.length+1; i++){
		//if i = 7, it is undefined in the array, therefore it is returned to 0 (Sun)
		if (i==(dayName.length)){
			j = 0;
		} else {
			j = i;
		}
		middle += ("<th>" + dayName[j] + "</th>");
	}
page = start + middle + end;
return page;
}

function setUpTheCalendar(){
var page = "";
var colCount = 0;
var days = getDaysInMonth();
var first = (getFirstDay()-1);;
if (first == -1) {
	first = 6;
}
//alert(first);

/************************
* days is now the number of days in the month plus the number of the first day
* (thus if today is April 4th, days = 30 + (4-1)).
* This allows us to push the month comfortably into the calendar table by buffering
* with blank cells at the start and the end.
* The total number of cells in the table is 42.
* 0 to first = blank cells
* first to days = the days of the month
* days to 42 = blank cells at the end of the month.
************************/
days += first;
var blankcell = "<td class='TD_CalBlank' width=26 height=16>&nbsp;</td>";

//set up table start
page += "<table border=0>"

//display warning, noarrow is for NS4, winNT, it needs an image else it doesn't display the message, WIERD!
page += "<tr><td colspan=" + dayName.length + " class='TD_CalError'>" + warning + "</td></tr>";

//add tableheaders to page variable
page += writeMonthHeader();
page += writeDayHeaders();
//add the correct number of blank cells to the start of the calendar
for (d=0; d<first; d++){
	page += blankcell;
	colCount ++;
}
//add the month-days to the calendar
/***********************
* This for loop checks several things at once:
* It uses a variable called currentDay which is the day of the month.
* Check whether thisMonth is greater than thisIsThisMonth or whether thisYear is greater than thisIsThisYear -
* if so all the days of this month are bookable, so the class of the cell has to show that: newweekend for w/e,
* and weekday for weekdays(!).
* If currentDay is less than the date today, it is not bookable, so the class changes: weekend for w/e, and default
* for weekdays.
* If currentDay is greater than the date today, again it is bookable.
* If currentDay is the same as the date today, it gets a special class="today"
* Then colCount is increased by 1.
* Then if colCount, when divided by 7, equals 0, the table row is closed and
* a new one opened.
***********************/
for (d=first; d<days; d++){
	currentDay = d - first + 1;
	/**********************
	* if thisMonth is nine months ahead of the today
	* check the value of todate to see whether it is the same as today or higher.
	* If so, it should be unbookable.
	**********************/
	if (thisYear == thisIsThisYear){
		if (thisMonth == thisIsThisMonth){
			if (currentDay < todate){
				if ((colCount % 7 == 6) || (colCount % 7 == 5)) {
					page += "<td class='TD_CalWeekEnd' width=26' height=16><a id='" + colCount + "' class='TD_CalWeekEnd'>" + currentDay + "</a></td>";
				} 
				else {
					page += "<td class='TD_CalPastDay' width=26 height=16><a id='" + colCount + "'>" + currentDay + "</a></td>";
				}
			} 
			else if (currentDay > todate){
				if ((colCount % 7 == 6) || (colCount % 7 == 5)) {
					page += "<td class='TD_CalWeekEnd' width=26 height=16><a id='" + colCount + "' class='TD_CalWeekEnd'  href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
				} 
				else {
					page += "<td class='TD_CALWeekDay' width=26 height=16><a id='" + colCount + "'  class='TD_CalWeekDay' href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
				}
			} 
			else if (currentDay == todate){
				if (currentTime < "16"){
					page+= "<td class='TD_CalToday' width=26 height=16><a id='" + colCount + "' class='TD_CalToday'  href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
				} 
				else {
					page+= "<td class='TD_CalToday'  width=26 height=16 id='" + colCount + "' title='It is too late to book for today' style='cursor:default'>" + currentDay + "</td>";
				}
			}
		}
		else {
			if ((colCount % 7 == 6) || (colCount % 7 == 5)) {
				page += "<td class='TD_CalWeekEnd' width=26 height=16><a id='" + colCount + "' class='TD_CalWeekEnd'  href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
			} 
			else 
			{
				page += "<td class='TD_CalWeekDay' width=26 height=16><a id='" + colCount + "'  class='TD_CalWeekDay' href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
			}
		}
	}
	else if (thisYear > thisIsThisYear){
		if ((colCount % 7 == 6) || (colCount % 7 == 5)) {
			page += "<td class='TD_CalWeekEnd' width=26 height=16><a id='" + colCount + "' class='TD_CalWeekEnd'  href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
		} 
		else 
		{
			page += "<td class='TD_CalWeekDay' width=26 height=16><a id='" + colCount + "'  class='TD_CalWeekDay' href='javascript:returnDate(" + currentDay + ")'>" + currentDay + "</a></td>";
		}
	}
	
	colCount ++;
	if (colCount % 7 == 0) {
		page += "</tr><tr>";
	}
}


// The remaining cells of the table (days to 42) are made blank, and again, checked for closing/
// opening table rows.
for (d=days; d<42; d++) {
	page += blankcell;
	colCount ++;
	if (colCount % 7 == 0) {
		page += "</tr><tr>";
	}
}
/*********************
//The colCount is checked for the last time to see whether the last table row can be closed.
if (colCount % 7 == 0) {
   	page += "</tr>";
      	if (d<41) {
        	page += "<tr>";
     	}
	}
********************/
//page is completed and then written into the page.
page += "</tr></table>";
//alert(page);
return page;
}


