var monthName = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")
var monthShortName = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var weekName = new Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun")

function getMonthNumber(input){
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description: converts month string from form into number for use later
// *
// ****************************************************************
if (input == "Jan")
	{return "1"}
if (input == "Feb")
	{return "2"}
if (input  == "Mar")
	{return "3"}
if (input  == "Apr")
	{return "4"}
if (input  == "May")
	{return "5"}
if (input  == "Jun")
	{return "6"}
if (input == "Jul")
	{return "7"}
if (input  == "Aug")
	{return "8"}
if (input  == "Sep")
	{return "9"}
if (input  == "Oct")
	{return "10"}
if (input == "Nov")
	{return "11"}
if (input  == "Dec")
	{return "12"}
}


function isBrowserSupp() {
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description: Checks if browser is Netscape 2.0 since the options
// *				array properties don't work with Netscape 2.0x
// ****************************************************************

    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
        return true;
    }
}


function isLeapYear(yrStr)
{
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Checks if Year selected is a leap year
// ****************************************************************
var leapYear=false;
// every fourth year is a leap year
if ((parseInt(yrStr, 10)%4) == 0)
	{
	 if ((parseInt(yrStr, 10)%400) == 0)
	 {
	 	leapYear=false;	
	 }else
	 {
		leapYear=true;
	 }
	}
return leapYear;
}

function getDaysInMonth(mthIdx, YrStr)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Retrieves the number of days in a given month
// ****************************************************************
{
//Default number of days in a month is 31
var maxDays=31
// expect Feb.
if (mthIdx==2)
	{
	if (isLeapYear(YrStr))
		{
		maxDays=29;
		}
	else
		{
		maxDays=28;
		}
	}
// All the rest of the months have 30 days
if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11)
	{
	maxDays=30;
	}
return maxDays;
}


function adjustDate(mthIdx, Dt, Yr)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Adjusts the format of the Date
// ****************************************************************
{
var value=0;
var numDays=getDaysInMonth(mthIdx, Yr.options[Yr.options.selectedIndex].text);

if (mthIdx==2)
	{
	if (Dt.options.selectedIndex < numDays)
		{
		return 0;
		}
	else
		{
		//check for leap year
		Dt.options.selectedIndex=numDays;
		if (numDays==29)
			{
			return 99;
			}
		else
			{
			return 1;
			}
		}
	}
if (Dt.options.selectedIndex < numDays)
	{
	value=0;
	}
else
	{
	if (Dt.options.selectedIndex > numDays)
		{
		Dt.options.selectedIndex;
		value=3;
		}
	else
		{
		//index is 31 or 30
		value=2;
		}
	}
return value;
}


function parseMonth(mth, inM)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Parses a string and returns a month value
// ****************************************************************
{
var i=1;
var retval =1;
for (i=1;i<=12;i++)
	{
	if (mth == inM.options[i].text)
		{
		retval=i;
		break;
		}
	}
	return retval;
}

function parseDay(day, inD)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Parses a string and returns a day value
// ****************************************************************
{
var i=1;
var retval =1;
for (i=1;i<=31;i++)
	{
	if (day == inD.options[i].text)
		{
		retval=i;
		break;
		}
	}
return retval;
}

function parseYear(year, inY)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Parses a string and returns a year value
// ****************************************************************
{
var retval=0;
var i=0;
     for (i=0; i<=5; i++)
     {

	if (year == inY.options[i].text)
		{
		retval=i;
		break;
		}
     }
return retval;
}

//Calendar Section

//calculation functions
function nextMonth(month)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Retrieves the next Month's value
// ****************************************************************
{
if (month==12)
	{
	return 1;
	}
else
	{
	return (month+1);
	}
}


function prevMonth(month)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Retrieves the previous Month's value
// ****************************************************************
{
var prevMonth = (month-1)
if (month==1)
	{
	prevMonth = 12;
	}
return prevMonth
}

function changeYear(direction,month,year)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	Increments or decrements month when it goes
// *					past Jan or Dec
// ****************************************************************
{
var theYear = year
if (direction=="next")
	{
	if (month == 12)
		{
		theYear = (year+1)
		}
	}
if (direction=="prev")
	{
	if (month == 1)
		{
		theYear = (year-1)
		}
	}
return theYear
}


