﻿// JScript File

function CheckLeapYear(year)
{
	year = parseInt(year);
	
	if(year%4 == 0)
	{
		if(year%100 != 0)
		{
			return true;
		}
		else
		{
			if(year%400 == 0)
			{
				return true;
			}
			else
			{
				return false;
		    }
		}
	}
	else
	{
        return false;
    }
}

function InitializeDaysInMonth(year)
{
    var isLeapYear = CheckLeapYear(year);
    if (isLeapYear == true)
    {
        // if month is 4, 6, 9 or 11, daysInMonth = 30. if month is 2, daysInMonth = 29. otherwise daysInMonth = 31
        var daysInMonth = (month==4 || month==6 || month==9 || month==11) ? 30 : (month==2) ? 29 : 31 
    }
    else
    {
        // if month is 4, 6, 9 or 11, daysInMonth = 30. if month is 2, daysInMonth = 28. otherwise daysInMonth = 31
        var daysInMonth = (month==4 || month==6 || month==9 || month==11) ? 30 : (month==2) ? 28 : 31 
    }
    return daysInMonth;
}

function InitializeMonthArray()
{     
    var months = new Array(); // array holding months of the year
    months[1] = "1月"; // start array index at 1 for easy reading
    months[2] = "2月";
    months[3] = "3月";
    months[4] = "4月";
    months[5] = "5月";
    months[6] = "6月";
    months[7] = "7月";
    months[8] = "8月";
    months[9] = "9月";
    months[10] = "10月";
    months[11] = "11月";
    months[12] = "12月";
    return months;
}

function correctDate(form, menu)
{ 
    var changed = "year";
    if (menu.value >= 1 && menu.value <= 12)
    {
        changed = "month";
    }

	// 'form' is a reference to the form
	// 'menu' is a reference to the 1st or 3rd menu in that form. 
	// both are passed from the onChange event handler of the 1st select menu which invokes this function
	// loop through the form's elements for the purpose of creating a variable representing the 2nd and 3rd menus   
    for(i = 0; i < form.elements.length; i++)
    {
        if(form.elements[i] == menu)
        { 
            if (changed == "year")
            {
                var monthMenu = form.elements[i-2];
                var dateMenu = form.elements[i-1];
                var yearMenu = form.elements[i]; 
            }
            else //changed == "year"
            {
                var monthMenu = form.elements[i];
                var dateMenu = form.elements[i+1];
                var yearMenu = form.elements[i+2];
            }
            break;
        }
    }
    
    // Change date options based on the month
    correctDateOptionsByMonth(monthMenu, dateMenu, yearMenu);
    
    // display the last day of selected arrival month
    if (monthMenu.name == "arrivalMonth")
    {
        dateMenu.selectedIndex = 0; 
    }
} 

function correctDateOptionsByMonth(monthMenu, dateMenu, yearMenu)
{
    var formerlength = dateMenu.options.length; //formerlength is number of options currently in menu
    var thelength; // thelength is number of options the menu will have, based on the month selected
    
    // if 2nd option (feb) is selected,
    if(monthMenu.options[1].selected)
    { 
        if (CheckLeapYear(yearMenu.value) == true)
        {
            thelength = 29; // thelength is 29
        }
        else
        {
            thelength = 28; // thelength is 28
        }
    }
    // if 3rd (apr), 5th (jun), 8th (sep) or 10th (nov) option is selected,
    else if(monthMenu.options[3].selected || monthMenu.options[5].selected || monthMenu.options[8].selected || monthMenu.options[10].selected)
    {
        thelength = 30; // thelength is 30
    }
    else
    {
        thelength = 31; //otherwise thelength is 31
    }
    dateMenu.options.length = thelength; //re-set the number of options displayed in dateMenu

    for(var i = formerlength; i < thelength; i++)
    { 
        // if number of current options is less than options to be displayed, write more options
        dateMenu.options[i].value = i+1; // give each option a value and text label
        dateMenu.options[i].text = i+1;
    }
}

// Change departure date 1 day later than the arrival date
function correctDepartureDate(form)
{
    var arrivalMonth = document.form.arrivalMonth;
    var arrivalDay = document.form.arrivalDay;
    var arrivalYear = document.form.arrivalYear;
    
    var departureMonth = document.form.departureMonth;
    var departureMonthFormerIndex = departureMonth.selectedIndex;
    var departureYear = document.form.departureYear;
    var departureYearFormerIndex = departureYear.selectedIndex;
    var departureDay = document.form.departureDay;

    if (arrivalDay.selectedIndex == 0) // just corrected date
    {
        departureMonth.selectedIndex = arrivalMonth.selectedIndex;
        departureYear.selectedIndex = arrivalYear.selectedIndex;
        departureDay.selectedIndex = 1;
    }
    else
    {
        if (arrivalDay.value == arrivalDay.options.length) // last day of the arrival month is selected
        {
            if (arrivalMonth.value == 12) // last month of the arrival year is selected
            {
                var arrivalYearFormerLength = arrivalYear.options.length;
                if (arrivalYear.value == arrivalYear.options[arrivalYearFormerLength-1].value) // last year index is selected, add another year
                {
                    departureYear.options.length = arrivalYearFormerLength + 1;
                    // give an incremented year value and text
                    var departureYearValue = arrivalYear.value;
                    departureYearValue++;
                    departureYear.options[arrivalYearFormerLength].text = departureYearValue;
                    departureYear.options[arrivalYearFormerLength].value = departureYearValue;
                }
                departureMonth.selectedIndex = 0;
                departureYear.selectedIndex = arrivalYear.selectedIndex + 1;
                departureDay.selectedIndex = 0;                
            }
            else
            {
                departureMonth.selectedIndex = arrivalMonth.selectedIndex + 1;
                departureYear.selectedIndex = arrivalYear.selectedIndex;
                departureDay.selectedIndex = 0;
            }
        }
        else
        {
            departureMonth.selectedIndex = arrivalMonth.selectedIndex;
            departureYear.selectedIndex = arrivalYear.selectedIndex;
            departureDay.selectedIndex = arrivalDay.selectedIndex + 1;
        }
    }
    
    // Change departure date options based on the departure month
    if (departureMonthFormerIndex != departureMonth.selectedIndex 
        || departureYearFormerIndex != departureYear.selectedIndex)
    {
        correctDateOptionsByMonth(departureMonth, departureDay, departureYear);
    }
}