JavaScript to validate date
<p>Below is the function to validate date. The should be between <strong>Today - 15</strong> and <strong>Today</strong>. Can some one refactor this code.</p>

<p>phpdatetoday is a string in the form <strong>2010,Dec,3</strong></p>

<pre><code>function validate(page, phpdatetoday)
{
    var i = 0;
    var fields    = new Array();
    var fieldname = new Array();

    var day   = document.getElementById('date_of_inspection_day').value;
    var month = document.getElementById('date_of_inspection_month').value;
    var year  = document.getElementById('date_of_inspection_year').value;
    var datesubmitted = new Date(year,month-1,day);

    var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

    if(month.length&lt;1 )
    {
        alert("Please enter a valid month");
        return false;
    }
    if(year.length != 4 )
    {
        alert("Please enter a valid year");
        return false;
    }

    if (day.length&lt;1 || day &gt; daysInMonth[month-1] || month == 2 &amp;&amp; year%4 != 0 &amp;&amp; day &gt;28 )
    {
        alert("Please enter a valid day");
        return false;
    }

    var dateToday  = new Date(phpdatetoday);
    var day15      = dateToday.getDate()-15; // 15 days old
    var month15    = dateToday.getMonth();
    var year15     = dateToday.getFullYear();

    if(day15 &lt; 0 &amp;&amp; month15 ==1)
    {
        month15 = 12;
        year15  = year15-1;
    }
    else if(day15 &lt; 0 &amp;&amp; month15 !=1)
    {
        month15 = month15-1;
    }

    day15   = daysInMonth[month15-1] + day15;

    var date15DayOld  = new Date(year15,month15,day15);

    if(date15DayOld &gt; datesubmitted )
    {
        alert("Your date is older than 15 days");
    }

    else if(datetoday &lt; datesubmitted )
    {
        alert("invalid Date");
    }
}
</code></pre>
