function Glue_Core( debug )
{
	if(debug) this._debug = debug;
}

Glue_Core.prototype =
{
	_debug: false,

	init: function()
	{
	},
	getByID: function( element_id )
	{
		return document.getElementById
			? document.getElementById(element_id)
			: document.all[element_id];
	},
	getByTag: function( tag_name, parent )
	{
		if( parent == null ){
			parent = document;
		}

		return parent.getElementsByTagName(tag_name).item(0);
	},
	getByTags: function( tag_name, parent )
	{
		if( parent == null ){
			parent = document;
		}

		return parent.getElementsByTagName(tag_name);
	},
	reset: function( form_id )
	{
		this.getByID(form_id).reset();
	},
	save: function( form_id )
	{
		this.getByID(form_id).submit();
	},
	show: function( element_id, display_type )
	{
		if( display_type == null ){
			display_type = 'block';
		}
		var e = this.getByID(element_id);
		e.style.display = display_type;

		return e;
	},
	hide: function( element_id )
	{
		this.getByID(element_id).style.display = 'none';
	},
	getPageSize: function()
	{
		var x, y;
		if( window.innerHeight && window.scrollMaxY ){	
			x = window.innerWidth + window.scrollMaxX;
			y = window.innerHeight + window.scrollMaxY;
		}else if( document.body.scrollHeight > document.body.offsetHeight ){
			// all but Explorer Mac
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		}else{
			// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}

		var ww, wh;
		if(self.innerHeight){
			// all except Explorer
			if(document.documentElement.clientWidth){
				ww = document.documentElement.clientWidth; 
			}else{
				ww = self.innerWidth;
			}
			wh = self.innerHeight;
		}else if(   document.documentElement
				 && document.documentElement.clientHeight
		){
			// Explorer 6 Strict Mode
			ww = document.documentElement.clientWidth;
			wh = document.documentElement.clientHeight;
		}else if(document.body){
			// other Explorers
			ww = document.body.clientWidth;
			wh = document.body.clientHeight;
		}

		var pw, ph;
		if( y < wh ){
			ph = wh;
		}else{ 
			ph = y;
		}

		if( x < ww ){	
			pw = ww; //x;
		}else{
			pw = x; //ww;
			if( y > wh ){
				// スクロールバーの幅を減算
				pw = pw - 15;
			}
		}

		return new Array( pw, ph, ww, wh );
	},
	getPageScroll: function()
	{
		var x, y;
		if(self.pageYOffset){
			y = self.pageYOffset;
			x = self.pageXOffset;
		}else if(   document.documentElement
				 && document.documentElement.scrollTop
		){
			// Explorer 6 Strict
			y = document.documentElement.scrollTop;
			x = document.documentElement.scrollLeft;
		}else if(document.body){
			// all other Explorers
			y = document.body.scrollTop;
			x = document.body.scrollLeft;
		}

		return new Array( x, y );
	},

	// for Ajax
	getXmlHttpRequest: function()
	{
		var q;
		if(window.XMLHttpRequest){
			q = new XMLHttpRequest();
		// IE
		}else if(window.ActiveXObject){
			try{
				q = new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e){
				q = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}

		return q;
	},

	// for formatter
	getSelected: function( id )
	{
		var e = this.getByID(id);
		if(document.selection){
			e.focus();
			var range = document.selection.createRange();

			return range.text;
		}else{
			var len = e.textLength;
			var start = e.selectionStart;
			var end = e.selectionEnd;
			if( end == 1 || end == 2 && len != undefined ) end = len;

			return e.value.substring( start, end );
		}
	},

	setSelection: function( id, val )
	{
		var e = this.getByID(id);
		if(document.selection){
			e.focus();
			var range = document.selection.createRange();
			range.text = val;
		}else{
			var top = e.scrollTop;
			var len = e.textLength;
			var start = e.selectionStart;
			var end = e.selectionEnd;
			if( end == 1 || end == 2 && len != undefined ) end = len;
			e.value = e.value.substring( 0, start ) + val + e.value.substr( end, len );
			e.selectionStart = start + val.length;
			e.selectionEnd = start + val.length;
			e.scrollTop = top;
		}
		e.focus();
	},

	encloseTag: function( id, tag, attr, val )
	{
		var str = this.getSelected(id);
		//if(str){
		var html  = '<'+ tag;
		if( attr != null ){
			html += ' '+ attr +'="'+ val +'"';
		}
		html += '>'+ str +'</'+ tag +'>';
		this.setSelection( id, html );
	//}

		return;
	},

	encloseList: function( id, tag, attr, val )
	{
		var str = this.getSelected(id);
		var reg = new RegExp( "\\n", "gm" );
		str = str.replace( reg, "</li>\n<li>");
		var html = '<'+ tag;
		if( attr != null ){
			html += ' '+ attr +'="'+ val +'"';
		}
		html += '>'+"\n"+'<li>'+ str +'</li>'+"\n"+'</'+ tag +'>'+"\n";
		this.setSelection( id, html );

		return;
	},

	encloseLink: function( id, is_mail )
	{
		var str = this.getSelected(id);
		var link = '';
		if(!is_mail){
			if( str.match(/^https?:/)){
				link = str;
			}else if( str.match(/^(\w+\.)+\w{2,5}\/?/)){
				link = 'http://'+ str;
			}else{// if( str.match(/ /)){
				link = 'http://';
			}
		}else{
			if( str.match(/@/)){
				link = str;
			}
		}
		var msg = is_mail ? 'Enter email address:' : 'Enter URL:';
		var link_str = prompt( msg, link );
		if( link_str != null ){
			if( str == '' ) str = link_str;
			if(is_mail) link_str = 'mailto:'+ link_str;
			this.setSelection( id, '<a href="'+ link_str +'">'+ str +'</a>' );
		}

		return;
	}
}

function initCore()
{
	glue = new Glue_Core();
	glue.init();

}
initCore();
//Event.observe( window, 'load', initCore, false );




function reset( form_id )
{
	glue.getByID(form_id).reset();
}

function toggle( element_id )
{
	var e = glue.getByID(element_id);
	if( e.style.display == 'block' ){
		hide(element_id);
	}else{
		show(element_id);
	}
}
function show( element_id )
{
	glue.getByID(element_id).style.display = 'block';
}

function hide( element_id )
{
	glue.getByID(element_id).style.display = 'none';
}
/*
function getSelected( id )
{
	var e = glue.getByID(id);
	if(document.selection){
		e.focus();
		var range = document.selection.createRange();

		return range.text;
	}else{
		var len = e.textLength;
		var start = e.selectionStart;
		var end = e.selectionEnd;
		if( end == 1 || end == 2 && len != undefined ) end = len;

		return e.value.substring( start, end );
	}
}

function setSelection( id, val )
{
	var e = glue.getByID(id);
	if(document.selection){
		e.focus();
		var range = document.selection.createRange();
		range.text = val;
	}else{
		var top = e.scrollTop;
		var len = e.textLength;
		var start = e.selectionStart;
		var end = e.selectionEnd;
		if( end == 1 || end == 2 && len != undefined ) end = len;
		e.value = e.value.substring( 0, start ) + val + e.value.substr( end, len );
		e.selectionStart = start + val.length;
		e.selectionEnd = start + val.length;
		e.scrollTop = top;
	}
	e.focus();
}

function encloseTag( id, tag, attr, val )
{
	var str = getSelected(id);
	//if(str){
		var html  = '<'+ tag;
		if( attr != null ){
			html += ' '+ attr +'="'+ val +'"';
		}
		html += '>'+ str +'</'+ tag +'>';
		setSelection( id, html );
	//}

	return;
}

function encloseList( id, tag, attr, val )
{
	var str = getSelected(id);
	var reg = new RegExp( "\\n", "gm" );
	str = str.replace( reg, "</li>\n<li>");
	var html = '<'+ tag;
	if( attr != null ){
		html += ' '+ attr +'="'+ val +'"';
	}
	html += '>'+"\n"+'<li>'+ str +'</li>'+"\n"+'</'+ tag +'>'+"\n";
	setSelection( id, html );

	return;
}

function encloseLink( id, is_mail )
{
	var str = getSelected(id);
	var link = '';
	if(!is_mail){
		if( str.match(/^https?:/)){
			link = str;
		}else if( str.match(/^(\w+\.)+\w{2,5}\/?/)){
			link = 'http://'+ str;
		}else{// if( str.match(/ /)){
			link = 'http://';
		}
	}else{
		if( str.match(/@/)){
			link = str;
		}
	}
	var msg = is_mail ? 'Enter email address:' : 'Enter URL:';
	var link_str = prompt( msg, link );
	if( link_str != null ){
		if( str == '' ) str = link_str;
		if(is_mail) link_str = 'mailto:'+ link_str;
		setSelection( id, '<a href="'+ link_str +'">'+ str +'</a>' );
	}

	return;
}*/