// 変数の設定
var monthDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var weekHead = new Array('日','月','火','水','木','金','土');
var today=new Date();
var thisYear = today.getYear();
if (thisYear < 1900) { thisYear = thisYear + 1900; }
var thisMonth = today.getMonth();
var thisDay = today.getDate();
function Cal(name, own, obj) {
  this.name = name;
  this.own = own;
  this.obj = obj;
  this.crntYear = 0;
  this.crntMonth = 0;
  this.crntDay = 0;
  this.crntMonthDay = 0;
  this.crntStartDay = 0;
  this.head = '';
  this.foot = '';
  return(this);
}
Cal.prototype.disp = function (yyyy, mm, dd) {
  this.setCrnt(yyyy, mm, dd);
  this.writeCal();
}
Cal.prototype.dispPrev = function () {
  this.setCrntPrev();
  this.writeCal();
}
Cal.prototype.dispNext = function () {
  this.setCrntNext();
  this.writeCal();
}
Cal.prototype.writeCal = function () {
  var d = this.obj.document;
  var str;
  var i, w;
  // HTMLヘッダの表示
  if (this.head) {
    d.open();
    d.writeln(this.head);
  }
  // カレンダタイトルの表示
  str = '';
  str += '
';
  str += '| ';
  str += '△';
  str += ' | ';
  str += '';
  str += this.crntYear + '年' + (this.crntMonth + 1) + '月';
  str += ' | ';
  str += '';
  str += '▽';
  str += ' | ';
  // 曜日タイトルの表示
  str += '
';
  str += '| ' + weekHead[0] + ' | ';
  for (i=1; i<6; i++) {
    str += '' + weekHead[i] + ' | ';
  }
  str += '' + weekHead[6] + ' | ';
  // カレンダの表示
  str += '
';
  // 月初の空白表示
  for (i = 0; i < this.startDay; i++) {
    str += '|   | ';
  }
  // 1ヶ月の表示
  for (i = 1, w = this.startDay; i <= this.crntMonthDay; i++, w++) {
    // 週単位の改行
    if (w > 6) {
      w = 0;
      str += '
';
    }
    // 今日の色分け表示
    if ((i == thisDay)
        && (this.crntMonth == thisMonth)
        && (this.crntYear == thisYear)) {
      str += '' + i + ' | ';
    } else {
      // 曜日による色分け表示
      if (w == 0) {
        str += '' + i + ' | ';
      } else if (w == 6) {
        str += '' + i + ' | ';
      } else {
        str += '' + i + ' | ';
      }
    }
  }
  // 月末の空白表示
  for (; w < 7; w++) {
    str += '  | ';
  }
  str += '
';
  // カレンダの表示
  d.writeln(str);
  // HTMLフッタの表示
  if (this.foot) {
    d.writeln(this.foot);
    d.close();
  }
}
Cal.prototype.setHead = function (str) {
  this.head = str;
}
Cal.prototype.setFoot = function (str) {
  this.foot = str;
}
Cal.prototype.setCrnt = function (yyyy, mm, dd) {
//alert(this.crntYear+'/'+(this.crntMonth+1) + ', ' + yyyy+'/'+mm);
  // 年変換(1901-2099)
  if ((typeof(yyyy) == 'undefined') || (yyyy < 1901) || (yyyy > 2099)) {
    this.crntYear = thisYear;
  } else {
    this.crntYear = yyyy;
  }
  // 月変換(0-11)
  if ((typeof(mm) == 'undefined') || (mm < 1) || (mm > 12)) {
    this.crntMonth = thisMonth;
  } else {
    this.crntMonth = mm - 1;
  }
  // うるう年変換(1901年から2099年まで対応)
  if ((this.crntMonth == 1) && (this.crntYear % 4 == 0)) {
    this.crntMonthDay = 29;
  } else {
    this.crntMonthDay = monthDay[this.crntMonth];
  }
  // 日変換(1-31:this.crntMonthDay)
  if ((typeof(dd) == 'undefined') || (dd < 1) || (dd > this.crntMonthDay)) {
    this.crntDay = thisDay;
  } else {
    this.crntDay = dd;
  }
  // 開始曜日の設定
  var d1 = new Date(this.crntYear + '/' + (this.crntMonth + 1) + '/01');
  this.startDay = d1.getDay();
}
Cal.prototype.setCrntPrev = function () {
  // 前月のカレンダ表示
  var yyyy = this.crntYear;
  var mm = this.crntMonth;
  var dd = this.crntDay;
  if (mm < 1) {
    yyyy--;
    mm = 12;
  }
  this.setCrnt(yyyy, mm, dd);
}
Cal.prototype.setCrntNext = function () {
  // 翌月のカレンダ表示
  var yyyy = this.crntYear;
  var mm = this.crntMonth + 2;
  var dd = this.crntDay;
  if (mm > 12) {
    yyyy++;
    mm = 1;
  }
  this.setCrnt(yyyy, mm, dd);
}