//Declare Global Variables
var ie4=(document.all)? true:false;
var ns4=(document.layers)? true:false;
var ns6=(document.getElementById) ? true : false;
var ie5=(ie4 && ns6)?true:false;
if (ie5) { ns6 = false }
// Path to Images
var Image_Path = '/st/common/images/buttons/'
var HTMLMonths
var Calendar = new Date();
var TodaysDate = new Date();
var MaxDate = new Date();
var SelectedDate = new Date();
var FieldObjDay1 = new Object();
var FieldObjMonth1 = new Object();
var FieldObjYear1 = new Object();

var HomePage = true;
var hideHolidayType = false;
var year;

// Define Day Names and Month Names
var day_of_week = new Array('S','M','T','W','T','F','S');
var MonthName = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
// Netscape Fix on Y2K
if (ns4 || ns6) { year += 1900 };

// Write Layer onto Page
document.write('<div id="divcalendar"></div>');

function getRef(id) {
  if (ie5 || ns6) { return document.getElementById(id); }
  if (ie4) { return document.all[id]; }
  if (ns4) { return document.layers[id]; }
}

function generatetable() {
  // Create HTML
	cal = '<table class="calendartable" border="0" cellspacing="0" cellpadding="1">'
	cal += '<tr>';

  // Create row for days of week
  for(index=0; index < 7; index++) {
  	cal += '<th>' + day_of_week[index] + '</th>';
  }
  cal += '</tr>';
  day1 = Calendar.getDay()

  // Use empty cells to add gaps until today
  for(index=0; index < day1; index++) {
  	cal += '<td>&nbsp;</td>';
  }

  // Loop round all days in month
  for(index=0; index < 31; index++) {
	  var day  = Calendar.getDate();
	  if( day > index ) {
  		//Get the Day No.
		  week_day =Calendar.getDay();
	  	//Start new table row if Sunday
	  	if(week_day == 0) { cal += '<tr>'; }
		  if(week_day != 7) {
			  if (((day <= TodaysDate.getDate()-1) && (Calendar.getMonth() == TodaysDate.getMonth()) && (Calendar.getYear() == TodaysDate.getYear())) || ((day > MaxDate.getDate()) && (Calendar.getMonth() == MaxDate.getMonth()) && (Calendar.getYear() == MaxDate.getYear()))) {
				  HTMLtd = '<td>'
				  // Use different style for weekend
				  if ((week_day == 0) || (week_day == 6)) { HTMLtd = '<td class="daycellweekend">' }
				  if ((Calendar.getDate()==TodaysDate.getDate()-1) && (Calendar.getMonth() == TodaysDate.getMonth()) && (Calendar.getYear() == TodaysDate.getYear())) {
					  // Highlight Todays Date
					  cal += HTMLtd + '<span class="calendardayshighlight">' + day + '</span></td>';
				  } else {
					  // Disabled
					  cal += HTMLtd + '<span class="calendardaysdisabled">' + day + '</span></td>';
				  }
			  } else {
				  // Normal Day
				  HTMLtd = '<td>'
				  if ((week_day == 0) || (week_day == 6)) { HTMLtd = '<td class="daycellweekend">' }
				  // Highlight Selected Date
				  if ((day == SelectedDate.getDate()) && (Calendar.getMonth() == SelectedDate.getMonth()) && (Calendar.getYear() == SelectedDate.getYear())) {
					  cal += '<td class="daycellhighlight"><a href="javascript:setFieldDate(' + day + ')">' + day + '</a></td>'
				  } else {
					  cal += HTMLtd + '<a class="calendardays" href="javascript:setFieldDate(' + day + ')">' + day + '</a></td>';
				  }
			  }
		  }
		  // End Row if Saturday
		  if (week_day == 7) {  cal += '</tr>'; }
		  // Move to next Day in month
		  Calendar.setDate(day+1);
		}
	}
  cal += '</table>';
  return cal;
}


