﻿/// <summary>ページが読み込まれると発生するイベントを処理します。</summary>
window.onload = function ()
{
	// マスタ ページ
	$('ctl00_body_btnSearchProduct').onclick = function () {
		return InputChkSearch('ctl00_body_txtSearchProduct', 'ctl00_body_rdbSearchProduct_2');
	};
	$('ctl00_body_btnSearchGoogle').onclick = function () {
		return InputChkGsearch('ctl00_body_txtSearchGoogle');
	};
	$('ctl00_body_txtSearchProduct').onkeypress = function (e) {
		return FireClick(e, 'ctl00_body_btnSearchProduct');
	};
	$('ctl00_body_txtSearchGoogle').onkeypress = function (e) {
		return FireClick(e, 'ctl00_body_btnSearchGoogle');
	};
	$('ctl00_body_rdbSearchProduct_0').onclick = function () {
		EnableIME('ctl00_body_txtSearchProduct', false);
	};
	$('ctl00_body_rdbSearchProduct_1').onclick = function () {
		EnableIME('ctl00_body_txtSearchProduct', true);
	};
	$('ctl00_body_rdbSearchProduct_2').onclick = function () {
		EnableIME('ctl00_body_txtSearchProduct', false);
	};
	$('ctl00_body_rdbSearchProduct_3').onclick = function () {
		EnableIME('ctl00_body_txtSearchProduct', true);
	};

	// トップ ページ
	$('ctl00_btnSearchProduct').onclick = function () {
		return InputChkSearch('ctl00_txtSearchProduct', 'ctl00_rdbSearchProduct_2');
	};
	$('ctl00_btnSearchGoogle').onclick = function () {
		return InputChkGsearch('ctl00_txtSearchGoogle');
	};
	$('ctl00_txtSearchProduct').onkeypress = function (e) {
		return FireClick(e, 'ctl00_btnSearchProduct');
	};
	$('ctl00_txtSearchGoogle').onkeypress = function (e) {
		return FireClick(e, 'ctl00_btnSearchGoogle');
	};
	$('ctl00_rdbSearchProduct_0').onclick = function () {
		EnableIME('ctl00_txtSearchProduct', false);
	};
	$('ctl00_rdbSearchProduct_1').onclick = function () {
		EnableIME('ctl00_txtSearchProduct', true);
	};
	$('ctl00_rdbSearchProduct_2').onclick = function () {
		EnableIME('ctl00_txtSearchProduct', false);
	};
	$('ctl00_rdbSearchProduct_3').onclick = function () {
		EnableIME('ctl00_txtSearchProduct', true);
	};
};

// 商品検索ボタンクリック時
function InputChkSearch(keyword, casno) {
	// キーワード必須チェック
	if (isRequired($(keyword), '商品検索キーワード', 2) == false) {
		$(keyword).focus();
		return false;
	}
	// CAS番号書式チェック
	if ($(casno).checked) {
		if (isWellFormed($(keyword), 'CasNo.', /[0-9]+-[0-9]+-[0-9]+/) == false) {
			$(keyword).focus();
			return false;
		}
	}
	return true;
}

// Google検索ボタンクリック時
function InputChkGsearch(keyword) {
	// キーワード必須チェック
	if (isRequired($(keyword), 'Google検索キーワード', 2) == false) {
		$(keyword).focus();
		return false;
	}
	return true;
}

// IME制御
function EnableIME(id, enabled) {
	if (enabled) {
		$(id).style.imeMode='active';
	} else {
		$(id).style.imeMode='disabled';
	}
}

// ボタンクリック
function FireClick(e, id) {
	if (!e) {
		e = window.event;
	}
	if(e.keyCode == 13) {
		$(id).click();
		return false;
	}
	return true;
}

//---------------------- チェック部品 -------------------------
// 必須チェック
function isRequired(selObj, itemName, minLength) {
	if (selObj.value.length < minLength) {
		alert(format('{0}は{1}文字以上で入力してください。', [itemName, minLength]));
		return false;
	}
	return true;
}

// 書式チェック
function isWellFormed(selObj, itemName, pattern) {
	if (pattern.test(selObj.value) == false) {
	    alert(format('{0}が正しくありません。\n9999999-99-99の形式で入力して下さい。', [itemName]));
		return false;
	}
	return true;
}
