// JavaScript Document
function calendar(id,d,p){
	this.id = id;
	this.dateObject = d;
	this.pix = p;
	this.write = writeCalendar;
	this.length = getLength;
	this.month = d.getMonth();
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	this.getFormattedDate = getFormattedDate;
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);
}

function calendar2(id,d,p){
	this.id = id;
	this.dateObject = d;
	this.pix = p;
	this.write = writeCalendar2;
	this.length = getLength;
	this.month = d.getMonth();
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	this.getFormattedDate = getFormattedDate;
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);
}

var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var monthnum = new Array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12');

function getFormattedDate(){
	return monthnum[this.month] + '/' + this.date + '/' + this.year;
	//return this.month + '/' + this.date + '/' + this.year;
}

function writeCalendar(){
	var calString = '<div id="calContainer">';
	//write month and year at top of table
	calString += '<table id="cal' + this.id + '" cellspacing="0" width="200" style="border:1px black solid;">';
	//write the image – comment out to hide images
	//calString += '<tr><th colspan="7"><img src="' + this.pix[this.month].src + '"/></th></tr>';
	//write the month
	calString += '<tr><th colspan="7" style="background-color: navy; color: white; font: 10pt sans-serif;">' + months[this.month] + ', ' + this.year + '</th></tr>';
	//write a row containing days of the week
	calString += '<tr>';
	
	for(i=0;i<days.length;i++){
		calString += '<th bgcolor="CCCCCC" style="color: black; font: 10pt sans-serif; border-bottom: 1px black solid; font-weight: bold;">' + days[i].substring(0,3) + '</th>';
	}
	
	//write the body of the calendar
	calString += '<tr>';
	//create 6 rows so that the calendar doesn't resize
	for(j=0;j<42;j++){
		var displayNum = (j-this.firstDay+1);
		if(j<this.firstDay){
			//write the leading empty cells
			calString += '<td style="background-color: white; border-bottom: 1px black solid;">&nbsp;</td>';
		}else if(displayNum==this.date){
			calString += '<td id="' + this.id +'selected" style="color: maroon; font: 10pt sans-serif; font-weight: bold; border-bottom: 1px black solid; border-left: 1px black solid; border-right: 1px black solid; cursor: pointer; cursor: hand;" onClick="javascript:changeDate(this,\'' + this.id + '\')">' + displayNum + '</td>';
		}else if(displayNum > this.length()){
			//Empty cells at bottom of calendar
			calString += '<td>&nbsp;</td>';
		}else{
			//the rest of the numbered cells
			calString += '<td id="" style="color: black; background-color: rgb(235,235,235); font: 10pt sans-serif; border-bottom: 1px black solid; border-left: 1px black solid; border-right: 1px black solid; cursor: pointer; cursor: hand;" onClick="javascript:changeDate(this,\'' + this.id + '\')">' + displayNum + '</td>';
		}
		if(j%7==6){
			calString += '</tr><tr>';
		}
	}
	//close the last number row
	calString += '</tr>';
	//write the nav row
	calString += '<tr>';
	calString += '<td style="cursor: pointer; background-color: navy; color: white; font: 10pt sans-serif;cursor: hand; text-decoration:underline;" onClick="changeMonth(-12,\'' + this.id + '\')">&lt;</td>';
	calString += '<td style="cursor: pointer; cursor: hand;background-color: navy; color: white; font: 10pt sans-serif;" onClick="changeMonth(-1,\'' + this.id + '\')">&lt;</td>';
	calString += '<td style="background-color: navy; color: white; font: 10pt sans-serif;" colspan="3">&nbsp;</td>';
	calString += '<td style="cursor: pointer; cursor: hand;background-color: navy; color: white; font: 10pt sans-serif;" onClick="changeMonth(1,\'' + this.id + '\')">&gt;</td>';
	calString += '<td style="cursor: pointer; cursor: hand;background-color: navy; color: white; font: 10pt sans-serif; text-decoration:underline;text-align:right;" onClick="changeMonth(12,\'' + this.id + '\')">&gt;</td>';
	calString += '</tr>';
	
	calString += '</table>';
	calString += '</div>';
	return calString;
}

