/* ********* 汎用関数 ********* */

/* 
----
show_hide( id, s_h )
is_show( id )
is_exist( id )
set_color( id, txt_color )
set_bg_img( id, txt_url )
set_img( name, txt_url )
resizeInnerTo( width, height )
*/

/* 
----
check_browser_6_or_later()
open_window( txt, title, headers, parameters )
*/

/*
----
function insetRule(selector,declarations)
*/


function show_hide( id, s_h ) {
	var obj = document.getElementById( id );	
	var dirty = 1;
	var txt_disp = "";

	if ( ! s_h ) txt_disp = "none";
	if ( obj != null ) {
		if ( null != obj.style.display ) {
			obj.style.display = txt_disp;
			dirty = 0;
		}
	}
	return dirty;
}

function is_show( id ) {
	var obj = document.getElementById( id );
	if ( obj != null ) {
		if ( null != obj.style.display ) {
			if ( "" == obj.style.display ) {
				return true;
			}
		}
	}
	return false;
}

function is_exist( id ) {
/*	alert( "is_exist( " + txtArg + " ) is called." );	*/
	var obj = document.getElementById( id );
	if ( obj != null ) {
		return true;
	}
	return false;
}

function set_color( id, txt_color ) {
	var obj = document.getElementById( id );
	if ( obj == null ) {
		return true;
	} else {
		if ( null != obj.bgColor ) {
			obj.bgColor = txt_color;
		}
		if ( null != obj.style.background ) {
			obj.style.background = txt_color;
		}
	}
	return false;
}

function set_bg_img( id, txt_url ) {
	var obj = document.getElementById( id );
	if ( null == obj ) {
		alert( "エラー：このページ上には、" + id + " という id のスタイルシートがありません。");
	} else {
		obj.style.backgroundImage = "url(" + txt_url + ")";
	}
}

function set_img( name, txt_url ) {
	var obj = document.images[ name ];
	if ( null == obj ) {
		alert( "エラー：このページ上には、" + name + "という名前の画像がありません。" );
	} else {
		obj.src = txt_url;
	}
	return false;
}

function resizeInnerTo( width, height ){
/*	alert( "resizeInnerTo( " + width + ", " + height + " ) is called." );	*/
	var sw = screen.availWidth;
	var sh = screen.availHeight;

	var obj = parent;
	if( window.opera || document.layers ){	//o6-7, n4
		var w = obj.innerWidth;
		var h = obj.innerHeight;
	} else if( document.all ){				//ie4-
		var w = obj.document.body.clientWidth;
		var h = obj.document.body.clientHeight;
	} else if( document.getElementById ){	//n6-7, m1, s1
		var w = obj.innerWidth;
		var h = obj.innerHeight;
	}
/*	alert( "width: " + width + ", w: " + w + ", height: " + height + ", h: " + h );	*/
	if( width <= 0  || null == w ) {
		w = 0;
		width = 0;
	}
	if( height <= 0 || null == h ) {
		h = 0;
		height = 0;
	}
	if( width != w || height != h ) {
/*		alert( "resizeBy( " + (width - w) + ", " + (height - h) + " ) is called next." );	*/
		obj.resizeBy( width - w, height - h );
	}
}

function check_browser_6_or_later() {
	var ret = 0;
	if ( document.getElementById ) { 
		ret = 1;
	}
	if( navigator.userAgent.indexOf("MSIE 4") != -1 
		||
		navigator.userAgent.indexOf("MSIE4") != -1 
	) {
		ret = 0;
	}
	if( navigator.userAgent.indexOf("MSIE 5") != -1 
		||
		navigator.userAgent.indexOf("MSIE5") != -1 
	){
		ret = 0;
	}
	if ( document.layers ) {
		ret = 0;
	}
	return ret;
}

function open_window( txt, title, headers, parameters ) {
	var txt_tmp = "";

	txt_tmp += "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n";
	txt_tmp += "	\"http://www.w3.org/TR/html4/loose.dtd\">\n";
	txt_tmp += "<html lang=\"ja\"><head>\n";
	txt_tmp += "<meta http-equiv=\"content-type\" content=\"text/html; charset=shift_JIS\">\n";
	txt_tmp += "<meta http-equiv=\"content-style-type\" content=\"text/css\">\n";
	txt_tmp += "<meta http-equiv=\"content-script-type\" content=\"text/javascript\">\n";
	txt_tmp += "\n";
	txt_tmp += headers;
	txt_tmp += "<title>" + title + "</title>\n";
	txt_tmp += "</head><body>\n";
	txt_tmp += "\n";
	txt_tmp += "<h1>" + title + "</h1>\n";
	txt_tmp += txt;
	txt_tmp += "</body>\n</html>\n";

	var win_sitemap = window.open( "" , title, parameters );
	win_sitemap.document.open("text/html");
	win_sitemap.document.write( txt_tmp );
	win_sitemap.document.close( ); 
}

function insetRule(selector,declarations){
	var sheets = document.styleSheets;

	if( sheets ){
		var tSheet=sheets[ sheets.length-1 ]; // 一番最後のスタイルシート

		if(document.all) {
			tSheet.addRule( selector,declarations );
		} else {
			tSheet.insertRule( selector + "{" + declarations + "}", tSheet.cssRules.length );
		}
	}
}
