/* ______________________________________________________________________

   MK Java Script Common Library

   Script written by mami komula <mami@imagesync.net>
   http://www.imagesync.net/
   http://www.warehouse56.com/

   Supports Netscape 6 / Gecko
			IE 4, 5 and 5.5 (Windows)
			IE 4.5 and IE 5 (MacOS)
			NN 4.5, 4.7 (Windows / MacOS)

   2001.5.20 [added: setDisplay,getDisplay / renamed: setVisibility]
   2001.3.22 [added: chkStrNumber,chkStrHalfKana,chkStrFullKanaK,
              chkStrFullKanaH / fixed: cngClip for NN4]
   2001.3.21 [added: getEventCoords / fixed:getBrouwserName]
   2001.3.17 [fixed: cngPosY]
______________________________________________________________________ */

var Mac = navigator.appVersion.indexOf('Mac',0) != -1;
var Win = navigator.appVersion.indexOf('Win',0) != -1;
var IE  = getBrouwserName() == 'Explorer';
var NN  = getBrouwserName() == 'Netscape';
var Moz = navigator.userAgent.indexOf("Gecko") != -1;
var VER = parseInt(navigator.appVersion);
var MacIE5 = ((Mac && navigator.appVersion.indexOf('MSIE 5',0) != -1) || (Mac && IE && VER > 4));
var Vmajor = parseInt(navigator.appVersion);	// ex. 3
var Vminor = parseFloat(navigator.appVersion);	// ex. 3.01
var getX = 0;
var getY = 0;

function getBrouwserName() {
var ap = navigator.appName;
var ua = navigator.userAgent;
if (ap.indexOf("Microsoft Internet Explorer",0) != -1) return "Explorer";
if (ap.indexOf("Netscape",0) != -1) return "Netscape";
if (ap.indexOf("Opera",0) != -1) return "Opera";
if (ap.indexOf("iCab",0) != -1) return "iCab";
if (ap.indexOf("WebTV",0) != -1) return "WebTV";
if (ua.indexOf("DreamPassport",0) != -1) return "DreamPassport";
return "";
}

function getEventCoords(e){
	if (IE) {
		getX = window.event.clientX;
		getY = window.event.clientY;
	} else if (Moz) {
		getX = e.clientX;
		getY = e.clientY;
	} else if (document.layers) {
		getX = e.pageX;
		getY = e.pageY;
	}
	// window.status = 'X : ' + getX + ', Y : ' + getY;
}

function setObj(id) {
	if (document.all) {
		return document.all(id);
	} else if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.layers) {
		return document.layers[id];
	}
	return false;
}

// ========== changeImage ==========

var preloadFlag = false;
function preloadImages(){
	if (document.images) {
	preloadimg = new Array();
	for (i = 0; i < preimages.length; i++) {
		preloadimg[i] = new Image();
		preloadimg[i].src =  preimages[i];
	}
	preloadFlag = true;
	}
}

function cngImg(imgname,filename) {
	document.images[imgname].src = filename;
}

function cngImg4lyrLv1 (lyrname,imgname,filename) {
	if (document.all && document.images) {
		document.all(lyrname).document.images[imgname].src = filename;
    } else if (document.getElementsByName) {
		document.getElementsByTagName("IMG").namedItem(imgname).src = filename;
	} else if (document.layers && document.images) {
		document.layers[lyrname].document.images[imgname].src = filename;
	}
}

// ========== Screen ==========

function getScrollTop() {
	if (IE) {
		return document.body.scrollTop;
	} else if (Moz) {
		return window.pageYOffset;
	} else if (NN) {
		return window.pageYOffset;
	}
	return 0;
}

function getScrollLeft() {
	if (IE) {
		return document.body.scrollLeft;
	} else if (Moz) {
		return window.pageXOffset;
	} else if (NN) {
		return window.pageXOffset;
	}
	return 0;
}

function getScrollWidth() {
	if (IE) {
		return document.body.scrollWidth;
	} else if (Moz) {
		return window.innerWidth;
	} else if (NN) {
		return window.innerWidth;
	}
	return 0;
}

