var dateset = false;

// JavaScript dates are 0-based
// Budget.com month-year fields are 0-based
function setDropoffDate() {
    var selectedMonthYear = document.forms[0].puMonth;
	var selectedDay = document.forms[0].puDay;
    var setMonthYear = document.forms[0].doMonth;
    var setDay = document.forms[0].doDay;
    var puMonth = selectedMonthYear.options[selectedMonthYear.selectedIndex].value;
	var now = new Date();
    var puYear = now.getYear();
	if(puMonth<now.getMonth()){
			puYear+=1;
	}
    if(puYear < 1000){ puYear += 1900;}
    var puDay = selectedDay.options[selectedDay.selectedIndex].value;
	var leapyear;
    // take leap-year into account
    if(puMonth==1&&puDay==29) {
		if((puYear% 400 !=0 && puYear% 100 ==0) || (puYear% 4 !=0))
		{
			puDay=28;
			leapyear=false;
		}
        else{
			leapyear=true;
		}
		
    }
	
	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)
	{
    	if((puDate.getDate()==30 && (puMonth ==3 || puMonth==5 ||puMonth==8 || puMonth==10)) || (puDate.getDate()==31 && (puMonth ==0 ||puMonth==2|| puMonth==4 ||puMonth==6||puMonth==7 || puMonth==9||puMonth==11)) || (puDay ==29 && leapyear==true) || (puDay ==28 && leapyear==false)){
				 if (doMonth == 11) {
					doMonth = 0;
				}else{doMonth=doMonth+1;}
				setMonthYear.options[findInArray(doMonth,setMonthYear.options)].selected=1;
				setDay.options[0].selected=1;
		}
		else{
			setMonthYear.options[findInArray(doMonth,setMonthYear.options)].selected=1;
			setDay.options[doDate.getDate()].selected=1;
			}
	}
}
function validDropoffDate() {
    var selectedMonthYear = document.forms[0].doMonth;
	var selectedDay = document.forms[0].doDay;
	var doMonth = selectedMonthYear.options[selectedMonthYear.selectedIndex].value;
	var now = new Date();
    var doYear = now.getYear();
	if(doMonth<now.getMonth()){
			doYear+=1;
	}
    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 ); }
}
