// 検索画面
// 画面内共通変数
var fldLastInputCityFld = "F0";
var map_file_last_selected;
var map_name_last_selected;

//引数として受け取った文字列をダイアログに表示する関数
function showDialog(message) {
    alert("駅名:" + message);
}

//出発駅設定
function f_setDepStaName(name){
    document.form1.F0.value = name;
    document.form1.T0.focus();
}

//到着駅設定
function f_setArrStaName(name){
    document.form1.T0.value = name;
    document.form1.F0.focus();
}

// チェックボックスに使用している名前
chn = new Array("p");
// 全てのチェックボックをチェックする
function chBxOn(){
  for(i=0; i<chn.length; i++) {
    document.form1.elements[chn[i]].checked = true;
  }
}

// 検索画面の入力チェック（検索ボタン押下時処理）
function chk_and_send(oj){
    var ng_msg="";
    
    //年を取得
    
    for(;;){
        //必須チェック
        //出発駅
        if(!oj.F0.value) {
            ng_msg="出発駅が入力されていません";
            oj.F0.focus();
            break;
        }
        //到着駅
        if(!oj.T0.value) {
            ng_msg="到着駅が入力されていません";
            oj.T0.focus();
            break;
        }
        //往路出発日（月）
        if(!oj.m0.value) {
            ng_msg="往路出発日が入力されていません";
            oj.m0.focus();
            break;
        }
        //往路出発日（日）
        if(!oj.d0.value) {
            ng_msg="往路出発日が入力されていません";
            oj.d0.focus();
            break;
        }
        //往路出発時刻（自）時
        if(!oj.hf0.value) {
            ng_msg="往路出発日（時刻）が入力されていません";
            oj.hf0.focus();
            break;
        }
        //往路出発時刻（自）分
        if(!oj.nf0.value) {
            ng_msg="往路出発日（時刻）が入力されていません";
            oj.nf0.focus();
            break;
        }
        //往路出発時刻（至）時
        if(!oj.ht0.value) {
            ng_msg="往路出発日（時刻）が入力されていません";
            oj.ht0.focus();
            break;
        }
        //往路出発時刻（至）分
        if(!oj.nt0.value) {
            ng_msg="往路出発日（時刻）が入力されていません";
            oj.nt0.focus();
            break;
        }
        
        //必須チェック（往復選択時）
        if(oj.rf[0].checked){
            //復路出発日（月）
            if(!oj.m1.value) {
                ng_msg="復路出発日が入力されていません";
                oj.m1.focus();
                break;
            }
            //復路出発日（日）
            if(!oj.d1.value) {
                ng_msg="復路出発日が入力されていません";
                oj.d1.focus();
                break;
            }
            //復路出発時刻（自）時
            if(!oj.hf1.value) {
                ng_msg="復路出発日（時刻）が入力されていません";
                oj.hf1.focus();
                break;
            }
            //復路出発時刻（自）分
            if(!oj.nf1.value) {
                ng_msg="復路出発日（時刻）が入力されていません";
                oj.nf1.focus();
                break;
            }
            //復路出発時刻（至）時
            if(!oj.ht1.value) {
                ng_msg="復路出発日（時刻）が入力されていません";
                oj.ht1.focus();
                break;
            }
            //復路出発時刻（至）分
            if(!oj.nt1.value) {
                ng_msg="復路出発日（時刻）が入力されていません";
                oj.nt1.focus();
                break;
            }
        }
        
        //人数（数値チェック）
        if(!isNumeric(oj.nA.value) ) {
            ng_msg="人数に数値がが入力されていません";
            oj.nA.focus();
            break;
        }
        if(!isNumeric(oj.nC.value) ) {
            ng_msg="人数に数値がが入力されていません";
            oj.nC.focus();
            break;
        }
        if(!isNumeric(oj.nY.value) ) {
            ng_msg="人数に数値がが入力されていません";
            oj.nY.focus();
            break;
        }
        if(!isNumeric(oj.nS.value) ) {
            ng_msg="人数に数値がが入力されていません";
            oj.nS.focus();
            break;
        }
        
        //人数（合計人数チェック）
        var sum = parseInt(oj.nA.value) + parseInt(oj.nC.value) + parseInt(oj.nY.value) + parseInt(oj.nS.value);
        if( (sum < 1) || (9 < sum) ) {
            ng_msg="人数は合計が１～９人の範囲で入力してください";
            oj.nA.focus();
            break;
        }
        
        //日付妥当性
        //往路出発日
        year_o = getYearFromMMDD(oj.m0.value,oj.d0.value);
        if( !isDate(year_o, oj.m0.value, oj.d0.value) ) {
            ng_msg="往路出発日が正しくありません";
            oj.m0.focus();
            break;
        }
        
        if(oj.rf[0].checked){
            //復路出発日（往復選択時のみ）
            year_f = getYearFromMMDD(oj.m1.value,oj.d1.value);
            if( !isDate(year_f, oj.m1.value, oj.d1.value) ) {
                ng_msg="復路出発日が正しくありません";
                oj.m1.focus();
                break;
            }
        }
        
        //時刻（数値チェック）
        //往路出発時刻（自）時
        if( !isNumeric(oj.hf0.value) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.hf0.focus();
            break;
        }
        //往路出発時刻（自）分
        if( !isNumeric(oj.nf0.value) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.nf0.focus();
            break;
        }
        //往路出発時刻（至）時
        if( !isNumeric(oj.ht0.value) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.ht0.focus();
            break;
        }
        //往路出発時刻（至）分
        if( !isNumeric(oj.nt0.value) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.nt0.focus();
            break;
        }
        
        if(oj.rf[0].checked){
            //復路出発時刻（自）時
            if( !isNumeric(oj.hf1.value) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.hf1.focus();
                break;
            }
            //復路出発時刻（自）分
            if( !isNumeric(oj.nf1.value) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.nf1.focus();
                break;
            }
            //復路出発時刻（至）時
            if( !isNumeric(oj.ht1.value) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.ht1.focus();
                break;
            }
            //復路出発時刻（至）分
            if( !isNumeric(oj.nt1.value) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.nt1.focus();
                break;
            }
        }
        
        //時刻（時刻チェック）
        //往路出発時刻（自）時
        buf=parseInt(oj.hf0.value);
        if( (buf<0) || (24<buf) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.hf0.focus();
            break;
        }
        //往路出発時刻（自）分
        buf=parseInt(oj.nf0.value);
        if( (buf<0) || (59<buf) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.nf0.focus();
            break;
        }
        //往路出発時刻（至）時
        buf=parseInt(oj.ht0.value);
        if( (buf<0) || (24<buf) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.ht0.focus();
            break;
        }
        //往路出発時刻（至）分
        buf=parseInt(oj.nt0.value);
        if( (buf<0) || (59<buf) ) {
            ng_msg="往路出発時刻が正しくありません";
            oj.nt0.focus();
            break;
        }
        
        if(oj.rf[0].checked){
            //復路出発時刻（自）時
            buf=parseInt(oj.hf1.value);
            if( (buf<0) || (24<buf) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.hf1.focus();
                break;
            }
            //復路出発時刻（自）分
            buf=parseInt(oj.nf1.value);
            if( (buf<0) || (59<buf) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.nf1.focus();
                break;
            }
            //復路出発時刻（至）時
            buf=parseInt(oj.ht1.value);
            if( (buf<0) || (24<buf) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.ht1.focus();
                break;
            }
            //復路出発時刻（至）分
            buf=parseInt(oj.nt1.value);
            if( (buf<0) || (59<buf) ) {
                ng_msg="復路出発時刻が正しくありません";
                oj.nt1.focus();
                break;
            }
        }
        
        //時刻（時刻範囲チェック）
        //往路出発時刻
        buf_f=parseInt(oj.hf0.value, 10)*100 + parseInt(oj.nf0.value, 10);
        buf_t=parseInt(oj.ht0.value, 10)*100 + parseInt(oj.nt0.value, 10);
        if( buf_t < buf_f ) {
            ng_msg="往路出発時刻の範囲が正しくありません";
            oj.hf0.focus();
            break;
        }
        //復路出発時刻
        if(oj.rf[0].checked){
            buf_f=parseInt(oj.hf1.value, 10)*100 + parseInt(oj.nf1.value, 10);
            buf_t=parseInt(oj.ht1.value, 10)*100 + parseInt(oj.nt1.value, 10);
            if( buf_t < buf_f ) {
                ng_msg="復路出発時刻の範囲が正しくありません";
                oj.hf1.focus();
                break;
            }
        }
        
        //日付（期間チェック）
        if(oj.rf[0].checked){
            buf_f=parseInt(year_o)*10000 + parseInt(oj.m0.value, 10)*100 + parseInt(oj.d0.value, 10);
            buf_t=parseInt(year_f)*10000 + parseInt(oj.m1.value, 10)*100 + parseInt(oj.d1.value, 10);
            if( buf_t < buf_f ) {
                ng_msg="復路出発日が往路出発日より前に設定されています";
                oj.m1.focus();
                break;
            }
        }
        
        break;
    }
    
    if(ng_msg==""){
        oj.mfile.value = map_file_last_selected;
        oj.mname.value = map_name_last_selected;
        ////▼20080417 アルテシアナイト、シティナイトライン、エリプソス対応
        //	if(map_file_last_selected=="Artesia.gif"){ document.form1.action = "result_artesia.php"; } 
        //	else if(map_file_last_selected=="Citynight.swf"){ document.form1.action = "result_citynight.php"; }
        //	else if(map_file_last_selected=="Elipsos.gif"){ document.form1.action = "result_elipsos.php"; }
        ////▲20080417
        oj.submit();
    }else{
        alert(ng_msg);
        return false;
    }
}

