<% /*
     $Revision$
     $Id$

*/ %>
<%

function YWDtoDate(Y, W, D) { // Server-side
  var d = new Date(Y, 0, 3)
  d.setDate( 3 - d.getDay() + (W-1) * 7 + D )
  return d
}

function getWnDow(d) { // ISO 8601 WkNo. m=1..12
  if (!d) d = new Date();
  var ms1d = 864e5; // milliseconds one day
  var ms7d = 7*ms1d; // miliseconds 7 days
  var DC3 = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()+3)/ms1d; // An absolute day number
  var dayofweek = 1 + (DC3+7777777)%7; // Day of Week
  var AWN = Math.floor(DC3/7); // An absolute week number
  var Wyr = new Date(AWN*ms7d).getUTCFullYear();
  var weeknr = AWN - Math.floor(Date.UTC(Wyr, 0, 0)/ms7d);
  return [weeknr, dayofweek ]
}

function getWeeknumber(d) {
  return getWnDow(d)[0];
}

function weeksInYear(year) {
  var X1, X2, NW
  with (X1=new Date(year,  0,  4)) setDate(getDate() - (6+getDay())%7) // first day of week 1
  with (X2=new Date(year, 11, 28)) setDate(getDate() + (7-getDay())%7) // last day of week NW
  NW = Math.round((X2-X1)/(7*864e5)) // number of weeks in year
  return NW;
}
%>
