﻿
function getclass(element){
	var class_obj="";
	if(element){
		if(element.className){
			class_obj = element.className;
		}
		else if(element .classname){
			class_obj = element.classname;
		}
		else if(element.getAttribute("class")){
			class_obj = element.getAttribute("class");
		}
		/*
		else if(element.class_obj)
		{
			class_obj = element.class;
		}
		*/
	}
	return class_obj;
}





function setclass(element,name)
{
	if(element){
		element.className=name;
		if(getclass(element)==name){
			return element;
		}
		
		element.setAttribute("class");
		if(getclass(element)==name){
			return element;
		}
	}
	return false;
}




function get_alltag()
{
	if(document.getElementsByTagName("*")){
		all = document.getElementsByTagName("*");
	}else
	{
		all = document.all;
	}
	return all;
}





function getbyclass(class_name,tag_name)
{
	var ii = 0;
	var ret = new Array();
	if(!tag_name){
		all = get_alltag();
		for(var i = 0; i<all.length ;i++){
			if(getclass(all[i]) == class_name){
				ret[ii] = all[i];
				ii++;
			}
		}
	}
	else{
		tag = document.getElementsByTagName(tag_name);
		
		for(var i = 0; i<tag.length ;i++){
			if(getclass(tag[i]) == class_name){
				ret[ii] = tag[i];
				ii++;
			}
		}
	}
	return ret;
}

function getbycindex(class_name,tag_name)
{
	var ii = 0;
	var ret = new Array();
	if(!tag_name){
		all = get_alltag();
		for(var i = 0; i<all.length ;i++){
			if(getclass(all[i]).indexOf(class_name)>=0){
				ret[ii] = all[i];
				ii++;
			}
		}
	}
	else{
		tag = document.getElementsByTagName(tag_name);
		
		for(var i = 0; i<tag.length ;i++){
			if(getclass(tag[i]).indexOf(class_name)>=0){
				ret[ii] = tag[i];
				ii++;
			}
		}
	}
	return ret;
}





function getbyname(elm_name,tag_name)
{
	var ii = 0;
	var ret = new Array();
	if(!tag_name){
		all = get_alltag();
		for(var i = 0; i<all.length ;i++){
			if(all[i].name == elm_name){
				ret[ii] = all[i];
				ii++;
			}
		}
	}
	else{
		tag = document.getElementsByTagName(tag_name);
		for(var i = 0; i<tag.length ;i++){
			if(tag[i].name == elm_name){
				ret[ii] = tag[i];
				ii++;
			}
		}
	}
	return ret;
}




var mousex;
var mousey;
var is_event_tag;

window.document.onmousemove = function(e){
	if(e)
	{
		
		mousex = e.pageX+document.body.scrollLeft;
		mousey = e.pageY+document.body.scrollTop;
	}
	else
	{
		is_event_tag = 1;
	}
}

function get_mouse_pos()
{
	var mouse;
	
	if(is_event_tag == 1)
	{	
	
		if ( document.compatMode == 'CSS1Compat')
		{ 
			mousey=event.clientY+document.documentElement.scrollTop;
		}
		else
		{
			mousey=event.clientY+document.body.scrollTop; 
		}
	
		
		mousex= event.clientX+document.body.scrollLeft;
	}
	
	mouse["x"] = mousex;
	mouse["y"] = mousey;
	
	return mouse;
}





function getbyid(id)
{
	if(document.getElementById(id)){
		return document.getElementById(id);
	}else
	{
		return false;
	}
}




function getbytag(tag)
{
	return document.getElementsByTagName(tag); 
}




function include_js(jspath){
 jspath = jspath.toString();

 var scriptElm = document.createElement('script');
 scriptElm.type = 'text/javascript';
 scriptElm.charset = 'utf-8';
 scriptElm.src = jspath;
 var head = document.getElementsByTagName('head')[0];
 head.appendChild(scriptElm);
}




function v_change(element)
{
	if(element.style.display!="none")
	{
		element.style.display = "none";
	}else
	{
		element.style.display = "block";
	}
}




