function Calendar () {
 this.instanceName = (arguments[0])?arguments[0]:"Calendar"
 this.daysArray = [31,28,31,30,31,30,31,31,30,31,30,31]
 this.daysArrayLY = [31,29,31,30,31,30,31,31,30,31,30,31]
 this.dayNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
 this.monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"]
 this.today = new Date()
 this.today.day = this.today.getDay()
 this.today.date = this.today.getDate()
 this.today.month = this.today.getMonth()
 this.today.monthAsString = this.monthNames[this.today.month]
 this.today.year = this.today.getFullYear()
 
 this.selDate = new Object()
 this.selDate.date = this.today.date
 this.selDate.month = this.today.month + 1
 this.selDate.jsMonth = this.today.month
 this.selDate.monthAsString = this.monthNames[this.selDate.jsMonth]
 this.selDate.year = this.today.year

 this.highlightDate = new Object()
 this.highlightDate.date = false
 this.highlightDate.month = false
 this.highlightDate.jsMonth = false
 this.highlightDate.monthAsString = false
 this.highlightDate.year = false

 
 this.retDate = new Object()
 this.retDate.date = false
 this.retDate.month = false
 this.retDate.jsMonth = false
 this.retDate.monthAsString = false
 this.retDate.year = false
 
 this.cellPrefs = new Object()
 this.cellPrefs.font = "arial, sans-serif, helvetica"
 this.boundFormField = false
 this.allowCalendarClick = true
 this.allowSelectBeforeToday = false
 this.monthsToView = 2

 this.minDate = new Date(2004,8)
 this.maxDate = new Date(2008,7)
 this.popupPrefs = new Object()
 this.popupPrefs.windowTitle = ""
 this.popupPrefs.popupWindow = false
}
Calendar.prototype.setSelectedDate = function (theDate, theMonth, theYear) {
	this.selDate.date = theDate;
	this.selDate.month = theMonth;
	this.selDate.jsMonth = theMonth - 1;
	this.selDate.monthAsString = this.monthNames[this.selDate.jsMonth];
	this.selDate.year = theYear;
	
	if (Date.parse(new Date(this.selDate.year, this.selDate.jsMonth)) > Date.parse(this.maxDate)) {
		this.prevMonth();
	}

}
Calendar.prototype.setHighlightDate = function (theDate, theMonth, theYear) {
	this.highlightDate.date = theDate;
	this.highlightDate.month = theMonth;
	this.highlightDate.jsMonth = theMonth - 1;
	this.highlightDate.monthAsString = this.monthNames[this.highlightDate.jsMonth];
	this.highlightDate.year = theYear;
	this.setSelectedDate(theDate, theMonth, theYear);
}
Calendar.prototype.calendarHTML = function () {
var mouseover ="";
var hand = "";
 var allowClick = this.allowCalendarClick
 var month = (arguments[0])?(arguments[0]-1):this.selDate.jsMonth
 var year = (arguments[1])?arguments[1]:this.selDate.year
 var startOfMonth = new Date(year,month,1)
 var startDayOfWeek = startOfMonth.getDay()
 var daysInMonth = (this.isLeapYear(year))?this.daysArrayLY[month]:this.daysArray[month]
 var code = ""
 var day = 1 - startDayOfWeek
 code += "<table border=0 cellpadding=0 cellspacing=0 ><tr><td height=17 width=164 align=\"center\" style='font-family:Arial;font-size:12px;font-weight:bold;background-color:#ffffff;' class='calMon'>"
 code += this.monthNames[month] + " " + year
 code += "<\/td><\/tr><tr><td height=120 width=168 valign='top' >"
 code += "<table border=0 cellpadding=0 cellspacing=1 bgcolor=#efefff>\n<tr>"
 font = this.cellPrefs.font
 styleBlock = "font-family:" + font + ";color:#FFFFFF;font-size:10px;font-weight:bold;"
 for (dayName in this.dayNames) { 
  	code += "<td  width='24' height='15' align=\"right\" valign=\"bottom\" class='qRow2'>" +  "<span style=\"" + styleBlock + "\">" +  this.dayNames[dayName] +   "</span></td>"
 }
 code += "<\/tr>"
 while (day<=daysInMonth) {
  code += "<tr>\n"
  for (deys=0; deys<7; deys++) {
   today = false
	 validDay = false
	 mouseover= "";
	 hand="";

   if (day == this.today.date && month == this.today.month && year == this.today.year && this.today.highlight == true) {
    today = true
    bgcolor = "#E6E6E6"
    textColor = "#000000"
    
   } else if (day == this.highlightDate.date && month == this.highlightDate.jsMonth && year == this.highlightDate.year) {
		allowClick = true
    bgcolor = "#2B1B62"
    textColor = "#FFFFFF"
	 } else if (day > 0 && day <= daysInMonth) {
	 	beforeToday = (Date.parse(new Date(year, month, day)) < Date.parse(this.today)) ? true : false;
    if (beforeToday && !this.allowSelectBeforeToday) {
			allowClick = false
	    bgcolor = "#F5F5F5"
	    textColor = "#CCCCCC"
		} else {
			allowClick = true
	    bgcolor = "#FFFFFF"
	    textColor = "#000000"
	    mouseover= "onmouseover= \"changeBg(this,'bgrad.gif');\" onmouseout=' changeBg(this,\"no\");' onClick=\"window." + this.instanceName + ".popupSelectDate(" + day + ", " + month + ", " + year + ")\"";
	    hand='style=\"cursor:pointer;\"';
		}
   } else { 
    bgcolor = "#eceeee"
    textColor = "#000000"
   } 
   styleBlock = "font-family:" + font + ";color:" + textColor + ";font-size:11px;font-weight:bold;"
   code += "<TD  width='24' height='15' align=\"right\" valign=\"bottom\" bgcolor=\"" + bgcolor + "\" " +mouseover+ "  "+hand+" >" +
   "<span style=\"" + styleBlock + "\">" +
   ((allowClick) ? ((day>0 && day<=daysInMonth)?("<A style=\"" + styleBlock + "text-decoration: none;\"  onClick=\"window." + this.instanceName + ".popupSelectDate(" + day + ", " + month + ", " + year + ")\">" + day + "<\/A>"):("&nbsp;")) : ((day>0 && day<=daysInMonth)?(day):("&nbsp;"))) + 
   "</span>" +
   "<\/TD>"
   day ++
  }
  code += "<\/tr>\n"
 }
 code += "<\/table>"
 code += "<\/td><\/tr><\/table>"
 return code
}
Calendar.prototype.isLeapYear = function (year) {
 return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
}
Calendar.prototype.retNext = function () {
 currentMonth = this.selDate.jsMonth
 RetObject = new Object()
 if (currentMonth + 1 > 11) {
  RetObject.month = 1
  RetObject.year = this.selDate.year + 1
 } else {
  RetObject.month = this.selDate.month + 1
	RetObject.year = this.selDate.year
 }
 return RetObject
}

