// trimester calc

var msPerDay = 24*60*60*1000
function DaysToWD( days )
 {
  w = Math.floor(days/7)
  d = Math.floor(days - 7*w)
  s = w + " weeks + " + d + " day" + (d > 1 ? "s" : "")
  return s
 }
function formatDate( d )
 {
  f = document.TriCalc
  y = d.getYear()
  if( y < 1000 ) y = y + 1900
    return f.Month.options[d.getMonth()].text + " "  + f.Day.options[d.getDate()-1].text  + ", " + y
 }
function doTriCalc()
 {
  f = document.TriCalc
  month = f.Month.selectedIndex
  day   = f.Day.selectedIndex + 1
  year  = f.Year.selectedIndex + 2002
  lmp = new Date(year,month,day)
  if( f.refType[1].checked )
   {
    lmp.setTime( lmp.getTime() - 13*msPerDay )
   }
  //--------- Calc current LMP --------
  c = new Date()
  f.lmpDate.value = formatDate(lmp)
  days = (c.getTime()-lmp.getTime()) / msPerDay
  f.currentDate.value = DaysToWD(days)
  c.setTime( lmp.getTime() + 10*msPerDay )
  f.conceptionDate.value = formatDate(c)

	c.setTime( lmp.getTime() + 17*msPerDay )
  f.conceptionDate2.value = formatDate(c)

  //------ Calc Heuristic Dates -------
  d = new Date()
  d.setTime( lmp.getTime() + 84*msPerDay )
  f.heuristic_second.value = formatDate(d)
  d.setTime( lmp.getTime() + 189*msPerDay )
  f.heuristic_third.value = formatDate(d)
  //----- Calc Gestational Dates ------
  d.setTime( lmp.getTime() + 94*msPerDay )
  f.gestational_second.value = formatDate(d)
  d.setTime( lmp.getTime() + 187*msPerDay )
  f.gestational_third.value = formatDate(d)
  //----- Calc Conceptual Dates ------
  d.setTime( lmp.getTime() + 103*msPerDay )
  f.conceptual_second.value = formatDate(d)
  d.setTime( lmp.getTime() + 192*msPerDay )
  f.conceptual_third.value = formatDate(d)
  //-------- Calc Due Date ---------
  d.setTime( lmp.getTime() + 280*msPerDay )
  f.DueDate.value = formatDate(d)
  d.setTime( lmp.getTime() + 259*msPerDay )
  f.DueDate2.value = formatDate(d)
  d.setTime( lmp.getTime() + 231*msPerDay )
  f.DueDate3.value = formatDate(d)
  d.setTime( lmp.getTime() + 224*msPerDay )
  f.DueDate4.value = formatDate(d)
  return
 }

