/**
 * Javascript ライブラリ
 *
 * Noriko Shimazaki
 * 2007/07/21(Sut.)
 */

 /**
 * 開発中一時設定
 * ドキュメントルートの指定
 * 2007/07/21(Sut.)
 */
	function getDocumentRoot()	{
		return '/h';
	}

 /**
 * Ajax共通モジュール
 *
 * 2007/07/21(Sut.)
 * Noriko Shimazaki
/*--------------------------------------------------------------------------*/
/**
* POSTデータ送信
*/
	function sendPost( url, params, sendForm )	{
		// エレメント初期化
		error_areas = document.getElementsByClassName( 'error_area' );
		Element.hide( $( 'response_message' ) );
		$( 'response_message' ).innerHTML = '';
		for ( i = 0; i < error_areas.length; i++ )	{
			error_areas[ i ].innerHTML = '';
		}

		validates = document.getElementsByClassName( 'validate' );
		for ( i = 0; i < validates.length; i++ )	{
			 validates[ i ].style.backgroundColor  = '#FFF';
		}

		// POSTデータ送信
		new Ajax.Request( url,
							{
								method: 'post',
								onComplete: getResponse,
								postBody: params
							}
						);

		function getResponse( req )	{
//alert( req.responseText );

			if ( req.responseText == 'OK' )	{
				sendForm.submit();
				return;
			}
			// 受け取ったレスポンスをeval
			errors = eval( req.responseText );
			// エラーを表示
			for( i = 0; i < errors.length; i++ )		{
				var elemName = errors[ i ][ 'elem' ];

				if ( elemName == 'response_message' )	{
					Element.show( $( elemName ) );
					$( elemName ).innerHTML = errors[ i ][ 'message' ];
				} else	{
					$( 'error_' + elemName ).innerHTML = errors[ i ][ 'message' ];
					Element.show( $( 'error_' + elemName ) );
					$( elemName ).style.backgroundColor  = '#FAF4FA';
				}
			}
		}

	}

/**
* GETデータ送信
*/
	function sendGet( url, type, mes, thunk )	{

		// GETデータ送信
		new Ajax.Request( url,
							{
								method: 'get',
								onComplete: getResponse
							}
						);

		function getResponse( req )	{
//alert( req.responseText );
			// 受け取ったレスポンスをeval
			res = eval( req.responseText );

			// レスポンスを表示
			for( i = 0; i < res.length; i++ )		{
				var elemName = res[ i ][ 'elem' ];

				if ( elemName == 'error' )	{
					alert( res[ i ][ 'value' ] );
					return;
				} else	{
					if ( type == 'input' )	{
						$( elemName ).value = res[ i ][ 'value' ];
					} else	{
						$( elemName ).innerHTML = res[ i ][ 'value' ];
					}
				}
			}
                   thunk();
		}
	}

/**
* スケジュールデータ送受信
*/
	function editSchedule( url, params )	{

		// POSTデータ送信
		new Ajax.Request( url,
							{
								method: 'post',
								onComplete: getResponse,
								postBody: params
							}
						);

		function getResponse( req )	{

			// 受け取ったレスポンスをeval
			res = eval( req.responseText );

			// レスポンスを表示
			for( i = 0; i < res.length; i++ )		{
				var elemName = res[ i ][ 'elem' ];
				$( elemName ).innerHTML = res[ i ][ 'value' ];
			}
		}

	}


 /**
 * フォームの入力フィールドをリセットする
 *
 * 2007/07/23(Mon.)
 * Noriko Shimazaki
/*--------------------------------------------------------------------------*/
	function resetForm( frm )	{
		var	elems = Form.getElements( frm );

		for( var i = 0; i < elems.length;  i++ )		{
	    		switch ( elems[i].type )	{

				case "checkbox":
				case "radio":
	        			elems[i].checked = false;
					break;

				case "select-one":
				case "select-multiple":
					elems[i].selectedIndex = 0;
					break;

				case "file":
				case "textarea":
				case "text":
					elems[i].value="";
					break;

				default:
					break;
			}

		}

	}


 /**
 * ButtonEffectクラス
 * ボタンにエフェクトをセットする
 *
 * Noriko Shimazaki
/*--------------------------------------------------------------------------*/

