//--------------------------------------
// エンバーゴ期間設定
//--------------------------------------
embargo = {
  '2month' : { // ２ヵ月後
    '2011/12/29' : '2011/12/28',
    '2011/12/30' : '2011/12/28',
    '2011/12/31' : '2011/12/28',
    '2012/1/1' : '2011/12/28',
    '2012/1/2' : '2011/12/28',
    '2012/1/3' : '2011/12/28',
    '2012/1/4' : '2011/12/28'
      // 最後の行は行末にカンマ(,)不要です
  },
  '28days' : { // 28日後
    '2011/12/29' : '2012/1/5',
    '2011/12/30' : '2012/1/5',
    '2011/12/31' : '2012/1/5',
    '2012/1/1' : '2012/1/5',
    '2012/1/2' : '2012/1/5',
    '2012/1/3' : '2012/1/5',
    '2012/1/4' : '2012/1/5'
      // 最後の行は行末にカンマ(,)不要です
  }
};

embargo['2month1day'] = embargo['28days']; // 2ヶ月+1日後は28日後と同じ対応を使う。

//--------------------------------------
// 初期処理（28日後）
//--------------------------------------
currentDate = new Date(); // 現在日の取得
currentSec  = currentDate.getTime();
offsetDay   = 28; // 旅割の場合は[28]
offsetSec   = offsetDay * 1000 * 60 * 60 * 24; // ミリ秒指定

targetDate  = new Date();
targetDate.setTime(currentSec + offsetSec); // offsetDay 日後の日付を取得

targetYear  = targetDate.getFullYear();
targetMonth = targetDate.getMonth() + 1;
targetDay   = targetDate.getDate();
targetWeek  = targetDate.getDay();

// エンバーゴ処理
if (embargo['28days'][targetYear + '/' + targetMonth+'/' + targetDay]) {
  targetDate = new Date(embargo['28days'][targetYear + '/' + targetMonth+'/' + targetDay]);

  targetYear  = targetDate.getFullYear();
  targetMonth = targetDate.getMonth() + 1;
  targetDay   = targetDate.getDate();
  targetWeek  = targetDate.getDay();
}

//--------------------------------------
// 初期処理（2ヶ月後）
//--------------------------------------
// 各月の最終日　　　01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12
lastDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

y = currentDate.getFullYear();
m = currentDate.getMonth() + 2;
d = currentDate.getDate();

// 翌年に繰り越した場合の年月を調整
if (m > 11) {
  m = m % 12;
  y++;
}


//うるう年対応
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
  lastDays[1] = 29; // 2月の最終日を調整
}

// 月の最終日を確定
if (lastDays[m] < d) {
  d = lastDays[m];
}

// 当日が2/28or2/29の場合→4/30に設定
if ((currentDate.getMonth() + 1) == 2 && (currentDate.getDate() == 28 || currentDate.getDate() == 29)) {
  m = 3;
  d = 30;
}
// 当日が6/30の場合→8/31に設定
if ((currentDate.getMonth() + 1) == 6 && currentDate.getDate() == 30) {
  m = 7;
  d = 31;
}
// 当日が11/30の場合→1/31に設定
if ((currentDate.getMonth() + 1) == 11 && currentDate.getDate() == 30) {
  m = 0;
  d = 31;
}

// エンバーゴ処理
//alert((m + 1) + '/' + d);
if (embargo['2month'][y + '/' + (m + 1) + '/' + d]) {
  
  targetDate  = new Date(embargo['2month'][y + '/' + (m+1) + '/'+d]);
  
  targetYear2  = targetDate.getFullYear();
  targetMonth2 = targetDate.getMonth() + 1;
  targetDay2   = targetDate.getDate();
  targetWeek2  = targetDate.getDay();

  targetDate  = new Date(y + '/' + (m + 1) + '/' + d);
} else {
  
  targetDate  = new Date(y + '/' + (m + 1) + '/' + d);
  targetYear2  = targetDate.getFullYear();
  targetMonth2 = targetDate.getMonth() + 1;
  targetDay2   = targetDate.getDate();
  targetWeek2  = targetDate.getDay();
}

//--------------------------------------
// 初期処理（2ヶ月+1日後）
//--------------------------------------
targetDate.setDate(targetDate.getDate() + 1);

