/**********************************************************************
	These routines control the arrival & departure dates for 
	RoomType1.asp and Rooms1.asp
***********************************************************************/
var strTmp = "";
//NOTE:  For some reason changing the Nights setting doesn't change
//       this number until the web browser or server is stopped & started.
var iNights = parseInt(2);

var iArrFrToday = parseInt(0);
//alert ("ArrFrToday " + iArrFrToday);

var msPerDay = 24 * 60 * 60 * 1000;			//MilliSeconds per day
var DaysInMonth = new Array();	//Used To check the arrival date entry
		DaysInMonth[1] = 31;		//Jan
		DaysInMonth[2] = 28;		//Feb - Will dynamically change this based on the year entered
		DaysInMonth[3] = 31;		//Mar
		DaysInMonth[4] = 30;		//Apr
		DaysInMonth[5] = 31;		//May
		DaysInMonth[6] = 30;		//Jun
		DaysInMonth[7] = 31;		//Jul
		DaysInMonth[8] = 31;		//Aug
		DaysInMonth[9] = 30;		//Sep
		DaysInMonth[10] = 31;		//Oct
		DaysInMonth[11] = 30;		//Nov
		DaysInMonth[12] = 31;		//Dec



var SystemDate = new Date();
var SystemDay = SystemDate.getDate();
var SystemMonth = SystemDate.getMonth();
var SystemYear = SystemDate.getFullYear();
//alert (parseInt(SystemDay));
//alert (parseInt(SystemMonth));
//alert (parseInt(SystemYear));
SystemDate = new Date(SystemYear, SystemMonth, SystemDay);

var dtArrival = new Date();
//dtArrival.setFullYear(2009);
var dtDeparture = new Date();
dtDeparture.setDate(dtDeparture.getDate() + 2);
//dtDeparture.setFullYear(2008);

//alert("Arrival: " + dtArrival + "  Departure: " + dtDeparture);

var currentDate = new Date()
var todaysDate = currentDate.getDate();
var todaysYear = currentDate.getFullYear();
var todaysMonth = currentDate.getMonth();


var strArrDate = (todaysMonth + 1) + "/" + todaysDate + "/" + todaysYear;
//var strDepDate = init();
var strDepDate = "";
//
//var strArrDate = "1/6/2009";
//var strDepDate = "1/8/2009";

//________________________________________________________________________

function isLeapYear(yrVal)
{
	var leapYear=false;
	var year = parseInt(yrVal, 10);

	if (year%4 == 0)
		{
		leapYear=true;
		}
	return leapYear;
}

function getDaysInMonth(monthVal, YrVal)
{
	var maxDays=31
	if (monthVal==1)
	{
		if (isLeapYear(YrVal))
			{
			maxDays=29;
			}
		else
			{
			maxDays=28;
			}
	}
	if (monthVal==3 || monthVal==5 || monthVal==8 || monthVal==10)
		{
		maxDays=30;
		}
	return maxDays;
}

function init() {
	var advancedDay = 2;
	var dt = new Date();

	var yr = dt.getFullYear();
	var curYr = dt.getFullYear();
	var mo = dt.getMonth();
	var da = dt.getDate();


	var daysInCurrent = getDaysInMonth(mo, yr);

	if (da + advancedDay > daysInCurrent)
	{	da = ((da+advancedDay) % daysInCurrent);
		if (mo == 11)
		{	mo = 0;
			yr++;	}
		else
		{	mo++;	}	}
	else
	{	
	da = da + advancedDay;
	//alert(advancedDay);
	//alert(mo + " " + da + " " + yr)
		}
	//da--;
	//yr-=curYr;

	var departureDate = (mo + 1) + "/" + da + "/" + yr;
	//alert(departureDate);
	return departureDate; 
	}




//________________________________________________________________________

function SetDefault() {
	SetArrival();
	SetDeparture();
	CalcNights();
}

//________________________________________________________________________