//都市をセット
//地図で都市をクリックされた時に呼び出される
// function f_setCityName(pnm) {
// 	if (pnm != ""){
// 	  //都市名を入力欄に設定
// 	  switch (fldLastInputCityFld){
// 	    case "F0":
// 	      document.form1.F0.value = pnm;
// 	      document.form1.T0.focus();
// 	      break;
// 	    case "T0":
// 	      document.form1.T0.value = pnm;
// // ▼ upd 20071219
// //	      document.form1.K0.focus();
// 	      document.form1.m0.focus();
// // ▲ upd 20071219
// 	      break;
// //	    case "K0":
// //	      document.form1.K0.value = pnm;
// //	      document.form1.m0.focus();
// //	      break;
// 	    default:
// 	      break;
// 	  }
// 	}
// }
function f_setCityName(pnm) {
    //alert("駅名:+" + pnm);
    if (pnm != ""){
        //alert("SET " + pnm);
        //都市名を入力欄に設定
        switch (fldLastInputCityFld){
          case "F0":
            document.form1.F0.value = pnm;
            document.form1.T0.focus();
            break;
          case "T0":
            document.form1.T0.value = pnm;
            document.form1.m0.focus();
            break;
          default:
            break;
        }
    }else{
        alert("ERROR!+" + pnm);
    }
}