function getbyindex(id_string,tag_name)
{
	var ret=new Array();
	var i2=0;
	if(tag_name)
	{
		var elements=getbytag(tag_name);
	}else
	{
		var elements=get_alltag();
	}
	
	for(var i = 0; i < elements.length ; i++)
	{
		if(elements[i].id)
		{
			if(elements[i].id.indexOf(id_string)>=0)
			{
				ret[i2]=elements[i];
				i2++;
			}
		}
	}
	return ret;
}




function setopacity (element,opacity) {
	element.style.filter = 'alpha(opacity=' +opacity+ ')';
	element.style.MozOpacity = opacity / 100;
	element.style.opacity = opacity / 100;
	
	
	if(/MSIE/.test(navigator.userAgent) && !window.opera && (!element.currentStyle.hasLayout))
	{
		element.style.zoom=1;
	}

}




function visible(element,flag)
{
	if(!flag)
	{
		element.style.display = "none";
	}else
	{
		element.style.display = "block";
	}
}






function getarearange(obj) {
	var pos = new Object();
	var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);

	if (isIE) {
		obj.focus();
		var range = document.selection.createRange();
		var clone = range.duplicate();
		 
		clone.moveToElementText(obj);
		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 = obj.selectionStart;
		pos.end = obj.selectionEnd;
	}

	return pos;
}




function geteventelm(event) {
	if(!event){
		elm=window.event.srcElement;
	}else{
		elm=event.target;
	}
	return elm;
}




function insertafter(parent, node, referenceNode) {
        parent.insertBefore(node, referenceNode.nextSibling);
}




function create(tagname){
	return document.createElement(tagname);
}


function tcreate(tagname){
	return document.createTextNode(tagname);
}



function appfirstchild(parentnode,addnode){
	return parentnode.insertBefore(addnode,parentnode.firstChild);
}





function is_object(obj) {
	return (Object.prototype.toString.call(obj) == "[object Object]");
}
function is_func(obj) {
	return (Object.prototype.toString.call(obj) == "[object Function]");
}
function is_array(obj) {
	return (Object.prototype.toString.call(obj) == "[object Array]");
}
function is_bool(obj) {
	return (Object.prototype.toString.call(obj) === "[object Boolean]");
}
function is_string(obj) {
	return (Object.prototype.toString.call(obj) === "[object String]");
}
function is_number(obj) {
	return (Object.prototype.toString.call(obj) === "[object Number]");
}
function is_element(obj) {
	var check = false;
	var nothing_contents=0;
	switch(Object.prototype.toString.call(obj)) {
	  case "[object Object]":
	    if (obj.length) check = true;
	    break;
	  case "[object Window]":
	  case "[object HTMLParagraphElement]":
	    if (! is_undefined(obj) && ! is_null) check = true;
	  	else  nothing_contents=1;
	    break;
	  default:
	    break;
	}
	if(!nothing_contents && Object.prototype.toString.call(obj).match(/\[object\s+HTML.*?Element\]/)){
		check = true;
	}
	return check;
}
function is_elementCollection(obj) {
	return (Object.prototype.toString.call(obj) === "[object HTMLCollection]");
}
function is_null(obj) {
	return (obj === null);
}
function is_undefined(obj) {
	return (obj === undefined);
}

function gettype(obj) {
	if (is_null(obj)) { return "null"; }
	if (is_undefined(obj)) { return "undefined"; }
	if (is_element(obj)) { return "HTMLElement"; }
	return Object.prototype.toString.call(obj).split(" ")[1].replace("]", "");
}




function surroundtext(textEl, Front, Back) {
	
	var target = textEl;
	var pos = getAreaRange(target);
	 
	var val = target.value;
	var range = val.slice(pos.start, pos.end);
	var beforeNode = val.slice(0, pos.start);
	var afterNode = val.slice(pos.end);
	var insertNode;
	 
	if (range || pos.start != pos.end) {
		insertNode =  Front  + range + Back;
		target.value = beforeNode + insertNode + afterNode;
	}
	 
	else if (pos.start == pos.end) {
		insertNode = Front+Back;
		target.value = beforeNode + insertNode + afterNode;
	}
}