function SetArrival() {
	with (document.frmMain) {

		ArrMonth.options[dtArrival.getMonth()].selected = true;
		ArrDay.options[dtArrival.getDate()-1].selected = true;
		//alert ("SetArrival " + dtArrival.getMonth());

		// NOTE: the .getYear method returns (Year - 1900), so 2000 is 
		//       returned as "100" in IE.  Netscape returns the year 
		//       2000 as "2000".

		var YearIndex = dtArrival.getFullYear();
		//alert ("1-Year " + YearIndex);
		//alert ("Session Year " + 2009);
		//alert ("Year-System " + 2009);

		if (YearIndex >= 2009) {
			YearIndex -= 2009;			//Netscape
		}  else {
			YearIndex -= 100;																		//IE
		};
		
		ArrYear.options[YearIndex].selected = true;
		ArrDOW = DayOfWeek(dtArrival);
		ArrDayOfWeek.value = ArrDOW;
	} //End With
}

//________________________________________________________________________

function SetDeparture() {
	with (document.frmMain) {

		DepMonth.selectedIndex = dtDeparture.getMonth();
		//alert ("SetDeparture " + dtDeparture.getMonth());

		DepDay.options[dtDeparture.getDate()-1].selected = true;

		// NOTE: the .getYear method returns (Year - 1900), so 2000 is 
		//       returned as "100" in IE.  Netscape returns the year 
		//       2000 as "2000".

		var YearIndex = dtDeparture.getFullYear();
		//alert ("tmp Yr in SetDeparture " + tmpYear);
		//alert ("Year-System " + 2009);

		if (YearIndex >= 2009) {
			YearIndex -= 2009;			//Netscape
		}  else {
			YearIndex -= 100;																		//IE
		};

		//alert ("1 - tmp Yr in SetDeparture " + YearIndex);

		DepYear.options[YearIndex].selected = true;
		DepDOW = DayOfWeek(dtDeparture);
		DepDayOfWeek.value = DepDOW;
	} //End With
}

//________________________________________________________________________

function CalcNights() {
	var CurrentIndex;

	with (document.frmMain) {

		CurrentIndex = ArrYear.selectedIndex;
		iArrYear = ArrYear.options[CurrentIndex].text
		//alert ("Index: |" + ArrYear.selectedIndex + "|  Year: |" + ArrYear.options[ArrYear.selectedIndex].text + "|");

		iArrMonth = ArrMonth.selectedIndex + 1;
		CurrentIndex = ArrDay.selectedIndex;
		iArrDay = ArrDay.options[CurrentIndex].text
		dtArrival = new Date(iArrYear,iArrMonth-1,iArrDay);
		ArrDOW = DayOfWeek(dtArrival);
		ArrDayOfWeek.value = ArrDOW;
		strArrDate = iArrMonth + "/" + iArrDay + "/" + iArrYear;
		//alert("CalcNights: Arrival Date " + dtArrival);

		//alert ("Calnights-tmpYear: " + tmpYear);
		CurrentIndex = DepYear.selectedIndex;
		iDepYear = DepYear.options[CurrentIndex].text

		//alert ("DepYear-1: " + iDepYear + " | CurrentIndex: " + CurrentIndex)
		iDepMonth = DepMonth.selectedIndex + 1;
		CurrentIndex = DepDay.selectedIndex;
		iDepDay = DepDay.options[CurrentIndex].text

		//Set Departure Date
		//alert ("DepYear-2: " + iDepYear)
		dtDeparture = new Date(iDepYear,iDepMonth-1,iDepDay);
		DepDOW = DayOfWeek(dtDeparture);
		DepDayOfWeek.value = DepDOW;
	
		strDepDate = iDepMonth + "/" + iDepDay + "/" + iDepYear;
		//alert("CalcNights: Departure Date " + dtDeparture + " | "+strDepDate);
	
		//Calculate Number of Nights
		iNights = (dtDeparture.getTime() - dtArrival.getTime()) / msPerDay;
		iNights = Math.round(iNights);

		
		//Set Nights Drop Down Box
		//alert("CalcNights: Nights " + iNights + "  Length " + document.frmMain.Nights.length);
		if (iNights > 10){
			Nights.options[document.frmMain.Nights.length - 1].selected = true;
			return 0;
		}
		

		//alert("CalNights - Min Nights: " + 1);
		if (iNights < 1){
      iNights = 1;
			//Nights.options[Nights.length - 1].selected = true;
			//return 0;
		}

		//alert("CalNights - iNights: " + iNights);
		Nights.options[iNights-1].selected = true;
		return iNights;
	} //End With
}

