//************************************************************************
// Global variables
//************************************************************************
var submitting = false;
//************************************************************************
// Function:  initDates
// Purpose:   Initialize Arrival Date and Departure Date controls.
// Input:     language - language used by the WEB page.
//            locale - locale used by the WEB page.
// Output:    None
//************************************************************************
function initDates (language, locale)
{
// BUGBUG Check this code...
	// Check all date code for leap year...   
    var datef = document.arrivalDate;
    var myDate = new Date ();
    var month = myDate.getMonth () + 1;
    var day = myDate.getDate ();
    var year = getFullYear (myDate);
    var index;

    // Initialize arrival date.
    // Initalize form year.
    for (index = 0; (index < datef.DATERANGESTART_YEAR.length) && (datef.DATERANGESTART_YEAR.options[index].value != year); index++);
    datef.DATERANGESTART_YEAR.options[index].selected = true;
    // Initalize form month.
    for (index = 0; (index < datef.DATERANGESTART_MONTH.length) && (datef.DATERANGESTART_MONTH.options[index].value != month); index++);
    datef.DATERANGESTART_MONTH.options[index].selected = true;
    // Initalize form day.
    for (index = 0; (index < datef.DATERANGESTART_DAY.length) && (datef.DATERANGESTART_DAY.options[index].value != day); index++);
    datef.DATERANGESTART_DAY.options[index].selected = true;
    // Initalize Day of the week.
    datef.DATERANGESTART_DOW.value = getDayOfWeek (buildCRSDate (year, month, day));
	// other dropdowns are inited in XSL.
}

//************************************************************************
// Function:  validateDay
// Purpose:   Ensure that a selected day is valid. Example 2/31 is an 
//            invalid day. If an invalid date is selected, select the
//            previous valid day.
// Input:     yearCtrl - Year dropdown.
//            monthCtrl - Month dropdown.
//            dayCtrl - Day dropdown.
// Output:    None
//************************************************************************

function validateDay (yearCtrl, monthCtrl, dayCtrl)
{
	var datef = document.arrivalDate;
	eval ("var year = parseInt (datef." + yearCtrl + ".options[datef." + yearCtrl + ".selectedIndex].value, 10)");
	eval ("var month = parseInt (datef." + monthCtrl + ".options[datef." + monthCtrl + ".selectedIndex].value, 10)");
	eval ("var day = parseInt (datef." + dayCtrl + ".options[datef." + dayCtrl + ".selectedIndex].value, 10)");
	if (day > (maxDay = getDaysInMonth (month, year)))
	{
		for (index = 0; (index < eval ("datef." + dayCtrl + ".length")) && (eval ("datef." + dayCtrl + ".options[" + index+ "].value != " + maxDay)); index++);
			eval ("datef." + dayCtrl + ".options[" + index + "].selected = true");
	}
}

//************************************************************************
// Function:  updateDOW
// Purpose:   Set the Day of the Week to the valid string (Monday, Tuesday, etc.)
// Input:     yearCtrl - Year dropdown.
//            monthCtrl - Month dropdown.
//            dayCtrl - Day dropdown.
//            dowCtrl - Day of the Week Control - Control to set.
//            language - language used by the WEB page.
//            locale - locale used by the WEB page.
// Output:    None
//************************************************************************

function updateDOW (yearCtrl, monthCtrl, dayCtrl, dowCtrl, language, locale)
{
	var datef = document.arrivalDate;
	eval ("var year = parseInt (datef." + yearCtrl + ".options[datef." + yearCtrl + ".selectedIndex].value, 10)");
	eval ("var month = parseInt (datef." + monthCtrl + ".options[datef." + monthCtrl + ".selectedIndex].value, 10)");
	eval ("var day = parseInt (datef." + dayCtrl + ".options[datef." + dayCtrl + ".selectedIndex].value, 10)");
	eval ("datef." + dowCtrl + ".value = getDayOfWeek (buildCRSDate (year, month, day))");
}

//************************************************************************
// Function:  updateDATERANGEEND
// Purpose:   Update the Departure Date to the Arrival Date + 1.
// Input:     language - language used by the WEB page.
//            locale - locale used by the WEB page.
// Output:    None
//************************************************************************

function updateDATERANGEEND (language, locale)
{
	var datef = document.arrivalDate;
	// Get the current arrival date.
	var startYear = parseInt (datef.DATERANGESTART_YEAR.options[datef.DATERANGESTART_YEAR.selectedIndex].value, 10);
	var startMonth = parseInt (datef.DATERANGESTART_MONTH.options[datef.DATERANGESTART_MONTH.selectedIndex].value, 10);
	var startDay = parseInt (datef.DATERANGESTART_DAY.options[datef.DATERANGESTART_DAY.selectedIndex].value, 10);
	var startDate = new Date (startYear, startMonth - 1, startDay);
}

//************************************************************************
// Function:  submitNextFormIfValid
// Purpose:   Validate form variables and submit form variables to the 
//            booking engine.
// Input:     None
// Output:    None
//************************************************************************