function getScrollHeight() {
	if (IE) {
		return document.body.scrollHeight;
	} else if (Moz) {
		return window.innerHeight;
	} else if (NN) {
		return window.innerHeight;
	}
	return 0;
}

function getScreenWidth() {
	if (IE) {
		return document.body.offsetWidth;
	} else if (Moz) {
		return window.innerWidth;
	} else if (NN) {
		return window.innerWidth;
	}
	return 0;
}

function getScreenHeight() {
	if (IE) {
		return document.body.offsetHeight;
	} else if (Moz) {
		return window.innerHeight;
	} else if (NN) {
		return window.innerHeight;
	}
	return 0;
}


// ========== layer control ==========

function moveDivTo(lyrname,Xpx,Ypx) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		obj.style.pixelLeft = Xpx;
		obj.style.pixelTop  = Ypx;
	} else if (document.getElementById && obj.style) {
		obj.style.left = Xpx + 'px';
		obj.style.top  = Ypx + 'px';
	} else if (document.layers) {
		obj.moveTo(Xpx,Ypx);
	}
}

function cngPosX(lyrname,Xpx) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		obj.style.pixelLeft = Xpx;
	} else if (document.getElementById && obj.style) {
		obj.style.left  = Xpx + 'px';
	} else if (document.layers) {
		obj.left = Xpx;
	}
}

function cngPosY(lyrname,Ypx) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		obj.style.pixelTop = Ypx;
	} else if (document.getElementById && obj.style) {
		obj.style.top  = Ypx + 'px';
	} else if (document.layers) {
		obj.top = Ypx;
	}
}

function moveDivBy(lyrname,Xpx,Ypx) {
	var obj = setObj(lyrname);
	Xpx = parseInt(Xpx); Ypx = parseInt(Ypx);
	if (document.all && obj.style) {
		obj.style.pixelLeft = Xpx + getDivPosX(lyrname);
		obj.style.pixelTop  = Ypx + getDivPosY(lyrname);
	} else if (document.getElementById && obj.style) {
		obj.style.left = Xpx + getDivPosX(lyrname) + 'px';
		obj.style.top  = Ypx + getDivPosY(lyrname) + 'px';
	} else if (document.layers) {
		obj.moveTo(Xpx,Ypx);
	}
}

function getDivPosX(lyrname) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		return parseInt(obj.style.pixelLeft);
	} else if (document.getElementById && obj.style) {
		if (Moz) {
			style = getComputedStyle(obj,null);
			return parseInt(style.getPropertyValue('left'));
		}
		return parseInt(obj.style.left);
	} else if (document.layers) {
		return parseInt(obj.left);
	}
	return '';
}

function getDivPosY(lyrname) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		return parseInt(obj.style.pixelTop);
	} else if (document.getElementById && obj.style) {
		if (Moz) {
			style = getComputedStyle(obj,null);
			return parseInt(style.getPropertyValue('top'));
		}
		return parseInt(obj.style.top);
	} else if (document.layers) {
		return parseInt(obj.top);
	}
	return '';
}

function getDivWidth(lyrname) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		return obj.clientWidth;
	} else if (document.getElementById && obj.style) {
		if (Moz) {
			style=getComputedStyle(obj,null);
			return parseInt(style.getPropertyValue('width'));
		}
		return parseInt(obj.style.width);
	} else if (document.layers) {
		return obj.clip.width;
	}
	return '';
}

function getDivHeight(lyrname) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		return obj.clientHeight;
	} else if (document.getElementById && obj.style) {
		if (Moz) {
			style=getComputedStyle(obj,null);
			return parseInt(style.getPropertyValue('height'));
		}
		return parseInt(obj.style.height);
	} else if (document.layers) {
		return obj.clip.height;
	}
	return '';
}

function resizeDivTo (lyrname,width,height) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		obj.style.pixelWidth  = width + 'px';
		obj.style.pixelHeight = height + 'px';
	} else if (document.layers) {
		obj.resizeTo(width,height);
	}
}

function setBackgroundRepeat(lyrname,order) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		obj.style.backgroundRepeat = order;
	} else if (document.layers) {
		document.layers[lyrname].backgroundRepeat = order;
	}
}

