function vanaclock( clock )
{
  var T , Y , M , D , H , N , W , G;
  var rWEEK = new Array( "日" , "月" , "火" , "水" , "木" , "金" , "土" );
  var vWEEK = new Array( "火" , "土" , "水" , "風" , "氷" , "雷" , "光" , "闇" );
  var vMOON = new Array( "新月" , "三日月" , "七日月" , "上弦の月" , "十日夜" , "十三夜" ,
               "満月" ,"十六夜" , "居待月" , "下弦の月 " , "二十日余月" , "二十六夜" );
  var vVFLD = new Array( "ジャグナー" , "パシュハウ" , "メリファト" );
  var vVLVL = new Array( "30制限" , "40制限" , "50制限" , "60制限" , "無制限" );

  T = ( ( clock == undefined ? new Date().getTime() : clock ) + 92514960000 ) * 25;
  Y = Math.floor( T / 31104000000 );
  M = Math.floor( ( T % 31104000000 ) / 2592000000 ) + 1;
  D = Math.floor( ( T % 2592000000 ) / 86400000 ) + 1;
  H = Math.floor( ( T % 86400000 ) / 3600000 );
  N = Math.floor( ( T % 3600000 ) / 60000 );
  W = Math.floor( ( T % 691200000 ) / 86400000 );
  G = Math.floor( ( T - 1036800000 ) / 604800000 % 12 );

  this.vanaYear = Y;
  this.vanaMonth = M;
  this.vanaDate = D;
  this.vanaMoon = vMOON[ G ];
  this.vanaDateString = ( Y > 999 ? "" : "0" ) + Y + "/" + ( M > 9 ? "" : "0" ) + M + "/" + ( D > 9 ? "" : "0" ) + D;
  this.vanaTimeString = ( H > 9 ? "" : "0" ) + H + ":" + ( N > 9 ? "" : "0" ) + N;
  this.vanaFullString =
    ( Y > 999 ? "" : "0" ) + Y + "/" + ( M > 9 ? "" : "0" ) + M + "/" + ( D > 9 ? "" : "0" ) + D +
     "[" + vWEEK[ W ] + "]" + ( H > 9 ? "" : "0" ) + H + ":" + ( N > 9 ? "" : "0" ) + N;
  this.vanaWeekNum = W;
  this.vanaVarField = Math.floor( T / 86400000 % 2 ) ? vVFLD[ Math.floor( T / 86400000 % 6 / 2 ) ] : "&nbsp;";
  this.vanaVarLevel = Math.floor( T / 86400000 % 2 ) ? vVLVL[ Math.floor( T / 86400000 % 30 / 6 ) ] : "&nbsp;";


  T = new Date( clock == undefined ? new Date().getTime() : clock )
  Y = T.getFullYear();
  M = T.getMonth() + 1;
  D = T.getDate();
  H = T.getHours();
  N = T.getMinutes();
  W = T.getDay();

  this.realWeekNum = W;
  this.realFullString =
    ( Y > 999 ? "" : "0" ) + Y + "/" + ( M > 9 ? "" : "0" ) + M + "/" + ( D > 9 ? "" : "0" ) + D +
     "[" + rWEEK[ W ] + "]" + ( H > 9 ? "" : "0" ) + H + ":" + ( N > 9 ? "" : "0" ) + N;
}



function now( max , title )
{
  listdate( new Date().getTime() - ( ( new Date().getTime() - 2160000 ) % 3456000 ) , max , title );
}



function changerealdate()
{
  var date = document.setdate.realyear.value + "/" + document.setdate.realmonth.value + "/" + document.setdate.realdate.value;
  if( isNaN( new Date( date ).getTime() ) ){
    alert("設定された地球日付の値に誤りがあります");
  } else if( new Date( date ).getTime() < -91270800000 ){
    alert("設定された地球日付の値が、\\n天昌0001年01月01日以前になります");
  } else {
    listdate( new Date( date ).getTime() , 80 , true );
    var vana = new vanaclock( new Date( date ).getTime() );
    document.setdate.vanayear.value = vana.vanaYear;
    document.setdate.vanamonth.value = vana.vanaMonth;
    document.setdate.vanadate.value = vana.vanaDate;
  }
}



function changevanadate()
{
  var date = (
    document.setdate.vanayear.value * 31104000000 +
    ( document.setdate.vanamonth.value - 1 ) * 2592000000 +
    ( document.setdate.vanadate.value - 1 ) * 86400000 ) /25 - 92514960000;
  if( isNaN( new Date( date ).getTime() ) ){
    alert("設定されたヴァナ日付の値に誤りがあります");
  } else if( date < -91270800000 ){
    alert("設定されたヴァナ日付の値が、\\n天昌0001年01月01日以前になります");
  } else {
    listdate( new Date( date ).getTime() , 80 , true );
    document.setdate.realyear.value = new Date( date ).getFullYear();
    document.setdate.realmonth.value = new Date( date ).getMonth() + 1;
    document.setdate.realdate.value = new Date( date ).getDate();
  }
}



function listdate( timer , max , title )
{
  var vWEEKC = new Array( "#ffaaaa" , "#ffff80" , "#aaaaff" , "#aaffaa" , "#80ffff" , "#ff80ff" , "#ffffff" , "#aaaaaa" );
  var rWEEKC = new Array( "#ffccff" , "#eeeedd" , "#ffcccc" , "#ddccff" , "#ccffcc" , "#ffffcc" , "#ccccff" );

  var str = "<table class='calender' cellpadding='5' cellspacing='0'>";
  if( title ){
    str += "<tr>";
    str += "<th width='200'>地球日時</th>";
    str += "<th width='200'>Vana'diel日時</th>";
    str += "<th width='100'>月齢</th>";
    str += "<th colspan='2'>バリスタ開催日程</th></tr>";
  }
  for( var i = 0 , h = 0 ; h < max ; i++ ){
    var vana = new vanaclock( timer + i * 3456000 );
    if( document.setdate.vanaweek.selectedIndex == 0 || document.setdate.vanaweek.selectedIndex == vana.vanaWeekNum + 1 ){
      str += "<tr>";
      str += "<td bgcolor='" + rWEEKC[ vana.realWeekNum ] + "'>" + vana.realFullString + "</td>";
      str += "<td bgcolor='" + vWEEKC[ vana.vanaWeekNum ] + "'>" + vana.vanaFullString + "</td>";
      str += "<td>" + vana.vanaMoon + "</td>";
      str += "<td>" + vana.vanaVarField + "</td>";
      str += "<td>" + vana.vanaVarLevel + "</td>";
      str += "</tr>";
      h++;
    }
  }
  str += "</table>";
  document.getElementById("calender").innerHTML = str;

}