function changeDayList(month, year, day){
    var daysInMonth = calculateDaysInMonth(month.value, year.value)
    var today = parseInt(day.value);
    day.options.length=0;
    for(var i=0; i < daysInMonth; i++){
        var j = i + 1;
        if(today == j)
            day.options[i] = new Option(""+j, ""+j, false, true);
        else
            day.options[i] = new Option(""+j, ""+j, false, false);
    }
}
function calculateDaysInMonth(month, year)
{
    var imonth = parseInt(month);
    var daysInMonth = 30;
    if(imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11){
        daysInMonth = 30;
    }else if(imonth == 2){
        if(isLeapYear(year))
            daysInMonth = 29;
        else
            daysInMonth = 28;
    }else{
        daysInMonth = 31;
    }
    return daysInMonth;
}

function isLeapYear(year)
{
    year = parseInt(year);

    if(year%4 == 0)
    {
        if(year%100 != 0)
        {
            return true;
        }
        else
        {
            if(year%400 == 0)
                return true;
            else
                return false;
        }
    }
    return false;
}

function selectToday(month, day, year, dType)
{
    cal = new Date();
    var thisMonth =cal.getMonth()+1;
    var today = cal.getDate();
    var thisYear = cal.getFullYear();
    var startYear = thisYear;

    // drop down formatting
    ourUrl = document.location.href;

    agent = navigator.userAgent.toLowerCase();

    var monthWidth = 46;
    var dayWidth = 38;
    var yearWidth = 48;


    if (agent.indexOf('safari') != -1){
        monthWidth = parseInt(monthWidth + 3);
        dayWidth = parseInt(dayWidth + 6);
        yearWidth = parseInt(yearWidth + 7);
    }



    if (ourUrl.indexOf("search/advanced") != -1) {

        monthWidth = parseInt(monthWidth + 10);
        dayWidth =  parseInt(dayWidth + 10);
        yearWidth =  parseInt(yearWidth + 10);

    }
    //get time of two weeks ago plus hours back to that midnight. Using 13 instead of 14 b/c today counts as Day 1.
    var twoWeeks = 1000*60*24*60*14;

    //var millsSinceMidnight = parseInt(cal.getHours()*1000*60*60) + parseInt(cal.getMinutes()*1000*60) + parseInt(cal.getSeconds()*1000);

    //Correcting by 1 hour
    //twoWeeks += (millsSinceMidnight - (60*60*1000));

    //see single argument date object
    var twoWeeksAgo = new Date(parseInt(cal-twoWeeks));
    var sixtyDaysAgo = new Date(parseInt(cal-60*60*60*60*1000));
    var endYear = sixtyDaysAgo.getFullYear();

    if (month=="FromRangeMonth")
        thisMonth=twoWeeksAgo.getMonth()+1;
    if (day=="FromRangeDay")
        today=twoWeeksAgo.getDate();
    if (year=="FromRangeYear")
        thisYear=twoWeeksAgo.getFullYear();

    var months= new Array(12);
    months[0] ="Jan";
    months[1] ="Feb";
    months[2] ="Mar";
    months[3] ="Apr";
    months[4] ="May";
    months[5] ="Jun";
    months[6] ="Jul";
    months[7] ="Aug";
    months[8] ="Sep";
    months[9] ="Oct";
    months[10] ="Nov";
    months[11] ="Dec";

    var searchDateOutput ="";

    searchDateOutput += '<table border="0" cellspacing="0" cellpadding="0" width="130"><tr>';
    searchDateOutput += '<TD>';

    searchDateOutput+= '<SELECT name='+month+' onchange="javascript:radio_change(\''+dType+'\');changeDayList('+month+','+year+','+day+');" class="yourselectionclick" style="width:'+monthWidth+'px;" >';

    for (i=0; i<12; ++i)
    {
        var j = i+1;
        var monthSelected ="";
        if (thisMonth == j)
            monthSelected = " selected ";
        searchDateOutput += '<option value="'+j+'"'+monthSelected+ '>'+months[i];
    }
    searchDateOutput += '</SELECT>';

    searchDateOutput += '</TD>';


    searchDateOutput += '<TD>';
    searchDateOutput += '<SELECT name='+day+' onchange="javascript:radio_change(\''+dType+'\');" class="yourselectionclick" style="width:'+dayWidth+'px;">';
    var daysInMonth = calculateDaysInMonth(thisMonth, thisYear)
    for (i=0; i<daysInMonth; ++i)
    {
        var j = i+1;
        var daySelected ="";

        if (today == j)
            daySelected = " selected ";
        searchDateOutput += '<option value="'+j+'"'+daySelected+ '>'+j;
    }
    searchDateOutput += '</SELECT>';
    searchDateOutput += '</TD>';
    searchDateOutput += '<TD>';
    searchDateOutput += '<SELECT name='+year+' onchange="javascript:radio_change(\''+dType+'\');changeDayList('+month+','+year+','+day+');" class="yourselectionclick" style="width:'+yearWidth+'px;">';

    for (i=startYear; i>=endYear; i--)
    {
        if(thisYear == i){
            searchDateOutput += '<option value="'+i+'" selected>'+i;
        }else{
            searchDateOutput += '<option value="'+i+'">'+i;
        }
    }

    searchDateOutput += '</SELECT>';
    searchDateOutput += '</TD>';
    searchDateOutput += '</TR></TABLE>';

    var single ="";

    return searchDateOutput;


}

function radio_change(dType) {


    if (dType == 'single') {
        document.ArticleAdvSearch.daterange[1].checked = true;
    } else {
        document.ArticleAdvSearch.daterange[2].checked = true;
    }


}