targetYear3  = targetDate.getFullYear();
targetMonth3 = targetDate.getMonth() +1;
targetDay3   = targetDate.getDate();
targetWeek3  = targetDate.getDay();

// エンバーゴ処理
if (embargo['28days'][targetYear3 + '/' + targetMonth3+'/' + targetDay3]) {
  targetDate = new Date(embargo['28days'][targetYear3 + '/' + targetMonth3+'/' + targetDay3]);

  targetYear3  = targetDate.getFullYear();
  targetMonth3 = targetDate.getMonth() + 1;
  targetDay3   = targetDate.getDate();
  targetWeek3  = targetDate.getDay();
}

//--------------------------------------
// 月部分の描画（28日後）
//--------------------------------------
function drawTargetMonth() {
  column1 = parseInt(targetMonth / 10);
  if (column1 == 0) {
    file1 = 'no.gif';
  }
  else {
    file1 = column1 + '.gif';
  }
  file2 = (targetMonth % 10) + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file1 + "'></td>\n");
  document.write("<td><img src='image070201/fday_img/" + file2 + "'></td>\n");
}


//--------------------------------------
// 月部分の描画（2ヶ月後）
//--------------------------------------
function drawTargetMonth2() {
  column1 = parseInt(targetMonth2 / 10);
  if (column1 == 0) {
    file1 = 'no.gif';
  }
  else {
    file1 = column1 + '.gif';
  }
  file2 = (targetMonth2 % 10) + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file1 + "'></td>\n");
  document.write("<td><img src='image070201/fday_img/" + file2 + "'></td>\n");
}


//--------------------------------------
// 月部分の描画（2ヶ月+1日後）
//--------------------------------------
function drawTargetMonth3() {
  column1 = parseInt(targetMonth3 / 10);
  if (column1 == 0) {
    file1 = 'no.gif';
  }
  else {
    file1 = column1 + '.gif';
  }
  file2 = (targetMonth3 % 10) + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file1 + "'></td>\n");
  document.write("<td><img src='image070201/fday_img/" + file2 + "'></td>\n");
}

//--------------------------------------
// 日部分の描画（28日後）
//--------------------------------------
function drawTargetDay() {
  column1 = parseInt(targetDay / 10);
  if (column1 == 0) {
    file1 = 'no.gif';
  }
  else {
    file1 = column1 + '.gif';
  }
  file2 = (targetDay % 10) + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file1 + "'></td>\n");
  document.write("<td><img src='image070201/fday_img/" + file2 + "'></td>\n");
}


//--------------------------------------
// 日部分の描画（2ヶ月後）
//--------------------------------------
function drawTargetDay2() {
  column1 = parseInt(targetDay2 / 10);
  if (column1 == 0) {
    file1 = 'no.gif';
  }
  else {
    file1 = column1 + '.gif';
  }
  file2 = (targetDay2 % 10) + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file1 + "'></td>\n");
  document.write("<td><img src='image070201/fday_img/" + file2 + "'></td>\n");
}


//--------------------------------------
// 日部分の描画（2ヶ月+1日後）
//--------------------------------------
function drawTargetDay3() {
  column1 = parseInt(targetDay3 / 10);
  if (column1 == 0) {
    file1 = 'no.gif';
  }
  else {
    file1 = column1 + '.gif';
  }
  file2 = (targetDay3 % 10) + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file1 + "'></td>\n");
  document.write("<td><img src='image070201/fday_img/" + file2 + "'></td>\n");
}

//--------------------------------------
// 曜日部分の描画（28日後）
//--------------------------------------
function drawTargetWeek() {
  nodeWeeks = new Array("sun","mon","tue","wed","thu","fri","sat");
  file = nodeWeeks[targetWeek] + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file + "'></td>\n");
}


//--------------------------------------
// 曜日部分の描画（2ヶ月後）
//--------------------------------------

function drawTargetWeek2() {
  nodeWeeks = new Array("sun","mon","tue","wed","thu","fri","sat");
  file = nodeWeeks[targetWeek2] + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file + "'></td>\n");
}


//--------------------------------------
// 曜日部分の描画（2ヶ月後）
//--------------------------------------
function drawTargetWeek3() {
  nodeWeeks = new Array("sun","mon","tue","wed","thu","fri","sat");
  file = nodeWeeks[targetWeek3] + '.gif';

  document.write("<td><img src='image070201/fday_img/" + file + "'></td>\n");
}

