var at=1;

var top = "/map/uc/Top?";
var map = "/map/uc/Maps?";

/*
 * 共通
 */
function commonRequest( _url ){
	MXmlRequest.open(null,'GET',_url,true,
								function( http_obj ){
									eval( http_obj.responseText );
								}
	);
};

/*
 * POIフリーワード検索
 * 2008/01/09
 */
function freeSearch( f ){
	if( !f ) return;
	searchPOI(f);
}

/*
 * ランドマーク用検索
 * 2008/01/09
 */
function landSearch( fm, val ){
	if( !val ) return false;
	
	// 都道府県・市区町村を初期化
	document.getElementById("kentmp").value="";
	document.getElementById("citytmp").value="";
	document.getElementById("stationtmp").value="";
	
	fm.submit();
	return false;
};

/*
 * マウスカーソルを変更する
 * 2008/01/09
 */
function changeCursor(obj){
	obj.style.cursor="pointer";
};

/*
 * PoiCountの再検索(料理)
 * 2008/01/10
 */
function research2(flg){
	// リセット
	document.getElementById("ken").innerHTML = '';
	document.getElementById("city").innerHTML = '';
	//document.getElementById("station").innerHTML = '';

	if( flg == 1 ) {
		document.search3.reset();
		document.getElementById("foodg").style.color="#000000";
		document.getElementById("foodst").style.color="#999999";
		
		// フォームをdisableにする
		doDisabled( document.search2, true );
		doDisabled( document.search3, true );
	
	} else if( flg == 2 ) {
		document.search2.reset();
		document.getElementById("foodst").style.color="#000000";
		document.getElementById("foodg").style.color="#999999";
		
		// フォームをdisableにする
		doDisabled( document.search2, true );
		doDisabled( document.search3, true );
		
	}
	
	ph2 = createParamForCook( document.getElementById("search2") );
	ph3 = createParamForCook( document.getElementById("search3") );
	// 両方共チェックがなかったら何もしない
	if( ph2.getkeys() == "" && ph3.getkeys() == "" ) {
		document.getElementById("cautionText").style.display="block";
		
		// 文言出力
		document.getElementById("cautionText").innerHTML="※ 該当する都道府県がありません。</br>&nbsp;&nbsp;&nbsp;上の料理のチェックボックスにチェックを入れてください。";
		
		// 見た目変更
		document.images["prefimg"].src="/sevenandifs/images/list_bbtn_disabled.gif";
	
		// ロックフラグを戻す
		lockflg=false;
		// フォームのdisabledをやめる
		doDisabled( document.search2, false );
		doDisabled( document.search3, false );
		return;
	}
	
	document.getElementById("cautionText").style.display="none";
	
	makeRequest( "ken", "getList" );
};

/*
 * タブの切り替え1
 * 2008/01/08
 */
function changeTab(type){
	if( lockflg ) return;
	// アクティブなら何もしない
	if( type == "TabsBrand" && at == 1 ) return;
	if( type == "TabsFood" && at == 2 ) return;
	// タブの画像とメニューを変更
	document.getElementById(type).style.display="block";
	document.getElementById(type+"Image").src="/sevenandifs/images/top_"+type+".gif";
	
	// 検索が終わるまでロックする
	lockflg = true;
	
	// 非表示にする。PoiCount再検索
	if( type=="TabsBrand") {
		// activeTabフラグを変更
		at=1;
		document.getElementById("TabsFood").style.display="none";
		document.getElementById("TabsFoodImage").src="/sevenandifs/images/top_TabsFood_off.gif";
		
		// フォームのdisabledにする
		doDisabled( document.search, true );
		
		// 見た目変更
		document.images["prefimg"].src="/sevenandifs/images/list_bbtn_disabled.gif";
		
		research();
	} else {
		// activeTabフラグを変更
		at=2;
		document.getElementById("TabsBrand").style.display="none";
		document.getElementById("TabsBrandImage").src="/sevenandifs/images/top_TabsBrand_off.gif";
	
		// フォームのdisabledにする
		doDisabled( document.search2, true );
		doDisabled( document.search3, true );

		research2();
	}
};