// Set the value in the date field specified
function setFieldDate(DayNo) {
  DisplayedMonth = Calendar.getMonth()
  DisplayedYear = Calendar.getYear();
  // Identify which month is selected
  if (DisplayedMonth == 0) {
	  DisplayedMonth = 12
  	DisplayedYear -= 1
  }
  if (DayNo < 10) { DayNo = '0' + DayNo }
  if (ns4 || ns6) { DisplayedYear += 1900 };	    		// Netscape Fix on Y2K
  // Set field value
  FieldObjDay1.options[DayNo-1].selected = true
  for (i=0;i<FieldObjMonthYear1.options.length;i++) {
	 if (FieldObjMonthYear1.options[i].value == (DisplayedMonth + '/' + DisplayedYear)) {
	  FieldObjMonthYear1.options[i].selected = true
	 }
  }

  // Hide the Layer
  HideLayer()
  DynamicUpdate('calendar_day','change')
}


// Display new date when Month selector is used
function DispNewDate() {
  // Get value of select field
 	IDRef = getRef('MonthSelection');
  NewDisplayDate = IDRef.value

  // Split Month and Year
  var NewDisplaySplit = NewDisplayDate.split(",")
  NewMonth = NewDisplaySplit[0]
  NewYear = NewDisplaySplit[1]
  // Set Calendar to show new month and year
  Calendar.setDate(1);
  Calendar.setMonth(NewMonth-1);
  Calendar.setYear(NewYear);
  // Update HTML
  UpdateDisplay()
}


// Move onto the next month
function NextMonth_Click() {
	Calendar.setDate(1);
	UpdateDisplay()
}


// Move to the previous month
function PrevMonth_Click() {
  NewMonth = Calendar.getMonth() - 1
  NewYear = Calendar.getYear()
  if (ns4 || ns6) { NewYear += 1900 };	    		// Netscape Fix on Y2K
  // If January then need to move to December and previous year
  if (NewMonth < 1) {
	  NewMonth = 12 + NewMonth
  	NewYear -= 1
  }
  // Set Calendar to new Month and Year
  Calendar.setMonth(NewMonth-1);
  Calendar.setYear(NewYear);
  Calendar.setDate(1);
  // Update HTML
  UpdateDisplay()
}


// Generate Months as HTML options
function HTMLMonths() {
  HTMLStr = ''
  // Identify how many months are required
  MaxMonthsDisp = (MaxDate.getYear() - TodaysDate.getYear()) * 12
  MaxMonthsDisp += (MaxDate.getMonth() - TodaysDate.getMonth())  + 1

  for(index=0; index < MaxMonthsDisp; index++) {
	  // Add months to current date and identify new month and year
  	ShowMonth = TodaysDate.getMonth() + index
	  ShowYear = TodaysDate.getYear()
	  if (ns4 || ns6) { ShowYear += 1900 };
  	ShowYear += Math.floor(ShowMonth / 12)
	  ShowMonth = (ShowMonth % 12) + 1

  	// Get the selected month and Year
	  SelMonth = Calendar.getMonth() + 1
	  SelYear = Calendar.getYear()
	  if (ns4 || ns6) { SelYear += 1900 };	    		// Netscape Fix on Y2K
  	SelectedOption = ''

  // Show current selection in select field
	  if ((SelMonth == ShowMonth) && (SelYear == ShowYear)) { SelectedOption = ' selected ' }
	  HTMLStr += '<option ' + SelectedOption + ' value="' + ShowMonth + ',' + ShowYear + '">' + MonthName[ShowMonth-1] + ' ' + ShowYear + '</option>';
  } // for
  return HTMLStr
}


// Update the HTML and show on Layer
function UpdateDisplay() {
	PrevMonthHTML = '&nbsp;'
	NextMonthHTML = '&nbsp;'

  // Generate HTML for Previous Month Link
	if ((Calendar.getMonth() != TodaysDate.getMonth()) || (Calendar.getYear() != TodaysDate.getYear())) {
  	PrevMonthHTML = '<a href="javascript:PrevMonth_Click()"><img src="'+Image_Path+'small_arrow_left.gif" border="0"></a>'
  }
  // Generate HTML for Next Month Link
  if ((Calendar.getMonth() != MaxDate.getMonth()) || (Calendar.getYear() != MaxDate.getYear())) {
  	NextMonthHTML = '<a href="javascript:NextMonth_Click()"><img src="'+Image_Path+'small_arrow_right.gif" border="0"></a>'
  }
  // Start to generate HTML for Page
  outHTML = '<div class="calendarmain">'

  // Add Month Navigation
  outHTML += '<div class="monthnav">' + PrevMonthHTML + '</div>'
  outHTML += '<select id="MonthSelection" onchange="DispNewDate()" name="MonthSel">' + HTMLMonths() + '</select>'
  outHTML += '<div class="monthnav">' + NextMonthHTML + '</div>'

  // Add Calendar Table
  outHTML += generatetable();

  // Add Close link
  outHTML += '<a class="actionclose" href="javascript:HideLayer()">Close</a></div>'

  // Output HTML to Layer
 	IDRef = getRef('divcalendar');
  IDRef.innerHTML = unescape(outHTML);
}

