var isNav=false;
if(parseInt(navigator.appVersion) >= 4)
{
	if(navigator.appName == "Netscape")
	{
		isNav=true;
	}
}
else
{
	isNav=true;
}

var today = new Date(new Date().valueOf());

var dateBoxPieceBlank = "    <td width=\"22\" height=\"18\" align=\"center\" valign=\"middle\" bgcolor=\"#FFFFFF\" class=\"calendar\">&nbsp;</td>";

var dateBoxPiece = "    <td class=\"calendar\" width=\"22\" height=\"18\" align=\"center\" valign=\"middle\" onMouseOver=\"this.style.background = '#FFFF93';\" onMouseOut=\"this.style.background = '##BGCOLOR##';\" onClick=\"setDateBoxes('##DATESET##',##MONTH##,##DAY##,##YEAR##)\" bgcolor=\"##BGCOLOR##\">##DAY##</td>";

var baseDateBoxHeader = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"158\">\n";baseDateBoxHeader += "  <tr>\n";baseDateBoxHeader += "    <td colspan=\"7\" align=\"center\">\n";baseDateBoxHeader += "      <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"158\">\n";baseDateBoxHeader += "        <tr>\n";baseDateBoxHeader += "          <td width=\"33%\" class=\"calendar\"><div name=\"back\" style=\"width: 15; border: 1 solid buttonface; border: 2 outset buttonhighlight; background-color: menu;\" align=\"center\" onMouseDown=\"this.style.border = '2 inset buttonhighlight'\" onMouseUp=\"this.style.border = '2 outset buttonhighlight'\" onClick=\"previousMonth('##DATESET##')\">&lt;</div></td>\n";baseDateBoxHeader += "          <td width=\"33%\" class=\"calendar\" nowrap><font face=\"Arial\" size=\"2\" color=\"#B92333\"><b>##MONTHNAME## ##YEAR##</b></font></td>\n";
baseDateBoxHeader += "          <td width=\"33%\" class=\"calendar\" align=\"right\"><div name=\"next\" style=\"width: 15; border: 1 solid buttonface; border: 2 outset buttonhighlight; background-color: menu;\" align=\"center\" onMouseDown=\"this.style.border = '2 inset buttonhighlight'\" onMouseUp=\"this.style.border = '2 outset buttonhighlight'\" onClick=\"nextMonth('##DATESET##')\">&gt;</div></td>\n";baseDateBoxHeader += "        </tr>\n";baseDateBoxHeader += "      </table>\n";baseDateBoxHeader += "    </td>\n";baseDateBoxHeader += "  </tr>\n";baseDateBoxHeader += "  <tr>\n";
baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">S</font></td>\n";baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">M</font></td>\n";baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">T</font></td>\n";baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">W</font></td>\n";baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">T</font></td>\n";baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">F</font></td>\n";
baseDateBoxHeader += "    <td class=\"calendar\" bgcolor=\"#B92333\" align=\"center\"><font color=\"#FFFFFF\">S</font></td>\n";baseDateBoxHeader += "  </tr>";

var dateBoxFooter = "  <tr>\n";dateBoxFooter += "    <td class=\"calendar\" colspan=\"7\" align=\"center\"><div name=\"close_btn\" style=\"width: 50; height: 15; border: 1 solid buttonface; border: 2 outset buttonhighlight; background-color: menu;\" align=\"center\" onMouseDown=\"this.style.border = '2 inset buttonhighlight'\" onMouseUp=\"this.style.border = '2 outset buttonhighlight'\" onClick=\"hideCalendar()\">Close</div></td>\n";dateBoxFooter += "  </tr>\n</table>\n";

var calBuffer="";

var caldays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

