var ref = document.referrer;
var url = document.URL;
var today_date = new Date();
var today_year = today_date.getYear();
var today_month = today_date.getMonth();
var today_day = today_date.getDate();
if (today_year < 1900) today_year += 1900;

// クッキーの取得
function getCookie(name) {
  var key = name + '=';
  var value = '';
  var cokies = document.cookie;
  var start = 0;
  var end = 0;
  if (parseInt(cokies.length) > 0) {
    start = parseInt(cokies.indexOf(key));
    if (start >= 0) {
      start += parseInt(key.length);
      end = parseInt(cokies.indexOf(";", start));
      if (end < 0) {
        end = parseInt(document.cookie.length);
       }
      value = unescape(cokies.substring(start, end));
     }
   }
  return(value);
 }

// クッキーの設定
function setCookie(key, value, times, domainpath, secureflag) {
  var domain, path, secure, pos, cokies;
  var expire = new Date();
  domain = domainpath;
  path = cokies = '';
  if (parseInt(secureflag) == 1) {
    secure = 'true';
   }
  else {
    secure = '';
   }
  if (domain == null) {
    domain = '';
   }
  else {
    pos = domain.indexOf("/");
    if (pos < 0) {
      path = '';
     }
    else {
      path = domain.substring(pos, domain.length);
      domain = domain.substring(0, pos);
      if (path == '') {
        path = '/';
       }
     }
   }
  cokies = key + '=' + escape(value);
  if (times != null) {
    if (parseInt(times) > 0) {
      expire.setTime(expire.getTime() + parseInt(times));
      cokies = cokies + '; expires=' + expire.toGMTString();
     }
   }
  if (parseInt(path.length) > 0) {
    cokies = cokies + '; path=' + path;
   } 
  if (parseInt(domain.length) > 0) {
    cokies = cokies + '; domain=' + domain;
   } 
  if (parseInt(secure.length) > 0) {
    cokies = cokies + '; ' + secure;
   }
  document.cookie = cokies;
  return(cokies);
 }

//会員ログオンのチェック
function ck_member_logon(url_ok, url_ng) {
  if (location.protocol != 'file:') {
    if (getCookie('@t_member_logon') == '' || getCookie(getCookie('@t_member_logon')) == '') {
      if (url_ng == '') {
        return(0);
       }
      else {
        location.replace(url_ng);
       }
     }
    else if (url_ok == '') {
      return(1);
     }
    else {
      location.replace(url_ok);
     }
   }
  else if (url_ok == '') {
    return(1);
   }
  else {
    location.replace(url_ok);
   }
 }

// 送信ボタンの連続押し防止
var submit_time = 0;
function click_submit() {
  var now = new Date();
  var now_time = now.getTime();
  if (now_time - submit_time < 3000) {
    alert("サーバーが混み合っている可能性があります。\n恐れ入りますが、しばらくお待ちください。");
    return(false);
   }
  else {
    submit_time = now_time;
    return(true);
   }
 }

//カレンダー
function makeArray() {
  return this;
 }
function get_month_str(month) {
  var month_str = new makeArray();
  month_str[0] = 'January';  month_str[1] = 'February';month_str[2] = 'March';    month_str[3] = 'April';
  month_str[4] = 'May';      month_str[5] = 'June';    month_str[6] = 'July';     month_str[7] = 'August';
  month_str[8] = 'September';month_str[9] = 'October'; month_str[10] = 'November';month_str[11] = 'December';
  return(month_str[month]);
 }
function opencalender() {							// 別ウィンドウにカレンダーを表示
  var year, month;
  year = parseInt(document.forms[0].elements[0].selectedIndex) + 18;
  year = year * 10 + parseInt(document.forms[0].elements[1].selectedIndex);
  year = year * 10 + parseInt(document.forms[0].elements[2].selectedIndex);
  month = parseInt(document.forms[0].elements[3].selectedIndex) + 1;
  calender(year, month, 1);
 }
