function getActualMonthVal(curMonth)
{
	var actualMonthVal = curMonth;
	if (actualMonthVal > 12)
	{
		actualMonthVal = actualMonthVal - 12
		actualMonthVal = getActualMonthVal(actualMonthVal)
	}
	//alert(actualMonthVal);
	return actualMonthVal;
}
function getMyYear(startMonth, dateIndex)
{
	// this function gets the year from the 12 mo drop down
	var myYear = 0;
	//	alert ("startMonth = " + startMonth)
	//	alert ("dateIndex = " + dateIndex)
	if (dateIndex > 12)
	{
		myYear = getActualYearVal(dateIndex, myYear)
	}
	if (startMonth > dateIndex)
	{
		myYear = myYear + 1;
	}
//	alert (myYear);
	return myYear;
}
function getActualYearVal(curMonth, curYear)
{
	if (curMonth > 12) 
	{
		curMonth = curMonth - 12
		curYear = curYear + 1
		curYear = getActualYearVal(curMonth, curYear)
	}
	return curYear;
}
function setDaysDropDown(selectBoxMonthYear, selectDay, timeDirect)
{
	var f = selectBoxMonthYear.form;
	var curDays;
	var thisDate = new Date();
	var totalDays;
	var myMonth;
	var myYear;
	
	var thisMonth = thisDate.getMonth() + 1;
	var thisYear = thisDate.getFullYear();
	// need to increment Month and day b/c they are arrays
	var ddMonthYearVal = (selectBoxMonthYear[selectBoxMonthYear.selectedIndex].value - 0) + 1;
	var selectDayIndex = selectDay.selectedIndex;
	if (timeDirect > 0)
	{
		myMonth = getActualMonthVal(ddMonthYearVal)
		myYear = thisYear + getMyYear(thisMonth, ddMonthYearVal);
	}
	else
	{
		myMonth = ddMonthYearVal;
		myYear = thisYear + getMyPastYear(thisMonth, ddMonthYearVal);
	}
	totalDays = getTotalDaysInMonth(myMonth, myYear) 
	//alert(totalDays)
	// find out how many days currently
	curDays = selectDay.options.length;
	if (curDays	> totalDays)
	{
		selectDay.options.length = totalDays;
	}
	else if (curDays < totalDays)
	{
		var myNewOpt; 
		for (i=curDays+1; i<=totalDays; i++)
		{
			myNewOpt = new Option(i, i)
			selectDay.options[selectDay.options.length] = myNewOpt;
		}
	}
	// if the total number of days in the new month is > previous selected month
	// select the top one
	//alert("for " + selectDay.name + "\ntotalDays " + totalDays + "\nselectDayIndex " + selectDayIndex);
	if (totalDays <= selectDayIndex)
	{
		//alert("should set selectedIndex = " + (totalDays - 1))
		selectDay.selectedIndex = (totalDays - 1);
		//alert(selectDay.selectedIndex);
	}
	else
	{
		// otherwise select the previously selected one
		selectDay.selectedIndex = selectDayIndex;
	}
}
function setDepartDate(selectInMonthYear, selectInDay, selectOutMonthYear, selectOutDay, selectNights)
{
	// set defaults
	var thisDate = new Date();
	var thisMonth = thisDate.getMonth() + 1;
	var thisYear = thisDate.getFullYear();
	var s = "";
	
	var myInDateVal = getMyDate(selectInMonthYear, selectInDay)	
	s = "my in date = " + myInDateVal;

	// get num nights
	var numNightsVal = (selectNights.options[selectNights.selectedIndex].value - 0);
	s = s + "\nNum nights = " + numNightsVal;

	// lets increment by the value in selectNights;
	var dtIn = new Date(myInDateVal)
	var nextDay = (dtIn.getMonth() + 1) + "/" + (dtIn.getDate() + numNightsVal) + "/" + dtIn.getFullYear();
	nextDay = Date.parse(nextDay)
	nextDay = new Date(nextDay)
	
	// convert back to something useful
	var nextDayYear = nextDay.getFullYear();
	var nextDayMonth = nextDay.getMonth() + 1
	var yearsOut = nextDayYear - thisYear;
	
	selectOutMonthYear.selectedIndex = getMonthIndex(thisMonth, nextDayMonth, yearsOut);
	selectOutDay.selectedIndex = (nextDay.getDate() -1)

	s = s + "\nmy out date = " + nextDay;	
	
	//alert(s);

}
function setArriveDate(selectInMonthYear, selectInDay, selectOutMonthYear, selectOutDay, selectNights)
{

	// set defaults
	var thisDate = new Date();
	var thisMonth = thisDate.getMonth() + 1;
	var thisYear = thisDate.getFullYear();
	var s = "";
	
	var myOutDateVal = getMyDate(selectOutMonthYear, selectOutDay)	
	s = "my out date = " + myOutDateVal;

	// get num nights
	var numNightsVal = (selectNights.options[selectNights.selectedIndex].value - 0);
	s = s + "\nNum nights = " + numNightsVal;

	// lets increment by the value in selectNights;
	var dtOut = new Date(myOutDateVal)
	var prevDay = new Date((dtOut.getMonth() + 1) + "/" + (dtOut.getDate() - numNightsVal) + "/" + dtOut.getFullYear());
	prevDay = Date.parse(prevDay)
	prevDay = new Date(prevDay)
	
	// convert back to something useful
	var prevDayYear = prevDay.getFullYear();
	var prevDayMonth = prevDay.getMonth() + 1
	var yearsOut = prevDayYear - thisYear;

	var MonthIndex = getMonthIndex(thisMonth, prevDayMonth, yearsOut);
	
	if (MonthIndex >= 0) {
	  selectInMonthYear.selectedIndex = MonthIndex;
	  selectInDay.selectedIndex = (prevDay.getDate() -1);
	}

	s = s + "\nmy in date = " + prevDay;	
}