/*
 * エリアから探すプルダウン作成(Request)
 * 2008/01/08
 */
function areaSelect(grpby, val){
	if( val == "00" ) {
		opt = document.createElement("option");
		opt.value="00";
		opt.appendChild(  document.createTextNode("選択して下さい") );
		document.getElementById( "areacity" ).disabled="disabled";
		document.getElementById( "areacity" ).innerHTML="";
		document.getElementById( "areacity" ).appendChild(opt);
		// 見た目変更
		document.images["areaimg"].src="/sevenandifs/images/map_btn_disabled.gif";
		return;
	};
	var param="";
	param += "grp="+ph.get("grp");
	param += "&grpby=" + (grpby?grpby:"ken");
	if( val ) param += "&kencode="+val;
	param += "&callback=area&json=1&vo=mbml&pt=1&pp=print_start_date,print_end_date";
	// ブランド毎のカウントを取得する
	if( parseInt(ph.get("br")) > 1 ) param += "&disp_str40="+ph.get("br");
	commonRequest( poicount + param );
};
/*
 * エリアから探すプルダウン作成
 * 2008/01/08
 */
function area( obj ){
	// プルダウンオブジェクト取得
	var s = document.getElementById( "area" + obj.mbml.Parameter.grpby );
	s.innerHTML = "";
	if( obj.mbml.Parameter.grpby == "ken" ) {
		opt = document.createElement("option");
		opt.value="00";
		opt.appendChild(  document.createTextNode("選択して下さい") );
		s.appendChild(opt);
		// cityのところ
		opt2 = document.createElement("option");
		opt2.value="00";
		opt2.appendChild(  document.createTextNode("選択して下さい") );
		document.getElementById( "areacity" ).disabled="disabled";
		document.getElementById( "areacity" ).innerHTML="";
		document.getElementById( "areacity" ).appendChild(opt2);
		// 見た目変更
		document.images["areaimg"].src="/sevenandifs/images/map_btn_disabled.gif";
	}
	// リストを配列で取得
	var counts = obj.mbml.CountList.Count instanceof Array ? obj.mbml.CountList.Count : [obj.mbml.CountList.Count];
	for( i=0; i<counts.length; i++ ) {
		opt = document.createElement("option");
		txt = document.createTextNode(counts[i].count_name);
		opt.value=counts[i].count_id;
		opt.setAttribute("nl", counts[i].count_ido);
		opt.setAttribute("el", counts[i].count_keido);
		opt.appendChild(txt);
		s.appendChild(opt);
	}
	
	// disabledを外す
	if( obj.mbml.Parameter.grpby == "city" ) {
		document.getElementById( "areacity" ).disabled="";
		// 見た目変更
		document.images["areaimg"].src="/sevenandifs/images/btn_map.gif";
	};
};

/*
 * Form内でパラメータを作成する(料理で絞り込み用)
 */
function createParamForCook( form ){
	var element = form.elements.length;
	var cookph = new mapionFuncs.text.ParamHash();
	
	for( var i=0; i<element; i++ ){
		if( form.elements[i].type != "checkbox" ) continue;
		if( form.elements[i].checked ) cookph.put( form.elements[i].id, form.elements[i].value );
	};
	return cookph;
};

/*
 * POI一覧ページへ遷移
 */
