//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear) {
    var DaysInMonth = 31;
    if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov")
        DaysInMonth = 30;
    if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))
        DaysInMonth = 28;
    if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))
        DaysInMonth = 29;
    return DaysInMonth;
}

//function to change the available days in a months
function ChangeoptionDays(Which) {
    DaysObject = $("select[name='" + Which + "Day']").get(0);
    MonthObject = $("select[name='" + Which + "Month']").get(0);
    YearObject = $("select[name='" + Which + "Year']").get(0);
  
    Month = MonthObject[MonthObject.selectedIndex].text;
    Year = YearObject[YearObject.selectedIndex].text;
    Day = DaysObject[DaysObject.selectedIndex].text;
    DaysForThisselection = DaysInMonth(Month, Year);
    CurrentDaysInselection = DaysObject.length;
    if (CurrentDaysInselection > DaysForThisselection) {
        for (i=0; i<(CurrentDaysInselection-DaysForThisselection); i++) {
          DaysObject.options[DaysObject.options.length - 1] = null;
        }
    }
    if (DaysForThisselection > CurrentDaysInselection) {
        for (i=0; i<(DaysForThisselection-CurrentDaysInselection); i++) {
            //console.log(DaysObject.options.length + 1);
            Newday = DaysObject.options.length + 1;
            //Newoption = new option(DaysObject.options.length + 1);
            $(DaysObject).append("<option>"+Newday+"</option>");
        }
    }
    
    if(Day <= DaysForThisselection)
        DaysObject.selectedIndex = Day-1;
    else //if (DaysObject.selectedIndex < 0)
        DaysObject.selectedIndex = 0;
    
}