// 日本アスコ 製品検索JavaScript

function AscoProduct( 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.catalog_button_id    = conf.catalog_button_id;
	this.flow_calc_button_id  = conf.flow_calc_button_id;
	this.inquiry_button_id    = conf.inquiry_button_id;
	this.number_class_name    = conf.number_class_name;
	this.catalog_dir          = conf.catalog_dir;
	this.inquiry_url          = conf.inquiry_url;
	
	this.selected_product     = null;
}

AscoProduct.current_object = null;

AscoProduct.init = function ( conf ) {
	var asco = new AscoProduct( conf );
	var form = $(conf.search_form_id);
	var i;
	
	AscoProduct.current_object = asco;
	
	form.onsubmit = function () {
		asco.run_search( this );
		return false;
	};
	
	var input_numbers = form.getElementsByTagName( 'input' );
	
	for( i = 0; i < input_numbers.length; ++i ) {
		var input = input_numbers[i];
		
		if( input.className != conf.number_class_name ) continue;
		
		input.onchange = function () {
			this.style.backgroundColor = '';
			if( this.value == '' ) return;
			
			if( ! this.value.match( /^[0-9]+(?:\.[0-9]*)?$/ ) ) {
				alert( '数値が正しく入力されていません' );
				this.style.backgroundColor = '#ff0000';
				this.select();
				this.focus();
			}
		};
	}
	
	$(conf.catalog_button_id).onclick = function () {
		if( asco.selected_product != null ) {
			if( asco.selected_product.cat_index == '' ) {
				alert( 'この製品のカタログはお問合せください' );
				return;
			}
			
			var url = asco.catalog_dir + escape( asco.selected_product.cat_index ) + '.pdf';
			var win = window.open( url );
			win.focus();
		}
	};
	
	$(conf.flow_calc_button_id).onclick = function () {
		var query_string = null;
		
		if(
			   asco.selected_product.cv == null
			|| asco.selected_product.max_opd == null
			|| asco.selected_product.min_opd == null
			|| asco.selected_product.orifice == null
		) {
			alert( 'この製品の流量計算はできません。' );
			return;
		}
		
		if( asco.selected_product != null ) {
			query_string = 'cv2=' + escape( asco.selected_product.cv )
						 + '&p_max2=' + escape( asco.selected_product.max_opd )
						 + '&p_min2=' + escape( asco.selected_product.min_opd );
		}
		
		open_flowcalc( query_string );
	};
	
	$(conf.inquiry_button_id).onclick = function () {
		if( asco.selected_product != null ) {
			var p = asco.selected_product;
			var query_string = '?product=' + encodeURI( p.series + 'シリーズ ' + p.key );
			location.href = asco.inquiry_url + query_string;
		}
	};
	
	(function(){
		var query_param  = parse_query_string();
		var require_keys = [ 'min_opd', 'max_opd', 'cv' ];
		
		for( var i = 0; i < require_keys.length; ++i ) {
			var key = require_keys[i];
			if( key in query_param ) {
				$(key).value = query_param[key];
			}
		}
	})();
};

AscoProduct.set_selected_product = function( product ) {
	AscoProduct.current_object.selected_product = product;
}

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

AscoProduct.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';
};

AscoProduct.prototype.run_search = function ( form ) {
	var asco = this;
	
	this.selected_product = null;
	$(this.result_num_box_id).style.display = 'none';
	this.show_loading( this.loading_box_id, this.search_result_box_id );
	
	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 ) {
			}
		}
	);
};