function isLeapYear(yr)
{
	if (((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function GetDOW2(d,m,y)
{
	var dt=new Date(y,m-1,d);
	return(dt.getDay())%7;
}

var selectBoxFlag=false;
var maxYear=0;
var minYear=0;

function displayCalendar(hideSelects,inDateSet)
{
	selectBoxFlag=hideSelects;
	if(isNav)  
	{
		var calWinHndl = 
		window.open("http://www.lodging.com/auto/popup_calendar.cgi","calendar","scrollbars=yes,resizable=yes,width=500,height=275");    
		calWinHndl.focus(); 
	}
	else
	{
  	theForm = document.searchform;
		var inYear = theForm.ciy.value;
		var mon = parseInt(theForm.cim.options[theForm.cim.selectedIndex].value,10);
		var day = parseInt(theForm.cid.options[theForm.cid.selectedIndex].value,10);
		var year = parseInt(theForm.ciy.value,10);
		currentMonth = mon;
		currentYear = year;
		currentDay = day; 
		maxYear = inYear+1; //parseInt(theForm.inyear.options[(theForm.inyear.options.length-1)].value,10);
		minYear = inYear; //parseInt(theForm.inyear.options[0].value,10);
		drawCalendarBody(inDateSet,mon,day,year);
		document.all.CAL.style.left = (event.x - event.offsetX + document.body.scrollLeft);
		document.all.CAL.style.top = (event.y - event.offsetY + document.body.scrollTop);
		document.all.CAL.style.visibility = "visible";
		if(hideSelects)
		{
			if (document.all.searchform.formtype.value == "centerbox")
			{
				if(document.all.searchform.non) 
				{
					document.all.searchform.non.style.visibility = "hidden";
				} 
			}
			else if (document.all.searchform.formtype.value == "leftbox")
			{
				if(document.all.searchform.r1) 
				{
					document.all.searchform.r1.style.visibility = "hidden";
				} 
				if(document.all.searchform.r2) 
				{
					document.all.searchform.r2.style.visibility = "hidden";
				} 
				if(document.all.searchform.r3) 
				{
					document.all.searchform.r3.style.visibility = "hidden";
				}
			}
		}
	}
}

function hideCalendar(hideSelects)
{
	document.all.CAL.style.visibility = "hidden"; 
	if(hideSelects || selectBoxFlag)
	{   
		if (document.all.searchform.formtype.value == "centerbox")
		{
			if(document.all.searchform.non) 
			{
				document.all.searchform.non.style.visibility = "visible";
			} 
		}
		else if (document.all.searchform.formtype.value == "leftbox")
		{
			if(document.all.searchform.r1) 
			{
				document.all.searchform.r1.style.visibility = "visible";
			} 
			if(document.all.searchform.r2) 
			{
				document.all.searchform.r2.style.visibility = "visible";
			} 
			if(document.all.searchform.r3) 
			{
				document.all.searchform.r3.style.visibility = "visible";
			} 
		}
	}
}

function nextMonth(inDateSet)
{
	currentMonth += 1;
	if(currentMonth > 12 && currentYear + 1 <= maxYear)
	{
		currentMonth = 1;
		currentYear++;
	}
	if(currentMonth > 12)
	{
		currentMonth = 12;
	}
	if(currentMonth == (today.getMonth()+1) && currentYear == today.getYear())
	{
		drawCalendarBody(inDateSet,currentMonth,currentDay,currentYear);
	}
	else
	{
		drawCalendarBody(inDateSet,currentMonth,0,currentYear);
	}
}

function previousMonth(inDateSet)
{
	if((currentMonth - 1) < (today.getMonth()+1) && currentYear <= today.getYear())
	{
	}
	else
	{
		currentMonth -= 1;
		if(currentMonth < 1 && currentYear - 1 >= minYear)
		{
			currentMonth = 12;
			currentYear -= 1;
		}
   		if(currentMonth == (today.getMonth()+1) && currentYear == today.getYear())
		{
			drawCalendarBody(inDateSet,currentMonth,currentDay,currentYear);
		}
		else
		{
			drawCalendarBody(inDateSet,currentMonth,0,currentYear);
		}
	}
}

var currentMonth=1;
//var currentYear=y2k(today.getYear());
var currentDay=1;
function drawCalendarBody(inDateSet,mon,day,year)
{
	calObj = document.all.CAL_BODY;
	theForm = document.searchform;
	var dowStartPos = GetDOW2(1,mon,year);
	var finalDay = caldays[(mon-1)];
	if(isLeapYear(year) && mon == 2)
	{
		finalDay += 1;
	}
	re_MonthName = /##MONTHNAME##/g;
	re_Year = /##YEAR##/g;
	re_DateSet = /##DATESET##/g;
	dateBoxHeader = baseDateBoxHeader;
	dateBoxHeader = dateBoxHeader.replace(re_MonthName,months[(mon-1)]);
	dateBoxHeader = dateBoxHeader.replace(re_Year,""+year);
	dateBoxHeader = dateBoxHeader.replace(re_DateSet,""+inDateSet);
	calBuffer = generateGrid(inDateSet,dowStartPos,finalDay, mon,day,year);
	document.all.CAL_BODY.innerHTML = dateBoxHeader + calBuffer + dateBoxFooter;
}

function generateGrid(inDateSet,startPos,endPos, m,d,y)
{
	var currentDay=1;
	var gridCount=0;
	var doBlanks=true;
	var tmpBuffer="";
	re_Month = /##MONTH##/g;
	re_Day = /##DAY##/g;
	re_Year = /##YEAR##/g;
	re_BGColor = /##BGCOLOR##/g;
	re_DateSet = /##DATESET##/g;
	for(var i=0;i<6;i++)
	{
		tmpBuffer += "<tr>\n";
		for(var j=0;j<7;j++)
		{
			if(gridCount < startPos || currentDay > endPos)
			{
				tmpBuffer += dateBoxPieceBlank + "\n";
			}
			else
			{
				pasrsedDBP = dateBoxPiece;
				pasrsedDBP = pasrsedDBP.replace(re_Month,""+m);
				pasrsedDBP = pasrsedDBP.replace(re_Day,""+currentDay);
				pasrsedDBP = pasrsedDBP.replace(re_Year,""+y);
				pasrsedDBP = pasrsedDBP.replace(re_DateSet,""+inDateSet);
				if(currentDay == d)
				{
					pasrsedDBP = pasrsedDBP.replace(re_BGColor,"#BFC1FF");
				}
				else
				{
					pasrsedDBP = pasrsedDBP.replace(re_BGColor,"#FFFFFF");
				}
				tmpBuffer += pasrsedDBP + "\n";
				currentDay++;
			}
			gridCount++;
		}
		tmpBuffer += "</tr>\n";
	}
	return tmpBuffer;
}

function setDateBoxes(whichDateSet,m,d,y)
{
	theForm = document.searchform;
	if (whichDateSet == "ci")
	{
		eval("monthObj = theForm.cim;");
		eval("dayObj = theForm.cid;");
		eval("yearObj = theForm.ciy;");
	}
	else
	{
		eval("monthObj = theForm.com;");
		eval("dayObj = theForm.cod;");
		eval("yearObj = theForm.coy;");
	}
	var yearOffset = parseInt(theForm.ciy.value,10);
	monthObj.options[(m-1)].selected = true;
	dayObj.options[(d-1)].selected = true;
	yearObj.value = y;

	if (whichDateSet == "ci")
		changeDates1();
	else
		changeDates2();		
	hideCalendar();
}

function changeDates1()
{
	/*theForm = document.searchform;

	// check if cin is not past
	nowTime = new Date();
	now = new Date(
		nowTime.getFullYear(),
		nowTime.getMonth(),
		nowTime.getDate()
	);
	ciDate = new Date(theForm.ciy.options[theForm.ciy.selectedIndex].value, 
		parseInt(theForm.cim.options[theForm.cim.selectedIndex].value)-1, 
		parseInt(theForm.cid.options[theForm.cid.selectedIndex].value));
	timePast = (ciDate - now) / (3600 * 1000 * 24);*/

	/*if (timePast < 3)
	{
		alert("You can select arrival date at least 3 day prior to check-in date.");
		oldCiDate = new Date(theForm.coy.options[theForm.coy.selectedIndex].value, 
			parseInt(theForm.com.options[theForm.com.selectedIndex].value)-1, 
			parseInt(theForm.cod.options[theForm.cod.selectedIndex].value)
			 - parseInt(theForm.non.options[theForm.non.selectedIndex].value));

		theForm.cid.options[oldCiDate.getDate()-1].selected = true;
		theForm.cim.options[oldCiDate.getMonth()].selected = true;
		theForm.ciy.options[oldCiDate.getYear()-2005].selected = true;		
	}*/

	/*coDate = new Date(theForm.ciy.options[theForm.ciy.selectedIndex].value, 
		parseInt(theForm.cim.options[theForm.cim.selectedIndex].value)-1, 
		parseInt(theForm.cid.options[theForm.cid.selectedIndex].value)
			+ parseInt(theForm.non.options[theForm.non.selectedIndex].value));

	theForm.cod.options[coDate.getDate()-1].selected = true;
	theForm.com.options[coDate.getMonth()].selected = true;
	theForm.coy.options[coDate.getYear()-2004].selected = true;*/
}

function changeDates2()
{
	/*theForm = document.searchform;
	cinTime = new Date(theForm.ciy.options[theForm.ciy.selectedIndex].value, 
		parseInt(theForm.cim.options[theForm.cim.selectedIndex].value)-1, 
		theForm.cid.options[theForm.cid.selectedIndex].value
		);
	conTime = new Date(theForm.coy.options[theForm.coy.selectedIndex].value, 
		parseInt(theForm.com.options[theForm.com.selectedIndex].value)-1, 
		theForm.cod.options[theForm.cod.selectedIndex].value
		);
	non = (conTime - cinTime) / (3600 * 1000 * 24);

	if (non > 30)
	{
		alert("Number of nights can not be more than 30 days.");
		theForm.non.options[29].selected = true;
		changeDates1();
	}	
	else if (non <= 0)
	{
		alert("Leaving date can not be the same as or before than the arrival date.");
		theForm.non.options[0].selected = true;
		changeDates1();
	}
	else if (non > 0)
		theForm.non.options[non - 1].selected = true;*/
}