function is_rgb(rgb) {
  var i, j = parseInt(rgb.length);
  if (j != 6) return(false);
  for (i = 0; i < j; ++i) {
    if ("0123456789abcdefABCDEF".indexOf(rgb.charAt(i)) < 0) {
      return(false);
     }
   }
  return(true);
 }
function is_day(day) {
  var i, j = parseInt(day.length);
  for (i = 0; i < j; ++i) {
    if ("0123456789/".indexOf(day.charAt(i)) < 0) {
      return(false);
     }
   }
  return(true);
 }
function get_numofday(year, month) {						// 月末の日付の取得
  year  = parseInt(year);
  month = parseInt(month);
  var num_days = new makeArray();
  num_days[0] = 31;num_days[1] = 28;num_days[2] = 31;num_days[3] = 30;
  num_days[4] = 31;num_days[5] = 30;num_days[6] = 31;num_days[7] = 31;
  num_days[8] = 30;num_days[9] = 31;num_days[10] = 30;num_days[11] = 31;
  if ((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0)) {
    num_days[1] = 29;
   }
  return(parseInt(num_days[month]));
 }
function is_special(year, month, day, week) {					// 特別な日の判定
  year  = parseInt(year);
  month = parseInt(month);
  day   = parseInt(day);
  week  = parseInt(week);
  week  = (((((day - 1) % 7) + week) % 7) * -1) - 1;
  var the_date1 = (month + 1) * 100 + day;
  var the_date2 = ((year * 100) + month + 1) * 100 + day;
  var i = 0;
  var special = new makeArray();						// 特別な日の設定
  special[i++] = 20050102; special[i++] = 20050103;
  special[i++] = 20051229; special[i++] = 20051230; special[i++] = 20051231; special[i++] = 20060102; special[i++] = 20060103; special[i++] = 20060104;
  special[i++] = 20060814; special[i++] = 20060815; special[i++] = 20060816;
  special[i++] = 20061229; special[i++] = 20061230; special[i++] = 20061231; special[i++] = 20070102; special[i++] = 20070103; special[i++] = 20070104;
  special[i++] = 20070501; special[i++] = 20070502;
  special[i++] = 20070814; special[i++] = 20070815; special[i++] = 20070816;
  special[i++] = 20071231; special[i++] = 20080102; special[i++] = 20080103; special[i++] = 20080104;
  special[i++] = 0;
	// 特別な日の設定方法
	// 曜日指定：日曜日から土曜日まで順に -1,-2,-3,-4,-5,-6,-7
	// 日付指定：毎月X日の場合は X、毎年X月Y日の場合は X*100+Y、X年Y月Z日の場合は X*10000+Y*100+Z
	// ※重要※：配列の最後の要素は必ず 0 にする。
  for (i = 0; special[i] != 0; ++i) {
    if (special[i] == week || special[i] == day || special[i] == the_date1 || special[i] == the_date2) {
      return(true);
     }
   }
  return(false);
 }
