/**************************************************************************
 * グローバル変数
 *************************************************************************/

// 店舗XMLデータオブジェクト 
var shopObj = null;

// フォームリストオブジェクト 
var formArray = new Array();

// ハンドラリストオブジェクト※キーはフォームID
var handlerArray = new Array();

// 店舗一覧タグID / 都道府県コード 
var shopListTagId = null;
var stateId = null;

// 各種URL(本番) 
var yoyakuUrl = "https://www.hokende.com/hokende2/shopcal/calendar.asp";
var cloudUrl = "https://www.hokende.com/hokende2/shopcal/get_ShopList.asp?cb=setTempoJason";

// 各種URL(テスト) 
//var yoyakuUrl = "http://test.hokende.com/shopcal/calendar.asp";
//var cloudUrl = "http://test.hokende.com/shopcal/get_ShopList.asp?cb=setTempoJason";

// 各種URL(開発) 
//var yoyakuUrl = "http://kai.hokende.com/shopcal/calendar.asp";
//var cloudUrl = "http://kai.hokende.com/shopcal/get_ShopList.asp?cb=setTempoJason";

// 動的更新対応ドメイン 
var domainList = new Array( 
	"hokende.com",
	"hoken-ichiba.com",
	"www.online-hoken.com",
	"www.hoken-sougou.com",
	"www.hoken-ichiba-navi.jp",
	"8798.net",
	"www.hokenpark.com",
	"www.bestprice.net",
	"www.excite.co.jp/insurance",
	"woman.excite.co.jp/lifeplanning/insurance",
	"service.become.co.jp/insurance",
	"www.hoken-hikakude.com"
);

/**************************************************************************
 * 店舗予約ページJUMP
 *
 * @param selectElement 店舗SELECTタグ
 * @param formId フォームID
 *************************************************************************/
function checkDomain() {
	var nowDomain = location.hostname;
	var nowUrl = location.href;

	for ( var i = 0; i<domainList.length; i++ ) {
		if ( nowDomain.indexOf( domainList[i] ) != -1 || nowUrl.indexOf( domainList[i] ) != -1 ) {
			return true;
		}
	}

	return false;
}


/**************************************************************************
 * 都道府県コンボボックス
 *
 * @param formId フォームID
 *
 * 「フォームID_pref」というIDを持つSELECTタグがない場合はレンダリングする。
 *
 *************************************************************************/
function select_preflist( formId ) {
	pushFormArray( formId );

	var parentElement = document.getElementsByName( formId );

	if ( parentElement == null )
		return;

	var prefSelectElement = document.getElementById( formId + "_pref" );
	/* SELECTタグがなければレンダリング */
	if ( prefSelectElement == null ) {
		document.write( "<select id='" + formId + "_pref' name=\"pref\" class=\"reservation reservation-area\" onChange='render_shop( \"" + formId + "\" );'>\n" );
		document.write( "<option value=\"\">全国</option>\n" );
		document.write( "</select>" );
	}

	render_pref();
}


/**************************************************************************
 * 店舗コンボボックス
 *
 * @param formId フォームID
 *
 * 「フォームID_shop」というIDを持つSELECTタグがない場合はレンダリングする。
 *
 *************************************************************************/
function select_shoplist( formId ){
	pushFormArray( formId );

	/* ハンドラ関数が指定されていた場合は格納しておく。 */
	if ( arguments.length >= 2 ) {
		handlerArray[formId] = arguments[1];
	}

	var parentElement = document.getElementsByName( formId );
	if ( parentElement == null )
		return;

	var shopSelectElement = document.getElementById( formId + "_shop" );
	/* SELECTタグがなければレンダリング */
	if ( shopSelectElement == null ) {
		document.write( "<select id=\"" + formId + "_shop\" name=\"shop_id\" class=\"reservation reservation-shop\" onChange=\"select_shop( this, '" + formId + "' );\">" );
		document.write( "<option value=\"\">店舗を選択してください。</option>\n" );
		document.write( "</select>" );
	}

	render_shop();
}


function unordershoplist( tagId, todofukenID ) {
	shopListTagId = tagId;
	stateId = todofukenID;

	document.write( "<dl id='" + tagId + "'>\n" );
	document.write( "</dl>\n" );
}


