function TravelDateValidator( dateObjFrom, dateObjTo )
{
    this.curDate = new Date();
    this.daysTillDeparture = 7;
    this.maxTravelWindow = 28;
    //Instance Variables
    this.num_Nights = 3;
    
    this.travel_From = dateObjFrom;
    this.travel_To = dateObjTo;
    
    this.restriction_From = new Date( this.curDate.getFullYear(), this.curDate.getMonth(), parseInt( this.curDate.getDate() ) + this.daysTillDeparture );
    this.restriction_To = new Date( this.travel_From.getFullYear(), this.travel_From.getMonth(), parseInt(this.travel_From.getDate()) + this.maxTravelWindow );
    this.errMessage = "";
    
    //instance methods
    this.getNumberOfNights = getNumberOfNights;
    this.setNumberOfNights = setNumberOfNights;
    this.setRestrictedFromDate = setRestrictedFromDate;
    this.getRestrictedFromDate = getRestrictedFromDate;
    this.setRestrictedToDate = setRestrictedToDate;
    this.getRestrictedToDate = getRestrictedToDate;
    
    this.getTravelFrom = getTravelFrom;
    this.setTravelFrom = setTravelFrom;
    this.getTravelTo = getTravelTo;
    this.setTravelTo = setTravelTo;
    this.getErrorMessage = getErrorMessage;
    this.IsValidTravelDates = IsValidTravelDates;
    
    this.toString = toString;
}

function toString()
{
    var stringOutput = "Travel Date Validator:"
                     + "\nTravel From Date = " + this.travel_From 
                     + "\nTravel To Date = " + this.travel_To
                     + "\nRestriction From Date = " + this.restriction_From
                     + "\nRestriction To Date = " + this.restriction_To
                     + "\nNumber Of Nights = " + this.num_Nights;

    return stringOutput;
}                    

function getErrorMessage()
{
    return this.errMessage;
}

function getTravelFrom()
{
    return this.travel_From;
}

function getTravelTo()
{
    return this.travel_To;
}
function setTravelFrom( monthInt, dateInt, yearInt  )
{
    if ( parseInt( monthInt ) )
    {
        if ( parseInt(monthInt) < 0 || parseInt(monthInt) > 11)
        {
            monthInt = parseInt( this.curDate.getMonth() );
        }
    }
    else
    {
        monthInt = parseInt( this.curDate.getMonth() );    
    }
    if ( parseInt( dateInt ) )
    {
        if ( parseInt(dateInt) < 1 || parseInt(dateInt) > 32)
        {
            dateInt = parseInt( this.curDate.getDate() );
        }
    }  
    else
    {
        dateInt = parseInt( this.curDate.getDate() );    
    }
    if ( parseInt( yearInt ) )
    {
        if ( parseInt(yearInt) < parseInt(this.curDate.getFullYear()) )
        {
            yearInt = parseInt( this.curDate.getFullYear() );
        }
    }        
    else
    {
        yearInt = parseInt( this.curDate.getFullYear() );
    }
    this.travel_From = new Date( yearInt, monthInt, dateInt);

}
function setTravelTo( monthInt, dateInt, yearInt  )
{
    if ( parseInt( monthInt ) )
    {
        if ( parseInt(monthInt) < 0 || parseInt(monthInt) > 11)
        {
            monthInt = parseInt( this.curDate.getMonth() );
        }
    }
    else
    {
        monthInt = parseInt( this.curDate.getMonth() );    
    }
    if ( parseInt( dateInt ) )
    {
        if ( parseInt(dateInt) < 1 || parseInt(dateInt) > 32)
        {
            dateInt = parseInt( this.curDate.getDate() );
        }
    }  
    else
    {
        dateInt = parseInt( this.curDate.getDate() );    
    }
    if ( parseInt( yearInt ) )
    {
        if ( parseInt(yearInt) < parseInt(today.getFullYear()) )
        {
            yearInt = parseInt( this.curDate.getFullYear() );
        }
    }        
    else
    {
        yearInt = parseInt( this.curDate.getFullYear() );
    }
    this.travel_To = new Date( yearInt, monthInt, dateInt);
}


