//<script>

function LibGoDates_AddDays(dInDate,iDays)
{
	//the extra 60*60*1000 is to handle the extra hour for October DST
	return new Date(dInDate.getTime() + iDays*24*60*60*1000 + 60*60*1000);
}

function LibGoDates_getDaysPerMonth(year, month)
{
	return 32 - new Date(year, month, 32).getDate();
}

function LibGoDates_getDateDiffDays(d1, d2)
{
	return Math.ceil((d2.getTime() - d1.getTime())/(1000*60*60*24));
}

function LibGoDates_repopPreserveSelectedDates(oDepartMonthSelect, oDepartDaySelect,
	oReturnMonthSelect, oReturnDaySelect)
{
	var aConfig = LibGoDates_getConfigArray();
	var dNow = new Date();
	var dFirstAllowableDepartDate = LibGoDates_AddDays(dNow, aConfig[0]);

	//The asp populates with the correct Departure Dates selected
	//However, Return Dates may be bogus upon a start over
	//Repopulate anyway just to be safe, while preserving selected dates

	//1. repop the Departure Months (the asp only populates selected dates)
	var iSelectedDepartMonth = oDepartMonthSelect.options[oDepartMonthSelect.options.selectedIndex].value;
	var iSelectedDepartDay =   oDepartDaySelect.options[oDepartDaySelect.options.selectedIndex].value;
	var iSelectedDepartYear = dNow.getFullYear();
	if ( dFirstAllowableDepartDate.getMonth() - iSelectedDepartMonth > 0 )
	{
		iSelectedDepartYear++;   
	}
	var dSelectedDepartDate = new Date(iSelectedDepartYear, iSelectedDepartMonth-1, iSelectedDepartDay);
	LibGoDates_repopMonthSelect(oDepartMonthSelect, dFirstAllowableDepartDate.getMonth(), 12, dSelectedDepartDate.getMonth());

	//2. repop the Departure Days
	var iStartDay, iEndDay, iNumDays;
	if(dSelectedDepartDate.getMonth() == dFirstAllowableDepartDate.getMonth()){
		iStartDay = dFirstAllowableDepartDate.getDate();
	}else{
		iStartDay = 1;
	}
	iEndDay = LibGoDates_getDaysPerMonth(dSelectedDepartDate.getFullYear(), dSelectedDepartDate.getMonth()) - iStartDay + 1;
	LibGoDates_RepopulateDaySelect(oDepartDaySelect, iStartDay, iEndDay, dSelectedDepartDate.getDate());

	//3. repop the Return Months
	var dFirstAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[2]);
	var dLastAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[4]);
	var iSelectedReturnMonth = oReturnMonthSelect.options[oReturnMonthSelect.options.selectedIndex].value;
	var iSelectedReturnDay = oReturnDaySelect.options[oReturnDaySelect.options.selectedIndex].value;
	var iSelectedReturnYear = dNow.getFullYear();
	//may need to tighten this up to check the day as well?
	

    // The below logic is incorrect  , //FP 30468 sathya
//	if ( dFirstAllowableReturnDate.getMonth() - iSelectedReturnMonth > 0 )
//	{
//		iSelectedReturnYear++;   
//	}


////FP 30468 sathya

	if ( dNow.getMonth() - iSelectedReturnMonth > 0 )
	{
		iSelectedReturnYear++;   
	}
	var dSelectedReturnDate = new Date(iSelectedReturnYear, iSelectedReturnMonth-1, iSelectedReturnDay);
	
	if(LibGoDates_getDateDiffDays(dFirstAllowableReturnDate, dSelectedReturnDate) < 2){
		dSelectedReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[3]);
	}
	var iNumMonths;
	if( dLastAllowableReturnDate.getMonth() < dFirstAllowableReturnDate.getMonth() )
	{
		iNumMonths = dLastAllowableReturnDate.getMonth() + 12 - dFirstAllowableReturnDate.getMonth() + 1;
	}else{
		iNumMonths = dLastAllowableReturnDate.getMonth() - dFirstAllowableReturnDate.getMonth() + 1;
	}
	LibGoDates_repopMonthSelect(oReturnMonthSelect, 
		dFirstAllowableReturnDate.getMonth(), 
		iNumMonths, 
		dSelectedReturnDate.getMonth());
	
	//4. repop the Return Days
	if(dSelectedReturnDate.getMonth() == dFirstAllowableReturnDate.getMonth()){
		iStartDay = dFirstAllowableReturnDate.getDate();
	}else{
		iStartDay = 1;
	}
	if(dSelectedReturnDate.getMonth() == dLastAllowableReturnDate.getMonth()){
		iEndDay = dLastAllowableReturnDate.getDate();
	}else{
		iEndDay = LibGoDates_getDaysPerMonth(dSelectedReturnDate.getFullYear(), dSelectedReturnDate.getMonth());
	}
	iNumDays = iEndDay - iStartDay + 1;
	LibGoDates_RepopulateDaySelect(oReturnDaySelect, iStartDay, iNumDays, dSelectedReturnDate.getDate());
}