//ヨーロッパ地図を表示する(初期のみ)
function f_country(agent_id) {
//  map_file = "Europe.gif";
    map_file = "euromap.swf";
    map_name = "ヨーロッパ地図";
    map_file_last_selected = map_file;
    map_name_last_selected = map_name;
    map_link = "http://www.railohshu.jp/ohs_rail/premium_af/index.php?Ag=" + agent_id;
    
    html_src  = '<table width="100%"><tr>';
    //  html_src += '<td align="left"><b>' + map_name + '</b></td>';
    html_src += '<td align="left"><img src="img/tit_euromap.gif" alt="' + map_name + '"></TD>';
    html_src += '<td align="right"><a href="' + map_link + '" target="_blank"><img src="img/tit_syoukai.gif" alt="鉄道紹介" border="0"></a></td>';
    html_src += '</tr></table>';
    
    html_src += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="550" id="euromap" align="middle">';
    html_src += '<param name="allowScriptAccess" value="sameDomain" />';
    html_src += '<param name="allowFullScreen" value="false" />';
    html_src += '<param name="movie" value="euromap.swf" />';
    html_src += '<param name="menu" value="false" />';
    html_src += '<param name="quality" value="high" />';
    html_src += '<param name="bgcolor" value="#555555" />';
    html_src += '<embed src="euromap.swf" menu="false" quality="high" bgcolor="#aec5c5" width="550" height="550" name="euromap" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    html_src += '</object>';
    
    html_src += '<map name="map_icon">';
    html_src += getMapTagDataFromMap(map_file);
    html_src += '</map>';
    
    //  html_src += '<br><br>イタリアおよびスペインの列車は、午前中にお申込みいただいた際には予約が確定していない場合があります。あらかじめご了承下さい。';
    document.getElementById("fld").innerHTML = html_src;
    //document.getElementById("rosen_pankzu").innerHTML = "";
    
    //出発時間初期値をセット
    set_default_time(false);
}