function setNumberOfNights( paramInt )
{
    if ( !parseInt(paramInt) ) 
    {
        this.num_Nights = 3;
    }
    else
    {
        if ( parseInt(paramInt) > 3  )
        {
            this.num_Nights = parseInt(paramInt);
        }
        else
        {
             this.num_Nights = 3;
        }
    }
}

function getNumberOfNights()
{  
    return this.num_Nights;
}

function getRestrictedFromDate()
{
    return this.restriction_From;
}

function setRestrictedFromDate( monthInt, dateInt, yearInt  )
{
    if ( parseInt( monthInt ) )
    {
        if ( parseInt(monthInt) < 0 || parseInt(monthInt) > 11)
        {
            monthInt = parseInt( this.curDate.getMonth() );
        }
    }
    else
    {
        monthInt = parseInt( this.curDate.getMonth() );    
    }
    if ( parseInt( dateInt ) )
    {
        if ( parseInt(dateInt) < 1 || parseInt(dateInt) > 32)
        {
            dateInt = parseInt( this.curDate.getDate() );
        }
    }  
    else
    {
        dateInt = parseInt( this.curDate.getDate() );    
    }
    if ( parseInt( yearInt ) )
    {
        if ( parseInt(yearInt) < parseInt(this.curDate.getFullYear()) )
        {
            yearInt = parseInt( this.curDate.getFullYear() );
        }
    }        
    else
    {
        yearInt = parseInt( this.curDate.getFullYear() );
    }
    this.restriction_From = new Date( yearInt, monthInt, dateInt);
}


function getRestrictedToDate()
{
    return this.restriction_To;
}

function setRestrictedToDate( monthInt, dateInt, yearInt  )
{
    
    if ( parseInt( monthInt ) )
    {
        if ( parseInt(monthInt) < 0 || parseInt(monthInt) > 11)
        {
            monthInt = parseInt( this.curDate.getMonth() );
        }
    }
    else
    {
        monthInt = parseInt( this.curDate.getMonth() );    
    }
    if ( parseInt( dateInt ) )
    {
        if ( parseInt(dateInt) < 1 || parseInt(dateInt) > 32)
        {
            dateInt = parseInt( this.curDate.getDate() );
        }
    }  
    else
    {
        dateInt = parseInt( this.curDate.getDate() );    
    }
    if ( parseInt( yearInt ) )
    {
        if ( parseInt(yearInt) < parseInt(this.curDate.getFullYear()) )
        {
            yearInt = parseInt( this.curDate.getFullYear() );
        }
    }        
    else
    {
        yearInt = parseInt( this.curDate.getFullYear() );
    }
    this.restriction_To = new Date( yearInt, monthInt, dateInt + this.num_Nights );
}

function IsValidTravelDates( )
{
    var ONE_DAY = 1000 * 60 * 60 * 24;  
    var rdFrom =  parseInt(this.restriction_From.getTime());
    var rdTo = parseInt(this.restriction_To.getTime());

    var from = parseInt(this.travel_From.getTime());
    var to = parseInt(this.travel_To.getTime());   
   
    if ( from - rdFrom < 0 )
    { //the travel from date falls below today's date
        this.errMessage = "Booking this package requires a travel date after " 
                         + ( parseInt(this.restriction_From.getMonth()) + 1 ) 
                         + "/" + this.restriction_From.getDate() 
                         + "/" + this.restriction_From.getFullYear()
                         +"\nPlease check your travel dates to continue.";
        return false;
    }
    if ( Math.round( (to - from)/ONE_DAY ) < this.num_Nights )
    {  //they have not selected the correct number of nights
        this.errMessage = "Booking this package requires a minimum stay of " + this.num_Nights + " nights."
                         +"\nPlease check your travel dates and length of stay to continue.";
        return false;
    }  
    
    if ( Math.round( (to - from)/ONE_DAY ) > this.maxTravelWindow )
    {  //they have not selected the correct number of nights
        this.errMessage = "Booking this package requires a stay that does not exceed " + this.maxTravelWindow + " nights."
                         +"\nPlease check your travel dates and length of stay to continue.";
        return false;
    }  

    if ( to - rdTo > 0 ) 
    { //The Travel-To date is larger than the restricted to date
        this.errMessage = "Booking this package requires less a travel date prior to "
                         + ( parseInt( this.restriction_To.getMonth() ) + 1 )
                         + "/" + this.restriction_To.getDate() 
                         + "/" + this.restriction_To.getFullYear()
                         +"\nPlease check your travel dates to continue.";
        return false;
    }
    return true;
}

