/* scroll to top of page */

function toPageTop() {
	window.scroll(0,0);
}


// POST送信先URL
var resultURL = 'ctrl/ctrl_result.php';

var latestResultRequest = 0.0; // 最新の通信を行った瞬間のタイムスタンプ

function goResult(target_name, target_value) {
	
	// チェックされている項目を取得
	var chkboxes_foruse = Form.getInputs('foruse-form', 'checkbox', 'foruse');
	var checks_foruse = '';
	for (i in chkboxes_foruse) {
		if (chkboxes_foruse[i].checked) {
			if (checks_foruse != '') {
				checks_foruse += ',';
			}
			checks_foruse += chkboxes_foruse[i].value;
		}
	}
	
	var chkboxes_function = Form.getInputs('function-form', 'checkbox', 'function');
	var checks_function = '';
	for (i in chkboxes_function) {
		if (chkboxes_function[i].checked) {
			if (checks_function != '') {
				checks_function += ',';
			}
			checks_function += chkboxes_function[i].value;
		}
	}
	
	// タイムスタンプ作成
	var now = new Date();
	var nowStr = '0.' + String(now.getHours()) + String(now.getMinutes()) + String(now.getSeconds()) + String(now.getMilliseconds());
	
	// 送信するデータ
	var queryString = 'mode=get_query&checks_foruse=' + checks_foruse + '&checks_function=' + checks_function + '&time=' + nowStr;
	
	queryString += '&target_name=' + target_name + '&target_value=' + target_value;
	
	// 最新リクエストのタイムスタンプ
	latestResultRequest = nowStr;
	
	new Ajax.Request(
		resultURL, {
			method : 'post',
			parameters : queryString,
			onSuccess : function(response) {
				
				// リターン値をJSONオブジェクトとして取得
				var resultText = response.responseText.evalJSON();
				
				// 最新のリクエストの場合はresult.phpに遷移
				if (latestResultRequest <= resultText.time) {
					if (resultText.result) {
						location.href = 'result.php?t=' + nowStr;
					}
				}
				
			},
			onFailure : function(response) {
				// alert('失敗です。' + response.status + response.statusText);
			}
		}
	);
}

// サイドバーをクリック
function clickSidebar(page_name) {
	
	switch (page_name) {
		case 'result':
			// 画面遷移なしで検索結果を更新
			updateResult();
			break;
			
		case 'foruse':
		case 'function':
			// 処理なし
			break;
			
		/*
		case 'content':
		case 'content_list':
			doAdvSearch();
			break;
		*/
		
		default:
			// 検索を行い検索結果一覧画面に表示
			doSearch();
			break;
	}
}

// 使用用途・機能から検索
function doFuncUseSearch() {
	doSearch();
}