function goList(){
	var search = document.addForm;
	// 市区町村まで選択されていないと何もしない
	if( !search.city.value ) return;
	
	var param="grp="+ph.get("grp")+"&br="+ph.get("br")+"&xsl=searchTop";
	param += "&citycode="+search.city.value;

	// 駅まで選択されている場合、駅コードも一緒に飛ばす
	//if( search.station.selectedIndex > -1) {
	//	param += "&stcode="+search.station.options[search.station.selectedIndex].value;
	//}
	
	// ブランド、料理選択、サービス状態を引き継ぐ
	if( at==1 ) {
		var s1="";
		if( parseInt( ph.get("br") ) == 1 ) {
			s1 = createParam(document.search);
			param += "&disp_str40="+s1;
		} else {
			s1 = createParam2(document.search);
			param += "&svc="+s1;
		}
	} else {
		s2 = createParamForCook(document.search2);
		s3 = createParamForCook(document.search3);
		
		if( s2.getkeys().toString() != "" ) param += "&search=" + s2.getkeys().toString();
		else param += "&search=" + s3.getkeys().toString();
	}
	
	// チェック状態フラグ削除
	document.getElementById("kentmp").value="";
	document.getElementById("citytmp").value="";
	document.getElementById("stationtmp").value="";
	
	// リストへ飛ぶ
	window.location = top + param;
};

/*
 * 市区町村にある駅の最寄検索を行う(Request)
 * 2008/01/08
 * @param val 市区町村コード
 * サービス、料理で絞込み
 */
function stationSelect( val ){
	// 見た目変更
	document.images["prefimg"].src="/sevenandifs/images/btn_list3.gif";
	
	// チェック状態を引き継ぐ
	document.getElementById("citytmp").value = val;

	// タブをロック解除
	lockflg=false;
	if( at == 1 ) {
		doDisabled( document.search, false );
	} else {
		doDisabled( document.search2, false );
		doDisabled( document.search3, false );
	}
				
	/*
	var param="";
	param += "grp="+ph.get("grp")+"&sgrp=station01&ngrp="+ph.get("grp")+"&ndist=1500";
	param += "&callback=station&json=1&vo=mbml&pm=50";
	param += "&citycode="+val;
	
	// 最寄店舗を汎用条件で絞込み
	if( at == 1 ) {
		if( ph.get("br") == "01" ) {
			param += createParamForService(document.getElementById('search'));
			param += "&nsearch=disp_str40";
		} else {
			var srvch = createParamForService2( document.getElementById("search") ) ;
			if( srvch.getkeys() != "" ) {
				param += "&" + srvch;
				param += "&nsearch=disp_str40,"+srvch.getkeys();
			}
		}
	} else {
		cph2 = createParamForCook(document.getElementById('search2'));
		cph3 = createParamForCook(document.getElementById('search3'));
		//if( cph2.getkeys() == "" && cph3.getkeys() == "" ) return;
		if( cph2.getkeys() != "" ) {
			param += "&" + cph2;
			param += "&nsearch="+(cph2.getkeys()).toString();
			param += "&unit="+(cph2.getkeys()).toString().replace(/,/g,":");
		} else {
			param += "&" + cph3;
			param += "&nsearch="+(cph3.getkeys()).toString();
			param += "&unit="+(cph3.getkeys()).toString().replace(/,/g,":");
		}
	}
	
	// チェック状態を引き継ぐ
	document.getElementById("citytmp").value = val;
	commonRequest( poiattr + param );
	*/
};

/*
 * 市区町村にある駅の最寄検索を行う2(Request)
 * @param val 市区町村コード
 * フリーワードで絞込み
 */
function stationSelect2( val ){
	var param="";
	param += "grp="+ph.get("grp")+"&sgrp=station01&ngrp="+ph.get("grp")+"&ndist=1500";
	param += "&callback=station&json=1&vo=mbml&pm=50";
	param += "&citycode="+val;
	
	// フリーワードセット
	if( ph.get("q") ) {
		param += "&q="+ph.get("q");
		param += "&nsearch=q";
	}
	// チェック状態を引き継ぐ
	document.getElementById("citytmp").value = val;
	commonRequest( poiattr + param );
	
};
	
/*
 * 市区町村にある駅の最寄検索プルダウン作成
 * 2008/01/08
 */
