// Initialization
var monthDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var weekHead = new Array('日','月','火','水','木','金','土');
var monthHead = new Array('1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月');

var today=new Date();
var thisYear = today.getYear();
if (thisYear < 1900) { thisYear = thisYear + 1900; }
var thisMonth = today.getMonth();
var thisDay = today.getDate();

// Calender object
function Cal(name, boxy,boxm,boxd, obj) {
  this.name = name;
  this.boxy = boxy;
  this.boxm = boxm;
  this.boxd = boxd;
  this.obj = obj;
  this.crntYear = 0;
  this.crntMonth = 0;
  this.crntDay = 0;
  this.selectYear = thisYear;
  this.selectMonth = thisMonth;
  this.selectDay = thisDay+1;
  this.rangefromYear = thisYear;
  this.rangefromMonth = thisMonth;
  this.rangefromDay = thisDay;
  this.rangetoYear = thisYear + 1;
  this.rangetoMonth = thisMonth;
  this.rangetoDay = thisDay;
  this.crntMonthDay = 0;
  this.crntStartDay = 0;
  return(this);
}

// Setup and display calender

Cal.prototype.disp = function (yyyy, mm, dd) {
  this.setCrnt(yyyy, mm, dd);
  this.writeCal();
}

// Change selected date
Cal.prototype.select = function (yyyy, mm, dd) {
  this.setSelect(yyyy, mm, dd);
  this.boxy.value = this.selectYear;
  if (this.selectMonth < 9) {
	  this.boxm.value = '0' + (this.selectMonth + 1);
  } else {
	  this.boxm.value = this.selectMonth + 1;
  }
  this.boxd.value = this.selectDay;
  this.writeCal();
}


// Previous month
Cal.prototype.dispPrev = function () {
  this.setCrntPrev();
  this.writeCal();
}

// Next month
Cal.prototype.dispNext = function () {
  this.setCrntNext();
  this.writeCal();
}

// Draw calender
Cal.prototype.writeCal = function () {
  var d = this.obj;
  var str;
  var i, w;

  // Display calender title
  str = '';
  str += '<table class="calender_back"><tr>';
  str += '<th class="calender_head">';
  str += '<a href="javascript:';
  str += this.name + '.dispPrev();">-</a>';
  str += '</th>';
  str += '<th colspan="5" class="calender_head">';
  str += this.crntYear  + '年' + monthHead[this.crntMonth];
  str += '</th>';
  str += '<th class="calender_head">';
  str += '<a href="javascript:';
  str += this.name + '.dispNext();">+</a>';
  str += '</th>';

  // Display calender head
  str += '</tr><tr>';
  str += '<th class="calender_sun">' + weekHead[0] + '</th>';
  for (i=1; i<6; i++) {
    str += '<th class="calender_back">' + weekHead[i] + '</th>';
  }
  str += '<th class="calender_sat">' + weekHead[6] + '</th>';

  // Display calender dates
  str += '</tr><tr>';

  // Space out until 1st day
  for (i = 0; i < this.startDay; i++) {
    str += '<td class="calender_back"></td>';
  }

  // Show 1 month
  for (i = 1, w = this.startDay; i <= this.crntMonthDay; i++, w++) {
    // Per week
    if (w > 6) {
      w = 0;
      str += '</tr></tr>';
    }

    // If date is within range, create link
    linked_date = '<a href="javascript:' + this.name + '.select('+ this.crntYear +',' + (this.crntMonth + 1) + ',' + i + ');">' + i + '</a>';
    // Today
    if ((i == thisDay)
        && (this.crntMonth == thisMonth)
        && (this.crntYear == thisYear)) {
      str += '<td class="calender_today">' + linked_date + '</td>';
    // Selected date
    } else if ((i == this.selectDay)
        && (this.crntMonth == this.selectMonth)
        && (this.crntYear == this.selectYear)) {
      str += '<td class="calender_select">' + linked_date + '</td>';
      
    } else {
      // By weekday, add link if later than tod
      if (w == 0) {
        str += '<td class="calender_sun">' + linked_date + '</td>';
      } else if (w == 6) {
        str += '<td class="calender_sat">' + linked_date + '</td>';
      } else {
        str += '<td class="calender_back">' + linked_date + '</td>';
      }
    }
  }

  // Space out nonexistent days
  for (; w < 7; w++) {
    str += '<td class="calender_back"></td>';
  }

  str += '</tr></table>';

  // Print
  d.innerHTML = str;
}

// Fix selected date
Cal.prototype.setSelect = function (yyyy, mm, dd) {
//alert(this.selectYear+'/'+(this.selectMonth+1) + ', ' + yyyy+'/'+mm);

  // Year transform（1901-2099）
  if ((typeof(yyyy) == 'undefined') || (yyyy < 1901) || (yyyy > 2099)) {
    this.selectYear = thisYear;
  } else {
    this.selectYear = yyyy;
  }

  // Month transform（0-11）
  if ((typeof(mm) == 'undefined') || (mm < 1) || (mm > 12)) {
    this.selectMonth = thisMonth;
  } else {
    this.selectMonth = mm - 1;
  }
  // Leap year（1901-2099）
  if ((this.selectMonth == 1) && (this.selectYear % 4 == 0)) {
    this.selectMonthDay = 29;
  } else {
    this.selectMonthDay = monthDay[this.selectMonth];
  }

  // Day transform (1-31:this.crntMonthDay)
  if ((typeof(dd) == 'undefined') || (dd < 1) || (dd > this.crntMonthDay)) {
    this.selectDay = thisDay;
  } else {
    this.selectDay = dd;
  }
}
// Fix current date input
Cal.prototype.setCrnt = function (yyyy, mm, dd) {
//alert(this.crntYear+'/'+(this.crntMonth+1) + ', ' + yyyy+'/'+mm);

  // Year transform（1901-2099）
  if ((typeof(yyyy) == 'undefined') || (yyyy < 1901) || (yyyy > 2099)) {
    this.crntYear = thisYear;
  } else {
    this.crntYear = yyyy;
  }

  // Month transform（0-11）
  if ((typeof(mm) == 'undefined') || (mm < 1) || (mm > 12)) {
    this.crntMonth = thisMonth;
  } else {
    this.crntMonth = mm - 1;
  }
  // Leap year（1901-2099）
  if ((this.crntMonth == 1) && (this.crntYear % 4 == 0)) {
    this.crntMonthDay = 29;
  } else {
    this.crntMonthDay = monthDay[this.crntMonth];
  }

  // Day transform (1-31:this.crntMonthDay)
  if ((typeof(dd) == 'undefined') || (dd < 1) || (dd > this.crntMonthDay)) {
    this.crntDay = thisDay;
  } else {
    this.crntDay = dd;
  }

  // Beginning week
  var d1 = new Date(this.crntYear + '/' + (this.crntMonth + 1) + '/01');
  this.startDay = d1.getDay();
}

// Previous month jump
Cal.prototype.setCrntPrev = function () {
  // Previous month
  var yyyy = this.crntYear;
  var mm = this.crntMonth;
  var dd = this.crntDay;

  if (mm < 1) {
    yyyy--;
    mm = 12;
  }

  this.setCrnt(yyyy, mm, dd);
}


// Next month jump

Cal.prototype.setCrntNext = function () {
  // Next month
  var yyyy = this.crntYear;
  var mm = this.crntMonth + 2;
  var dd = this.crntDay;

  if (mm > 12) {
    yyyy++;
    mm = 1;
  }

  this.setCrnt(yyyy, mm, dd);
}


