var verifyValidDate = true;
var AddDays = 331;  //# of days ahead
MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
function isLeapYear (Year) {
        if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
                return true;
        } else {
                return false;
        }
}
// start with current date
var CurrentDate = new Date(); // mm/dd/yyyy
CurMonth = CurrentDate.getMonth(); // mm
CurDay = CurrentDate.getDate(); // dd
CurYear = CurrentDate.getFullYear(); // yyyy
if(isLeapYear(CurYear)){
        MonthDays[1] = 29; // adjust # of days in February if it is a leap year
}
days = MonthDays[CurMonth]; // # of days in current month
// get new date info
CurDay += AddDays; // add # of days ahead to dd
while(verifyValidDate){         
        if (CurDay > days) { // if new dd is greater than # of days in current month then
                CurMonth = CurMonth + 1; // new mm
                if (CurMonth == 12) {
CurMonth = 0; // correct new mm to January if mm was December
CurYear = CurYear + 1 // new yy
if(isLeapYear(CurYear)){
        MonthDays[1] = 29; // # of days in February is 29 if a leap year
} else {
        MonthDays[1] = 28; // # of days in February is 28 otherwise
}
                }
                CurDay = CurDay - days; // new dd
                days = MonthDays[CurMonth]; // # of days in new month
        }else{
                verifyValidDate = false;
        }
}
var TheDate = new Date((CurMonth+1) + '/' + CurDay + '/' + CurYear);
function getMonthNumber(mName){
        if (mName == "Jan"){
                return "1";
        }else if (mName == "Feb"){
                return "2";
        }else if (mName == "Mar"){
                return "3";
        }else if (mName == "Apr"){
                return "4";
        }else if (mName == "May"){
                return "5";
        }else if (mName == "Jun"){
                return "6";
        }else if (mName == "Jul"){
                return "7";
        }else if (mName == "Aug"){
                return "8";
        }else if (mName == "Sep"){
                return "9";
        }else if (mName == "Oct"){
                return "10";
        }else if (mName == "Nov"){
                return "11";
        }else{
                return "12";
        }
}
function getFullMonthName(mName){
        if (mName == "Jan"){
                return "January";
        }else if (mName == "Feb"){
                return "February";
        }else if (mName == "Mar"){
                return "March";
        }else if (mName == "Apr"){
                return "April";
        }else if (mName == "May"){
                return "May";
        }else if (mName == "Jun"){
                return "June";
        }else if (mName == "Jul"){
                return "July";
        }else if (mName == "Aug"){
                return "August";
        }else if (mName == "Sep"){
                return "September";
        }else if (mName == "Oct"){
                return "October";
        }else if (mName == "Nov"){
                return "November";
        }else{
                return "December";
        }
}
function ChkRequest() {
        var thisYear = CurrentDate.getFullYear();
        tmpCurrentDate = new Date((CurrentDate.getMonth()+1) + '/' + CurrentDate.getDate() + '/' + CurrentDate.getFullYear());
        numDaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        selectedDepMonth = getMonthNumber(document.CarSearch.pickupMonth.value);
        selectedDepDay = document.CarSearch.pickupDay.value;
        selectedRetMonth = getMonthNumber(document.CarSearch.dropoffMonth.value);
        selectedRetDay = document.CarSearch.dropoffDay.value;
        if (new Date(selectedDepMonth + '/' + selectedDepDay + '/' + thisYear) < tmpCurrentDate){
                selectedDepYear = thisYear + 1;
        }else{
                selectedDepYear = thisYear;
        }
        if(isLeapYear(selectedDepYear)){
                numDaysInMonth[1] = 29; // adjust # of days in February if it is a leap year
        }
        maxDaysDepMonth = numDaysInMonth[selectedDepMonth-1];
        if(selectedDepDay > maxDaysDepMonth){
                if(selectedDepMonth == 2){
alert('Pick-up Date selected is not a valid date. There are only ' + maxDaysDepMonth + ' days in February ' + selectedDepYear + '. Please select a valid Pick-up Date.');
                }else{
alert('Pick-up Date selected is not a valid date. There are only ' + maxDaysDepMonth + ' days in ' + getFullMonthName(document.CarSearch.pickupMonth.value) + '. Please select a valid Pick-up Date.');
                }
                document.CarSearch.pickupDay.focus();
                return false;
        }
        selectedDepDate = new Date(selectedDepMonth + '/' + selectedDepDay + '/' + selectedDepYear);
        if (new Date(selectedRetMonth + '/' + selectedRetDay + '/' + thisYear) < tmpCurrentDate){
                selectedRetYear = thisYear + 1;
        }else{
                selectedRetYear = thisYear;
        }
        if(isLeapYear(selectedRetYear)){
                numDaysInMonth[1] = 29; // adjust # of days in February if it is a leap year
        }else{
                numDaysInMonth[1] = 28; // default back to 28 days if not a leap year
        }
        maxDaysRetMonth = numDaysInMonth[selectedRetMonth-1];
        if(selectedRetDay > maxDaysRetMonth){
                if(selectedRetMonth == 2){
alert('Drop-off Date selected is not a valid date. There are only ' + maxDaysRetMonth + ' days in February ' + selectedRetYear + '. Please select a valid Drop-off Date.');
                }else{
alert('Drop-off Date selected is not a valid date. There are only ' + maxDaysRetMonth + ' days in ' + getFullMonthName(document.CarSearch.dropoffMonth.value) + '. Please select a valid Drop-off Date.');
                }
                document.CarSearch.dropoffDay.focus();
                return false;
        }
        selectedRetDate = new Date(selectedRetMonth + '/' + selectedRetDay + '/' + selectedRetYear);
        if (!(document.CarSearch.pickupCity.value.length >= 3) || (document.CarSearch.pickupCity.value == "")) {
                alert('Please enter Destination airport code, city, or city, state.');
    document.CarSearch.pickupCity.focus();
    return false;
        }
        if (selectedDepDate < tmpCurrentDate || selectedDepDate > TheDate) {
                alert('Pick-up Date cannot be in the past or more than 331 days in the future. Please select a Pick-up Date between ' + (CurrentDate.getMonth()+1) + '/' + CurrentDate.getDate() + '/' + CurrentDate.getFullYear() + ' and ' + (TheDate.getMonth()+1) + '/' + TheDate.getDate() + '/' + TheDate.getFullYear() + '.');
                document.CarSearch.pickupDay.focus();
                return false;
        }
        if (selectedRetDate < tmpCurrentDate || selectedRetDate > TheDate) {
                alert('Drop-off Date cannot be in the past or more than 331 days in the future. Please select a Drop-off Date between ' + (CurrentDate.getMonth()+1) + '/' + CurrentDate.getDate() + '/' + CurrentDate.getFullYear() + ' and ' + (TheDate.getMonth()+1) + '/' + TheDate.getDate() + '/' + TheDate.getFullYear() + '.');
                document.CarSearch.dropoffDay.focus();
                return false;
        }
        if (selectedRetDate < selectedDepDate) {
                alert('Drop-off Date cannot be prior to the Pick-up Date. Please select a Drop-off Date between ' + (selectedDepDate.getMonth()+1) + '/' + selectedDepDate.getDate() + '/' + selectedDepDate.getFullYear() + ' and ' + (TheDate.getMonth()+1) + '/' + TheDate.getDate() + '/' + TheDate.getFullYear() + '.');
                document.CarSearch.dropoffDay.focus();
                return false;
        }
        if ((selectedRetDate.getMonth() + '/' + selectedRetDate.getDate() + '/' + selectedRetDate.getFullYear()) == (selectedDepDate.getMonth() + '/' + selectedDepDate.getDate() + '/' + selectedDepDate.getFullYear())) {
                alert('Drop-off Date cannot be the same as the Pick-up Date. Please select a Drop-off Date between ' + (selectedDepDate.getMonth()+1) + '/' + (selectedDepDate.getDate()+1) + '/' + selectedDepDate.getFullYear() + ' and ' + (TheDate.getMonth()+1) + '/' + TheDate.getDate() + '/' + TheDate.getFullYear() + '.');
                document.CarSearch.dropoffDay.focus();
                return false;
        }
}

/*
automatically set today's date in teh search form
*/

var thisDate = new Date();
var thisMonth = thisDate.getMonth();
var thisDay = thisDate.getDate();
var pm = document.getElementById('pickupMonth');
var pd = document.getElementById('pickupDay');
var dm = document.getElementById('dropoffMonth');
var dd = document.getElementById('dropoffDay');
if (pm) { pm[thisMonth].selected = true; }
if (pd) { pd[thisDay].selected = true; }
if (dm) { dm[thisMonth].selected = true; }
if (dd) { dd[thisDay].selected = true; }
