/* 出力用メインフィールド */
var main = null;

/* 結果用フィールド */
var result = null;

/* 表示オブジェクト */
var display_stat = new Array();

/**
 *	初期化
 */
function init_selection() {
	var div = null;

	main = document.getElementById( "selectMain" );
	result = document.getElementById( "selectResult" );
	if( main && result ) {
		//最初の設問を取得
		div = get_question( 0, 0 );
		if( div ) {
			main.appendChild( div );

			//表示オブジェクトに追加
			display_stat.push( div );
		}
	}
}

/**
 *	次設問表示
 */
function next_question( parentid ) {
	var idx = 0;
	var questionid = 0;
	var div = null;
	var id = "";

	id = "c" + parentid;

	//チェックされたのが最下部の設問でなかった場合に、それ以下を削除する
	for( idx=display_stat.length -1; idx>=0; idx-- ) {
		if( id == display_stat[ idx ].id ) {
			break;
		}
		else {
			switch( display_stat[ idx ].id.charAt( 0 ) ) {
			case "c":
				main.removeChild( display_stat[ idx ] );
				break;
			case "p":
				result.removeChild( display_stat[ idx ] );
				break;
			}
			display_stat.pop();
		}
	}

	//次の設問IDを取得
	id = "q" + parentid;
	questionid = get_checkedvalue( document.getElementsByName( id ) );

	//設問取得
	div = get_question( parentid, questionid );
	if( div ) {
		switch( div.id.charAt( 0 ) ) {
		case "c":
			//設問が存在すれば追加
			main.appendChild( div );
			break;
		case "p":
			//結果を追加
			result.appendChild( div );
			break;
		}

		//表示オブジェクトに追加
		display_stat.push( div );
	}
}

/**
 *	設問取得
 */
function get_question( parentid, questionid ) {
	var idx = 0;
	var question = "";
	var answer_list = new Array();

	//設問文取得
	for( idx=0; idx<category_num; idx++ ) {
		if( category_list[ idx ][ "parentid" ] == parentid ) {
			if( questionid == 0 ) {
				questionid = category_list[ idx ][ "categoryid" ];
				question = category_list[ idx ][ "explain" ];
				break;
			}
			else {
				if( questionid == category_list[ idx ][ "categoryid" ] ) {
					question = category_list[ idx ][ "explain" ];
					break;
				}
			}
		}
	}

	//回答取得
	if( questionid > 0 ) {
		for( idx=0; idx<category_num; idx++ ) {
			if( category_list[ idx ][ "parentid" ] == questionid ) {
				answer_list.push( category_list[ idx ][ "categoryid" ] + "\t" + category_list[ idx ][ "name" ] );
			}
		}
	}

	if( answer_list.length == 0 ) {
		//回答なし
		//＞＞製品表示
		return( product_html( questionid ) );
	}
	else {
		//設問表示
		return( question_html( questionid, question, answer_list ) );
	}
}

/**
 *	設問用HTML
 */
function question_html( questionid, question, answer_list ) {
	var idx = 0;
	var div = null;
	var html = "";

	//オブジェクト生成
	div = document.createElement( "div" );

	//ID設定
	div.id = "c" + questionid;

	//HTML開始--------------------------------------

	//問題文
	html += '<h3>' + question + '</h3>' + "\n";

	//回答
	for( idx=1; idx<=answer_list.length; idx++ ) {
		var id = questionid + "_" + idx;
		var name = "q" + questionid;
		var answer = answer_list[ idx -1 ].split( "\t" );

		html += '<p><input name="' + name + '" type="radio" id="' + id  +'" value="' + answer[ 0 ] + '" onclick="next_question(' + questionid + ');"><label for="' + id + '">' + answer[ 1 ] + '</label></p>' + "\n";
	}

	//HTML終了--------------------------------------

	//HTML設定
	div.innerHTML = html;

	return( div );
}

/**
 *	結果用HTML
 */
function product_html( questionid ) {
	var idx = 0;
	var html = "";
	var div = null;
	var cnt = 0;

	//オブジェクト生成
	div = document.createElement( "div" );

	//ID設定
	div.id = "p" + questionid;

	//HTML開始
	html += '<h3>ご指定の仕様に合致する製品をピックアップしました。</h3>' + "\n";
	html += '<div id="selectItem">' + "\n";

	for( idx=0; idx<product_num; idx++ ) {
		if( in_array( questionid, product_list[ idx ][ "categoryid" ] ) ) {
			cnt ++;

			if( cnt > 1 ) {
				//境界線
			}

			//製品名
			var link_name = product_list[ idx ][ "name" ];
			if( product_list[ idx ][ "product_url" ] != "" ) {
				link_name = '<a href="' + product_list[ idx ][ "product_url" ] + '">' + link_name + '</a>';
			}
			//製品画像
			var link_image = "";
			if( product_list[ idx ][ "product_thumb" ] != "" ) {
				link_image = '<img src="' + product_list[ idx ][ "product_thumb" ] + '" border="0" width="66" height="66" alt="' + product_list[ idx ][ "name" ] + '">';
			}
			else {
				//画像なし
				link_image = '<img src="../lib/images/nowprinting.gif" border="0" width="66" height="66" alt="' + product_list[ idx ][ "name" ] + '">';
			}
			if( link_image != "" && product_list[ idx ][ "product_url" ] != "" ) {
				link_image = '<a href="' + product_list[ idx ][ "product_url" ] + '">' + link_image + '</a>';
			}

			html += '	<div class="resultUnit">' + "\n";
			html += '		' + link_image + '' + "\n";
			html += '		<dl>' + "\n";
			html += '		<dt>' + link_name + '</dt>' + "\n";
			html += '		<dd>' + product_list[ idx ][ "typename" ] + '</dd>' + "\n";
			html += '		</dl>' + "\n";
			html += '	</div>' + "\n";
		}
	}

	//HTML終了
	html += '</div>' + "\n";
	html += '<div id="selectEnd">&nbsp;</div>' + "\n";

	if( cnt == 0 ) {
		html = '<img src="./img/no_match.gif" width="551" height="36">' + "\n";
	}

	//HTML設定
	div.innerHTML = html;

	return( div );
}

/**
 *	選択データ取得
 *		element		対象フィールド
 */
function get_checkedvalue( element ) {
	var idx;
	for( idx=0; idx<element.length; idx++ ) {
		if( element[ idx ].checked ) {
			return( element[ idx ].value );
		}
	}
	if( element.type == "hidden" ) {
		return( element.value );
	}
	return( "" );
}

/**
 *	配列チェック
 *		keyがlist配列内に存在するかチェックする
 */
function in_array( key, list ) {
	var idx = 0;

	for( idx=0; idx<list.length; idx++ ) {
		if( list[ idx ] == key ) {
			return( true );
		}
	}

	return( false );
}