function writeCalendar2(){
	var calString = '<div id="calContainer2">';
	//write month and year at top of table
	calString += '<table id="cal' + this.id + '" cellspacing="0" width="200" style="border:1px black solid;">';
	//write the image – comment out to hide images
	//calString += '<tr><th colspan="7"><img src="' + this.pix[this.month].src + '"/></th></tr>';
	//write the month
	calString += '<tr><th colspan="7" style="background-color: navy; color: white; font: 10pt sans-serif;">' + months[this.month] + ', ' + this.year + '</th></tr>';
	//write a row containing days of the week
	calString += '<tr>';
	
	for(i=0;i<days.length;i++){
		calString += '<th bgcolor="CCCCCC" style="color: black; font: 10pt sans-serif; border-bottom: 1px black solid; font-weight: bold;">' + days[i].substring(0,3) + '</th>';
	}
	
	//write the body of the calendar
	calString += '<tr>';
	//create 6 rows so that the calendar doesn't resize
	for(j=0;j<42;j++){
		var displayNum = (j-this.firstDay+1);
		if(j<this.firstDay){
			//write the leading empty cells
			calString += '<td style="background-color: white; border-bottom: 1px black solid;">&nbsp;</td>';
		}else if(displayNum==this.date){
			calString += '<td id="' + this.id +'selected" style="color: maroon; font: 10pt sans-serif; font-weight: bold; border-bottom: 1px black solid; border-left: 1px black solid; border-right: 1px black solid; cursor: pointer; cursor: hand;" onClick="javascript:changeDate2(this,\'' + this.id + '\')">' + displayNum + '</td>';
		}else if(displayNum > this.length()){
			//Empty cells at bottom of calendar
			calString += '<td>&nbsp;</td>';
		}else{
			//the rest of the numbered cells
			calString += '<td id="" style="color: black; background-color: rgb(235,235,235); font: 10pt sans-serif; border-bottom: 1px black solid; border-left: 1px black solid; border-right: 1px black solid; cursor: pointer; cursor: hand;" onClick="javascript:changeDate2(this,\'' + this.id + '\')">' + displayNum + '</td>';
		}
		if(j%7==6){
			calString += '</tr><tr>';
		}
	}
	//close the last number row
	calString += '</tr>';
	//write the nav row
	calString += '<tr>';
	calString += '<td style="cursor: pointer; background-color: navy; color: white; font: 10pt sans-serif;cursor: hand; text-decoration:underline;" onClick="changeMonth2(-12,\'' + this.id + '\')">&lt;</td>';
	calString += '<td style="cursor: pointer; cursor: hand;background-color: navy; color: white; font: 10pt sans-serif;" onClick="changeMonth2(-1,\'' + this.id + '\')">&lt;</td>';
	calString += '<td style="background-color: navy; color: white; font: 10pt sans-serif;" colspan="3">&nbsp;</td>';
	calString += '<td style="cursor: pointer; cursor: hand;background-color: navy; color: white; font: 10pt sans-serif;" onClick="changeMonth2(1,\'' + this.id + '\')">&gt;</td>';
	calString += '<td style="cursor: pointer; cursor: hand;background-color: navy; color: white; font: 10pt sans-serif; text-decoration:underline;text-align:right;" onClick="changeMonth2(12,\'' + this.id + '\')">&gt;</td>';
	calString += '</tr>';
	
	calString += '</table>';
	calString += '</div>';
	return calString;
}

function getLength(){
	//thirty days has September...
	switch(this.month){
		case 1:
			if((this.dateObject.getFullYear()%4==0&&this.dateObject.getFullYear()%100!=0)||this.dateObject.getFullYear()%400==0)
				return 29; //leap year
			else
				return 28;
		case 3:
			return 30;
		case 5:
			return 30;
		case 8:
			return 30;
		case 10:
			return 30
		default:
			return 31;
	}
}
function changeDate(td,cal){
	//Some JavaScript trickery
	//Change the cal argument to the existing calendar object
	//This is why the first argument in the constructor must match the variable name
	//The cal reference also allows for multiple calendars on a page
	cal = eval(cal);	
	document.getElementById(cal.id + "selected").className = "days";
	document.getElementById(cal.id + "selected").id = "";
	td.className = "date";
	td.id = cal.id + "selected";
	//set the calendar object to the new date
	cal.dateObject.setDate(td.firstChild.nodeValue);
	cal = new calendar(cal.id,cal.dateObject,cal.pix);
	//here is where you could react to a date change - I'll just display the formatted date
	document.myform.DepartureDate.value = cal.getFormattedDate();
	P7_autoLayers('menucloser');
	
}

function changeDate2(td,cal){
	//Some JavaScript trickery
	//Change the cal argument to the existing calendar object
	//This is why the first argument in the constructor must match the variable name
	//The cal reference also allows for multiple calendars on a page
	cal = eval(cal);
	document.getElementById(cal.id + "selected").className = "days";
	document.getElementById(cal.id + "selected").id = "";
	td.className = "date";
	td.id = cal.id + "selected";
	//set the calendar object to the new date
	cal.dateObject.setDate(td.firstChild.nodeValue);
	cal = new calendar2(cal.id,cal.dateObject,cal.pix);
	//here is where you could react to a date change - I'll just display the formatted date
	document.myform.ArrivalDate.value = cal.getFormattedDate();
	P7_autoLayers('menucloser');
}

function changeMonth(mo,cal){
	
	//more trickery!
	cal = eval(cal);
	//The Date object is smart enough to know that it should roll over in December
	//when going forward and in January when going back
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar(cal.id,cal.dateObject,cal.pix);
	cal.formattedDate = cal.getFormattedDate();
	document.getElementById('calContainer').innerHTML = cal.write();
	
}

function changeMonth2(mo,cal){
	//more trickery!
	cal = eval(cal);
	//The Date object is smart enough to know that it should roll over in December
	//when going forward and in January when going back
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar2(cal.id,cal.dateObject,cal.pix);
	cal.formattedDate = cal.getFormattedDate();
	document.getElementById('calContainer2').innerHTML = cal.write();
	
}

function MM_findObj(n, d) {//v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function P7_autoLayers() { //v1.1 PVII
 var g,b,k,f,args=P7_autoLayers.arguments;
 if(!document.p7setc) {p7c=new Array();document.p7setc=true;}
 for(k=0; k<p7c.length; k++) {
  if((g=MM_findObj(p7c[k]))!=null) {
   b=(document.layers)?g:g.style;b.visibility="hidden";}}
 for(k=0; k<args.length; k++) {
  if((g=MM_findObj(args[k])) != null) {
   b=(document.layers)?g:g.style;b.visibility="visible";f=false;
   for(j=0;j<p7c.length;j++) {
    if(args[k]==p7c[j]) {f=true;}}
  if(!f) {p7c[p7c.length++]=args[k];}}}
}
	