//________________________________________________________________________

function CalcDeparture() {
	/*
	This routine is the first one called if any of the Arrival 
	fields are changed.  It is also call if any of the Departure 
	fields are changed.

	This section formats the arrival date and checks to make sure 
	the date is valid.  If not it changes it to the last valid day 
	of the month.  
	*/

	tmpYear = dtArrival.getFullYear();

	with (document.frmMain) {
		CurrentIndex = ArrYear.selectedIndex;
		iArrYear = ArrYear.options[CurrentIndex].text

		iArrMonth = ArrMonth.selectedIndex + 1;
 
		CurrentIndex = ArrDay.selectedIndex;
		iArrDay = ArrDay.options[CurrentIndex].text

		//alert ("CalcDep: ArrivalMon " + iArrMonth);
		dtArrival = new Date(iArrYear,iArrMonth-1,iArrDay);

	//alert ("dtArrival " + dtArrival);
	//alert ("System Date " + SystemDate);
    if (dtArrival < SystemDate) {
		//alert("here");
      ArrYear.selectedIndex = ArrYear.selectedIndex + 1;
      CurrentIndex = ArrYear.selectedIndex;
      iArrYear = ArrYear.options[CurrentIndex].text;
      dtArrival = new Date(iArrYear,iArrMonth-1,iArrDay);
    };
    


		//Adjust the Days in February based on the year
		if (iArrYear % 4 == 0) {
			DaysInMonth[2] = 29;		//Leap Year
			}
		else {
			DaysInMonth[2] = 28;		//Non-Leap Year
		};

		//alert ("CalcDep: ArrDay " + iArrDay);
		//alert ("CalcDep: ArrMonth " + iArrMonth);
		//alert ("CalcDep: Days in Month " + DaysInMonth[iArrMonth]);
		if (iArrDay > DaysInMonth[iArrMonth]) {
			iArrDay = DaysInMonth[iArrMonth];
			dtArrival = new Date(iArrYear,iArrMonth-1,iArrDay);
			SetArrival();
			//alert ("CalcDep: ArrDay Changed " + iArrDay);
		};
		//alert ("CalcDep: Arrival " + dtArrival);

		/******************************************************************/

		if (Nights.selectedIndex == (Nights.length)-1) {
			return;
		};

		ArrDOW = DayOfWeek(dtArrival);
		ArrDayOfWeek.value = ArrDOW;
	
		strArrDate = iArrMonth + "/" + iArrDay + "/" + iArrYear;

		iNights = (Nights.selectedIndex) + 1;
		//alert("CalcDep: Nights " + iNights + "  Night Index " + Nights.selectedIndex);
	
		/******************************************************************/
		/*
		Calculate new departure date based on arrival + # Nights
		NOTE: Use .getTime to get the number of milliseconds since 1/1/1970 
		for the Arrival date and then add the number of nights in milliseconds
		and then use .setTime to set the departure date.

		I had to set the hours on the arrival date otherwise under certain 
		circumstances the departure date would get calculated 1 day less.  
		For example if the Arrival date is 10/23/97 and you then set the 
		number of nights to 7, the Departure date would get calculated as 
		10/29/97.
		*/

		dtArrival.setHours(22);
		dtArrival.setMinutes(0);
		dtArrival.setSeconds(0);

		dtDeparture.setTime(dtArrival.getTime() + (iNights * msPerDay));
		//alert("CalcDep: Departure " + dtDeparture);

		SetDeparture();			//Sets the departure dates fields to new date
	
		/******************************************************************/
		//Update String date with new departure date
		CurrentIndex = DepYear.selectedIndex;
		iDepYear = DepYear.options[CurrentIndex].text
	
		//alert ("CalcDep - Dep Year " + iDepYear);
		iDepMonth = DepMonth.selectedIndex + 1;
		CurrentIndex = DepDay.selectedIndex;
		iDepDay = DepDay.options[CurrentIndex].text

		DepDOW = DepDayOfWeek.value;
		strDepDate = iDepMonth + "/" + iDepDay + "/" + iDepYear;

		//Clear Room Type Display on the bottom
		
	} //End With
}
//________________________________________________________________________