function debug(obj){
	var obj_info=sprint_r(obj,0);
	
	if(!getbyid("debug_body")){
		debug_elm=create("div");
		debug_elm.setAttribute("id","debug_body");
		debug_elm.style.margin="10px";
		debug_elm.style.border="solid 1px black";
		appfirstchild(getbytag("body")[0],debug_elm);
	}
	var debug_body=getbyid("debug_body");
	debug_elm=create("div");
	debug_elm.className="debug_body_child";
	debug_elm.style.borderBottom="dotted 1px #010101";
	debug_elm.appendChild(obj_info);
	debug_body.appendChild(debug_elm);
	if(debug_body.offsetHeight>=800){
		debug_body.style.overflow="scroll";
		debug_body.style.height="800px";
	}
}

function sprint_r(theObj,is_super){
	if(!is_super){
		var content_elm=create("div");
		content_elm.appendChild(document.createTextNode(gettype(theObj)));
	}
	
	if (theObj.constructor == Array || theObj.constructor == Object || is_element(theObj)) {
		var superUlElement=document.createElement("UL");
		superUlElement.style.paddingLeft ="20px";
		superUlElement.style.textAlign="left";
		for (var p in theObj){
			//if(theObj[p]){
			if(!is_null(theObj[p])){
				var liElement;
				var text;
				liElement = document.createElement("LI");
				liElement.style.listStyleType ='none';
				if ( theObj[p].constructor == Array || theObj[p].constructor == Object ) {
					text=document.createTextNode("["+p+"] => " + gettype(theObj[p]) );
					liElement.appendChild(text);
					if ( childUlElement=sprint_r(theObj[p],1) ) {
						liElement.appendChild(childUlElement);
					}
				}else{
					text = document.createTextNode("["+p+"] => " + theObj[p] );
					liElement.appendChild(text);
				}
				superUlElement.appendChild(liElement);
			}
		}
		if(!is_super){
			content_elm.appendChild(superUlElement);
			
			return content_elm;
		}
		else{
			return superUlElement;
		}
	}
	else if(is_string(theObj) || is_number(theObj) || is_bool(theObj)){
		return tcreate(theObj);
	}
	else {
		return null;
	}
}

function print_r( theObj, outputObjId ){
	if(getbyid(outputObjId)){
		getbyid(outputObjId).appendChild(document.createTextNode(gettype(theObj)));
	}
		
	if ( theObj.constructor == Array || theObj.constructor == Object || is_element(theObj)) {
		var superUlElement = document.createElement("UL");
		superUlElement.style.paddingLeft ="20px";
		for ( var p in theObj ){
			if(theObj[p]){
				var liElement;
				var text;
				liElement = document.createElement("LI");
				liElement.style.listStyleType ='none';
				if ( theObj[p].constructor == Array || theObj[p].constructor == Object ) {
					text = document.createTextNode( "[" + p + "] => " + gettype(theObj[p]) );
					liElement.appendChild(text);
					if ( childUlElement = print_r( theObj[p], "__super__" ) ) {
						liElement.appendChild(childUlElement);
					}
				} else {
					text = document.createTextNode( "[" + p + "] => " + theObj[p] );
					liElement.appendChild(text);
				}
				superUlElement.appendChild(liElement);
			}
		}
		if ( outputObjId != "__super__" ) {
			document.getElementById(outputObjId).appendChild(superUlElement);
		} else {
			return superUlElement;
		}
	}
	else if(is_string(theObj) || is_number(theObj)|| is_bool(theObj)){
		return null;
	}
	else {
		return null;
	}
}


function get_active_elm(){
	var element = (document.activeElement || window.getSelection().focusNode);
	return element;
}

function is_in_child(child,parent){
	if(child.parentNode){
		if(child.parentNode===parent){
			return true;
		}
		else{
			var child_flag=is_in_child(child.parentNode,parent);
			return child_flag;
		}
	}
	else{
		return false;
	}
}


function add_event(target, type, listener) {
	if (target.addEventListener){
		target.addEventListener(type, listener, false);
	}
	else if (target.attachEvent){
		 target.attachEvent('on' + type, function() { listener.call(target, window.event); });
	}
	else{
		 target['on' + type] = function(e) { listener.call(target, e || window.event); };
	}
}



