var undefined;
var toDay;
var tomorrow;
var dayInMilli = 86400000;

function updateOut(checkIn)
{
	var checkOut = document.room_reservations.chkOut;
	var nextDay;
	var dtChkOut;
	var totalDays = 1;

	dtChkOut = checkOut.value;
	
	pChkIn = Date.parse(checkIn);
	pChkOut = Date.parse(dtChkOut);
	 
	totalDays = getTotalDays() - 0; // check to see the number of nights selected
	dtChkIn = new Date(pChkIn);
	
	/* This code was causing a problem in webkit browsers so it was converted to use milliseconds to create the date instead of strings
	nextDay = (dtChkIn.getMonth() + 1) + "/" + (dtChkIn.getDate() + totalDays) + "/" + dtChkIn.getFullYear();
	nextDay = Date.parse(nextDay);
	nextDay = new Date(nextDay);
	*/
	nextDay = new Date(dtChkIn.getTime() + (totalDays * dayInMilli));

	checkOut.value = (nextDay.getMonth() + 1) + "/" + nextDay.getDate() + "/" + nextDay.getFullYear();

   	if (checkOut.type == "hidden")
		updateDropDowns("Check-Out", checkOut);

	setNightsDropDown();
}

function updateIn(checkOut)
{
	var checkIn = document.room_reservations.chkIn;
	var dtChkIn = checkIn.value;
	var totalDays = 1;
	var prevDay;

	pChkIn = Date.parse(dtChkIn);
	pChkOut = Date.parse(checkOut);

	// only change checkIn if it is after chkOut
	if (pChkOut > pChkIn)
	{
		setNightsDropDown(); 
		return;
	}
	
	// check to see the number of nights selected
	totalDays = getTotalDays() - 0;
	dtChkOut = new Date(pChkOut);

	/* This code was causing a problem in webkit browsers so it was converted to use milliseconds to create the date instead of strings
	prevDay = (dtChkOut.getMonth() + 1) + "/" + (dtChkOut.getDate() - totalDays) + "/" + dtChkOut.getFullYear();
	prevDay = Date.parse(prevDay);
	prevDay = new Date(prevDay);
	*/
	prevDay = new Date(dtChkOut.getTime() - (totalDays * dayInMilli));

	checkIn.value = (prevDay.getMonth() + 1) + "/" + prevDay.getDate() + "/" + prevDay.getFullYear();

	if (checkIn.type == "hidden")
		updateDropDowns("Check-In", checkIn);

	setNightsDropDown();
}

function getTotalDays()
{
	//wanted to use the following line but it was throwing an error will test more later
	//var f = $('room_reservations');
	var f = document.getElement('form#room_reservations');
	var numNights =  f.getElement('select[name=numNights]');

	numNights.disabled = false;
	totalNights = (numNights && numNights.get('value') > 0)? numNights.get('value') : 1;
	numNights.disabled = true;

	return totalNights;
}

function setDefaults()
{
     var nextDate;
     toDay = new Date();
     tomorrow = new Date();
     nextDate = toDay.getDate() + 1;
     tomorrow.setDate(nextDate);

     setPeople();
     setDates();
}

function setDates()
{
     document.room_reservations.chkIn.value = (toDay.getMonth() + 1) + "/" + toDay.getDate() + "/" + toDay.getFullYear();
     document.room_reservations.chkOut.value = (tomorrow.getMonth() + 1) + "/" + tomorrow.getDate() + "/" + tomorrow.getFullYear();
}

function setDropDownDates()
{
	var arrivalMonth = toDay.getMonth() + 1;
	var arrivalDay = toDay.getDate();
	var arrivalYear = toDay.getFullYear();

	var departMonth = tomorrow.getMonth() + 1;
	var departDay = tomorrow.getDate();
	var departYear = tomorrow.getFullYear();
	
	setChkInDate(arrivalMonth, arrivalDay, arrivalYear);
	setChkOutDate(departMonth, departDay, departYear);
}


function setPeople()
{
     document.room_reservations.adult_no.value = 1;
     document.room_reservations.child_no.value = 0;
}