function submitNextFormIfValid () 
{
	if (!submitting)
	{
		submitting = true;
		var datef = document.arrivalDate;
		var nextf = document.nextForm;
		// Format dates as YYYYMMDD
		// Arrival Date
		var startYear = parseInt(datef.DATERANGESTART_YEAR.options[datef.DATERANGESTART_YEAR.selectedIndex].value, 10);
		var startMonth = parseInt(datef.DATERANGESTART_MONTH.options[datef.DATERANGESTART_MONTH.selectedIndex].value, 10);
		var startDay =  parseInt(datef.DATERANGESTART_DAY.options[datef.DATERANGESTART_DAY.selectedIndex].value, 10);
	}
}

var monthName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");  
var dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

//************************************************************************
// Function:  getFullYear
// Purpose:   Get the four digit year given a two digit year.
// Input:     date - date to get the four digit year for.
// Output:    Four digit year.
//
// Function required because NE 3.01 does not support date.getFullYear.
// Code copied from O'Reilly - JavaScript The Definitive Guide
//************************************************************************
function getFullYear (date)
{
	var year = date.getYear ();
	if (year < 1000) year += 1900;
	return (year);
}

//************************************************************************
// Function:  getDayOfWeek
// Purpose:   Get the day of the week for a given date.
// Input:     date - date to get the four digit year for.
// Output:    String containing the Day of the Week (Monday, Tuesday, etc).
//************************************************************************

function getDayOfWeek (crsDate)
{
	var myDate = new Date (crsDate.substring (0, 4), parseInt(crsDate.substring (4, 6), 10) - 1,  parseInt(crsDate.substring (6, 8), 10));
	return (dayName [myDate.getDay ()]);    
}

//************************************************************************
// Function:  getMonth
// Purpose:   Get the month description for a given date.
// Input:     month - month to get the description for.
// Output:    String containing the month (January, February, etc).
//************************************************************************

function getMonth (month)
{
	return (monthName [month - 1]);    
}

//************************************************************************
// Function:  formatDateDisplay
// Purpose:   Format a date in the form of January 1, 1999 Tuesday
// Input:     crsDate - date to format
//            language - language used by the WEB page.
//            locale - locale used by the WEB page.
// Output:    String containing the formatted date.
//************************************************************************

function formatDisplayDate (crsDate, language, locale)
{
	var monthStr = getMonth (parseInt (crsDate.substring (4, 6), 10));
    return (monthStr  + " " + crsDate.substring (6, 8) + "," + crsDate.substring (0, 4) + " " + getDayOfWeek (crsDate));
}

//************************************************************************
// Function:  formatDateDisplayShort
// Purpose:   Format a date in the form of January 1
// Input:     crsDate - date to format
//            language - language used by the WEB page.
//            locale - locale used by the WEB page.
// Output:    String containing the formatted date.
//************************************************************************

function formatDisplayDateShort (crsDate, language, locale)
{
	monthStr = getMonth (parseInt (crsDate.substring (4, 6), 10));
	return (monthStr  + " " + crsDate.substring (6, 8));
}

//************************************************************************
// Function:  buildCRSDate
// Purpose:   Format a date in the form YYYYMMDD
// Input:     year
//            month
//            day
// Output:    String containing the formatted date.
//************************************************************************
                                        
function buildCRSDate (year, month, day)
{
    if (day < 10)
    {
		dayString = "0" + day;
    }
    else
    {
        dayString = new String (day);
    }

    if (month < 10)
    {
        monthString = "0" + month;
    }
    else
    {
        monthString = new String(month);
    }
    return (year + monthString + dayString);
}

//************************************************************************
// Function:  isLeapYear
// Purpose:   Determine if a year is a leap year.
// Input:     yrStr - year to determine leapness..
// Output:    true - if the year is a leap year.
//            false - otherwise.
//************************************************************************
function isLeapYear(yrStr) 
{
    var leapYear=false; 
    // every fourth year is a leap year
    if ((parseInt(yrStr, 10)%4) == 0)
    {
    	leapYear=true;
    }
    return leapYear;
}

//************************************************************************
// Function:  getDaysInMonth
// Purpose:   Determine the number of days in a month.
// Input:     mthIdx - month 
//            yrStr - year 
// Output:    true - if the year is a leap year.
//            false - otherwise.
//************************************************************************
function getDaysInMonth(mthIdx, YrStr) 
{
    // all the rest have 31 
    var maxDays = 31 
    // expect Feb. (of course)
    if (mthIdx==2)
    {
        if (isLeapYear(YrStr))
        {
            maxDays=29;
        }
        else
        {
            maxDays=28;
        }
    }
    // thirty days hath...
    if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11)
    {
        maxDays=30;
    }
    return maxDays;
}

var calendarCtrlStrings = new Array ("Next","Previous","Cancel");  

function initDatesStub (language, locale)
{
	initDates (language, locale);
}

function addLoadEvent(func)
{   
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
       		func();
		}  
	}
}

addLoadEvent(initDatesStub);
addLoadEvent(function(){}); 