//--------------------------------------
// 搭乗日の初期選択
//--------------------------------------
function selectDefaultDate() {
  // 各月の最終日　　　01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12
  lastDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  y = currentDate.getFullYear();
  m = currentDate.getMonth() + 2;
  d = currentDate.getDate();

  // 翌年に繰り越した場合の年月を調整
  if (m > 11) {
    m = m % 12;
    y++;
  }

  //うるう年対応
  if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
    lastDays[1] = 29; // 2月の最終日を調整
  }

  // 月の最終日を確定
  if (lastDays[m] < d) {
    d = lastDays[m];
  }

  // 当日が2/28or2/29の場合→4/30に設定
  if ((currentDate.getMonth() + 1) == 2 && (currentDate.getDate() == 28 || currentDate.getDate() == 29)) {
    m = 3;
    d = 30;
  }
  // 当日が6/30の場合→8/31に設定
  if ((currentDate.getMonth() + 1) == 6 && currentDate.getDate() == 30) {
    m = 7;
    d = 31;
  }
  // 当日が11/30の場合→1/31に設定
  if ((currentDate.getMonth() + 1) == 11 && currentDate.getDate() == 30) {
    m = 0;
    d = 31;
  }

  document.segConditionForm.elements["segConditionForm.selectedEmbMonth"].selectedIndex = m;
  document.segConditionForm.elements["segConditionForm.selectedEmbDay"].selectedIndex   = d - 1;
}

//--------------------------------------------------------------
// ページ表示時点からn日後をモジュールのデフォルト値にセットする
//
// フォームの名前は 月 -> segConditionForm.selectedEmbMonth
//                  日 -> segConditionForm.selectedEmbDay
//--------------------------------------------------------------
function selectDefaultDateOffset (offset, form2)
{
  currentDate = new Date(); // 現在日の取得
  currentSec  = currentDate.getTime();
  offsetDay   = offset;
  offsetSec   = offsetDay * 1000 * 60 * 60 * 24; // ミリ秒指定

  targetDate  = new Date();
  targetDate.setTime(currentSec + offsetSec); // offsetDay 日後の日付を取得

  targetYear  = targetDate.getFullYear();
  targetMonth = targetDate.getMonth() + 1;
  targetDay   = targetDate.getDate();
  targetWeek  = targetDate.getDay();

if (embargo['28days'][targetYear + '/' + targetMonth+'/' + targetDay]) {
  targetDate = new Date(embargo['28days'][targetYear + '/' + targetMonth+'/' + targetDay]);

  targetYear  = targetDate.getFullYear();
  targetMonth = targetDate.getMonth() + 1;
  targetDay   = targetDate.getDate();
  targetWeek  = targetDate.getDay();
}
  // selectedIndex は 0 からカウントするので、月日それぞれ 1 を引く
  document.segConditionForm.elements["segConditionForm.selectedEmbMonth"].selectedIndex = targetMonth - 1;
  document.segConditionForm.elements["segConditionForm.selectedEmbDay"].selectedIndex   = targetDay - 1;

  if(typeof form2 != "undefined") {
      document.segConditionForm2.elements["segConditionForm.selectedEmbMonth"].selectedIndex = targetMonth - 1;
      document.segConditionForm2.elements["segConditionForm.selectedEmbDay"].selectedIndex   = targetDay - 1;
  }

}

//--------------------------------------------------------------
// 2か月日後の日付を指定されたフォーマットで文字列表示する
//
// %y -> 年(西暦4桁)
// %m -> 月(1-12)
// %d -> 日(1-31)
//--------------------------------------------------------------
function drawTargetDateString2 (format)
{  
  document.write(format.replace('%d', targetDay2).replace('%m', targetMonth2).replace('%y', targetYear2));
}

//--------------------------------------------------------------
// 2か月+1日後の日付を指定されたフォーマットで文字列表示する
//
// %y -> 年(西暦4桁)
// %m -> 月(1-12)
// %d -> 日(1-31)
//--------------------------------------------------------------
function drawTargetDateString3 (format)
{  
  document.write(format.replace('%d', targetDay3).replace('%m', targetMonth3).replace('%y', targetYear3));
}

