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){
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() {
  // 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)
{
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)
{
//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)
{
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)
{
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)
{
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)
{
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)
{
if (month==12)
	{
	return 1;
	}
else
	{
	return (month+1);
	}
}


function prevMonth(month)
{
var prevMonth = (month-1)
if (month==1)
	{
	prevMonth = 12;
	}
return prevMonth
}

function changeYear(direction,month,year)
{
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,io)
{
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=250,height=300,resizable=yes,scrollbars=no");
  	}
else
	{
	calendarWindow = window.open("","Calendar","width=250,height=300,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,io)
	calendarWindow.focus();		// Modified by Y.Hillion on 20/01/2003
}

//generates the meat of the calendar
function generateCalendar(target,month,year,io)
{
if (!isBrowserSupp())
	{
 	return;
	}

//begin table for calendar
target.document.open()
calendar = "<html><head><title>calendar "+year+"</title><link href=ucc.css rel=stylesheet type=text/css>"
calendar += "</head><body bgcolor=#FBFBFB>"

//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;

//months table
calendar +="<TABLE Border=0 bgcolor='black' cellpadding='1' cellspacing='0' width='98%'>"
calendar +="<tr><td>"
calendar +="<TABLE Border=0 bgcolor='#F0F0F0' cellpadding='3' cellspacing='1' width='100%'>"
calendar+="<tr><td colspan='7' bgcolor='#e0e0e0'><div align='center'><b><font color='blue' size='2'>" 
//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 +","+io+")'><b>" + year + "</b></a> / "
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+nextyear+ ","+io + ")'><b>" + nextyear + "</b></a>"
} else {	// display calendar for selected year
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+prevyear+ ","+io+ ")'><b>" + prevyear + "</b></a> / "
	calendar+="<a href='javascript:opener.generateCalendar(self,"+ (month) +","+year+ ","+io+ ")' class=ver10><b>" + year + "</b></a>"
}
calendar+="</font></b></div></td></tr>"
calendar +="<tr>"
for (var monthindex = 0; monthindex < 12; monthindex++){
	if (monthindex == 6) {
		calendar+="</tr><tr>"
	}
	if (monthindex == (mthIdx-1)) {
		calendar+="<TD bgcolor='yellow'><a href='javascript:opener.generateCalendar(self,"+ (monthindex+1) +"," + year + ","+io+")'>" + monthShortName[monthindex] + "</a></td>"
	}
	else {
		calendar+="<TD bgcolor='#F0F0F0' valign='left'><a href='javascript:opener.generateCalendar(self,"+ (monthindex+1) +"," + year +","+io+")'><font color='#BB0000'>" + monthShortName[monthindex] + "</font></a></td>"
	}
	
}
calendar+="</tr><TR><TD COLSPAN=6 ALIGN='CENTER' class='ver10' bgcolor='#F0F0F0'></TD></TR>"
calendar+="</table></td></tr></table><p>"

//month header
var index = (mthIdx-1)
//calendar += "<b>" + monthName[index] + " " + year + "</b><BR>"



// Weekdays
calendar +="<TABLE Border=0 bgcolor='black' cellpadding='1' cellspacing='0'>"
calendar +="<tr><td>"
calendar +="<TABLE BORDER=0 bgcolor='#F0F0F0' cellpadding='3' cellspacing='1' width='100%'>"
calendar+="<tr><td colspan='7' bgcolor='#e0e0e0'><div align='center'><b><font color='blue' size='2'>" + monthName[index] + " " + year + "</font></b></div></td></tr>"
calendar +="<tr valign=top align=center bgcolor='#FFFFFF'>"
//writes in the day of the week labels
calendar +="</tr><tr align=center>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Sun</font></td>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Mon</font></td>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Tue</font></td>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Wed</font></td>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Thu</font></td>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Fri</font></td>"
calendar +="<td width=25 class='ver10' bgcolor='#F0F0F0'><font color='green'>Sat</font></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 bgcolor='#F0F0F0'></td>"
		}
	else
		{
		// Current Year, previous month
		if (((month -1) < thisMonth) && (year == thisyear)){
			// Disabled dates
			calendar +="<td align='center' class='ver10' bgcolor='#F0F0F0'>"+(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='ver10' bgcolor='#F0F0F0'>"+(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='ver10' bgcolor='#F0F0F0'><a href='JavaScript:self.close();opener.closeCalendar" + io + "("+(i-firstDay) + ")'><b>"+(i-firstDay)+"</b></a></td>"
				} else {
					// The Other dates
					calendar +="<td align='center' class='ver10' bgcolor='#F0F0F0'><a href='JavaScript:self.close();opener.closeCalendar"+io+"("+(i-firstDay) + ")'><font color='BB0000'>"+(i-firstDay)+"</font></a></td>"
				}
			}
		}
	}
	//must start new row after each week
	if (i % 7 == 0 &&  i != lastDay)
		{
		calendar +="</tr><tr bgcolor='#F0F0F0'>"
		}
	}
calendar +="</tr></table></td></tr></table><br><A HREF='javascript:self.close();opener.clear()'><div align='center'>Close Calendar</div></A></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('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday', 'Saturday');
	//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 updDeptDay(strDay,strMonth,strYear,flag) {
 	var datDate, aryDayNames, wday;
	aryDayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday', 'Saturday');
	//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'].txtDeptDay.value=(aryDayNames[wday]+= ',');
}
function closeCalendar1(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.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 !
}
function closeCalendar2(day) {
	var datDate,strDay,strMonth,strYear;
	var yrIdx = parseYear(selectedyear,document.forms['frmSearch'].selDeptYear );	// Decrement index for day and month, because code assumes
	// that we have an extra defaultvalue at the start.
	document.forms['frmSearch'].selDeptMonth.options.selectedIndex=selectedmonth-1;
	document.forms['frmSearch'].selDeptYear.options.selectedIndex= yrIdx;
	document.forms['frmSearch'].selDeptDay.options.selectedIndex=parseInt(day)-1;

	strDay=document.forms['frmSearch'].selDeptDay.options[document.forms['frmSearch'].selDeptDay.selectedIndex].text;
	strMonth=document.forms['frmSearch'].selDeptMonth.options[document.forms['frmSearch'].selDeptMonth.selectedIndex].value - 1;
	strYear=document.forms['frmSearch'].selDeptYear.options[document.forms['frmSearch'].selDeptYear.selectedIndex].text;

	updDeptDay(strDay,strMonth,strYear);
	// No idea what this function call does - comment it out !
}