function createCalendar(month,year)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	//opens a new window for the calendar
// ****************************************************************
{
if (!isBrowserSupp())
	{
	alert("Your browser is outdated and does not support this feature")
 	return;
	}
	
if (navigator.appVersion.indexOf("Mac",0) != -1)
	{
    	calendarWindow = window.open("","Calendar","width=230,height=275,resizable=yes,scrollbars=no");
  	}
else
	{
	calendarWindow = window.open("","Calendar","width=230,height=255,resizable=yes,scrollbars=no");
  	}
	var mthIdx = month.options.selectedIndex
	var mthVal = getMonthNumber(month.options[mthIdx].text)
	var yearVal = year.options[year.options.selectedIndex].text

	//call the function to populate the window
	generateCalendar(calendarWindow,mthVal,yearVal)	
	calendarWindow.focus();		// Modified by Y.Hillion on 20/01/2003
}

//generates the meat of the calendar
function generateCalendar(target,month,year)
// ****************************************************************
// *	Author:	Geraldine Healy		Date:	25/08/1998
// *	Description:	generates the contents of the calender window
// *
// * 	Modified by: Y.Hillion		Date:	20/01/2003
// *		New look, display two years calendar
// *		with months on screen
// ****************************************************************
{
if (!isBrowserSupp())
	{
 	return;
	}

//begin table for calendar
target.document.open()
calendar = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><title>calendar "+year+"</title>"
calendar += "<STYLE><!-- TD.Active A{ FONT-WEIGHT: Bold; FONT-SIZE: 10px; COLOR: #000000; FONT-FAMILY: Verdana; TEXT-DECORATION: underline}"
calendar += "TD.NotActive { FONT-WEIGHT: normal; FONT-SIZE: 10px; COLOR: #000000; FONT-FAMILY: Verdana; TEXT-DECORATION: none}"
calendar += "A:hover{ COLOR: orangered}--></STYLE>"
calendar += "</head><body>"

//The parseInt function parses the string argument as a signed decimal integer.
var mthIdx = parseInt(month);
var endday = getDaysInMonth(mthIdx, year);
var prevyear = parseInt(year) - 1;
var nextyear = parseInt(year) + 1;
var today = new Date();
today.setDate(today.getDate());
var thisDate = today.getDate();
var thisDay = today.getDay();
var thisMonth = today.getMonth();
var thisyear = today.getYear();
if (thisyear < 2000)    // Y2K Fix, Yoan Hillion
thisyear = thisyear + 1900;


//year header - by default: two years displayed
if (thisyear == year) {  // display calendar for current year and the following year
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+year+")'><FONT SIZE=1 COLOR=#292074 face='verdana'><b>" + year + "</b></font></a> / "
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+nextyear+")'><FONT SIZE=1 face='verdana'><b>" + nextyear + "</b></font></a>"
} else {	// display calendar for selected year	
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+prevyear+")'><FONT SIZE=1 face='verdana'><b>" + prevyear + "</b></font></a> / "
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+year+")'><FONT SIZE=1 COLOR=#292074 face='verdana'><b>" + year + "</b></font></a>"
}
//months table
calendar +="<TABLE BORDER=1>"
calendar +="<tr valign=top align=center>"
for (var monthindex = 0; monthindex < 12; monthindex++){
	if (monthindex == 6) {		
		calendar +="</tr><tr>"	
	}
	if (monthindex == (mthIdx-1)) {
		calendar+="<TD WIDTH=17 BGCOLOR='yellow' class='Active'><a href='javascript:opener.generateCalendar(self,"+ (monthindex+1) +","+year+")'>" + monthShortName[monthindex] + "</a></td>"
	}
	else {
		calendar+="<TD WIDTH=17 class='Active'><a href='javascript:opener.generateCalendar(self,"+ (monthindex+1) +","+year+")'>" + monthShortName[monthindex] + "</a></td>"
	}
}
calendar+= "</tr><TR><TD COLSPAN=6 ALIGN='CENTER' class='Active'><A HREF='javascript:self.close();opener.clear()'>Clear</A></TD></TR>"
calendar+= "</table><p>"

//month header
var index = (mthIdx-1)
calendar += "<FONT SIZE='1' COLOR='white' face='verdana'><b>" + monthName[index] + " " + year + "</b></FONT><BR>"



// Weekdays
calendar +="<TABLE BORDER=1>"
calendar +="<tr valign=top align=center>"
//writes in the day of the week labels
calendar +="</tr><tr align=center>"
calendar +="<td width=25 class='NotActive'>Sun</td>"
calendar +="<td width=25 class='NotActive'>Mon</td>"
calendar +="<td width=25 class='NotActive'>Tue</td>"
calendar +="<td width=25 class='NotActive'>Wed</td>"
calendar +="<td width=25 class='NotActive'>Thu</td>"
calendar +="<td width=25 class='NotActive'>Fri</td>"
calendar +="<td width=25 class='NotActive'>Sat</td>"
calendar +="</tr>"

wholeDate = month + "/01/" + year
thedate = new Date(wholeDate)
firstDay = thedate.getDay()
selectedmonth = mthIdx;
selectedyear = year

var lastDay = (endday + firstDay+1)
calendar +="<tr>"
for (var i = 1; i < lastDay; i++)
	{
	if (i <= firstDay)
		{
		// 'empty' boxes prior to first day
		calendar +="<td></td>"
		}
	else
		{
		// Current Year, previous month
		if (((month -1) < thisMonth) && (year == thisyear)){
			// Disabled dates
			calendar +="<td align='center' class='NotActive'>"+(i-firstDay)+"</td>"
		} else {
			// Current Year, Current Month, previous day
			if (((i-firstDay) < thisDate) && ((month -1) == thisMonth) && (year == thisyear)){
				// Disabled dates
				calendar +="<td align='center' class='NotActive'>"+(i-firstDay)+"</td>"
			} else {		
				// Current Year, Current Month, Current day
				if (((i-firstDay) == thisDate) && ((month-1) == thisMonth) && (year == thisyear)){
					// highlighted day
					calendar +="<td align='center' BGCOLOR='yellow' class='Active'><a href='JavaScript:self.close();opener.closeCalendar("+(i-firstDay) + ")'><b>"+(i-firstDay)+"</b></a></td>"
				} else {
					// The Other dates
					calendar +="<td align='center' class='Active'><a href='JavaScript:self.close();opener.closeCalendar("+(i-firstDay) + ")'><b>"+(i-firstDay)+"</b></a></td>"
				}
			}
		}	
	}
	//must start new row after each week
	if (i % 7 == 0 &&  i != lastDay)
		{
		calendar +="</tr><tr>"
		}
	}
calendar +="</tr></table></body></html>"

if(navigator.userAgent.indexOf('MSIE',0) != -1)
	{
	target.document.close()
	}
target.document.write(calendar);
target.document.close()
}

