var dateset = false;

// JavaScript dates are 0-based
// Budget.com month-year fields are 0-based
function setDropoffDate() {
    var selectedMonthYear = document.forms[0].pickupMonth;
    var selectedDay = document.forms[0].pickupDay;
    var setMonthYear = document.forms[0].dropoffMonth;
    var setDay = document.forms[0].dropoffDay;
    var puMonth = selectedMonthYear.options[selectedMonthYear.selectedIndex].value;
    var now = new Date();
    var puYear = now.getYear();
    if(puYear < 1000){ puYear += 1900 }
    var puDay = selectedDay.options[selectedDay.selectedIndex].value;

    // take leap-year into account
    if(puMonth==1&&puDay==29) {
        if(!(((puYear % 4 == 0)&&(puYear% 100 != 0)) || (puYear% 400 == 0))) {
            puDay=28;
        }
    }

    var puDate = new Date(puYear,puMonth,puDay);
    var doDate = new Date(puYear,puMonth,puDay);
    var doMonth = doDate.getMonth();
    if (doMonth == 12) {
        doMonth = 0;
    }
    
    // set pickup and dropoff dropdowns
    selectedMonthYear.options[findInArray(puDate.getMonth(),selectedMonthYear.options)].selected=1;
    selectedDay.options[puDate.getDate()-1].selected=1;
    if(dateset==false) {
        setMonthYear.options[findInArray(doMonth,setMonthYear.options)].selected=1;
        setDay.options[doDate.getDate()].selected=1;
    }
}

function validDropoffDate() {
    var selectedMonthYear = document.forms[0].dropoffMonth;
    var selectedDay = document.forms[0].dropoffDay;
    var doMonth = selectedMonthYear.options[selectedMonthYear.selectedIndex].value;
	var now = new Date();
    var doYear = now.getYear();
    if(doYear < 1000){ doYear += 1900 }
    var doDay = selectedDay.options[selectedDay.selectedIndex].value;
    var doDate = new Date(doYear,doMonth,doDay);
        
    selectedMonthYear.options[findInArray(doDate.getMonth(),selectedMonthYear.options)].selected=1;
    selectedDay.options[doDate.getDate()-1].selected=1;
}

// finds a given element's index in a given array
// returns the index
// returns -1 if not found
function findInArray( searchS, arraySA ) {
 var I = -1, maxI = arraySA.length - 1;
 var s = "";
 var foundB = false;
 while(I <= maxI && !foundB){
  I++;
  s = arraySA[I].value;
  foundB = ( searchS == s );
 }
 if ( foundB ) { return I; }
 else { return( -1 ); }
}