function chkPeople(txtBoxName, txtBox)
{
	var val = txtBox.value;
	var newValue = parseFloat(val);
	if (val == "")
	{
		alert("The number of " + txtBoxName + " staying cannot be blank.");
		txtBox.value = 0;
	}
	else
	{
		if( isNaN(newValue) )
		{
			alert(val + " is not a valid number.\nPlease enter a valid number.");
			txtBox.value = 0;
		}
	}
	txtBox.focus();
}

function setCheckOut()
{
	var f = document.room_reservations;
	updateOut(f.chkIn.value);
}

function openCalendar()
{
	top.newWin = window.open('/hotelsite/reservations/calendar.html','cal','WIDTH=400,HEIGHT=230');
	top.newWin.focus();
}

function validateForm(f)
{
// check that the dates are after today
	if (chkDate('Check-In', f.chkIn) == false)
		return false;
	
	if (chkDate('Check-Out', f.chkOut) == false)
		return false;

	if (Date.parse(f.chkIn.value) >= Date.parse(f.chkOut.value))
	{
		alert ("The Check Out Date needs to be after the Check In Date.");
		updateOut(f.chkIn.value);

		if (f.chkOut.type != "hidden");
			f.chkOut.focus();

		return false;
	}
	return true;
}

function setChkInDate(arrivalMonthYear, arrivalDay)
{
	var f = document.room_reservations;
	var newChkInDate;
	var arrivalMonth;

	var thisDate = new Date();
	var thisMonth = thisDate.getMonth() + 1; // need to increment arrivalMonth and arrivaldate b/c they are arrays
	var thisYear = thisDate.getFullYear();
	var ddMonthYearVal = (arrivalMonthYear.options[arrivalMonthYear.selectedIndex].value - 0) + 1; // need to increment Month and day b/c they are arrays

	arrivalMonth = getActualMonthVal(ddMonthYearVal);
	arrivalYear = thisYear + getMyYear(thisMonth, ddMonthYearVal);
	arrivalDay = arrivalDay + 1;

	if (arrivalMonth < 10)
		arrivalMonth = "0" + arrivalMonth;

	if (arrivalDay < 10)
		arrivalDay = "0" + arrivalDay;

	if (arrivalYear < 10)
		arrivalYear = "0" + arrivalYear ;
	
	newChkInDate = arrivalMonth + "/" + arrivalDay + "/" + arrivalYear;

	setDate(f.chkIn, newChkInDate);
	updateOut(f.chkIn.value);
}

function setChkOutDate(departMonthYear, departDay)
{
	var f = document.room_reservations;
	var newChkOutDate;
	var departMonth;

	var thisDate = new Date();
	var thisMonth = thisDate.getMonth() + 1;
	var thisYear = thisDate.getFullYear();
	var ddMonthYearVal = (departMonthYear.options[departMonthYear.selectedIndex].value - 0) + 1; // need to increment Month and day b/c they are arrays

	departMonth = getActualMonthVal(ddMonthYearVal);
	departYear = thisYear + getMyYear(thisMonth, ddMonthYearVal);
	departDay = departDay + 1;

	if (departMonth < 10)
		departMonth = "0" + departMonth;

	if (departDay < 10)
		departDay = "0" + departDay;

	if (departYear < 10)
		departYear = "0" + departYear ;
	
	newChkOutDate = departMonth + "/" + departDay + "/" + departYear;

	setDate(f.chkOut, newChkOutDate);
	updateIn(newChkOutDate);
}

function getMyMonth_old(startMonth, dateIndex)
{
// this function gets the month from the 12 mo drop down
	var myMonth = startMonth + dateIndex;
	
	if (myMonth > 12)
		myMonth = myMonth - 12;

	return myMonth;
}

function getMyYear_old(startMonth, dateIndex)
{
	// this function gets the year from the 12 mo drop down
	var myMonth = startMonth + dateIndex;
	var myYear = 0;
	
	//alert ("startMonth = " + startMonth);
	//alert ("dateIndex = " + dateIndex);
	//alert ("myMonth  = " + myMonth);
	
	if (myMonth > 12)
		myYear = myYear + 1;

	//alert ("myYear = " + myYear);
	return myYear;
}

function getMonthIndex(todayMonth, selMonth, yearsOut)
{
	var myIndex;
	var monthsOut;
	
	//if (selMonth < startMonth)
	//{
	//	selMonth = selMonth + 12;
	//}
	//selMonth = selMonth + yearDif *12;
	
	if (yearsOut == 0) // calculate the drop down selection
		monthsOut = selMonth - todayMonth;
	else
		monthsOut = 12 - todayMonth + selMonth + (yearsOut - 1) * 12; // months to the end of the year and then the months in the new year -- if a new year subtract 1 b/c need only 12 new months

	myIndex = monthsOut;
	return myIndex;
}