// マウスエフェクト
var ButtonEffect = Class.create();
ButtonEffect.prototype = {

	 // Constructor
	 initialize : function( elem, onMouseColor )	{
 		this.onMouseColor = onMouseColor;
	 	this.outMouseColor = Element.getStyle( elem, 'color' );

	 	// エフェクトをセット
	 	Event.observe( elem,
	 				'mouseover',
	 				this.onMouse.bindAsEventListener( this ),
	 				false );

	 	Event.observe( elem,
	 				'mouseout',
	 				this.outMouse.bindAsEventListener( this ),
	 				false );


	 },

	onMouse : function( evt )    {
		target = Event.element( evt );
		target.style.color =  this.onMouseColor ;
	},

	outMouse : function ( evt )    {
		target = Event.element( evt );
		target.style.color =  this.outMouseColor ;
	}
}

 /**
 * ButtonLocationクラス
 * ClickイベントにURLを仕込む
 *
 * Noriko Shimazaki
 * 2007/07/24(Tue.)
/*--------------------------------------------------------------------------*/

var ButtonLocation = Class.create();
ButtonLocation.prototype = {

	 // Constructor
	 initialize : function( elem, action, url, id )	{
 		this.locationURL = url;
	 	this.action = action;
	 	this.ID = id;

	 	// イベントをセット
		 if( action == 'delete' )	{
		 	Event.observe( elem,
		 				'click',
		 				this.deleteEvent.bindAsEventListener( this ),
		 				false );
		 } else	{
		 	Event.observe( elem,
		 				'click',
		 				this.actionEvent.bindAsEventListener( this ),
		 				false );
		 }
	 },

	deleteEvent : function ( evt )    {
		if ( confirm( 'ID:' + this.ID + " のデータを削除します\nよろしいですか？"  ) )	{
			location.href = this.locationURL + 'delete/id/' + this.ID + '/'
		}
		return;
	},

	actionEvent : function( evt )    {
		location.href = this.locationURL + this.action + '/id/' + this.ID + '/'
		return;
	}
}

 /**
 * OpenSubWindowクラス
 * Clickイベントでサブウィンドウを表示
 *
 * Noriko Shimazaki
 * 2007/07/27(Fri.)
/*--------------------------------------------------------------------------*/

var OpenSubWindow = Class.create();
OpenSubWindow.prototype = {

	 // Constructor
	 initialize : function( elem, contents, url, width, height, scrollbars )	{

 		this.contents = contents;			// サブウィンドウに表示するコンテンツ
 		this.URL = url;					// サブウィンドウに表示するURL
 		this.width = width;				// サブウィンドウの幅
 		this.height = height;				// サブウィンドウの高さ
 		this.scrollbars = scrollbars;		// スクロールバーの有無

	 	// イベントをセット
	 	Event.observe( elem,
	 				'click',
	 				this.openWindow.bindAsEventListener( this ),
	 				false );
	 },

	openWindow : function ( evt )    {
		var scrollbars = '';
		if ( !this.scrollbars )	{
			scrollbars = ', scrollbars=no';
		} else	{
			scrollbars = ', scrollbars=yes';
		}

		subWindow = window.open( this.URL, 'sub_' + this.contents,
//					'status=0, width=' + this.width + ', height=' + this.height + ', resizable=1' + scrollbars )
					'location=1, status=1, width=' + this.width + ', height=' + this.height + ', resizable=1' + scrollbars )

		subWindow.focus();
		return;
	}
}

 /**
 * エラー解除
 *
 * Noriko Shimazaki
/*--------------------------------------------------------------------------*/
var CancelError = Class.create();
CancelError.prototype = {

	 // Constructor
	 initialize : function( elem, bgColor, elemError, elemClear, noSelf )	{
 		this.bgColor = bgColor;
 		this.elemError = elemError;
 		this.elemClear = elemClear;
 		this.noSelf = noSelf;

	 	// エフェクトをセット
	 	Event.observe( elem,
	 				'change',
	 				this.changeElems.bindAsEventListener( this ),
	 				false );


	 },

	changeElems : function( evt )    {
		target = Event.element( evt );

		if ( this.elemError != null )	{
		// 背景色変更
			target.style.backgroundColor =  this.bgColor ;

				this.elemError.innerHTML = "";
				Element.hide( this.elemError );
		} else	{
			if ( this.noSelf )	{
			// 背景色変更のみ
				target.style.backgroundColor =  this.bgColor ;
			}
		}

		// エラーエリアの文字列消去
		if( this.elemClear.length > 0 )	{
			for( var i = 0; i < this.elemClear.length; i++ )	{
				var elem = this.elemClear[ i ];

				Element.hide( elem );
				elem.innerHTML = "";
			}
		} else	{
			this.elemClear.innerHTML = "";
			Element.hide( this.elemClear );
		}
	}
}