Calendar.prototype.nextMonth = function () {
 currentMonth = this.selDate.jsMonth
 if (currentMonth + 1 > 11) {
  this.selDate.jsMonth = 0
  this.selDate.month = 1
  this.selDate.monthAsString = this.monthNames[this.selDate.jsMonth]
  this.selDate.year += 1
 } else {
  this.selDate.jsMonth += 1
  this.selDate.month += 1
  this.selDate.monthAsString = this.monthNames[this.selDate.jsMonth]
 }
}
Calendar.prototype.prevMonth = function () {
 currentMonth = this.selDate.jsMonth
 if (currentMonth - 1 < 0) {
  this.selDate.jsMonth = 11
  this.selDate.month = 12
  this.selDate.monthAsString = this.monthNames[this.selDate.jsMonth]
  this.selDate.year -= 1
 } else {
  this.selDate.jsMonth -= 1
  this.selDate.month -= 1
  this.selDate.monthAsString = this.monthNames[this.selDate.jsMonth]
 }
} 
Calendar.prototype.openDateChooser = function (formField) {

 this.boundFormField = formField

 var winHTML = this.popupHTML();
 /*
 this.popupPrefs.popupWindow = window.open("blank.html",this.instanceName,"width=445,height=270,innerHeight=270,scrollbars=0,resizable=0")
  
 this.popupPrefs.popupWindow.innerHeight = 270;
 this.popupPrefs.popupWindow.focus()
 with (this.popupPrefs.popupWindow.document) {
  write(winHTML)
  writeln("")
  close()
 }
 */
 	
	var calDHTMLDiv = document.getElementById('calDHTMLDiv');
 	calDHTMLDiv.innerHTML = winHTML;
 	calDHTMLDiv.style.display = '';
}
Calendar.prototype.popupHTML = function () {
  var windowTitle = this.popupPrefs.windowTitle
 
  var popupCode ="<div style='height: 153px; z-index:1000; width: 340px; position:relative; left: 0px; top: 0px;text-align:left;' >\n"
  
  popupCode += "<table cellpadding=0 cellspacing=0 align=\"center\" bgcolor='#FFFFFF' border='0'>\n"
  popupCode += "<TR><TD width=50% height='15' align=\"left\">&nbsp;"
	if (Date.parse(new Date(this.selDate.year, this.selDate.jsMonth)) > Date.parse(this.minDate)) {
  popupCode += "<A href=\"\" onClick=\"window." + this.instanceName + ".popupPrevMonth(); return false;\" style='font-size: 11px;font-weight:bold;color:#000000;text-align:center;letter-spacing:0px;font-family:Arial;text-decoration: none;'>&lt; prev month<\/A>"
	} else {
	popupCode += "<A>&nbsp;<\/A>"
	}
  popupCode += "<\/TD>"
  popupCode += "<TD width=50% height='15' align=\"right\">"
	if (Date.parse(new Date(this.selDate.year, this.selDate.jsMonth)) < Date.parse(this.maxDate)) {
  popupCode += "<A  href=\"\" onClick=\"window." + this.instanceName + ".popupNextMonth(); return false;\" style='font-size: 11px;font-weight:bold;color:#000000;text-align:center;letter-spacing:0px;font-family:Arial;text-decoration: none;'>next month &gt;<\/A>"
	} else {
	popupCode += "<A>&nbsp;<\/A>"
	}
  popupCode += "&nbsp;<\/TD><\/TR>\n"
  popupCode += "<TR><TD colspan=2 valign=\"top\" height=125><TABLE border=0 cellpadding=0 cellspacing=0 width=\"100%\">\n"
	popupCode += "<TR><TD  width=\"168\" height=\"120\" valign='top' align=\"right\" style=\"border:0px;vertical-align:text-top;\">"
  popupCode += this.calendarHTML(this.selDate.month);
	popupCode += "<\/TD><td width=4 valign='top'>&nbsp;</td>"
	popupCode += "<TD width=\"168\" height=\"120\" valign='top' align=\"left\" style=\"border:0px;vertical-align:text-top;\">"
  popupCode += this.calendarHTML(this.retNext().month,this.retNext().year)
	popupCode += "<\/TD><\/TR>\n"
  popupCode += "<\/TABLE><\/TD><\/TR>\n"
  popupCode += "<\/table>\n"
  popupCode += "<\/DIV>\n"

 return popupCode
}
Calendar.prototype.popupNextMonth = function () {
 this.nextMonth()
 /*
 with (this.popupPrefs.popupWindow.document) {
  write (this.popupHTML())
  writeln("")
  close ()
 }
 */
 	var calDHTMLDiv = document.getElementById('calDHTMLDiv');
 	calDHTMLDiv.innerHTML = this.popupHTML();
 	calDHTMLDiv.style.display = '';

}
Calendar.prototype.popupPrevMonth = function () {
 this.prevMonth()
 /*
 with (this.popupPrefs.popupWindow.document) {
  write (this.popupHTML())
  writeln("")
  close ()
 }
 */
 	var calDHTMLDiv = document.getElementById('calDHTMLDiv');
 	calDHTMLDiv.innerHTML = this.popupHTML();
 	calDHTMLDiv.style.display = '';


}
Calendar.prototype.popupSelectDate = function (date, month, year) {
 this.retDate.date = date
 this.retDate.jsMonth = month
 this.retDate.month = month + 1
 this.retDate.year = year
 //this.popupPrefs.popupWindow.close ()
 	var calDHTMLDiv = document.getElementById('calDHTMLDiv');
 	calDHTMLDiv.innerHTML = "";
 	calDHTMLDiv.style.display = 'none';
 	document.getElementById('calDiv').style.display='none';
 	document.getElementById('calParentDiv').style.display='none';
 	setTheDate(this.boundFormField)
}
Calendar.prototype.returnDateAsDouble = function () {
 return "" + ((this.retDate.date < 10)?"0":"") + this.retDate.date
}
Calendar.prototype.returnMonthAsDouble = function () {
 return "" + ((this.retDate.month < 10)?"0":"") + this.retDate.month
}
Calendar.prototype.returnMonthAsShortName = function () {
	return "" + this.monthNames[this.retDate.jsMonth].substr(0,3)
}
dateCalendar = new Calendar("dateCalendar");