function HideBottom() {
	//Clear Room Type Display on the bottom
  //document.all.AvailSection.style.display = "none";
}
//________________________________________________________________________

function DayOfWeek(dtTemp) {
	iDOW = dtTemp.getDay();
	if (iDOW == 0) strDOW="Sun";
	if (iDOW == 1) strDOW="Mon";
	if (iDOW == 2) strDOW="Tue";
	if (iDOW == 3) strDOW="Wed";
	if (iDOW == 4) strDOW="Thu";
	if (iDOW == 5) strDOW="Fri";
	if (iDOW == 6) strDOW="Sat";
	//alert("DayOfWeek: Day " + dtTemp.getDay() + " | " + strDOW);
	return strDOW;
}

//________________________________________________________________________

function CheckDates()	{
	var bPriorDate = false;
	
	with (document.frmMain) {
		//If nights is set to "Not Allowed"
		if (Nights.selectedIndex == Nights.length-1) {
			alert ("Dates are not valid.  Please set dates or number of nights.");
			ArrMonth.focus();
			return false;
		};

		//Check for arrival date in the past - This is the Server Date set in Startup.asp
		var today = new Date();
		if (dtArrival.getYear() < today.getYear()) {bPriorDate = true;};

		if (dtArrival.getYear() == today.getYear()) {
			if (dtArrival.getMonth() < today.getMonth()) {
				bPriorDate = true;
			};

			if (dtArrival.getMonth() == today.getMonth()) {
				if (dtArrival.getDate() < today.getDate()) {
					bPriorDate = true;
				};
			};
		};

		if (bPriorDate) {
			alert ("Reservations can not be made for past dates.");
			ArrMonth.focus();
			return false;
		};
	
		//Test for Arrival Before Future Default
		if (iArrFrToday != 0) {
			var dtMinArrival = today;
			dtMinArrival.setDate(dtMinArrival.getDate(dtMinArrival) + iArrFrToday);
			//Since getTime is used below to compare, make sure the time on both date variables are the same.
			dtMinArrival.setHours(dtArrival.getHours());
			dtMinArrival.setMinutes(dtArrival.getMinutes());
			dtMinArrival.setSeconds(dtArrival.getSeconds());

			//alert("Arrival " + dtArrival + " | MinArrival " + dtMinArrival + " | ");
			//alert("Arrival-GetTime " + dtArrival.getTime() + " | MinArrival " + dtMinArrival.getTime() + " | ");
			if (dtArrival.getTime() < dtMinArrival.getTime()) {
				alert ("Internet reservations that arrive within " + iArrFrToday + "  days of today are not allowed.  Please call 800-826-8272 to book your reservation.");
				ArrMonth.focus();
				return false;
			};
		};
		
		//Test for Arrival After Last Arrival Date
		//var dtLastArrival = new Date(2009,9,31);
		//var strLastArrival = "10/31/2009";
		//alert ("Arrival: " + dtArrival)
		//alert ("LastArr: " + dtLastArrival)
		//if (dtArrival > dtLastArrival) {
		//	alert ("Reservations can not be made after " + strLastArrival +" over the Internet at this time.")
		//	ArrMonth.focus();
		//	return false;
		//};

		//Test for Minimum Nights
		//alert("CheckDates - Min Nights: " + 1);
		iCurNights = parseInt(Nights.selectedIndex) + parseInt(1);

		
			iCurMin = parseInt(1);
			iCurMax = parseInt(10);
		
	
		if (iCurNights < iCurMin) {
			alert ("A stay of " + iCurNights + " is less than the minimum number nights ("+iCurMin+").");
			ArrMonth.focus();
			return false;
		};
	
		if (iCurNights > iCurMax) {
			alert ("A stay of " + iCurNights + " is greater than the maximum number nights ("+iCurMax+").");
			ArrMonth.focus();
			return false;
		};
		
		//alert("Submit |" + dtArrival + "|" + strArrDate + "|" + dtDeparture + "|" + strDepDate + "|");
		
		//String dates: mm/dd/yyyy
		PassArr.value = strArrDate;
		PassDep.value = strDepDate;
		PassNights.value = iNights;

		//Go to Room List or Room Type List Screen
		//submit();
	}; 	// End With
	return true;
}			// End Function