function setDate(hdDate, value)
{
	hdDate.value = value;
}

function setNightsDropDown()
{
	var f = document.room_reservations;
	var arrivalDate = f.chkIn.value; // get the arrival date
	var departDate = f.chkOut.value; // get the depart date
	var dateArrival = new Date(arrivalDate);
	var dateDepart = new Date(departDate);
	var daysBetween = getDateDifference(dateArrival, dateDepart);

	f.numNights.selectedIndex = daysBetween - 1
}

function getDateDifference(startDate, endDate)
{
	var dateDif;

	dateDif = endDate.getTime() - startDate.getTime();
	dateDif = Math.round(dateDif/(1000 * 60 * 60 * 24));
	return dateDif;
}

function updateDropDowns(txtBoxName, txtBox)
{
	var myDate = new Date(txtBox.value);
	var thisDate = new Date();
	var f = document.room_reservations;
	
	var ddMonth;
	var ddDay;
	var ddYear;
	var yearDif;

	if (txtBox.type != "hidden")
		return;
	
	if (txtBoxName == "Check-In")
	{
		ddDay = f.chkInDay;
		ddMonthYear = f.chkInMonthYear;
	}
	else
	{
		ddDay = f.chkOutDay;
		ddMonthYear = f.chkOutMonthYear;	
	}
	
	// update month
	var startMonth = thisDate.getMonth() + 1;
	var valMonth = myDate.getMonth() + 1;
	var startYear = thisDate.getYear();
	var valYear = myDate.getYear();
	
	yearDif = valYear - startYear;
	ddMonthYear.selectedIndex = getMonthIndex(startMonth, valMonth, yearDif);

	setDaysDropDown (ddMonthYear, ddDay, 1); // set # days for the month
	ddDay.selectedIndex = (myDate.getDate() -1) // decrement day b/c day is not 0 based
}

function setDaysDropDown_old(selectBoxMonthYear)
{
	var f = selectBoxMonthYear.form;
	var thisDate = new Date();
	var thisMonth = thisDate.getMonth() + 1;
	var thisYear = thisDate.getFullYear();

	var myMonth;
	var myYear;
	var totalDays;
	var selectDay;
	var curDays;
	var myNewOpt; 
		

	myMonth = getMyMonth(thisMonth, selectBoxMonthYear.selectedIndex);
	myYear = thisYear + getMyYear(thisMonth, selectBoxMonthYear.selectedIndex);
	totalDays = getTotalDaysInMonth(myMonth, myYear);
	
	if (selectBoxMonthYear.name == "chkInMonthYear")
		selectDay = f.chkInDay;		
	else
		selectDay = f.chkOutDay;

	curDays = selectDay.options.length; // find out how many days currently

	if (curDays	> totalDays)
	{
		selectDay.options.length = totalDays;
	}
	else if (curDays < totalDays)
	{
		for (i=curDays+1; i<=totalDays; i++)
		{
			myNewOpt = new Option(i, i);
			selectDay.options[selectDay.options.length] = myNewOpt;
		}
	}
}

// GET NUMBER OF DAYS IN MONTH
function getTotalDaysInMonth(month,year)  
{
    var days = 0;
    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 (month==2)  
    {
        if (isALeapYear(year)) 
            days=29;
        else 
            days=28;
    }
    return (days);
}

// CHECK TO SEE IF YEAR IS A LEAP YEAR
function isALeapYear (Year) 
{
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) 
        return (true);
    else 
        return (false);
}

function callMe()
{
	alert("on the line");
}

var exit = true;
var externalLink = false;

function loadRatesMessage(URL) {
    ihit = 420;
    iwid = 500;
    ttl = (screen.availHeight/2)-(ihit/2);
    lll = (screen.availWidth/2)-(iwid/2);
    if (exit || externalLink) {
        open(URL,'NoBookedReservation','toolbar=1,scrollbars=1,resizable=1,width=' + iwid + ',height=' + ihit + ',top=' + ttl + ',left=' + lll);
//		alert(window.location.href)
    }
}