function setTheDate(strBoundFormField) {

	var me_form = document.frmStep1
	var me_dateField = me_form[strBoundFormField + 'date'];
	//var me_monthField = me_form[strBoundFormField + 'month'];
	//var me_yearField = me_form[strBoundFormField + 'year'];
	
	var me_selDate = dateCalendar.retDate.date
	var me_selMonth = dateCalendar.retDate.month
	var me_selYear = dateCalendar.retDate.year
	
	me_dateField.value = me_selDate + '/' + me_selMonth + '/' + me_selYear;
	
	if (strBoundFormField == 'depart_') {
		try{
			window.departDateChange();
		}catch(e){
			window.main.departDateChange();

		}
	} else {
		try{
			window.returnDateChange();
		}catch(e){
			window.main.returnDateChange();

		}
	}
	
	/*
	for (var it_optNo = 0; it_optNo < me_dateField.length; it_optNo ++) {
		if (parseInt(me_dateField[it_optNo].value) == me_selDate) me_dateField.selectedIndex = it_optNo
	}
	for (var it_optNo = 0; it_optNo < me_monthField.length; it_optNo ++) {
		if (parseInt(me_monthField[it_optNo].value) == me_selMonth) me_monthField.selectedIndex = it_optNo
	}
	for (var it_optNo = 0; it_optNo < me_yearField.length; it_optNo ++) {
		if (parseInt(me_yearField[it_optNo].value) == me_selYear) me_yearField.selectedIndex = it_optNo
	}
	*/
}