function setBackgroundColor4Lyr(lyrname,color) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		obj.style.background = color;
	} else if (document.layers) {
		obj.bgColor = color;
	}
}

function setBackgroundImage4Lyr(lyrname,url) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		obj.style.backgroundImage = (url == ('none' || null)) ? 'none' : 'url(' + url + ')';
	} else if (document.layers) {
		document.layers[lyrname].background.src = (url == ('none' || null)) ? null : url;
	}
}

function setVisiblity(target, str) {
	var obj = setObj(target);
	var values = new Array(2);
	if ((document.getElementById || document.all) && obj.style) {
		if (str == null) str = ( obj.style.visibility == 'hidden' ) ? 1 : 0;
		obj.style.visibility = (str > 0) ? 'visible' : 'hidden';
	} else if (document.layers) {
		if (str == null) str = ( obj.visibility == 'hide' ) ? 1 : 0;
		obj.visibility = (str > 0) ? 'show' : 'hide';
	}
}

function setDisplay(target, str) {
	var obj = setObj(target);
	if ((document.getElementById || document.all) && obj.style) {
		if (str == null) str = ( obj.style.display == 'none' || obj.style.display == '') ? 1 : 0;
		obj.style.display = (str > 0) ? 'block' : 'none';
	}
}

function getDisplay(target) {
	var obj = setObj(target);
	if ((document.getElementById || document.all) && obj.style) {
		return obj.style.display;
	}
}


function setClip(lyrname,clipTop,clipRight,clipBottom,clipLeft) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		obj.style.clip = 'rect(' + clipTop + 'px,' + clipRight + 'px,' + clipBottom + 'px,' + clipLeft + 'px)';
	} else if (document.layers) {
		obj.clip.top = clipTop;
		obj.clip.bottom = clipBottom;
		obj.clip.left = clipLeft;
		obj.clip.right = clipRight;
	}
}

function getClip(lyrname,which) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		if (obj.style.clip == 'auto') return 'auto';
		var clipv = obj.style.clip.split('rect(')[1].split(')')[0].split(" ");
		if (which == "top")    return parseInt(clipv[0]);
		if (which == "right")  return parseInt(clipv[1]);
		if (which == "bottom") return parseInt(clipv[2]);
		if (which == "left")   return parseInt(clipv[3]);
	} else if (document.layers) {
		if (which == "top")    return obj.clip.top;
		if (which == "right")  return obj.clip.right;
		if (which == "bottom") return obj.clip.bottom;
		if (which == "left")   return obj.clip.left;
	}
}

function setDivZIndex(lyrname,order) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		obj.style.zIndex = order;
	} else if (document.layers) {
		obj.zIndex = order;
	}
}

function getDivZIndex(lyrname) {
	var obj = setObj(lyrname);
	if ((document.getElementById || document.all) && obj.style) {
		return obj.style.zIndex;
	} else if (document.layers) {
		return obj.zIndex;
	}	
}

function getDivScrollHeight(lyrname) {
	var obj = setObj(lyrname);
	if (document.all) {
		return obj.scrollHeight;
	} else if (document.layers) {
		return obj.document.height;
	}
	return 0;
}

function cngFontColor (targetId,r,g,b) {
	var obj = setObj(lyrname);
	if (document.all && obj.style) {
		obj.style.color = "#" + r + g + b;
	} else if (document.getElementById && obj.style) {
		obj.style.color = "#" + r + g + b;
	} else if (document.layers) {
		obj.document.fgColor = "#" + r + g + b;
	}
}

function writeLayer(lyrname,code) {
	var obj = setObj(lyrname);
	if (document.all) {
		obj.innerHTML=code;
	} else if (document.getElementById) {
		obj.innerHTML=code;
	} else if (document.layers) {
		obj.document.open();
		obj.document.write(code);
		obj.document.close();
	}
}


function toHex(i) {
	if (i < 0) {
		return '00';
	} else if (i > 255) {
		return 'ff';
	} else {
		return '' + hexbox[Math.floor(i/16)] + hexbox[i%16];
	}
}