//出発時間初期値をセットする
function set_default_time(is_night_train){
    var frm = document.form1;
    if(is_night_train){
        frm.hf0.value = "00";
        frm.nf0.value = "00";
        frm.ht0.value = "23";
        frm.nt0.value = "59";
        frm.hf1.value = "00";
        frm.nf1.value = "00";
        frm.ht1.value = "23";
        frm.nt1.value = "59";
    }else{
        frm.hf0.value = "08";
        frm.nf0.value = "00";
        frm.ht0.value = "10";
        frm.nt0.value = "00";
        frm.hf1.value = "08";
        frm.nf1.value = "00";
        frm.ht1.value = "10";
        frm.nt1.value = "00";
    }
}

//国地図もしくは路線図を表示する
function f_city(map_file, agent_id) {
    map_file_last_selected = map_file;
    
    map_type = "";
    map_height = 0;
    
    var is_night_train = false;
    
    switch (map_file){
      case "Eurostar.gif":
        map_name = "ユーロスター";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/eurostar.php?Ag=" + agent_id;
        break;
      case "Thalys.gif":
        map_name = "タリス〔Ｔｈａｌｙｓ〕";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/thalys.php?Ag=" + agent_id;
        break;
      case "Tgv.gif":
        map_name = "ＴＧＶ";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/tgv.php?Ag=" + agent_id;
        break;
      case "Es.gif":
        map_name = "ＥＳ";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/es.php?Ag=" + agent_id;
        break;
      case "Ice.gif":
        map_name = "ＩＣＥ";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/ice.php?Ag=" + agent_id;
        break;
      case "Ave.gif":
        map_name = "ＡＶＥ";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/ave.php?Ag=" + agent_id;
        break;
      case "Glacier.gif":
        map_name = "氷河特急・ベルニナ特急";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/gacier.php?Ag=" + agent_id;
        break;
      case "William.gif":
        map_name = "ウィリアムテル特急";
        map_link = "http://www.railohshu.jp/";
        break;
      case "Golden.gif":
        map_name = "ゴールデンパスライン";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/goldenpass.php?Ag=" + agent_id;
        break;
      case "X2000.gif":
        map_name = "Ｘ２０００ (スカンジナビア特急)";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/x2000.php?Ag=" + agent_id;
        break;
      case "Artesia.gif":
        map_name = "アルテシアナイト";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/artesianight.php?Ag=" + agent_id;
        is_night_train = true;
        break;
      case "Citynight.swf":
        map_name = "シティナイトライン";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/citynightline.php?Ag=" + agent_id;
        map_type = "swf";
        map_height = 620;
        is_night_train = true;
        break;
      case "Elipsos.gif":
        map_name = "エリプソス";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/elipsos.php?Ag=" + agent_id;
        is_night_train = true;
        break;
      case "TgvEurope.gif":
        map_name = "ＴＧＶヨーロッパ東線";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/tgvest.php?Ag=" + agent_id;
        break;
      case "MontSaintMichel.gif":
        map_name = "モン・サン・ミッシェル";
        map_link = "http://www.railohshu.jp/ohs_rail/premium_af/montsaintmichel.php?Ag=" + agent_id;
        break;
      default:
        map_name = "";
        map_link = "";
        break;
    }
    map_name_last_selected = map_name;
    set_default_time(is_night_train);
    
    if(map_type=="swf"){
        //FLASH
        
        html_src  = '<table width="100%"><tr>';
        //  html_src += '<td align="left"><b>' + map_name + '</b></td>';
        html_src += '<td align="left"><img src="img/tit_' + map_file.substring(0, map_file.length - 3) + 'gif" alt="' + map_name + '"></TD>';
        html_src += '<td align="right"><a href="' + map_link + '" target="_blank"><img src="img/tit_syoukai.gif" alt="鉄道紹介" border="0"></a></td>';
        html_src += '</tr></table>';
        
        html_src += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="' + map_height + '" id="euromap" align="middle">';
        html_src += '<param name="allowScriptAccess" value="sameDomain" />';
        html_src += '<param name="allowFullScreen" value="false" />';
        html_src += '<param name="movie" value="' + map_file + '" />';
        html_src += '<param name="menu" value="false" />';
        html_src += '<param name="quality" value="high" />';
        html_src += '<param name="bgcolor" value="#555555" />';
        html_src += '<embed src="' + map_file + '" menu="false" quality="high" bgcolor="#aec5c5" width="550" height="' + map_height + '" name="euromap" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
        html_src += '</object>';
        
        html_src += '<map name="map_icon">';
        html_src += getMapTagDataFromMap(map_file);
        html_src += '</map>';
        
        //  html_src += '<br><br>イタリアおよびスペインの列車は、午前中にお申込みいただいた際には予約が確定していない場合があります。あらかじめご了承下さい。';
        document.getElementById("fld").innerHTML = html_src;
        document.getElementById("rosen_pankzu").innerHTML = "";
        
        if(map_name != ""){
            document.getElementById("rosen_pankzu").innerHTML = " > " + map_name;
        }else{
            document.getElementById("rosen_pankzu").innerHTML = "";
        }
        
    }else{
        //gif
        html_src  = '<table width="100%"><tr>';
        
        //gif対応
        html_src += '<td align="left"><img src="img/tit_' + map_file + '" alt="' + map_name + '"></TD>';
        if(map_link!=""){
            html_src += '<td align="right"><a href="' + map_link + '" target="_blank"><img src="img/tit_syoukai.gif" alt="鉄道紹介" border="0"></a></td>';
        }
        html_src += '</tr></table>';
        
        html_src += '<IMG SRC="map/' + map_file + '" usemap="#map_icon_' + map_file + '">';
        html_src += '<map name="map_icon_' + map_file + '">';
        html_src += getMapTagDataFromMap(map_file);
        html_src += '</map>';
        
        //index.phpへ移行  
        //  html_src += '<br><table width="100%"><td align="left"><input type="button" name="to_allmap" value="ヨーロッパ地図へ" onClick="f_country()"></td>';
        //  html_src += '<td align="right"><a href="http://www.booking.com/index.html?aid=307829;label=s112" target="_blank">ホテル情報はこちら</a></td></table>';
        //  html_src += '<br><br>イタリアおよびスペインの列車は、午前中にお申込みいただいた際には予約が確定していない場合があります。あらかじめご了承下さい。';
        document.getElementById("fld").innerHTML = html_src;
        if(map_name != ""){
            document.getElementById("rosen_pankzu").innerHTML = " > " + map_name;
        }else{
            document.getElementById("rosen_pankzu").innerHTML = "";
        }
    }
}