function LibGoDates_repopWhenUserChangesDepartMonth(oDepartMonthSelect, oDepartDaySelect,
	oReturnMonthSelect, oReturnDaySelect)
{
	var aConfig = LibGoDates_getConfigArray();
	var dNow = new Date();
	var dFirstAllowableDepartDate = LibGoDates_AddDays(dNow, aConfig[0]);
	var iSelectedDepartYear = dNow.getFullYear();
	var iSelectedDepartMonth = oDepartMonthSelect[oDepartMonthSelect.selectedIndex].value -1;
	var iSelectedDepartDay   = oDepartDaySelect.options[oDepartDaySelect.selectedIndex].value;
	if( iSelectedDepartDay > LibGoDates_getDaysPerMonth(iSelectedDepartYear, iSelectedDepartMonth) )
	{ //There may be fewer days in the changed to month
		iSelectedDepartDay = LibGoDates_getDaysPerMonth(iSelectedDepartYear, iSelectedDepartMonth);
	}
	if ( dNow.getMonth() - iSelectedDepartMonth > 0 )
	{ //We are in a month that belongs to next year.
	    iSelectedDepartYear++;   
	}

	// Change the 2nd pulldown for Departure Days
	var dSelectedDepartDate = new Date(iSelectedDepartYear, iSelectedDepartMonth, iSelectedDepartDay);
	var iStartDay, iEndDay, iNumDays;
	if(dSelectedDepartDate.getMonth() == dFirstAllowableDepartDate.getMonth()){
		iStartDay = dFirstAllowableDepartDate.getDate();
	}else{
		iStartDay = 1;
	}
	iEndDay = LibGoDates_getDaysPerMonth(dSelectedDepartDate.getFullYear(), dSelectedDepartDate.getMonth());
	iNumDays = iEndDay - iStartDay + 1;
	LibGoDates_RepopulateDaySelect(oDepartDaySelect, iStartDay, iNumDays, dSelectedDepartDate.getDate());

	// Change the 3rd pulldown for Return Months

	var dFirstAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[2]);
	var dLastAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[4]);
	var dSelectedReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[3]);
	var iNumMonths;
	if( dLastAllowableReturnDate.getMonth() < dFirstAllowableReturnDate.getMonth() )
	{
		iNumMonths = dLastAllowableReturnDate.getMonth() + 12 - dFirstAllowableReturnDate.getMonth() + 1;
	}else{
		iNumMonths = dLastAllowableReturnDate.getMonth() - dFirstAllowableReturnDate.getMonth() + 1;
	}
	LibGoDates_repopMonthSelect(oReturnMonthSelect, 
		dFirstAllowableReturnDate.getMonth(), 
		iNumMonths, 
		dSelectedReturnDate.getMonth());

	// Change the 4th pulldown for Return Days
	if(dSelectedReturnDate.getMonth() == dFirstAllowableReturnDate.getMonth()){
		iStartDay = dFirstAllowableReturnDate.getDate();
	}else{
		iStartDay = 1;
	}
	if(dSelectedReturnDate.getMonth() == dLastAllowableReturnDate.getMonth()){
		iEndDay = dLastAllowableReturnDate.getDate();
	}else{
		iEndDay = LibGoDates_getDaysPerMonth(dSelectedReturnDate.getFullYear(), dSelectedReturnDate.getMonth());
	}
	iNumDays = iEndDay - iStartDay + 1;
	LibGoDates_RepopulateDaySelect(oReturnDaySelect, iStartDay, iNumDays, dSelectedReturnDate.getDate());
}


