// Global variables
var todayDate = new Date();
var thisMonth = todayDate.getMonth() +1;
var thisYear = todayDate.getYear();
var thisDay = todayDate.getDate();

months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var writeMonth = thisMonth;
var writeYear = thisYear;

var departing_date_selected = false;


// Called to validate mandatory information
function Check(){

var myform = document.forms[0]
var dYearMonthIndex = myform.B_MONTH.selectedIndex
var dDayIndex = myform.B_DAY.selectedIndex
var dYearMonthValue = myform.B_MONTH.options[dYearMonthIndex].value
var dDayValue = myform.B_DAY.options[dDayIndex].value
var dTimeIndex = myform.B_TIME.selectedIndex
var dTimeValue = myform.B_TIME.options[dTimeIndex].value


myform.B_DATE.value = dYearMonthValue + dDayValue + dTimeValue

var rYearMonthIndex = myform.E_MONTH.selectedIndex
var rDayIndex = myform.E_DAY.selectedIndex

var rYearMonthValue = myform.E_MONTH.options[rYearMonthIndex].value
var rDayValue = myform.E_DAY.options[rDayIndex].value
var rTimeIndex = myform.E_TIME.selectedIndex
var rTimeValue = myform.E_TIME.options[rTimeIndex].value

myform.E_DATE.value = rYearMonthValue + rDayValue + rTimeValue

myform.E_LOCATION.value = myform.B_LOCATION.value

return true;

}
// Update return date so it is after outbound date.
function ChangeReturnDate()
{
	var myform = document.forms[0]
	var selectedMonth
	var IsShortMonth
	var btnRetDate = true

	selectedMonth = myform.B_MONTH.options[myform.B_MONTH.selectedIndex].value.charAt(4) + myform.B_MONTH.options[myform.B_MONTH.selectedIndex].value.charAt(5)
	IsShortMonth = (selectedMonth == "02" || selectedMonth == "04" || selectedMonth == "06" || selectedMonth == "09" || selectedMonth == "11")

	if(btnRetDate && departing_date_selected)
	{
		if(myform.B_MONTH.selectedIndex > myform.E_MONTH.selectedIndex || myform.B_MONTH.options[myform.B_MONTH.selectedIndex].value == "XX")
		{
			if ( ((myform.E_DAY.options[myform.E_DAY.selectedIndex].value == 31)&&(IsShortMonth))||((myform.E_DAY.options[myform.E_DAY.selectedIndex].value > 28) && (selectedMonth == "02")))
			{
				myform.E_MONTH.selectedIndex = myform.B_MONTH.selectedIndex+1
				myform.E_DAY.selectedIndex = 0
			}
			else
			{
				myform.E_MONTH.selectedIndex = myform.B_MONTH.selectedIndex
			}
		}
		if((myform.B_DAY.selectedIndex >= myform.E_DAY.selectedIndex || myform.E_DAY.options[myform.E_DAY.selectedIndex].value == "XX")&&(myform.E_MONTH.selectedIndex == myform.B_MONTH.selectedIndex))
		{
			if(((myform.B_DAY.options[myform.B_DAY.selectedIndex].value > 29) && (IsShortMonth))||(myform.B_DAY.options[myform.B_DAY.selectedIndex].value == 31)||((myform.B_DAY.options[myform.B_DAY.selectedIndex].value > 27) && (myform.B_MONTH.selectedIndex == 6)))
			{
				myform.E_MONTH.selectedIndex = myform.B_MONTH.selectedIndex + 1
				myform.E_DAY.selectedIndex = 0
			}
			else
				myform.E_DAY.selectedIndex = myform.B_DAY.selectedIndex + 1
		}
	}
	departing_date_selected = false
}
function SetDate()
{
	var myform = document.forms[0];
	var dYearMonthValue = myform.B_MONTH.options[myform.B_MONTH.selectedIndex].value
	var dDayValue = myform.B_DAY.options[myform.B_DAY.selectedIndex].value
	myform.B_CAL_DATE_1.value = dYearMonthValue.substr(0,4) +'-'+dYearMonthValue.substr(4,2)+'-'+ dDayValue

	var rYearMonthValue = myform.E_MONTH.options[myform.E_MONTH.selectedIndex].value
	var rDayValue = myform.E_DAY.options[myform.E_DAY.selectedIndex].value
	myform.B_CAL_DATE_2.value = rYearMonthValue.substr(0,4) +'-'+rYearMonthValue.substr(4,2)+'-'+ rDayValue
}

function UpdateDate(bOutbound)
{
var MonthYear;
var Day;
var myform = document.forms[0];
var CalendarDate;
	if (bOutbound)
	{
		// format is 'YYYYMMDD'
		CalendarDate = myform.B_CAL_DATE_1.value;
		MonthYear = CalendarDate.substr(0,6)
		Day = CalendarDate.substr(6,2);
		// Now I need to set the dates in the drop lists.
		for (n=0;n<myform.B_MONTH.length;n++)
		{
			if (myform.B_MONTH.options[n].value == MonthYear)
				myform.B_MONTH.selectedIndex=n;
		}
		for (n=0;n<myform.B_DAY.length;n++)
		{
			if (myform.B_DAY.options[n].value == Day)
				myform.B_DAY.selectedIndex=n;
		}


		// And now I need to update the Return date!
		departing_date_selected = true;
		ChangeReturnDate();
	}
	else
	{
		// format is 'YYYYMMDD'
		CalendarDate = myform.B_CAL_DATE_2.value;
		MonthYear = CalendarDate.substr(0,6)
		Day = CalendarDate.substr(6,2);
		// Now I need to set the dates in the drop lists.
		for (n=0;n<myform.E_MONTH.length;n++)
		{
			if (myform.E_MONTH.options[n].value == MonthYear)
				myform.E_MONTH.selectedIndex=n;
		}
		for (n=0;n<myform.E_DAY.length;n++)
		{
			if (myform.E_DAY.options[n].value == Day)
				myform.E_DAY.selectedIndex=n;
		}

	}
}