function clear() {
	return;
}

function updArriveDay(strDay,strMonth,strYear) {
	var datDate, aryDayNames, wday;
	aryDayNames = new Array('Sun', 'Mon', 'Tue', 'Wed','Thu', 'Fri', 'Sat');
	//MG : Use the consructor for the date instead of the set method... 
	// It's to avoid problem with the 31 wich was set as 1...
	datDate = new Date(strYear,strMonth,strDay);
	wday=datDate.getDay(); 
	document.forms['frmSearch'].txtArriveDay.value=(aryDayNames[wday]+= ',');
}

function closeCalendar(day) {
	var datDate,strDay,strMonth,strYear;
	var yrIdx = parseYear(selectedyear,document.forms['frmSearch'].selArriveYear );	// Decrement index for day and month, because code assumes
	// that we have an extra defaultvalue at the start.
	document.forms['frmSearch'].selArriveMonth.options.selectedIndex=selectedmonth-1;
	document.forms['frmSearch'].selArriveYear.options.selectedIndex= yrIdx;
	document.forms['frmSearch'].selArriveDay.options.selectedIndex=parseInt(day)-1;
	
	strDay=document.forms['frmSearch'].selArriveDay.options[document.forms['frmSearch'].selArriveDay.selectedIndex].text;
	strMonth=document.forms['frmSearch'].selArriveMonth.options[document.forms['frmSearch'].selArriveMonth.selectedIndex].value - 1;
	strYear=document.forms['frmSearch'].selArriveYear.options[document.forms['frmSearch'].selArriveYear.selectedIndex].text;

	updArriveDay(strDay,strMonth,strYear);
	// No idea what this function call does - comment it out !
}