function render_unordershoplist() {
	// XMLデータが存在していれば店舗アイテム追加 
	if ( shopObj != null && stateId != null ) {
		var ulElement = document.getElementById( shopListTagId );
		if ( ulElement != null ) {
			var length = 0;
			var brandname = null;
			for ( var i=0; i<shopObj['xml']['rs:data']['z:row'].length; i++ ) {
				if ( stateId == shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'] ){
					if ( length > 50 ) {
						var brElement = document.createElement( "br" );
						ulElement.appendChild( brElement );
						length = 0;
					}

					if ( brandname == null || brandname != shopObj['xml']['rs:data']['z:row'][i]['-BRAND_NAME'] ) {
						var divlement = document.createElement( "dt" );
						divlement.innerHTML = "【" + shopObj['xml']['rs:data']['z:row'][i]['-BRAND_NAME'] + "】";
						ulElement.appendChild( divlement );
						length = 0;
						brandname = shopObj['xml']['rs:data']['z:row'][i]['-BRAND_NAME'];
					}

					var liElement = document.createElement( "dd" );
					liElement.className="shop-total";

					var strShopName = shopObj['xml']['rs:data']['z:row'][i]['-DISPLAY_NAME'];
					if ( shopObj['xml']['rs:data']['z:row'][i]['-COMPANY_ID'] != 1 )
						strShopName = strShopName + "(協力店)";
					liElement.innerHTML = "<a href='" + yoyakuUrl + "?shop_id=" + shopObj['xml']['rs:data']['z:row'][i]['-ID'] + "' target='_blank' rel='external'>" + strShopName + "</a>";
					ulElement.appendChild( liElement );
					length += (3 + strShopName.length);
				}
			}
		}
	}
}


/**************************************************************************
 * 都道府県コンボボックス
 *
 * @param formId フォームID
 *
 *************************************************************************/
