I wrote this script to automatically set the maximum date available in a select element based on the selected month. While the script works fine, I wonder if it could be made more efficient and if I even came close to good design with it. My programming background is primarily with PHP and VBScript, so I know the concepts but not the subtleties of JavaScript. Please take a look and give me any C&C that you think will help.
JavaScript:
function setDays(frm)
{
var d = new Date();
var cur_year = d.getFullYear();
var cur_month = d.getMonth();
cur_month = cur_month + 1;
//Set the max number of days for the selected month
if (frm.month.value == cur_month)
{
max_days = d.getDate();
}
else if (frm.month.value == "01" || frm.month.value == "03" || frm.month.value == "05" || frm.month.value == "07" || frm.month.value == "08" || frm.month.value == "10" || frm.month.value == "12")
{
max_days = 31;
}
else if (frm.month.value == "02")
{
//Determine if this is a leap year
if (cur_year%400 == 0)
{
is_leapyear = true;
}
else if (cur_year%100 != 0)
{
is_leapyear = false;
}
else if (cur_year%4 == 0)
{
is_leapyear = true;
}
else
{
is_leapyear = false;
}
if (is_leapyear)
{
max_days = 29;
}
else
{
max_days = 28;
}
}
else
{
max_days = 30;
}
//Write the options to the days select field
var i = 1;
var options = "<select name=\"days\">\n";
for (i=1; i<=max_days; i++)
{
if (i < 10)
{
val = "0" + i.toString();
}
else
{
val = i.toString();
}
options = options + "<option value=\"" + val + "\"";
if (i == 1)
{
options = options + " selected";
}
options = options + ">" + i + "<\/option>\n";
}
options = options + "<\/select>\n";
//Write the new options to the correct select element
document.getElementById('day').innerHTML = options;
}