function LibGoDates_repopWhenUserChangesDepartDay(oDepartMonthSelect, oDepartDaySelect,
	oReturnMonthSelect, oReturnDaySelect)
{
	var aConfig = LibGoDates_getConfigArray();
	var dNow = new Date();
	var dFirstAllowableDepartDate = LibGoDates_AddDays(dNow, aConfig[0]);
	var iSelectedDepartYear = dNow.getFullYear();
	var iSelectedDepartMonth = oDepartMonthSelect[oDepartMonthSelect.selectedIndex].value -1;
	var iSelectedDepartDay   = oDepartDaySelect.options[oDepartDaySelect.selectedIndex].value;
	if( iSelectedDepartDay > LibGoDates_getDaysPerMonth(iSelectedDepartYear, iSelectedDepartMonth) )
	{ //There may be fewer days in the changed to month
		iSelectedDepartDay = LibGoDates_getDaysPerMonth(iSelectedDepartYear, iSelectedDepartMonth);
	}
	if ( dNow.getMonth() - iSelectedDepartMonth > 0 )
	{ //We are in a month that belongs to next year.
	    iSelectedDepartYear++;   
	}
	var dSelectedDepartDate = new Date(iSelectedDepartYear, iSelectedDepartMonth, iSelectedDepartDay);

	// Change the 3rd pulldown for Return Months
	var iStartDay, iEndDay, iNumDays;
	var dFirstAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[2]);
	var dLastAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[4]);
	var dSelectedReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[3]);
	var iNumMonths;
	if( dLastAllowableReturnDate.getMonth() < dFirstAllowableReturnDate.getMonth() )
	{
		iNumMonths = dLastAllowableReturnDate.getMonth() + 12 - dFirstAllowableReturnDate.getMonth() + 1;
	}else{
		iNumMonths = dLastAllowableReturnDate.getMonth() - dFirstAllowableReturnDate.getMonth() + 1;
	}
	LibGoDates_repopMonthSelect(oReturnMonthSelect, 
		dFirstAllowableReturnDate.getMonth(), 
		iNumMonths, 
		dSelectedReturnDate.getMonth());

	// Change the 4th pulldown for Return Days
	if(dSelectedReturnDate.getMonth() == dFirstAllowableReturnDate.getMonth()){
		iStartDay = dFirstAllowableReturnDate.getDate();
	}else{
		iStartDay = 1;
	}
	if(dSelectedReturnDate.getMonth() == dLastAllowableReturnDate.getMonth()){
		iEndDay = dLastAllowableReturnDate.getDate();
	}else{
		iEndDay = LibGoDates_getDaysPerMonth(dSelectedReturnDate.getFullYear(), dSelectedReturnDate.getMonth());
	}
	iNumDays = iEndDay - iStartDay + 1;
	LibGoDates_RepopulateDaySelect(oReturnDaySelect, iStartDay, iNumDays, dSelectedReturnDate.getDate());
}


function LibGoDates_repopWhenUserChangesReturnMonth(oDepartMonthSelect, oDepartDaySelect,
	oReturnMonthSelect, oReturnDaySelect)
{
	var aConfig = LibGoDates_getConfigArray();
	var dNow = new Date();
	var dFirstAllowableDepartDate = LibGoDates_AddDays(dNow, aConfig[0]);
	var iSelectedDepartYear = dNow.getFullYear();
	var iSelectedDepartMonth = oDepartMonthSelect[oDepartMonthSelect.selectedIndex].value -1;
	var iSelectedDepartDay   = oDepartDaySelect.options[oDepartDaySelect.selectedIndex].value;
	if( iSelectedDepartDay > LibGoDates_getDaysPerMonth(iSelectedDepartYear, iSelectedDepartMonth) )
	{ //There may be fewer days in the changed to month
		iSelectedDepartDay = LibGoDates_getDaysPerMonth(iSelectedDepartYear, iSelectedDepartMonth);
	}
	if ( dNow.getMonth() - iSelectedDepartMonth > 0 )
	{ //We are in a month that belongs to next year.
	    iSelectedDepartYear++;   
	}
	var dSelectedDepartDate = new Date(iSelectedDepartYear, iSelectedDepartMonth, iSelectedDepartDay);

	var iStartDay, iEndDay, iNumDays;
	var dFirstAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[2]);
	var dLastAllowableReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[4]);
	//var dSelectedReturnDate = LibGoDates_AddDays(dSelectedDepartDate, aConfig[3]);

	// Change the 4th pulldown for Return Days
	var iSelectedReturnYear = dNow.getFullYear();
	var iSelectedReturnMonth = oReturnMonthSelect[oReturnMonthSelect.selectedIndex].value -1;
	var iSelectedReturnDay = oReturnDaySelect[oReturnDaySelect.selectedIndex].value -1;
	if( iSelectedReturnDay > LibGoDates_getDaysPerMonth(iSelectedReturnYear, iSelectedReturnMonth) )
	{ //There may be fewer days in the changed to month
		iSelectedReturnDay = LibGoDates_getDaysPerMonth(iSelectedReturnYear, iSelectedReturnMonth);
	}
	if ( dNow.getMonth() - iSelectedReturnMonth > 0 )
	{ //We are in a month that belongs to next year.
	    iSelectedReturnYear++;   
	}

	if(iSelectedReturnMonth == dFirstAllowableReturnDate.getMonth()){
		iStartDay = dFirstAllowableReturnDate.getDate();
	}else{
		iStartDay = 1;
	}
	if(iSelectedReturnMonth == dLastAllowableReturnDate.getMonth()){
		iEndDay = dLastAllowableReturnDate.getDate();
	}else{
		iEndDay = LibGoDates_getDaysPerMonth(iSelectedReturnYear, iSelectedReturnMonth);
	}
	iNumDays = iEndDay - iStartDay + 1;
	LibGoDates_RepopulateDaySelect(oReturnDaySelect, iStartDay, iNumDays, iSelectedReturnDay);
}