function render_pref( formId ) {
	// JASONデータが存在していれば都道府県アイテム追加 
	if ( shopObj != null ) {
		var prefSelectElement = document.getElementById( formId + "_pref" );
		if ( prefSelectElement != null ) {
			if ( prefSelectElement.tagName == 'SELECT' ) {
				var todofukencode = null;
				var todofukenCount = 1;
				prefSelectElement.length = 1;
				for( var i=0; i<shopObj['xml']['rs:data']['z:row'].length; i++ ){
					if ( todofukencode == shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'] )
						continue;

					prefSelectElement.length++;
					prefSelectElement.options[todofukenCount].value = shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'];
					prefSelectElement.options[todofukenCount].text = shopObj['xml']['rs:data']['z:row'][i]['-KOUMOKUMEI'];
					todofukencode = shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'];
					todofukenCount++;
				}
			}
		}
	}
}


/**************************************************************************
 * 店舗コンボボックス
 *
 * @param formId フォームID
 *
 *************************************************************************/
function render_shop( formId ){
	// XMLデータが存在していれば店舗アイテム追加 
	if ( shopObj != null ) {
		var prefSelectElement = document.getElementById( formId + "_pref" );
		var shopSelectElement = document.getElementById( formId + "_shop" );
		if ( shopSelectElement != null ) {
			shopSelectElement.length = 1;
			var shopCount = 1;
			var todoufukenCode = null;
			var brandname = null;
			for ( var i=0; i<shopObj['xml']['rs:data']['z:row'].length; i++ ) {
				if ( (prefSelectElement == null || prefSelectElement.value == '') && todoufukenCode != shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'] ) {
					shopSelectElement.length++;
					shopSelectElement.options[shopCount].value = "";
					shopSelectElement.options[shopCount].text = "------ " + shopObj['xml']['rs:data']['z:row'][i]['-KOUMOKUMEI'] + " -----------------------";
					shopCount++;
					todoufukenCode = shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'];
					brandname = null;
				}

				if ( prefSelectElement == null || prefSelectElement.value == '' || prefSelectElement.value == shopObj['xml']['rs:data']['z:row'][i]['-TODOFUKEN_CODE'] ){
					if ( checkDomain() == true ) {
						if ( brandname == null || brandname != shopObj['xml']['rs:data']['z:row'][i]['-BRAND_NAME'] ) {
							shopSelectElement.length++;
							shopSelectElement.options[shopCount].value = "";
							shopSelectElement.options[shopCount].text = "【" + shopObj['xml']['rs:data']['z:row'][i]['-BRAND_NAME'] + "】";
							shopCount++;
							brandname = shopObj['xml']['rs:data']['z:row'][i]['-BRAND_NAME'];
						}
					}

					var strShopName = "　" + shopObj['xml']['rs:data']['z:row'][i]['-DISPLAY_NAME'];
					if ( shopObj['xml']['rs:data']['z:row'][i]['-COMPANY_ID'] != 1 )
						strShopName = strShopName + "(協力店)";
					shopSelectElement.length++;
//					shopSelectElement.options[shopCount].value = shopObj['xml']['rs:data']['z:row'][i]['-TEMPO_CD'];
					shopSelectElement.options[shopCount].value = shopObj['xml']['rs:data']['z:row'][i]['-ID'];
					shopSelectElement.options[shopCount].text = strShopName;
					shopCount++;
				}
			}
		}
	}
}



/**************************************************************************
 * 店舗選択時ハンドラ関数呼び出し処理
 *
 * @param shopSelectElement 店舗SELECTタグ
 * @param formId フォームID
 *
 *************************************************************************/
function select_shop( shopSelectElement, formId ) {
	if ( shopSelectElement.value == "" )
		return;

	/* ハンドラ指抽ﾎはハンドラ関数呼ｄ双し。指猪bなけれｃ汪談予約ページへジャンプ  */
	if ( handlerArray[formId] != null ) {
		 handlerArray[formId]();
	} else {
		goSoudanYoyaku( shopSelectElement, formId );
	}
}


/**************************************************************************
 * フォームリスト追加
 *
 * @param text フォームID
 *
 *************************************************************************/
function pushFormArray( text ) {
	for ( var i = 0; i < formArray.length; i++ ) {
		if ( formArray[i] == text ) {
			return;
		}
	}

	formArray.push( text );
}


/**************************************************************************
 * 都道府県＆店舗レンダリング
 *************************************************************************/
function renderer() {
	var foundShop = false;

	for ( var i = 0; i < formArray.length; i++ ) {
		render_pref( formArray[i] );
		render_shop( formArray[i] );

		var prefSelectElement = document.getElementById( formArray[i] + "_pref" );
		if ( shopObj != null ) {
			for ( var j = 0; j<shopObj['xml']['rs:data']['z:row'].length; j++ ) {
				if ( prefSelectElement.value == shopObj['xml']['rs:data']['z:row'][j]['-TODOFUKEN_CODE'] ) {
					foundShop = true;
					break;
				}
			}
		}
	}
	render_unordershoplist();

	var shoplist1Element = document.getElementById( "shoplist1" );
	var shoplist2Element = document.getElementById( "shoplist2" );
	var shoplist3Element = document.getElementById( "shoplist3" );
	if ( foundShop == true) {
		if ( shoplist1Element != null )
			shoplist1Element.style.display = "";
		if ( shoplist2Element != null )
			shoplist2Element.style.display = "";
		if ( shoplist3Element != null )
			shoplist3Element.style.display = "none";
	} else {
		if ( shoplist1Element != null )
			shoplist1Element.style.display = "none";
		if ( shoplist2Element != null )
			shoplist2Element.style.display = "none";
		if ( shoplist3Element != null )
			shoplist3Element.style.display = "";
	}
};


/**************************************************************************
 * 店舗XMLデータ設定
 *
 * @param xmldata 受信XMLデータ
 *************************************************************************/
function setTempoJason( xmldata ) {
	var xotree = new XML.ObjTree();
	shopObj = xotree.parseXML( xmldata );

	renderer();
}


/**************************************************************************
 * 店舗予約ページJUMP
 *
 * @param selectElement 店舗SELECTタグ
 * @param formId フォームID
 *************************************************************************/
function goSoudanYoyaku( selectElement, formId )
{
	var formElement = document.getElementById( formId );

	if ( formElement != null ) {
		sOriginalMethod = formElement.method;
		sOriginalTarget = formElement.target;
		sOriginalAction = formElement.action;

		formElement.method = "get";
		formElement.target = "_blank";
		formElement.action = yoyakuUrl;
		formElement.submit();

		//もとに戻しておく
		formElement.method = sOriginalMethod;
		formElement.target = sOriginalTarget;
		formElement.action = sOriginalAction;

	} else {
		//formがなかったら 
		if ( selectElement.options[selectElement.selectedIndex].value != '' ) {
			location.href = yoyakuUrl + "?shop_id=" + selectElement.options[selectElement.selectedIndex].value;
		}
	}
}


/**************************************************************************
 * レンダリングハンドラ設定
 *
 * @param selectElement 店舗SELECTタグ
 * @param formId フォームID
 *************************************************************************/
if ( window.attachEvent ) {
	window.attachEvent('onload', renderer);
} else {
	window.addEventListener('load', renderer, false);
}


/**************************************************************************
 * イベントハンドラ設定
 *************************************************************************/
document.write( "<script language='javascript' type='text/javascript' src='/zenkoku/js2008/ObjTree.js'></script>" );
if ( checkDomain() == true ) {
	document.write( "<script language='javascript' type='text/javascript' src='" + cloudUrl + "' charset='Shift_JIS'></script>" );
} else {
	document.write( "<script language='javascript' type='text/javascript' src='" + cloudUrl + "&campany=1' charset='Shift_JIS'></script>" );
}