/**
* Select Object with Month values from 1-12
* Parameters: monthSelect = Select Object with month values from 1-12
*             dateSelect =  Select Object with dates from 1 - [28|29|30|31]
* Returns: No value
* REQUIRES IMPLEMENTATION OF...
* A. getNumberOfNights(), which returns the number of nights for this trip
* B. 
*/
function changeDates( monthSelect, dateSelect )
{
    var todaysDate = new Date();    
    var selectedMonth = parseInt( monthSelect.options[ monthSelect.selectedIndex ].value - 1 );
    var selectedDate =  parseInt( dateSelect.options[ dateSelect.selectedIndex ].value );    
    var selectedYear = parseInt( todaysDate.getFullYear() );   
    var num_Nights = 3;
    
    if ( parseInt(todaysDate.getMonth()) - selectedMonth > 0 )
    { //We are in a month that has already passed.
        selectedYear++;   
    }

    var selectedDateObj = new Date(selectedYear, selectedMonth, selectedDate);
    var arrayOfDateOptions = getDaysPerMonthInOptions(selectedDateObj);

    dateSelect.length = 0;
    for (var arrayIndex = 0; arrayIndex < arrayOfDateOptions.length; arrayIndex++ )
    {
        dateSelect.length = arrayIndex;
        dateSelect.options[ arrayIndex ] = arrayOfDateOptions[ arrayIndex ];
    }
	
	if ( monthSelect.name=="dMonth" )
    {
		document.MyVacation.aMonth.selectedIndex= selectedMonth;
		
		var selectedDateToObj = new Date(selectedYear, document.MyVacation.aMonth.value-1, selectedDate);
		var arrayOfDatetoOptions = getDaysPerMonthInOptions(selectedDateToObj);
		
		document.MyVacation.aday.length = 0;
		for (var arrayIndex = 0; arrayIndex < arrayOfDatetoOptions.length; arrayIndex++ )
		{
		    document.MyVacation.aday.length = arrayIndex;
		   document.MyVacation.aday.options[ arrayIndex ] = arrayOfDatetoOptions[ arrayIndex ];
		}
		if ( parseInt(selectedDate) + num_Nights <= dateSelect.length )
		{
			document.MyVacation.aday.selectedIndex= num_Nights
		}
    }
    else
    {
		if ( parseInt(selectedDate) <= dateSelect.length )
		{
			dateSelect.selectedIndex = selectedDate -1
		}
    }
    
    
}

function ChangeRDays(DaySelect, monthSelect)
{
	
    var selectedDate =  parseInt( DaySelect.options[ DaySelect.selectedIndex ].value );    
    var num_Nights = 3;
    
    if ( DaySelect.name=="dday" )
    {
		if ( parseInt(selectedDate) + num_Nights <= DaySelect.length )
		{
			document.MyVacation.aday.selectedIndex= DaySelect.selectedIndex+num_Nights;
    
		}
	}    

}

function isLeapYear( year )
{
    if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

/**
* Parameter: Date() object
* Returns the number of days in the month of the Date() object
*/
function getDaysPerMonth( dateObj )
{
    var answer = 30;
    switch( parseInt(dateObj.getMonth()) )
    {
        case 1:
            answer = 28;
            if ( isLeapYear(dateObj.getYear()) )
            {
                answer++;
            }
            break;
        case 0:
        case 2:
        case 4:        
        case 6:        
        case 9:        
        case 11:       
            answer = 31;
            break;
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
            answer = 30;
            break;            
        default:
            answer = 30;
            break;
    }   
    return answer;
}

/**
* Parameter: Date() object
* Returns an array of Option objects with the dates
*/
function getDaysPerMonthInOptions( dateObj )
{
    var daysPerMonth = parseInt( getDaysPerMonth( dateObj ) );
    var arrayOfDates = new Array();
    for (var index = 1; index <= daysPerMonth;  index++)
    {
        arrayOfDates.length = index;
        arrayOfDates[ index - 1 ] = new Option( index, index );
    }
    return arrayOfDates;
}