function LibGoDates_repopMonthSelect(oSelect, iFirstMonth, iNumMonths, iSelectedMonth){
	var arrMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var ixMonth;
	oSelect.options.length = 0;
	for(ixMonth=0; ixMonth<iNumMonths; ixMonth++){
		oSelect.options[ixMonth] = new Option(arrMonths[(ixMonth+iFirstMonth)%12], (ixMonth+iFirstMonth)%12+1);
		if(iSelectedMonth == (ixMonth+iFirstMonth)%12){
			oSelect.selectedIndex = ixMonth;
		}
	}
}


function LibGoDates_RepopulateDaySelect(oSelect, iStartDay, iEndDay, iSelectedDay){
	var ixDay;
	oSelect.options.length = 0;
	for(ixDay=0; ixDay < iEndDay; ixDay++){
		oSelect.options[ixDay] = new Option(ixDay+iStartDay,ixDay+iStartDay);
		var iToss = ixDay+iStartDay
		if( iSelectedDay == ixDay+iStartDay ){
			oSelect.selectedIndex = ixDay;
		}
	}
}


function LibGoDates_FixYear(iSelectedYear, iSelectedMonth, iSelectedDay)
{
	var dNow = new Date();
	var aConfig = LibGoDates_getConfigArray();
	var dFirstAllowableDate = LibGoDates_AddDays(dNow, aConfig[0]);
	iSelectedMonth--; //Jan=0

	if( dFirstAllowableDate.getFullYear() >= iSelectedYear ){
		if( dFirstAllowableDate.getMonth() - iSelectedMonth > 0 ){
			iSelectedYear++;
		}else if(dFirstAllowableDate.getMonth() - iSelectedMonth == 0 ){
			var dLastAllowableDate = LibGoDates_AddDays(dNow, 365 - 2 - aConfig[0]);
			if( dLastAllowableDate.getDate() - iSelectedDay > 0 ){
				iSelectedYear++;
			}
		}
	}
	return iSelectedYear;
}


function LibGoDates_getConfigArray()
{

	var aConfig = new Array();

	/* Spec:
		0. First Allowable Depart Date: Days to add to dNow();
		1. Selected Depart Date when starting over: Days to add to dNow();
		2. First Allowable Return Date: Days to add to (0.) above
		3. Selected Return Date when starting over or resetting: Days to add to dNow();
		4. Maximum trip duration: Days
	*/

	switch(0)
	{
		case 0: //Gogo
			aConfig = [3, 21, 2, 3, 30]; //FP#16840
			break;
		case 1: //Liberty
			aConfig = [8, 21, 2, 3, 30];
			break;
		default:
			aConfig = [8, 21, 2, 3, 30];
			break;
			
	}
	if(document.frm6 != null)
	{
		if(document.frm6.SiteID != null)
		{
	    		if (document.frm6.SiteID.value == 'INB' || document.frm6.SiteID.value == 'IN1')
	    		{
				aConfig = [4, 21, 2, 3, 30];
	    		}
		}
	}	return aConfig;
}