function setNumNights(selectInMonthYear, selectInDay, selectOutMonthYear, selectOutDay, selectNights)
{	
	var DAY = 86400000;
	
	var Dept = new Date(getMyDate(selectOutMonthYear, selectOutDay));
	var Arr = new Date(getMyDate(selectInMonthYear, selectInDay));
	var NumDays = Math.round(((Dept - Arr) / DAY) -1);
	
	if (NumDays < 14 && NumDays >= 0) {
	  selectNights.selectedIndex = NumDays;
	}
	else{
	  setArriveDate(selectInMonthYear, selectInDay, selectOutMonthYear, selectOutDay, selectNights);
	}
}

function getMonthIndex(todayMonth, selMonth, yearsOut)
{

	var myIndex;
	var monthsOut;
	
	// calculate the drop down selection
	if (yearsOut == 0)
	{
		monthsOut = selMonth - todayMonth
	}
	else
	{
		// 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
		monthsOut = 12 - todayMonth + selMonth + (yearsOut - 1) * 12
	}

	myIndex = monthsOut;
	
	//alert(myIndex);
	return myIndex;
	
}
function getMyDate(selectMonthYear, selectDay)
{
	var thisDate = new Date();
	var thisMonth = thisDate.getMonth() + 1;
	var thisYear = thisDate.getFullYear();

	// get the month
	var myMonthYearVal = (selectMonthYear.options[selectMonthYear.selectedIndex].value -0) + 1;
	var myMonthVal = getActualMonthVal(myMonthYearVal);

	// get the year
	var myYearVal = thisYear + getMyYear(thisMonth, myMonthYearVal);

	// get the day
	var myDayVal = selectDay.options[selectDay.selectedIndex].value - 0;

	// get the date
	var myDateVal = myMonthVal + "/" + myDayVal + "/" + myYearVal;
	
	return myDateVal;


}
// 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);
    }
}