// Main Function
function PopupDate(imgobjname,datemin,datemax) {
  var xpos,ypos
  FieldObjDay1 = getElem('calendar_day')
  FieldObjMonthYear1 = getElem('calendar_month_year')
  // Set Minimum Date
  var NewDateMin = datemin.split("/")
  TodaysDate.setDate(NewDateMin[0]);//Date passed in is day after today;
  TodaysDate.setMonth(NewDateMin[1]-1);
  TodaysDate.setYear(NewDateMin[2]);
  // Set Maximum Date
  var NewDateMax = datemax.split("/")
  MaxDay = NewDateMax[0]
  MaxMonth = NewDateMax[1]-1
  MaxYear = NewDateMax[2]
  MaxDate.setDate(MaxDay);
  MaxDate.setMonth(MaxMonth);
  MaxDate.setYear(MaxYear);

  // Set current selected Date
  SelDay = FieldObjDay1.options[FieldObjDay1.selectedIndex].value
  var arrDateSplit = FieldObjMonthYear1.value.split('/')
  SelMonth = arrDateSplit[0]-1
  SelYear = arrDateSplit[1]
  // Check that Entered date is below Maximum allowable
  if (ns4 || ns6) { MaxYear += 1900 };	    		// Netscape Fix on Y2K
  if (((SelMonth > MaxMonth) && (SelYear == MaxYear)) || (SelYear > MaxYear)) {
	  SelDay = 1
  	  SelMonth = 1
	  SelYear = 2000
  }
  // Check that Entered date is after today
  MinMonth = TodaysDate.getMonth()
  MinYear = TodaysDate.getYear()
  if (ns4 || ns6) { MinYear += 1900 };	    		// Netscape Fix on Y2K
  if (((SelMonth < MinMonth) && (SelYear == MinYear)) || (SelYear < MinYear)) {
    SelDay = 1
    SelMonth = 1
    SelYear = 2000
  }
  // Set current selection
  SelectedDate.setDate(SelDay);
  SelectedDate.setMonth(SelMonth);
  SelectedDate.setYear(SelYear);

  // If no selection then start with this month and year
  if (SelYear == 2000) {
    SelMonth = TodaysDate.getMonth()
    SelYear = TodaysDate.getYear();
    if (ns4 || ns6) { SelYear += 1900 };	    		// Netscape Fix on Y2K
  }
  // Set to curent selection
  Calendar.setDate(1);
  Calendar.setMonth(SelMonth);  // Start the calendar month at now
  Calendar.setYear(SelYear);    // Start the calendar month at now

  //Move DIV
	ShowDropDown(0);
  IDRef = getRef('divcalendar');
  IDRef.style.display="block"

	// Identify if in a frameset
	if (document.body.clientWidth > 300) {
  	IDRef.style.top = '164px'
  	IDRef.style.left = (((document.body.clientWidth - 780)/2) + 10) + 'px'
	}

  // Uppdate HTML and layer
  UpdateDisplay()
}

// Hide the Layer
function HideLayer() {
  IDRef = getRef('divcalendar');
  IDRef.style.display = 'none';
	ShowDropDown(1);
}

function ShowDropDown(show) {
	if (document.all)
	{
      // drop down select elements to hide under the calendar //
      select_array=new Array ("calendar_day","calendar_month_year","NoNights","SearchAirport","SearchLocation")
      for (var i=0; i<select_array.length; i++) {
         document.getElementById(select_array[i]).style.visibility=(show==0)?"hidden":"visible"
      }
   }
}
