/*
Cpoyright (C) 2005 OKAMURA Yuji, All rights reserved.
*/

/*-- クラス CInfoseekDic --*/
function CInfoseekDic() {
	/*-- メンバ変数 --*/
	// 検索要求先
	this.m_url = 'http://dictionary.www.infoseek.co.jp/';

	// 検索文字列
	this.m_qt = null;

	// 検索タイプ
	//	1 - 前方一致
	//	2 - 後方一致
	//	3 - 完全一致
	//	4 - 包含
	this.m_sm = 1;

	// 辞書
	//	AL - 全部
	//	2T - 英和・和英
	//	KO - 国語
	//	KN - カタカナ
	this.m_sv = 'AL';

	this.m_errMsg = '';

	/*-- メンバ関数 --*/
	this.SetSV = function (type) {
		type = String(type);

		switch (type) {
		case 'AL':
			this.m_sv = 'AL';
			break;
		case '2T':
			this.m_sv = '2T';
			break;
		case 'KO':
			this.m_sv = 'KO';
			break;
		case 'KN':
			this.m_sv = 'KN';
			break;
		default:
			this.m_errMsg = '辞書のタイプに \x27'+type+'\x27 が与えられましたが、\x27AL\x27, \x272T\x27, \x27KO\x27, \x27KN\x27 の何れかでなければなりません。';

			return false;
			break;
		}

		return true;
	}

	this.SetSM = function (sm) {
		sm = parseInt(sm);
		if (sm < 1 || 4 < sm) {
			this.m_errMsg = '検索タイプに '+sm+' が与えられましたが、1 から 4 の整数でなければなりません。';
			return false;
		}
		this.m_sm = sm;
		return true;
	}

	this.SetQT = function (qt) {
		qt = String(qt);
		if (qt == '') {
			this.m_errMsg = '検索する文字列が空です。';
			return false;
		}
		this.m_qt = qt;
		return true;
	}

	this.Set2Form = function (f) {
		if (!this.m_qt) {
			this.m_errMsg = '検索文字列が指定されていません。';
			return false;
		}
		f.action = this.m_url;
		f.sm.value = this.m_sm;
		f.sv.value = this.m_sv;
		f.qt.value = this.m_qt;
		return true;
	}
}

function CreateInfoseekDicFromQuery() {
	var	dic = new CInfoseekDic();
	var	query = location.search.substr(1).split('&');
	var	i;
	var	msg = '';

	for (i = 0; i < query.length; i++) {
		var	value;

		if (query[i].match(/^sm=(\d)$/)) {
			value = RegExp.$1;
			if (!dic.SetSM(value)) {
				msg += dic.m_errMsg+'\n';
			}
		}
		else if (query[i].match(/^qt=(.*)$/)) {
			value = decodeURIComponent(RegExp.$1);
			if (!dic.SetQT(value)) {
				msg += dic.m_errMsg+'\n';
			}
		}
		else if (query[i].match(/^sv=(.*)$/)) {
			value = decodeURIComponent(RegExp.$1);
			if (!dic.SetSV(value)) {
				msg += dic.m_errMsg+'\n';
			}
		}
	}

	if (msg != '') {
		alert(msg);
		return null;
	}

	return dic;
}