function station( obj ){
	// 最寄駅プルダウンオブジェクト取得
	var s = document.getElementById("station");
	s.disabled=false;
	var tmpcode = document.getElementById("stationtmp").value;

	// 0件フラグ
	var zerof = true;
	s.innerHTML="";
	// 0件なら何もしない
	//if( obj.mbml.PoiList.Poi ) {
		var pois = obj.mbml.PoiList.Poi instanceof Array ? obj.mbml.PoiList.Poi : [obj.mbml.PoiList.Poi];
		for( i=0; i<pois.length; i++ ) {
			if( pois[i].nearcount <= 0 ) continue;
			zerof = false;
			opt = document.createElement("option");
			txt = document.createTextNode(pois[i].st_name);
			opt.value=pois[i].id;
			// 緯度・経度をセット
			opt.setAttribute("nl", pois[i].latitude);
			opt.setAttribute("el", pois[i].longitude);
			if( tmpcode == pois[i].id ) opt.selected = true;
			opt.appendChild(txt);
			s.appendChild(opt);
		}
	//}
	// 最寄に店舗のある駅が0件数の場合
	if( zerof ) {
		opt = document.createElement("option");
		txt = document.createTextNode("該当駅0件");
		opt.appendChild(txt);
		s.appendChild(opt);
		s.disabled=true;
	}
	
	// タブのロック解除
	lockflg=false;
	// フォームのdisabledをやめる
	if( at == 1 ) {
		doDisabled( document.search, false );
	} else {
		doDisabled( document.search2, false );
		doDisabled( document.search3, false );
	}
};

/*
 * 地図へ遷移する
 */
function goMap() {
	var ac = document.getElementById('areacity');
	// 市区町村が選択されていないと何もしない
	if( ac.selectedIndex < 0 ) return;
	
	// 都道府県・市区町村を初期化
	document.getElementById("kentmp").value="";
	document.getElementById("citytmp").value="";
	document.getElementById("stationtmp").value="";
	
	var obj = ac.options[ac.selectedIndex];
	
	// 00だったら何もしない
	if( obj.value == "00" || !obj.value ) return;
	var url = map+'grp='+ph.get("grp")+'&nl='+obj.getAttribute("nl")+'&el='+obj.getAttribute("el")+'&scl=70000&br='+ph.get("br");
	window.location = url;
};

/*
 * Formないでパラメータを作成する(サービス絞り込み用)
 */
function createParamForService2( form ) {
	var element = form.elements.length;
	var srvph = new mapionFuncs.text.ParamHashForUC();
	// チェックついてない場合のため
	var nonChk = "";
	
	for( var i=0; i<element; i++ ) {
		if( form.elements[i].type != 'checkbox' ) continue;
		if( nonChk == "" ) nonChk = form.elements[i].name;
		if( form.elements[i].checked ) {
			srvph.put(form.elements[i].name, form.elements[i].value);
		} else {
			//srvph.put(form.elements[i].name, "null");
		}
	}
	return srvph;
};

/************************
 * フォームをdisabledにする
 * @param flg ( true : disabled、false : disabled外す)
 ************************/
function doDisabled( form, flg ){
	var elem = form.elements;
	
	for( i=0; i<elem.length; i++ ) {
		if( elem[i].type != "checkbox" && elem[i].type != "radio" ) continue;
		if( flg ) elem[i].disabled="disabled";
		else elem[i].disabled="";
	};
};

var timer;
/**********************************
 * textの値があるかないかをタイマーで調べる
 **********************************/
function suggest( txt ){
	// タイマー起動中なら何もしない
	if( timer ) return;
	timer = setInterval( function(){
									searchVal( txt );
								},
								500 );
};

function searchVal( obj ){
	if( obj.value != "" ) {
		if( obj.name == "q" ) document.images["qima"].src="/sevenandifs/images/btn_list2.gif";
		else document.images["pima"].src="/sevenandifs/images/btn_list2.gif";
	} else {
		if( obj.name == "q" ) document.images["qima"].src="/sevenandifs/images/list_btn_disabled.gif";
		else document.images["pima"].src="/sevenandifs/images/list_btn_disabled.gif";
	};
};

function offsuggest(){
	clearInterval( timer );
	timer="";
};

