function DoubleDigitString( iNumber )
{
  if( iNumber < 10 )
    result = "0" + iNumber
  else
    result = iNumber.toString();

  return result;
}

function FillDaysSelect(iDaysSelectName, iMonthNum, iYearNum, iAllowBlank)
{
	// Allow for all possible days if one value is not included
	iMonthNum *= 1;
	if( iMonthNum <= 0 )
		iMonthNum = 1;
	iYearNum *= 1;			//make it a month with 31 days
	if( iYearNum <= 0 )
		iYearNum = 2000;	//make it a year that is a leap year.
		
	if( (iMonthNum > 0) && (iYearNum > 0) )
	{
		theSelect = document.getElementById(iDaysSelectName);
		if( theSelect )
		{
			oldValue = (theSelect.options[theSelect.selectedIndex].value * 1);
			days = DaysInMonth(iMonthNum, iYearNum);
			theSelect.length = 0;
			if( iAllowBlank )
				theSelect.options[theSelect.length] = new Option("", "", false, false);
			for( x=1; x <= days; x++ )
			{
				if( x == oldValue )
					theSelect.options[theSelect.length] = new Option(DoubleDigitString(x), DoubleDigitString(x), true, true)
				else
					theSelect.options[theSelect.length] = new Option(DoubleDigitString(x), DoubleDigitString(x), false, false);
			}
		}
	}
} //end FillDaysSelect()


function DaysInMonth( iMonth, iYear )
{
  result = -1;
  if( iYear > 0 && (iMonth > 0 && iMonth <= 12) )
  {
    switch(iMonth)
    {
      case 2:
        if( ((iYear % 4 == 0) && (iYear % 100 != 0)) || (iYear % 400 == 0) )  // Leap Year
          result = 29;
        else
          result = 28;
        break;

      default:
        if( (iMonth == 4) || (iMonth == 6) || (iMonth == 9) || (iMonth == 11) )
          result = 30;
        else
          result = 31;
    } //end switch(iMonth)
  } //end if( iYear > 0 )

  return result;
} //end DaysInMonth()


function HookDateBoxes( iMonthBoxId, iDateBoxId, iYearBoxId )
{
  var monthBox = document.getElementById(iMonthBoxId);
  var dateBox = document.getElementById(iDateBoxId);
  var yearBox = document.getElementById(iYearBoxId);

  if( monthBox && dateBox && yearBox )
  {
    dateBox.MonthBox  = monthBox;
    dateBox.YearBox   = yearBox;
    dateBox.update = function(){
                        FillDaysSelect(this.id, this.MonthBox.options[this.MonthBox.selectedIndex].value, this.YearBox.options[this.YearBox.selectedIndex].value, false);
                      };

    monthBox.DateBox  = dateBox;
    monthBox.onchange = function(){
                          this.DateBox.update();
                        };

    yearBox.DateBox   = dateBox;
    yearBox.onchange  = function(){
                          this.DateBox.update();
                        };
  }
}
