
function renderDateSelect(type, day, month, year) {

	var dayId = 'day_'+type;
	var monthId = 'month_'+type;
	var yearId = 'year_'+type;
	var resetDaysFunc = "";

	if (type == 'start') {
		resetDaysFunc = 'setDaysStart();';
	}
	else if (type == 'end') {
		resetDaysFunc = 'setDaysEnd();';
	}
	else {
		alert('error: renderDateSelect must be called with a type of start or end');
	}

	document.writeln('<select id="'+dayId+'" name="'+dayId+'">');
	for (i = 1; i < 10; i++) {
		document.writeln('<option value="0' + i + '">' + i + '</option>');
	}
	document.writeln('</select>');

	document.writeln('<select id="'+monthId+'" name="'+monthId+'" onchange="'+resetDaysFunc+'">');
	document.writeln('<option value="01">Jan</option>');
	document.writeln('<option value="02">Feb</option>');
	document.writeln('<option value="03">Mar</option>');
	document.writeln('<option value="04">Apr</option>');
	document.writeln('<option value="05">May</option>');
	document.writeln('<option value="06">Jun</option>');
	document.writeln('<option value="07">Jul</option>');
	document.writeln('<option value="08">Aug</option>');
	document.writeln('<option value="09">Sep</option>');
	document.writeln('<option value="10">Oct</option>');
	document.writeln('<option value="11">Nov</option>');
	document.writeln('<option value="12">Dec</option>');
	document.writeln('</select>');

	document.writeln('<select id="'+yearId+'" name="'+yearId+'" onchange="'+resetDaysFunc+'" style="visibility:hidden;display:none;">');
	var currentDate = new Date();
	var currentYear = getYear(currentDate) - 2000;
	for (i = 0; i < 2; i++) {
		var displayYear = currentYear;
		if (currentYear<10) {
			currentYear = "0"+currentYear;
		}
		document.writeln('<option value="' + displayYear + '">' + currentYear + '</option>');
		currentYear++;
	}
	document.writeln('</select>');

	if (year != null && month != null && day != null) {
		if (type == 'start') {
			setCurrentDateStart(year,month-1,day);
		}
		else if (type == 'end') {
			var dmonth = month-1;
			setCurrentDateEnd(year,month-1,day);
		}
	}
	else {
		if (type == 'start') {
			setCurrentDateStart();
		}
		else if (type == 'end') {
			setCurrentDateEnd();
		}
	}

	document.writeln('<a href="javascript:var cal1 = new calendar2($(\''+dayId+'\'),$(\''+monthId+'\'),$(\''+yearId+'\'));cal1.popup();"><img src="/images/cal.gif" align="top" style="padding-top:2px;" width="16" height="16" border="0" /></a>');
}

function setCurrentDateStart($year, $month, $day) {
  /* changes the date selector menus to the current date or the specified date */
  var currentDate = new Date();

  var todaysYear = getYear(currentDate);

  if ($year && $day) {
    currentDate.setFullYear($year+2000,$month,$day);
  }

  if (getYear(currentDate) == todaysYear) {
    $('year_start').selectedIndex = 0;
  }
  else {
    $('year_start').selectedIndex = 1;
  }

  $('month_start').selectedIndex = currentDate.getMonth();

  setDaysStart();

  $('day_start').selectedIndex = currentDate.getDate() - 1;
}

function setDaysStart() {
  if (!$('year_start')) {
    return;
  }
  
  var y = 2000;
  try {
    y = $('year_start').options[$('year_start').selectedIndex].value;
  } catch(e) {
    y = $('year_start').value;
  }
  var m = $('month_start').selectedIndex;
  var d;

  // find number of days in current month
  if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {
    days = 30;
  }
  else if (m == 1) {
    // check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
    if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
      days = 29
    else
      days = 28
  }
  else {
    days = 31;
  }

  // if (days in new month > current days) then we must add the extra days
  if (days > $('day_start').length) {
    for (i = $('day_start').length; i < days; i++) {
      $('day_start').length = days;
      $('day_start').options[i].text = i + 1;
      $('day_start').options[i].value = i + 1;
    }
  }

  // if (days in new month < current days) then we must delete the extra days
  if (days < $('day_start').length) {
    $('day_start').length = days;
    if ($('day_start').selectedIndex == -1)
      $('day_start').selectedIndex = days - 1;
  }

}

function setCurrentDateEnd($year, $month, $day) {
  /* changes the date selector menus to the current date or the specified date */
  var currentDate = new Date();

  var todaysYear = getYear(currentDate)

  if ($year && $day) {
    currentDate.setFullYear($year,$month,$day);
  }

  if (getYear(currentDate) == todaysYear) {
    $('year_end').selectedIndex = 0;
  }
  else {
    $('year_end').selectedIndex = 1;
  }

  $('month_end').selectedIndex = currentDate.getMonth();

  setDaysEnd();
  $('day_end').selectedIndex = currentDate.getDate() - 1;
}

function setDaysEnd() {
  if (!$('year_end')) {
    return;
  }
  
  var y = 2000;
  try {
    y = $('year_end').options[$('year_end').selectedIndex].value;
  } catch(e) {
    y = $('year_end').value;
  }
  var m = $('month_end').selectedIndex;
  var d;

  // find number of days in current month
  if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {
    days = 30;
  }
  else if (m == 1) {
    // check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
    if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
      days = 29
    else
      days = 28
  }
  else {
    days = 31;
  }

  // if (days in new month > current days) then we must add the extra days
  if (days > $('day_end').length) {
    for (i = $('day_end').length; i < days; i++) {
      $('day_end').length = days;
      $('day_end').options[i].text = i + 1;
      $('day_end').options[i].value = i + 1;
    }
  }

  // if (days in new month < current days) then we must delete the extra days
  if (days < $('day_end').length) {
	$('day_end').length = days;
    if ($('day_end').selectedIndex == -1)
      $('day_end').selectedIndex = days - 1;
  }
}

// gets the full year i.e. 2007 and is browser independant
function getYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function validateDate(){
	if (!$('year_start') || !$('year_end')) {
	    document.dateform.submit();
	    return;
	}
	
	start_year = $('year_start').value;
	start_month = $('month_start').options[$('month_start').selectedIndex].value;
	start_day = $('day_start').options[$('day_start').selectedIndex].value;

	end_year = $('year_end').value;
	end_month = $('month_end').options[$('month_end').selectedIndex].value;
	end_day = $('day_end').options[$('day_end').selectedIndex].value;

	if (start_year > end_year) {
	    $('error').innerHTML = "The second date must be after the first date";
	}
	else if (start_year == end_year && start_month > end_month) {
	    $('error').innerHTML = "The second date must be after the first date";
	}
	else if (start_year == end_year && start_month == end_month && start_day > end_day) {
	    $('error').innerHTML = "The second date must be after the first date";
	}
	else {
	    document.dateform.submit();
	}
}