function is_holiday(year, month, day, week) {					// 祝日の判定
  year  = parseInt(year);
  month = parseInt(month);
  day   = parseInt(day);
  week  = parseInt(week);
  var the_date = (month + 1) * 100 + day;
  var the_week = (month + 1) * 100;
  the_week = the_week + (Math.floor((day - 1) / 7) + 5) * 10;
  the_week = the_week + (week + day - 1) % 7;
  var i = 0;
  var holiday = new makeArray();						// 祝日の設定
  holiday[i++] =      101;		// 元日			1月1日
  holiday[i++] =      161;		// 成人の日		1月の第2月曜日
  holiday[i++] =      211;		// 建国記念の日	2月11日
  holiday[i++] = 20070321;		// 春分の日
  holiday[i++] = 20080320;		// 春分の日
  holiday[i++] = 20090320;		// 春分の日
  holiday[i++] = 20100321;		// 春分の日
  holiday[i++] = 20110321;		// 春分の日
  holiday[i++] = 20120320;		// 春分の日
  holiday[i++] = 20130320;		// 春分の日
  holiday[i++] = 20140321;		// 春分の日
  holiday[i++] = 20150321;		// 春分の日
  holiday[i++] = 20160320;		// 春分の日
  holiday[i++] = 20170320;		// 春分の日
  holiday[i++] = 20180321;		// 春分の日
  holiday[i++] = 20190321;		// 春分の日
  holiday[i++] =      429;		// 昭和の日		4月29日
  holiday[i++] =      503;		// 憲法記念日	5月3日
  holiday[i++] =      504;		// みどりの日	5月4日
  holiday[i++] =      505;		// こどもの日	5月5日
  holiday[i++] =      771;		// 海の日		7月の第3月曜日
  holiday[i++] =      971;		// 敬老の日		9月の第3月曜日
  holiday[i++] = 20070923;		// 秋分の日
  holiday[i++] = 20080923;		// 秋分の日
  holiday[i++] = 20090923;		// 秋分の日
  holiday[i++] = 20100923;		// 秋分の日
  holiday[i++] = 20110923;		// 秋分の日
  holiday[i++] = 20120922;		// 秋分の日
  holiday[i++] = 20130923;		// 秋分の日
  holiday[i++] = 20140923;		// 秋分の日
  holiday[i++] = 20150923;		// 秋分の日
  holiday[i++] = 20160922;		// 秋分の日
  holiday[i++] = 20170923;		// 秋分の日
  holiday[i++] = 20180923;		// 秋分の日
  holiday[i++] = 20190923;		// 秋分の日
  holiday[i++] =     1061;		// 体育の日		10月の第2月曜日
  holiday[i++] =     1103;		// 文化の日		11月3日
  holiday[i++] =     1123;		// 勤労感謝の日	11月23日
  holiday[i++] =     1223;		// 天皇誕生日	12月23日
  holiday[i++] =        0;
  for (i = 0; holiday[i] != 0; ++i) {
    if (holiday[i] == the_date || holiday[i] == the_week) {
      return(true);
     }
   }
  if (week < 6 && day % 7 == ((9 - week) % 7)) {				// 振替休日の判定
    if (day > 1) {
      day = day - 1;
     }
    else if (month > 0) {
      month = month - 1;
      day = get_numofday(year, month);
     }
    else {
      year = year - 1;
      month = 11;
      day = get_numofday(year, month);
     }
    if (is_holiday(year, month, day, 7)) {
      return(true);
     }
   }
  return(false);
 }
