// dateLib.js
// Common functions used for dates

// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 0 and 11, and the
// year in ccyy format.
function y2k(number) { return (number < 1000) ? number + 1900 : number; }


function ctest( )  { alert( "Alert me" ); }


function isDate(day, month, year)
{   var today = new Date();
    year = ((!year) ? y2k(today.getYear()) : year);
    if (month != 0)
    { month = ((!month) ? today.getMonth()-1 : month); }
    if (!day)
        return false
    var test = new Date(year, month, day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    return false;
}


function isValidDates( arday, armonth, aryear, dpday, dpmonth, dpyear )
{   var d1 = parseInt(arday.value);
    var m1 = parseInt(armonth.value);
    var y1 = parseInt(aryear.value);

    if ( ! isDate(d1, m1, y1) )
    {   alert( "The Arrival Date is invalid.  Please correct." );
        return false;
    }
    
    var d2 = parseInt(dpday.value);
    var m2 = parseInt(dpmonth.value);
    var y2 = parseInt(dpyear.value);

    if ( ! isDate(d2, m2, y2) )
    {   alert( "The Departure Date is invalid.\nPlease correct." );
        return false;
    }

    //Business Logic
    if (y2 == y1 && m2 == m1 && d2 == d1)
    {   alert( "Short stay.\nThe Arrival and Departure Dates are the same.\nPlease correct." );
        return false;
    }

    if ( y2 < y1 )
    {   alert( "You are ahead of your time.\nThe Departure date is earlier than the Arrival date.\nPlease correct." );
        return false;
    }
    if ( y2 == y1 && m2 < m1 )
    {   alert( "You are ahead of your time.\nThe Departure date is earlier than the Arrival date.\nPlease correct." );
        return false;
    }
    if ( y2 == y1 && m2 == m1 && d2 < d1 )
    {   alert( "You are ahead of your time.\nThe Departure date is earlier than the Arrival date.\nPlease correct." );
        return false;
    }

    var today = new Date();
    var aday = today.getDate();
    var amon = today.getMonth();
    var ayear = today.getFullYear();

    aday += 1;
    if ( aday > 28 )
    {
       if ( amon == 1 )             //February
        {
            if ((ayear % 4) == 0)   //Leap year
            {
                if (aday > 29)      //Leap year
                {   amon += 1;
                    aday -= 29;
                }
            }
            else
            {   amon += 1;
                aday -= 28;
            }
        }
        if ( amon == 3 || amon == 5 || amon == 8 || amon == 10 )
        {   amon += 1;
            aday -= 30;
        }
        if ( amon > 11 )
        {   amon = 0;
            ayear += 1;
        }
    }

    //Check if less than 2 days ahead

    return true;
}


// Sets the values in the date option lists
function create_Options()
{
//alert( "Running create_Options" );
	var now = new Date();
	var nowyear = now.getYear();
	if ( nowyear < 2000 )	{	nowyear += 1900;	}
	var nextyear = nowyear + 1;
 	var nowmonth = now.getMonth();
	var monthnames = new Array( "January", "February", "March", "April", "May", "June",
								"July", "August", "September", "October", "November", "December" );
	var nowday = now.getDate();

	for ( var d = 1; d < 32; d++ )
    {	document.forms.CheckDates.ARDsel.options[d-1] = new Option( d , d );
		document.forms.CheckDates.DPDsel.options[d-1] = new Option( d , d );
    }
    
	for ( var m = 0; m < 12; m++ )
    {	document.forms.CheckDates.ARMsel.options[m] = new Option( monthnames[m] , m );
		document.forms.CheckDates.DPMsel.options[m] = new Option( monthnames[m] , m );
    }

	// Set Arrival Year options
	with( document.forms.CheckDates.ARYsel )
	{	options[0] = new Option( nowyear , nowyear );
		if ( nowmonth == 11 && nowday > 23 )
		{	options[0].text = nextyear;	options[0].value = nextyear;	}
		else
		{	if ( nowmonth >= 8 )		options[1] = new Option( nextyear , nextyear );		}
	}

	// Set Departure Year options
	with( document.forms.CheckDates.DPYsel )
	{
		options[0] = new Option( nowyear , nowyear );
		if ( nowmonth == 11 && nowday > 30 )
		{	options[0].text = nextyear;	options[0].value = nextyear;	}
		else
		{	if ( nowmonth >= 8 )		options[1] = new Option( nextyear , nextyear );		}
	}
}


// Sets the values in the date option lists
function set_Options()
{
//alert( "Running set_Options" );
	var now = new Date();

	var nowyear = now.getYear();
	if ( nowyear < 2000 )	{	nowyear += 1900;	}
	var nextyear = nowyear + 1;

 	var nowmonth = now.getMonth();
	var nowday = now.getDate();
	//monthdays is a 1 based array.  getMonth() is zero based.
	var monthdays  = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

	// This leap year code may not work  :(
	if ( nowyear % 4 == 0 )
		monthdays[2] = 29;

	// Set selection picks
	var pickday   = nowday + 2;
	var pickmonth = nowmonth;
	var pickyear  = nowyear;
//alert( "nowday = " + nowday + " ,nowmonth = " + nowmonth + " ,nowyear = " + nowyear );
//alert( "pickday = " + pickday + " ,pickmonth = " + pickmonth + " ,pickyear = " + pickyear );
	if ( pickday > monthdays[pickmonth+1] )
	{
		pickday = pickday - monthdays[pickmonth+1];
		pickmonth += 1;
		if ( pickmonth > 12 )
		{
			pickmonth = 0;
			pickyear += 1;
		}
	}
//alert( "arrival pickday = " + pickday + " ,pickmonth = " + pickmonth + " ,pickyear = " + pickyear );
	document.forms.CheckDates.ARDsel.selectedIndex = pickday-1; //drop down list is zero based.
	document.forms.CheckDates.ARMsel.selectedIndex = pickmonth;
	document.forms.CheckDates.ARYsel.selectedIndex = 0;

    pickday += 1;   //Add one to departure date
//alert ( "monthdays in pickmonth = " + monthdays[pickmonth+1] );
	if ( pickday > monthdays[pickmonth+1] )
	{
		pickday = pickday - monthdays[pickmonth+1];
		pickmonth += 1;
		if ( pickmonth > 11 )
		{
			pickmonth = 0;
			pickyear += 1;
		}
	}
//alert( "departure pickday = " + pickday + " ,pickmonth = " + pickmonth + " ,pickyear = " + pickyear );
	document.forms.CheckDates.DPDsel.selectedIndex = pickday-1; //drop down list is zero based.
	document.forms.CheckDates.DPMsel.selectedIndex = pickmonth;
	document.forms.CheckDates.DPYsel.selectedIndex = 0;

}