//入力駅項目名を記録
//出発駅・到着駅・経由駅のどこにクリックした都市をセットするかを制御するために使う
function setInputCity(a) {
    fldLastInputCityFld = a;
}

//駅名入力補完
function findValue(li) {
    if( li == null ) return;
    if( !!li.extra ) var sValue = li.extra[0];
    else var sValue = li.selectValue;
}

function selectItem(li) {
    findValue(li);
}

function formatItem(row) {
    //var str = row[0] + ' [' + row[1] + ']';
    var str = row[0];
    if(row[2] && row[2] != '')
        str += ' (' + row[2] + ')';
    return str;
}

// 日付入力カレンダー
function popUpCalendarIdx(ctl, day0, month0, format, day1, month1, numDayMod, y)
{
    offset = $('#' + ctl.id).offset();
    fixedX = offset.left - 100;
    fixedY = offset.top + 20;
    d1 = day1;
    m1 = month1;
    numUpdateCalDayMod = numDayMod;
    var dDate = new Date();
    dDate.setDate(dDate.getDate() + 5);
    gMinDate=dDate.getDate();
    gMinMonth=dDate.getMonth();
    gMinYear=dDate.getFullYear();
    popUpCalendar2(ctl,day0,month0,format);
}

function popUpCalendarIdx2(ctl, day0, month0, format, day1, month1, roundtrip, y){
    offset = $('#' + ctl.id).offset();
    fixedX = offset.left - 100;
    fixedY = offset.top + 20;
    if (!roundtrip.checked) return;
    numUpdateCalDayMod = 0;
    
    if((day1 != null)&&(month1 != null)){ 
        var date1 = getDate( day1, month1 );
        gMinDate=date1.getDate();
        gMinMonth=date1.getMonth();
        gMinYear=date1.getFullYear();
    }
    else {
        var dDate = new Date();
        dDate.setDate(dDate.getDate() + 5);
        gMinDate=dDate.getDate();
        gMinMonth=dDate.getMonth();
        gMinYear=dDate.getFullYear();   
    }
    popUpCalendar2(ctl,day0,month0,format);
}