function calender(year, month, newwin) {					// カレンダーの表示
  year  = parseInt(year);
  month = parseInt(month);
  if (!year && !month) {
    alert("「年」と「月」が未入力です！再度入力して下さい。\n");
   }
  else if (!year) {
    alert("「年」が未入力です！再度入力して下さい。\n");
   }
  else if (!month) {
    alert("「月」が未入力です！再度入力して下さい。\n");
   }
  else if (month < 1 || month > 12) {
    alert("「月」の数値が範囲外です！\n「月」は「１から12」までの数値を入力してください。\n(" + month + ")");
   }
  else {
    var str_month = new makeArray();
    var bgc_today   = '';	// 今日の日付の背景色
    var fcl_today   = '';	// 今日の日付の色
    var fst_today   = 'U';	// 今日の日付の書体
    var fsz_today   = '';	// 今日の日付のフォントサイズ
    var bgc_sunday  = '999999';	// 日曜日の日付の背景色
    var fcl_sunday  = 'FFFFFF';	// 日曜日の日付の色
    var fst_sunday  = '';	// 日曜日の日付の書体
    var fsz_sunday  = '';	// 日曜日の日付のフォントサイズ
    var bgc_satday  = '999999';	// 土曜日の日付の背景色
    var fcl_satday  = 'FFFFFF';	// 土曜日の日付の色
    var fst_satday  = '';	// 土曜日の日付の書体
    var fsz_satday  = '';	// 土曜日の日付のフォントサイズ
    var bgc_holiday = '999999';	// 祝日の日付の背景色
    var fcl_holiday = 'FFFFFF';	// 祝日の日付の色
    var fst_holiday = '';	// 祝日の日付の書体
    var fsz_holiday = '';	// 祝日の日付のフォントサイズ
    var bgc_special = '999999';	// 特別な日の日付の背景色
    var fcl_special = 'FFFFFF';	// 特別な日の日付の色
    var fst_special = '';	// 特別な日の日付の書体
    var fsz_special = '';	// 特別な日の日付のフォントサイズ
	// 書体は B=太字(<b>)、I=斜体(<i>)、S=抹消線付(<s>)、
	//        T=タイプライタテキスト(<tt>)、U=下線付(<u>)
    var bg_color   = '';
    var font_style = '';
    var font_size  = '';
    var font_color = '';
    var tag_trtd   = '';
    var tag_tdtr   = '';
    var htm_table  = '<table border="2" cellspacing="0" cellpadding="1" width="170" bgcolor="#FFFFFF" class="small_text" bordercolor="#137505" bordercolorlight="#137505" bordercolordark="#137505">';
    htm_table = htm_table + '<tr align="center" class="text"><td colspan="7"><font color="#094B00"><b>' + get_month_str(month - 1) + ' ( ' + month + '月 )</b></font></td></tr>';
    var htm_week   = '<tr align="center" bgcolor="#F3F3F3"><td><font color="#FF0000"><b>日</b></font></td><td><b>月</b></td><td><b>火</b></td><td><b>水</b></td><td><b>木</b></td><td><b>金</b></td><td><font color="#0083AE"><b>土</b></font></td></tr>';
    var today_year  = parseInt(date_year);
    var today_month = parseInt(date_month) - 1;
    var today_day   = parseInt(date_day);
    if (today_month < 0) today_month = parseInt(date_month.substring(1, 2)) - 1;
    if (today_day <= 0) today_day = parseInt(date_day.substring(1, 2));
    var the_day     = new Date();
    the_day.setYear(year);
    the_day.setDate(1);
    the_day.setMonth(month - 1);
    var the_year  = the_day.getYear();
    if (the_year < 2000) the_year = the_year + 1900;
    var the_month = the_day.getMonth();
    var the_date  = the_day.getDate();
    var the_week  = the_day.getDay();
    var week_sun  = (8 - the_week) % 7;
    var week_sat  = (7 - the_week) % 7;
    var num_day   = get_numofday(the_year, the_month);
    var htm_days  = '<tr align="center">';
    var i = 0;
    if (the_week > 0) {
      for (i = 1; i <= the_week; ++i) {
        htm_days = htm_days + "<td>　</td>";
       }
     }
    else {
      htm_days = '';
     }
    for (i = 1;i <= num_day; ++i){
      tag_trtd   = '';
      bg_color   = '';
      font_style = '';
      font_size  = '';
      font_color = '';
      tag_tdtr   = '';
      if (i == today_day && the_month == today_month && the_year == today_year) {
        if (bgc_today != '') bg_color   = bgc_today;
        if (fst_today != '') font_style = fst_today;
        if (fsz_today != '') font_size  = fsz_today;
        if (fcl_today != '') font_color = fcl_today;
       }
      if (i % 7 == week_sun) {
        tag_trtd   = '<tr align="center">';
        if (bgc_sunday != '') bg_color   = bgc_sunday;
        if (fst_sunday != '') font_style = fst_sunday;
        if (fsz_sunday != '') font_size  = fsz_sunday;
        if (fcl_sunday != '') font_color = fcl_sunday;
       }
      else if (i % 7 == week_sat) {
        if (bgc_satday != '') bg_color   = bgc_satday;
        if (fst_satday != '') font_style = fst_satday;
        if (fsz_satday != '') font_size  = fsz_satday;
        if (fcl_satday != '') font_color = fcl_satday;
        tag_tdtr   = '</tr>';
       }
      if (is_holiday(the_year, the_month, i, the_week)) {
        if (bgc_holiday != '') bg_color   = bgc_holiday;
        if (fst_holiday != '') font_style = fst_holiday;
        if (fsz_holiday != '') font_size  = fsz_holiday;
        if (fcl_holiday != '') font_color = fcl_holiday;
       }
      if (is_special(the_year, the_month, i, the_week)) {
        if (bgc_special != '') bg_color   = bgc_special;
        if (fst_special != '') font_style = fst_special;
        if (fsz_special != '') font_size  = fsz_special;
        if (fcl_special != '') font_color = fcl_special;
       }
      tag_tdtr = '</td>' + tag_tdtr;
      if (bg_color != '') {
        tag_trtd = tag_trtd + '<td bgcolor="#' + bg_color + '">';
       }
      else {
        tag_trtd = tag_trtd + '<td>';
       }
      if (font_style != '') {
        if (font_style.indexOf('B') >= 0) {
          tag_trtd = tag_trtd + '<b>';
          tag_tdtr = '</b>' + tag_tdtr;
         }
        if (font_style.indexOf('I') >= 0) {
          tag_trtd = tag_trtd + '<i>';
          tag_tdtr = '</i>' + tag_tdtr;
         }
        if (font_style.indexOf('S') >= 0) {
          tag_trtd = tag_trtd + '<s>';
          tag_tdtr = '</s>' + tag_tdtr;
         }
        if (font_style.indexOf('T') >= 0) {
          tag_trtd = tag_trtd + '<tt>';
          tag_tdtr = '</tt>' + tag_tdtr;
         }
        if (font_style.indexOf('U') >= 0) {
          tag_trtd = tag_trtd + '<u>';
          tag_tdtr = '</u>' + tag_tdtr;
         }
       }
      if (font_color != '' || font_size != '') {
        tag_trtd = tag_trtd + '<font';
        if (font_size != '')  tag_trtd = tag_trtd + ' size="' + font_size + '"';
        if (font_color != '') tag_trtd = tag_trtd + ' color="' + font_color + '"';
        tag_trtd = tag_trtd + '">'
        tag_tdtr = '</font>' + tag_tdtr;
       }
      htm_days = htm_days + tag_trtd + i + tag_tdtr;
     }
    if (num_day % 7 != week_sat) {
      htm_days = htm_days + '</tr>';
     }
    htm_days = htm_days + '</table>';
    if (newwin) {
      calewin = window.open('', 'calender', 'width=200,height=200,status=0,scrollbars=0,directories=0,menubar=0,resizable=1,toolbar=0');
      if (calewin != null) {
        calewin.document.write('<bo' + 'dy bgcolor="#FAFAFA" background="back.gif" TEXT="#000000" LINK="#0000FF" ALINK="#0000FF" VLINK="#400080">');
        calewin.document.write('<table cellspacing="0" cellpadding="3" border="0" width="100%" height="100%">');
        calewin.document.write('<tr align="center" valign="center"><td><font size="4" color="#804000"><b>' + year + '年 ' + month);
        calewin.document.write('月</b></font></td></tr><tr align="center" valign="center"><td>');
        calewin.document.write(htm_table);
        calewin.document.write(htm_week);
        calewin.document.write(htm_days);
        calewin.document.write('</td></tr><tr align="center" valign="center"><td>');
        calewin.document.write('<form name="calewin"><input type="button" value="　閉じる　" onClick="self.close()"></form>');
        calewin.document.write('</td></tr></table></body></HT' + 'ML>');
       }
     }
    else {
      document.write(htm_table);
      document.write(htm_week);
      document.write(htm_days);
     }
   }
 }