function SetPeople() {
	// Set Default People
	with (document.frmMain) {

	  Ppl1.options[1].selected = true;

  
	if (1 >= 2) {
		Ppl2.options[0].selected = true};
  
	  if (1 >= 3) {
		  Ppl3.options[0].selected = true};
  
  	if (1 == 4) {
	  	Ppl4.options[0].selected = true};
  
	
	//Set the radio buttons
	

		//document.frmMain.ToAvail.focus();
  
	} //End With
}


function SetHiddenField(FormNum,btnValue) {
  //alert ("Hi " + btnValue);
  document.forms[FormNum].HdnNextPage.value=btnValue;
}

function ShowDetailPage(sType,sDetailURL) {
	//alert ("Here: |" + sType + "|" + sDetailURL + "|");
	if (sType == "IRM") {
		//Display the IRM detail page
		location.href = sDetailURL;
	}
	else {
		//Open a new window with the customer defined URL
		var DetailWindow = window.open(sDetailURL,"","status,resizable,scrollbars,dependent,width=655,height=600");
		DetailWindow.focus();
	}	
}

function ExpandIt(ThisPart,ThisBtn) {
	ThisPart.style.display = (ThisPart.style.display == "block") ? "none" : "block";
	ThisBtn.value = (ThisBtn.value == "View Calendar") ? "Hide Calendar" : "View Calendar";
}

function SetDate(sDate,sVacant,sNotUsed) {
	var sText;
	var sOccText = "occupied";
	if (sVacant == "Yes") {sOccText = "vacant"};

	sText = sDate + " is currently " + sOccText + ".\n";
	
	if (sOccText == "vacant") {
		sText = sText + "To use this date, change the dates at the top of the page\n";
		sText = sText + "and press the 'Search Available " & IRM.RoomLabel & " Types' button";
	};
	alert(sText);
}

function ToRes() {
	if (CheckDates()) {
		location.href = "ResTA.asp?IRM=yes&BtrvID=2";
	};
}

function UpdateHidden(Element) {
	//If the top display count is changed, update the hidden field at the bottom
	//alert (Count.name);
	
		if (Element.name == "DispCount") {
			document.frmAvail3.DispCount.value = Element.value;
		};

		
}

function UpdateRoomType() {
  
  HideBottom();
}

function UpdatePropCode() {
  
  HideBottom();
}

function CheckPassedDates() {
	var bPriorDate = false;
  //alert ("In CheckPassedDates");
	//Check for arrival date in the past - This is the Server Date set in Startup.asp
	var today = new Date();
	if (dtArrival.getYear() < today.getYear()) {bPriorDate = true;};

	if (dtArrival.getYear() == today.getYear()) {
		if (dtArrival.getMonth() < today.getMonth()) {
			bPriorDate = true;
		};

		if (dtArrival.getMonth() == today.getMonth()) {
			if (dtArrival.getDate() < today.getDate()) {
				bPriorDate = true;
			};
		};
	};

	if (bPriorDate) {
		alert ("Reservations can not be made for past dates.  Please reset dates and click 'Search for Available Room Types'");
	};
}