// 日本アスコ マニュアル検索JavaScript

function AscoManual( conf ) {
	this.search_form_id       = conf.search_form_id;
	this.search_result_box_id = conf.search_result_box_id;
	this.loading_box_id       = conf.loading_box_id;
	this.result_num_box_id    = conf.result_num_box_id;
	this.run_mode_id          = conf.run_mode_id;
	this.column_id            = conf.column_id;
	this.selections           = conf.selections;
	
	this.other_value_string   = 'その他';
}

AscoManual.current_object = null;

AscoManual.init = function ( conf ) {
	var asco = new AscoManual( conf );
	var form = $(conf.search_form_id);
	var i;
	
	AscoManual.current_object = asco;
	
	form.onsubmit = function () {
		asco.run_search( this );
		return false;
	};
	
	// プルダウンにonchangeハンドラ設定
	for( i = 0; i < conf.selections.length - 1; ++i ) {
		var curr = conf.selections[i];
		var next = conf.selections[i + 1];
		asco.set_select_change_handler( curr, next );
	}
	
	$(conf.selections[ conf.selections.length - 1 ]).onchange = function () {
		asco.run_search( form );
		return false;
	};
	
	// 電磁部読み込み
	asco.load_selection( conf.selections[0] );
};

AscoManual.prototype.set_select_change_handler = function ( curr, next ) {
	var asco = this;
	
	$(curr).onchange = function () {
		for( var i = asco.selections.length - 1; i >= 0; --i ) {
			var id = asco.selections[i];
			
			if( id == curr ) break;
			
			var sel = $(id);
			sel.length   = 1;
			sel.disabled = true;
		}
		
		if( this.selectedIndex == 0 ) return;
		
		asco.load_selection( next );
	};
};

AscoManual.prototype.set_run_mode = function ( run_mode ) {
	$(this.run_mode_id).value = run_mode;
};

AscoManual.prototype.get_run_mode = function () {
	return $(this.run_mode_id).value;
};

AscoManual.prototype.set_target_column = function ( column ) {
	$(this.column_id).value = column;
};

AscoManual.prototype.set_select = function( id, list, offset ) {
	var sel = $(id);
	
	sel.length = offset ? offset: 0;
	
	for( var i = 0; i < list.length; ++i ) {
		var value = list[i];
		var opt   = document.createElement( 'option' );
		
		opt.value = value;
		
		if( id == 'sol' && value == '----' ) {
			opt.appendChild( document.createTextNode( this.other_value_string ) );
		}else {
			opt.appendChild( document.createTextNode( value ) );
		}
		
		sel.appendChild( opt );
	}
}

AscoManual.prototype.load_selection = function ( target ) {
	var asco          = this;
	var target_object = $(target);
	var loading_image = $( target + '_loading' );
	var form          = $(this.search_form_id);
	
	target_object.length   = 1;
	target_object.disabled = true;
	
	loading_image.style.display     = '';
	this.set_run_mode( 'manual_select' );
	this.set_target_column( target );
	
	new Ajax.Request(
		form.action,
		{
			"method": "get",
			"parameters": Form.serialize( form ),
			"onSuccess": function( request, json ) {
				if( !( target in json ) ) return;
				
				asco.set_select( target, json[target], 1 );
				loading_image.style.display     = 'none';
				target_object.disabled = false;
			},
			"onFailure": function( request ) {
				alert( '通信エラーが発生しました。' );
				loading_image.style.display     = 'none';
			},
			"onComplete": function( request, json ) {
			}
		}
	);
	
	
};


AscoManual.prototype.show_loading = function ( loading_id, target_id ) {
	$(target_id).innerHTML = $(loading_id).innerHTML;
};

AscoManual.prototype.show_result_num = function ( id, num, limit ) {
	var message   = num + '件の製品が見つかりました。';
	var css_class = 'resultNum';
	
	if( num == 0 ) {
		message = '該当する製品はありません。';
	}
	else if( num > limit ) {
		message   = num + '件中' + limit + '件のデータを表示します。検索条件を絞り込んでください。';
		css_class  = 'resultTooMany';
	}
	
	var div = $(id);
	div.innerHTML     = message;
	div.className     = css_class;
	div.style.display = 'block';
};

AscoManual.prototype.run_search = function ( form ) {
	var asco = this;
	
	$(this.result_num_box_id).style.display = 'none';
	this.show_loading( this.loading_box_id, this.search_result_box_id );
	asco.set_run_mode( 'manual_search' );
	
	new Ajax.Request(
		form.action,
		{
			"method": "get",
			"parameters": Form.serialize( form ),
			"onSuccess": function( request, json ) {
				asco.show_result_num( asco.result_num_box_id, json.num, json.limit );
				$(asco.search_result_box_id).innerHTML = request.responseText;
			},
			"onFailure": function( request ) {
				alert( '通信エラーが発生しました。' );
			},
			"onComplete": function( request, json ) {
			}
		}
	);
};