function updateResult() {
	
	$('loading_count').update('0');
	document.getElementById('overray').style.display = 'block';
	
	// カウントの停止
	if (timerStartFlag) {
		timer.stop();
		timerStartFlag = false;
	}
	maxCount = 0;
	count = 0;
	resultObj = null;
	
	// ローディングのカウントアップ
	// count = 0;
	// timer = new PeriodicalExecuter(countUp, 0.5);
	
	
	// チェックされている項目を取得
	var chkboxes_foruse = Form.getInputs('sidebar-form', 'checkbox', 'foruse');
	var checks_foruse = '';
	for (i in chkboxes_foruse) {
		if (chkboxes_foruse[i].checked) {
			if (checks_foruse != '') {
				checks_foruse += ',';
			}
			checks_foruse += chkboxes_foruse[i].value;
		}
	}
	
	var chkboxes_function = Form.getInputs('sidebar-form', 'checkbox', 'func');
	var checks_function = '';
	for (i in chkboxes_function) {
		if (chkboxes_function[i].checked) {
			if (checks_function != '') {
				checks_function += ',';
			}
			checks_function += chkboxes_function[i].value;
		}
	}
	
	// タイムスタンプ作成
	var now = new Date();
	var nowStr = '0.' + String(now.getHours()) + String(now.getMinutes()) + String(now.getSeconds()) + String(now.getMilliseconds());
	
	// 送信するデータ
	var queryString = 'mode=get_query&foruse_str=' + checks_foruse + '&func_str=' + checks_function + '&time=' + nowStr;
	
	// queryString += '&target_name=' + target_name + '&target_value=' + target_value;
	
	// 最新リクエストのタイムスタンプ
	latestResultRequest = nowStr;
	
	new Ajax.Request(
		resultURL, {
			method : 'post',
			parameters : queryString,
			onSuccess : function(response) {
				
				// リターン値をJSONオブジェクトとして取得
				var resultText = response.responseText.evalJSON();
				
				// console.log(resultText);
				
				// 最新のリクエストの場合は検索結果を更新
				if (latestResultRequest <= resultText.time) {
					
					// ローディングのカウントアップ
					count = 0;
					maxCount = resultText.count;
					timer = new PeriodicalExecuter(countUp, 0.05);
					
					timerStartFlag = true;
					
					resultObj = resultText;
					
					/*
					$('search-result').update(resultText.list);
					$('page-offset-top').update(resultText.page_offset);
					$('page-offset-bottom').update(resultText.page_offset);
					$('page-navi-top').update(resultText.page_navi);
					$('page-navi-bottom').update(resultText.page_navi);
					
					if (resultText.count == null) {
						$('to-box-top').hide();
						$('to-box-bottom').hide();
					} else {
						$('to-box-top').show();
						$('to-box-bottom').show();
					}
					
					document.getElementById('overray').style.display = 'none';
					
					// ローディングのカウント停止
					timer.stop();
					*/
					
				}
				
			},
			onFailure : function(response) {
				// alert('失敗です。' + response.status + response.statusText);
			}
		}
	);
}


function doSearch() {
	
	// チェックされている項目を取得
	
	var checks_foruse = '';
	var checks_function = '';
	var checks_foruse_2 = '';
	var checks_function_2 = '';
	
	// サイドバーがある場合
	if ($('sidebar-form')) {
		var chkboxes_foruse = Form.getInputs('sidebar-form', 'checkbox', 'foruse');
		for (i in chkboxes_foruse) {
			if (chkboxes_foruse[i].checked) {
				if (checks_foruse != '') {
					checks_foruse += ',';
				}
				checks_foruse += chkboxes_foruse[i].value;
			}
		}
		
		var chkboxes_function = Form.getInputs('sidebar-form', 'checkbox', 'func');
		for (i in chkboxes_function) {
			if (chkboxes_function[i].checked) {
				if (checks_function != '') {
					checks_function += ',';
				}
				checks_function += chkboxes_function[i].value;
			}
		}
	}
	
	// 使用用途テーブルがある場合
	if ($('foruse-form')) {
		var chkboxes_foruse_2 = Form.getInputs('foruse-form', 'checkbox', 'foruse');
		for (i in chkboxes_foruse_2) {
			if (chkboxes_foruse_2[i].checked) {
				if (checks_foruse_2 != '') {
					checks_foruse_2 += ',';
				}
				checks_foruse_2 += chkboxes_foruse_2[i].value;
			}
		}
	}
	
	
	// 機能テーブルがある場合
	if ($('func-form')) {
		var chkboxes_function_2 = Form.getInputs('func-form', 'checkbox', 'func');
		for (i in chkboxes_function_2) {
			if (chkboxes_function_2[i].checked) {
				if (checks_function_2 != '') {
					checks_function_2 += ',';
				}
				checks_function_2 += chkboxes_function_2[i].value;
			}
		}
	}
	
	if (checks_foruse != '' && checks_foruse_2 != '') {
		checks_foruse += ',' + checks_foruse_2;
	} else {
		checks_foruse += checks_foruse_2;
	}
	
	if (checks_function != '' && checks_function_2 != '') {
		checks_function += ',' + checks_function_2;
	} else {
		checks_function += checks_function_2;
	}
	
	$('foruse_str').value = checks_foruse;
	$('func_str').value = checks_function;
	
	if ($('sidebar-form')) {
		$('sidebar-form').submit();
	} else {
		$('funcuse-form').submit();
	}
}














