monthNames = Array("","January","February","March","April","May","June","July","August","September","October","November","December");

var year;
var month;
var day;
var returnField;
var title;
 
function daysOfMonth(year,month) {
  if ((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) 
       days = 31;
  else if ((month==4)||(month==6)||(month==9)||(month==11)) 
  		days = 30;
  else if ((((year % 100)==0) && ((year % 400)==0)) || (((year % 100)!=0) && ((year % 4)==0))) 
  		days = 29;
  else 
  		days = 28;
  return days;
};

function createMonthDropDown(curMonth) {
  var monthDropDown = "";
  monthDropDown = "<select name='month' size='1' onChange='javascript:opener.showCalendar(self.document.Form1.year[self.document.Form1.year.selectedIndex].value,self.document.Form1.month[self.document.Form1.month.selectedIndex].value);'>\r\n";
  for (var i=1; i<=12; i++) {
    monthDropDown = monthDropDown + "  <option value='" + i + "'";
    if (i == curMonth) monthDropDown  = monthDropDown  + " selected";
    monthDropDown = monthDropDown + ">" + monthNames[i] + "</option>\r\n";
  }
  monthDropDown = monthDropDown + "</select>\r\n";
  return monthDropDown ;
}


function createYearDropDown(curYear) {
  var yearDropDown= "";
  yearDropDown= "<select name='year' size='1' onChange='javascript:opener.showCalendar(self.document.Form1.year[self.document.Form1.year.selectedIndex].value,self.document.Form1.month[self.document.Form1.month.selectedIndex].value);'>\r\n";
  var i;
  maxYear = new Number(curYear) + 5;
  for (i=curYear; i<=maxYear; i++) {
    yearDropDown = yearDropDown + "  <option value='" + i + "'";
    if (i == curYear) 
    	yearDropDown = yearDropDown + " selected";
    yearDropDown = yearDropDown + ">" + i + "</option>\r\n";
  }
  yearDropDown = yearDropDown + "</select>";
  return yearDropDown;
}

function createDaysTable(numYear,numMonth) {
  var theTable = "<table border='1' cellpadding='2' cellspacing='0' bgcolor='#BDC4D5'>\r\n  <tr>";
  var theDate = new Date();
  var today = new Date();
  theDate.setYear(numYear);
  theDate.setMonth(numMonth-1);
  theDate.setDate(1);
  adjustment = theDate.getDay();
  theTable = theTable + "\r\n    <td align='center'><strong>Sun</strong></td><td align='center'><strong>Mon</strong></td><td align='center'><strong>Tue</strong></td><td align='center'><strong>Wed</strong></td><td align='center'><strong>Thu</strong></td><td align='center'><strong>Fri</strong></td><td align='center'><strong>Sat</strong></td></div>\r\n  <tr>";
  for (var j=1; j<=adjustment; j++) {
    theTable = theTable + "\r\n    <td></td>";
  }
  for (var i=1; i<10; i++) {
    theTable = theTable + "\r\n    <td"
    if ((i == getCurDay(today)) && (numMonth == getCurMonth(today)) && (numYear == getCurYear(today))) 
    	theTable = theTable + " bgcolor='#ff0000'";
	theDate.setDate(i);
	if(theDate.getDay() == 6)	
	    theTable = theTable + "><input type='button' value='0" + i + "' onClick='javascript:opener.year=self.document.Form1.year[self.document.Form1.year.selectedIndex].value; opener.month=self.document.Form1.month[self.document.Form1.month.selectedIndex].value; opener.day=" + i + "; opener.setDate(); if(opener.checkDate() == true) self.close(); else self.focus();  '></td>";
    else
		theTable = theTable + ">" + i + "</td>";
	if (((i+adjustment) % 7)==0) theTable = theTable + "\r\n  </tr>\r\n\  <tr>";
  }
  numDays = daysOfMonth(numYear,numMonth);
  
  for (var i=10; i<=numDays; i++) {
    theTable = theTable + "\r\n    <td"
    if ((i == getCurDay(today)) && (numMonth == getCurMonth(today)) && (numYear == getCurYear(today))) theTable = theTable + " bgcolor='#ff0000'";
    theDate.setDate(i);
	if(theDate.getDay() == 6)
		theTable = theTable + "><input type='button' value='" + i + "' onClick='javascript:opener.year=self.document.Form1.year[self.document.Form1.year.selectedIndex].value; opener.month=self.document.Form1.month[self.document.Form1.month.selectedIndex].value; opener.day=" + i + "; opener.setDate(); if( opener.checkDate() == true) self.close(); else self.focus(); '></td>";
	else
		theTable = theTable + ">" + i + "</td>";
		
    if (((i+adjustment) % 7)==0) theTable = theTable + "\r\n  </tr>\r\n\  <tr>";
  }
  theTable = theTable + "\r\n  </tr>\r\n</table>";
  return theTable;
}

function showCalendar(numYear,numMonth) {
	
  var html = "";
  html = html + "<html>\r\n<head>\r\n  <title>" + title + "</title>\r\n</head>\r\n<body bgcolor='#ffffff'>\r\n  <div align='center'>\r\n  <form name='Form1'>\r\n";
  html = html + createMonthDropDown(numMonth);
  html = html + createYearDropDown(numYear);
  html = html + createDaysTable(numYear,numMonth);
  html = html + "\r\n  </form>\r\n  </div>\r\n</body>\r\n</html>\r\n";
  calWindow = open("","calendar","width=270,height=250");
  calWindow.document.open();
  calWindow.document.writeln(html);
  calWindow.document.close();
  calWindow.focus();

}

function getCurYear(theDate) {
  if (theDate.getYear() < 1900 ) 
	return theDate.getYear() + 1900
  else return theDate.getYear();
}

function getCurMonth(theDate) {
 
  return theDate.getMonth()+1;
}

function getCurDay(theDate) {
  
  return theDate.getDate();
  
}

function getDate(startDate, dateField,windowTitle) 
{
  var begDate;
  var today = new Date();
	
  if(startDate == "")
  {
	begDate = new Date(2002, 11, 21,0,0,0,0);
  } else {
	dateComps = startDate.split("index.html");
	begDate = new Date(dateComps[2], dateComps[1]-1, dateComps[0],0,0,0,0);
  }
  
  if (begDate.getTime()<today.getTime())
  	begDate = today;
  
  year = getCurYear(begDate);
  month = getCurMonth(begDate);
  day = getCurDay(begDate);
 
  returnField= dateField;
  title = windowTitle;
  //alert('year: ' + year);
  //alert('month: ' + month);
  //alert('day: ' + day);
  showCalendar(year,month);
}

function setDate() 
{
  returnField.value = day + "/" + month + "/" + year;
  
}
 
function checkDate()
{
	selDate = new Date(year, month-1,day,0,0,0);
	var diff = 0.0;
	today = new Date();
	if(selDate.getTime()<today.getTime())
	{
		alert("The date you have selected is in the past. Please choose another date.");
		self.focus();
		return(false);
	} 
	
	if(document.forms[0].arrival_date.value == "")
		return(true);
	if(document.forms[0].departure_date.value == "")
		return(true);
		
	dateComps = document.forms[0].arrival_date.value.split("index.html");
	fromDate = new Date(dateComps[2], dateComps[1]-1, dateComps[0],0,0,0,0);
	dateComps = document.forms[0].departure_date.value.split("index.html");
	toDate = new Date(dateComps[2], dateComps[1]-1, dateComps[0],0,0,0,0);
	diff = toDate.getTime()-fromDate.getTime();
	if(diff < 0)
	{
		alert("The arrival date specified is after the departure date. Please check the dates specified.");
		return(false);
	}
	if(diff == 0)
	{
		alert("The arrival and departure date are the same. Please check the dates specified.");
		return(false);
	}
	
	return(true);
}

