/*
 * 名称　　　：mu_focus
 * バージョン：Ver.1.0.5b
 * 内容　　　：Enterキーでフォーカス移動
 * 動作環境　：IE6,NN6.21,NN7,Mozilla1.1,Mozilla1.2で動作確認。
 * 　　　　　　Opera6では動作しません。
 * 作成日　　：2002/09/29
 * 更新日　　：2003/01/31
 * 作成者　　：R.Sakamoto
 * 著作権　　：Copyright (c) 2002-2003 YDS. All rights reserved.
 * ライセンス：LGPL
 * 配布元　　：http://www.mula-net.com/mulib/
 */

//=====================================
/**
 * keypressイベントの処理
 *
 * Enterキーを取得したい項目のonkeypress()でこの関数を呼んでください
 */
function mu_focus_onKeyPress(e) {
	if (window.event) {	/* for IE */
		e = event;
		if (e.ctrlKey || e.altKey) return true;
		if (e.keyCode == 13) {
			if (e.shiftKey) {
				mu_focus_back(e.srcElement);
			} else {
				var object = e.srcElement;
				if ((object.type) && (
						(object.type == "button")
						|| (object.type == "submit")
						|| (object.type == "reset")
						|| (object.type == "image")
				)) return true;	/* ボタンではEnterで本来の動作 */
				mu_focus_forward(e.srcElement);
			}
			e.returnValue = false;
			e.cancelBubble = true;
			return false;
		}
	} else if(e) {
		if (e.ctrlKey || e.altKey) return true;
		if (e.keyCode == 13) {
			if (e.shiftKey) {
				mu_focus_back(e.target);
			} else {
				var object = e.target;
				if ((object.type) && (
					(object.type == "button")
					|| (object.type == "submit")
					|| (object.type == "reset")
					|| (object.type == "image")
				)) return true;	/* ボタンではEnterで本来の動作 */
				mu_focus_forward(e.target);
			}
			e.stopPropagation();
			e.preventDefault();
			return false;
		}
	}
	return true;
}

/**
 * フォーカスを次の項目へ
 *
 * この関数は単独で使うこともできます
 * パラメータ currentObject にはフォーム項目を渡してください
 */
function mu_focus_forward(currentObject) {
	if (!currentObject) {
		if (this.form) {
			/* currentObjectが渡らず、thisがform項目ならthisを使う */
			currentObject = this;
		} else {
			/* それもだめなら最初のフォーム項目にフォーカスを与えるようにする */
			currentObject = null;
		}
	} else {
		if (!currentObject.form)
			currentObject = null;	/* currentObjectがフォーム項目でない場合も */
	}
	
	var currentIndex = 1;
	var passFlag = false;
	var nextObject = null;
	var nextTabIndex = 32769;
	
	if (currentObject) {
		currentIndex = currentObject.tabIndex;
		if (currentIndex < 1) currentIndex = 1;
	} else {
		passFlag = true;
	}
	
	for (var i = 0; i < document.forms.length; i++) {
		for (var j = 0; j < document.forms[i].elements.length; j++) {
			var object = document.forms[i].elements[j];
			var index = object.tabIndex;
			
			/* 現在のフォーカス位置 */
			if (object == currentObject) {
				passFlag = true;
				continue;
			}
			
			/* 移動対象チェック */
			if (
				(index < 1)
				|| (object.type == "hidden")
				|| (object.disabled)
			) continue;	/* 移動対象外 */
			
			if (nextObject == null) nextObject = object;
			if (index < currentIndex) {
				continue;
			} else if (index == currentIndex) {
				if (passFlag) {
					nextObject = object;
					break;
				}
			} else if (index < nextTabIndex) {
				nextObject = object;
				nextTabIndex = index;
			}
		}
	}
	if (nextObject) nextObject.focus();
}

/**
 * フォーカスを前の項目へ
 *
 * この関数は単独で使うこともできます
 * パラメータ currentObject にはフォーム項目を渡してください
 */
function mu_focus_back(currentObject) {
	if (!currentObject) {
		if (this.form) {
			/* currentObjectが渡らず、thisがform項目ならthisを使う */
			currentObject = this;
		} else {
			/* それもだめなら最初のフォーム項目にフォーカスを与えるようにする */
			currentObject = null;
		}
	}
	
	var currentIndex = 1;
	var passFlag = false;
	var nextObject = null;
	var nextTabIndex = -2;
	
	if (currentObject) {
		currentIndex = currentObject.tabIndex;
		if (currentIndex < 1) currentIndex = 1;
	} else {
		passFlag = true;
	}
	for (var i = 0; i < document.forms.length; i++) {
		for (var j = document.forms[i].elements.length - 1; j >= 0; j--) {
			var object = document.forms[i].elements[j];
			var index = object.tabIndex;
			
			/* 現在のフォーカス位置 */
			if (object == currentObject) {
				passFlag = true;
				continue;
			}
			
			/* 移動対象チェック */
			if (
				(index < 1)
				|| (object.type == "hidden")
				|| (object.disabled)
			) continue;	/* 移動対象外 */
			
			if (nextObject == null) nextObject = object;
			if (index > currentIndex) {
				continue;
			} else if (index == currentIndex) {
				if (passFlag) {
					nextObject = object;
					break;
				}
			} else if (index > nextTabIndex) {
				nextObject = object;
				nextTabIndex = index;
			}
		}
	}
	if (nextObject) nextObject.focus();
}

//=====================================
/**
 * フォーム項目にonkeypressを追加
 *
 * この関数はonload()で呼ぶなど、ロード後に実行してください
 */
function mu_focus(e) {
	for (var i = 0; i < document.forms.length; i++) {
		for (var j = 0; j < document.forms[i].elements.length; j++) {
			var object = document.forms[i].elements[j];
			if ((object.type != "textarea") && (object.type != "hidden")) {
				if (object.onkeypress == null) {
					object.onkeypress = mu_focus_onKeyPress;
				}
			}
		}
	}
}

//=====================================
/*
 * 以下は関数でなく、ここで行う処理
 * 外部ファイルとして読み込むだけで動作させるため、ここでやってます
 */

/* onloadに設定 */
if (window.onload == null) window.onload = mu_focus;

/* フォーム項目以外でのEnterの対応。でもこれやるとEnterの通常の動作ができない */
//if (document.onkeypress == null) document.onkeypress = mu_focus_onKeyPress;
