/* Main field for output */
var main = null;

/* Result field */
var result = null;

/* list of display object */
var display_stat = new Array();

/**
 *	init
 */
function init_selection() {
	var div = null;

	main = document.getElementById( "selectMain" );
	result = document.getElementById( "selectResult" );
	if( main && result ) {
		//get first question
		div = get_question( 0, 0 );
		if( div ) {
			main.appendChild( div );

			//add to list of display object
			display_stat.push( div );
		}
	}
}

/**
 *	display next question
 */
function next_question( parentid ) {
	var idx = 0;
	var questionid = 0;
	var div = null;
	var id = "";

	id = "c" + parentid;

	//if checked question is not last, delete after question.
	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();
		}
	}

	//get next question id
	id = "q" + parentid;
	questionid = get_checkedvalue( document.getElementsByName( id ) );

	//get question
	div = get_question( parentid, questionid );
	if( div ) {
		switch( div.id.charAt( 0 ) ) {
		case "c":
			//if exists question, add to field
			main.appendChild( div );
			break;
		case "p":
			//add to field
			result.appendChild( div );
			break;
		}

		//add to list of display object
		display_stat.push( div );
	}
}

/**
 *	get question
 */
function get_question( parentid, questionid ) {
	var idx = 0;
	var question = "";
	var answer_list = new Array();

	//get text of question
	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;
				}
			}
		}
	}

	//get text of answer
	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 ) {
		//no answer
		//>>display result(products)
		return( product_html( questionid ) );
	}
	else {
		//display question
		return( question_html( questionid, question, answer_list ) );
	}
}

/**
 *	html for question
 */
function question_html( questionid, question, answer_list ) {
	var idx = 0;
	var div = null;
	var html = "";

	//create object
	div = document.createElement( "div" );

	//setting id
	div.id = "c" + questionid;

	//start html--------------------------------------

	//question
	html += '<h3>' + question + '</h3>' + "\n";

	//answer
	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";
	}

	//end html--------------------------------------

	//setting html
	div.innerHTML = html;

	return( div );
}

/**
 *	html for result
 */
function product_html( questionid ) {
	var idx = 0;
	var html = "";
	var div = null;
	var cnt = 0;

	//create object
	div = document.createElement( "div" );

	//setting id
	div.id = "p" + questionid;

	//start html--------------------------------------
	html += '<h3>pickup products for your selection spec.</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 ) {
				//separator
			}

			//product name
			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>';
			}
			//product image
			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 {
				//no image
				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";
		}
	}

	//end 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";
	}

	//setting html
	div.innerHTML = html;

	return( div );
}

/**
 *	get selected value
 *		element		target 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( "" );
}

/**
 *	list check
 *		key is in list or not
 */
function in_array( key, list ) {
	var idx = 0;

	for( idx=0; idx<list.length; idx++ ) {
		if( list[ idx ] == key ) {
			return( true );
		}
	}

	return( false );
}