/**
 * PageEffectクラス
 *
 * 2007/08/29(Wed.)
 * Noriko Shimazaki
/*--------------------------------------------------------------------------*/
var PageEffect = Class.create();
PageEffect.prototype = {
	 initialize : function()	{
	 },

	// 画像ロールオーバー
	imageRollOver : function( elem, onSrc, outSrc )    {
		Event.observe( elem, 'mouseover', function(){  elem.src=onSrc;  }, false );
		Event.observe( elem, 'mouseout', function(){  elem.src=outSrc;  }, false );
	}
}

/**
 * バックグラウンドEffectクラス
 *
 * 2007/07/24(Tue.)
 * Noriko Shimazaki
/*--------------------------------------------------------------------------*/
var BgEffect = Class.create();
BgEffect.prototype = {
	 initialize : function()	{
	 },

	// 背景色変更
	changeColor : function( elem, color )    {

		    elem.style.background = color;

	},


	// テーブルストライプ背景
	tableStripe : function(  oParent, sColor1, sColor2 )	{
		//    var oParent        = $( sId );
		    var oChild        = null;
		    var oChildren    = oParent.childNodes;
		    var bStripeFlg    = 0;

		    for (var i = 0; i < oChildren.length; i++) {
		        if (!oChildren[i])    continue;
		        oChild = oChildren[i];
		        break;
		    }

		    while (1) {
		        if (undefined == oChild.tagName) {
		            oChild = oChild.nextSibling;
		            continue;
		        }
		        // 例外を列挙 ※記法:(1|2|3...)
		        if (!oChild.tagName.match("(TBODY|THEAD|UL)"))    break;
		        oChild = oChild.firstChild;
		    }

		    while (oChild != null) {
		        if (bStripeFlg == 0) {
		            oChild.style.backgroundColor = sColor1;
		            bStripeFlg++;
		        } else {
		            oChild.style.backgroundColor = sColor2;
		            bStripeFlg--;
		        }
		        oChild = oChild.nextSibling;
		        if (oChild && oChild.nodeType == 3)    oChild = oChild.nextSibling;
		    }

	}
}

 /**
 * InsertTagクラス
 * テキストエリアにタグを挿入する
 *
 * Noriko Shimazaki
 * 2007/12/04(Tue.)
/*--------------------------------------------------------------------------*/

var InsertTag = Class.create();
InsertTag.prototype = {

	 // Constructor
	 initialize : function( elem )	{
	 	this.elem = elem;
	 	this.isIE = this.checkIE();
	 },

	 // IEかどうか確認
	 checkIE : function ()    {
		 return (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);
	},

	// 挿入
	Insert : function( tag )	{
		var	pos = this.getAreaRange();
		var	value = this.elem.value;
		var	range = value.slice( pos.start, pos.end );
		var	beforeNode = value.slice( 0, pos.start );
		var	afterNode = value.slice( pos.end );
		var	insertNode;

		if ( range || pos.start != pos.end )	{
			insertNode = tag.start + range + tag.end;
		} else if( pos.start == pos.end )	{
			insertNode = tag.start + tag.end;
		}

		this.elem.value = beforeNode + insertNode + afterNode;
	},

	 // 選択領域を取得
	getAreaRange : function()	{
		var	pos = new Object();

		if ( this.isIE )	{
			// IE仕様
			this.elem.focus();
			var	range = document.selection.createRange();
			var	clone = range.duplicate();

			clone.moveToElementText( this.elem );
			clone.setEndPoint( 'EndToEnd', range );

			pos.start = clone.text.length - range.text.length;
			pos.end = clone.text.length - range.text.length + range.text.length;

		} else if( window.getSelection() )	{
			pos.start = this.elem.selectionStart;
			pos.end = this.elem.selectionEnd;

		}
		return pos;
	},

	// 太字タグ挿入
	Bold : function()	{
		var	tag = new Object();

		tag.start = '<b>';
		tag.end = '</b>';
//		tag.start = '<span style="font-weight: bold;">';
//		tag.end = '</span>';

 		this.Insert( tag );

	},

	// リンク挿入
	Link : function()	{
		var	tag = new Object();

		tag.start = '<a href="http://">';
		tag.end = '</a>';

 		this.Insert( tag );
	},

	// テーブル挿入
	Table : function()	{
		var	tag = new Object();

		tag.start = "<table class=\"tour_point\">";
		tag.start = tag.start + "<tr><td></td><td></td><td></td></tr>";
		tag.start = tag.start + "<tr><td></td><td></td><td></td></tr>";
		tag.start = tag.start + "<tr><td></td><td></td><td></td></tr>";
		tag.end = "</table>";

 		this.Insert( tag );
	}


}

