// JRセットプラン検索 Ver.2　→目的地：北海道・1室定員設定なし

// デフォルト値と当日との差
var add = 5;

// 初期値を設定
function jrInit() {
  
  // IE戻るボタン対応
  // エリアリスト作成
  onSelectMenu(document.jrSearch.kc);

  var now = new Date();
  now.setTime(now.getTime() + add * 24 * 60 * 60 * 1000);
  var yy = now.getYear();
  if (yy < 2000) yy += 1900;
  var mm = now.getMonth() + 1;
  var dd = now.getDate();
  document.jrSearch.year.options.length = 0;
  for(i = 0; i < 2; i++) {
	document.jrSearch.year.options[i] = new Option(yy , yy, i==0, i==0);
	yy++;
  }
  document.jrSearch.month.options[mm - 1].selected = true;
  document.jrSearch.day.options[dd - 1].selected = true;
}

// 宿泊日の初期値を設定のオンロード
try {
	window.addEventListener('load', jrInit, false);
} catch (e) {
	window.attachEvent('onload', jrInit);
}

// 閏年チェック
function checkLeapYear(year) {
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
    return 29;
  }
  return 28;
}

// 年をまたぐ時の処理
function onChangeYear() {
  var y = document.jrSearch.year.value;
  var n = document.jrSearch.month.selectedIndex;

  if ( y < 2010 && n < 11 ){
  document.jrSearch.year.selectedIndex = 1;
  
  }
}

// 都道府県名定義
var p = [
	["----------"],
//	["都道府県を選択","北海道"],
	["都道府県を選択","青森県","岩手県","宮城県","秋田県","山形県","福島県"],
	["都道府県を選択","茨城県","栃木県","群馬県","埼玉県","千葉県","東京都","神奈川県"],
	["都道府県を選択","新潟県","山梨県","長野県"],
	["都道府県を選択","岐阜県","静岡県","愛知県","三重県"],
	["都道府県を選択","富山県","石川県","福井県"],
	["都道府県を選択","滋賀県","京都府","大阪府","兵庫県","奈良県","和歌山県"],
	["都道府県を選択","鳥取県","島根県","岡山県","広島県","山口県"],
	["都道府県を選択","徳島県","香川県","愛媛県","高知県"],
	["都道府県を選択","福岡県","佐賀県","長崎県","熊本県","大分県","宮崎県","鹿児島県"],
//	["都道府県を選択","沖縄県"]
];

// 都道府県コード定義
var a = [
	[""],
//	["","01"],
	["","02","03","04","05","06","07"],
	["","08","09","10","11","12","13","14"],
	["","15","19","20"],
	["","21","22","23","24"],
	["","16","17","18"],
	["","25","26","27","28","29","30"],
	["","31","32","33","34","35"],
	["","36","37","38","39"],
	["","40","41","42","43","44","45","46"],
//	["","47"]
];

// 方面、都道府県リスト切替
function onSelectMenu(sel) {
	var index = sel.selectedIndex;
	if (index > a.length-1) index = 0;

	document.jrSearch.tgpc.options.length = p[index].length;
	for (var i = 0; i < p[index].length; i++) {
		document.jrSearch.tgpc.options[i] = new Option(p[index][i], a[index][i], false, false);
	}
	document.jrSearch.tgpc.options[0].selected = true;
	if (document.layers) {
		top.resizeBy(-10,-10)
		top.resizeBy(10,10)
	}
	return;
}

// 都道府県、エリア選択チェック
function jrCheckCode() {
  // 交通機関、出発地
  if (document.jrSearch.dep.value == 0) {
    alert("出発地をお選びください。");
    document.jrSearch.dep.focus();
    return false;
  }
  
  /* 方面、都道府県
  if (document.jrSearch.kc.value == 0) {
    alert("方面をお選びください。");
    document.jrSearch.kc.focus();
    return false;
  }
  if (document.jrSearch.tgpc.value == 0) {
    alert("都道府県をお選びください。");
    document.jrSearch.tgpc.focus();
    return false;
  }*/

  /* １室定員
  if (document.jrSearch.type.value == 0) {
    alert("1室定員を選択して下さい。");
    document.jrSearch.type.focus();
    return false;
  }*/

  // 出発日
  var iyy = document.jrSearch.year.value;
  var imm = document.jrSearch.month.value;
  var idd = document.jrSearch.day.value;

  if (imm == 2 && idd > 28) {
    if (idd > checkLeapYear(iyy)) {
      alert("出発日が不正です。");
      document.jrSearch.day.focus();
      return false;
    }

    } else if (imm == 4 && idd > 30) {
      alert("出発日が不正です。");
      document.jrSearch.day.focus();
      return false;
    } else if (imm == 6 && idd > 30) {
      alert("出発日が不正です。");
      document.jrSearch.day.focus();
      return false;
    } else if (imm == 9 && idd > 30) {
      alert("出発日が不正です。");
      document.jrSearch.day.focus();
      return false;
    } else if (imm == 11 && idd > 30) {
      alert("出発日が不正です。");
      document.jrSearch.day.focus();
      return false;
    }

	return true;

}

function aftJrSearch(sid) {
  if (jrCheckCode()==true) {
    var dep = document.jrSearch.dep.value;
    var kc = document.jrSearch.kc.value;
    var tgpc = document.jrSearch.tgpc.value;
    var year = document.jrSearch.year.value;
    var month = document.jrSearch.month.value;
    var day = document.jrSearch.day.value;
	
	var urlstring = 'http://search.nta.co.jp/nta_dom/jrplan/index.jsp?' +
                     'Departure=' + dep +
                     '&Direction=' + kc +
                     '&Pref=' + tgpc +
                     //'&SelectDay=' + year + month + day +
					 '&StartDay=' + year + month + day +
 					 '&StartMonth=' + year + month;
	
	planUrl = escape(urlstring);
	rsvUrl = 'http://rsv.nta.co.jp/Affiliate/set/AF100101.aspx?SITE_ID=' + sid + '&REDI_URL=' + planUrl;
	
	window.open(rsvUrl);
	
  }
}
