var ns4 = (document.layers)? true : false;
var ie4up = (document.all)? true : false;

function isVisible(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		if (dom.visibility != 'hide')
			return(true);
	}
	if (ie4up && document.all[id].style.visibility != 'hidden')
		return(true);
	return(false);
}

function showLayer(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.visibility = 'show';
	}
	if (ie4up)
		document.all[id].style.visibility = 'visible';
}

function hideLayer(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.visibility = 'hide';
	}
	if (ie4up)
		document.all[id].style.visibility = 'hidden';
}

function switchVisible(id,viz) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.visibility = (viz)? 'show' : 'hide';
	}
	if (ie4up)
		document.all[id].style.visibility = (viz)? 'visible' : 'hidden';
}

function moveLayerTo(id,x,y) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.moveTo(x,y);
	}
	if (ie4up) {
		document.all[id].style.pixelLeft = x;
		document.all[id].style.pixelTop = y;
	}
}

function moveLayerBy(id,dx,dy) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.moveBy(dx,dy);
	}
	if (ie4up) {
		document.all[id].style.pixelLeft += dx;
		document.all[id].style.pixelTop += dy;
	}
}

var slideActive = false;

function slideTo(id,endx,endy,inc,speed) {
	var x = getLeft(id);
	var y = getTop(id);
	if (endx == null) endx = x;
	if (endy == null) endy = y;
	var distx = endx - x;
	var disty = endy - y;
	slideStart(id,endx,endy,distx,disty,inc,speed);
}

function slideBy(id,distx,disty,inc,speed) {
	var x = getLeft(id);
	var y = getTop(id);
	var endx = x + distx;
	var endy = y + disty;
	slideStart(id,endx,endy,distx,disty,inc,speed);
}

function slideStart(id,endx,endy,distx,disty,inc,speed) {
	if (slideActive) return;
	if (!inc) inc = 5;
	if (!speed) speed = 20;
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc;
	if (num == 0) return;
	var dx = distx/num;
	var dy = disty/num;
	slideActive = true;
	slideLayer(id,dx,dy,endx,endy,num,1,speed)
}

function slideLayer(id,dx,dy,endx,endy,num,i,speed) {
	if (!slideActive) return;
	if (i++ < num) {
		moveLayerBy(id,dx,dy);
		if (slideActive) {
			var a=id,b=dx,c=dy,d=endx,e=endy,f=num,g=i,h=speed;
			setTimeout('slideLayer("'+a+'",'+b+','+c+','+d+','+e+','+f+','+g+','+h+')',speed);
		}
	} else {
		slideActive = false;
		moveLayerTo(id,endx,endy);
	}
}

function getLeft(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.left);
	}
	if (ie4up)
		return(document.all[id].style.pixelLeft);
	return(-1);
}

function getTop(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.top);
	}
	if (ie4up)
		return(document.all[id].style.pixelTop);
	return(-1);
}

function getRight(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.left + getWidth(id));
	}
	if (ie4up)
		return(document.all[id].style.pixelLeft + getWidth(id));
	return(-1);
}

function getBottom(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.top + getHeight(id));
	}
	if (ie4up)
		return(document.all[id].style.pixelTop + getHeight(id));
	return(-1);
}

function getPageLeft(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.pageX);
	}
	if (ie4up)
		return(document.all[id].offsetLeft);
	return(-1);
}

function getPageTop(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.pageY);
	}
	if (ie4up)
		return(document.all[id].offsetTop);
	return(-1);
}

function getWidth(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		if (dom.document.width)
			return(dom.document.width);
		else
			return(eval(dom.clip.right - dom.clip.left));
	}
	if (ie4up) {
		if (document.all[id].style.pixelWidth)
			return(document.all[id].style.pixelWidth);
		else
			return(document.all[id].clientWidth);
	}
	return(-1);
}

function getHeight(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		if (dom.document.height)
			return(dom.document.height);
		else
			return(dom.clip.bottom - dom.clip.top);
	}
	if (ie4up) {
		if (false && document.all[id].style.pixelHeight)
			return(document.all[id].style.pixelHeight);
		else
			return(document.all[id].clientHeight);
	}
	return(-1);
}

function getzIndex(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.zIndex);
	}
	if (ie4up)
		return(document.all[id].style.zIndex);
	return(-1);
}

function setzIndex(id,z) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.zIndex = z;
	}
	if (ie4up)
		document.all[id].style.zIndex = z;
}

function clipLayer(id,clipleft,cliptop,clipright,clipbottom) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.clip.left = clipleft;
		dom.clip.top = cliptop;
		dom.clip.right = clipright;
		dom.clip.bottom = clipbottom;
	}
	if (ie4up)
		document.all[id].style.clip = 'rect(' + cliptop + ' ' +	clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function getClipLeft(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.clip.left);
	}
	if (ie4up) {
		var str = document.all[id].style.clip;
		if (!str)
			return(0);
		var clip = getClipValues(document.all[id].style.clip);
		return(clip[3]);
	}
	return(-1);
}

function getClipTop(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.clip.top);
	}
	if (ie4up) {
		var str = document.all[id].style.clip;
		if (!str)
			return(0);
		var clip = getClipValues(document.all[id].style.clip);
		return(clip[0]);
	}
	return(-1);
}

function getClipRight(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.clip.right);
	}
	if (ie4up) {
		var str = document.all[id].style.clip;
		if (!str)
			return(document.all[id].style.pixelWidth);
		var clip = getClipValues(document.all[id].style.clip);
		return(clip[1]);
	}
	return(-1);
}

function getClipBottom(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.clip.bottom);
	}
	if (ie4up) {
		var str = document.all[id].style.clip;
		if (!str)
			return(document.all[id].style.pixelHeight);
		var clip = getClipValues(document.all[id].style.clip);
		return(clip[2]);
	}
	return(-1);
}

function getClipWidth(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.clip.width);
	}
	if (ie4up) {
		var str = document.all[id].style.clip;
		if (!str)
			return(document.all[id].style.pixelWidth);
		var clip = getClipValues(document.all[id].style.clip);
		return(clip[1] - clip[3]);
	}
	return(-1);
}

function getClipHeight(id) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		return(dom.clip.height);
	}
	if (ie4up) {
		var str =	document.all[id].style.clip;
		if (!str)
			return(document.all[id].style.pixelHeight);
		var clip = getClipValues(document.all[id].style.clip);
		return(clip[2] - clip[0]);
	}
	return(-1);
}

function getClipValues(str) {
	var clip = new Array();
	var i;
	i = str.indexOf("(");
	clip[0] = parseInt(str.substring(i + 1, str.length), 10);
	i = str.indexOf(" ", i + 1);
	clip[1] = parseInt(str.substring(i + 1, str.length), 10);
	i = str.indexOf(" ", i + 1);
	clip[2] = parseInt(str.substring(i + 1, str.length), 10);
	i = str.indexOf(" ", i + 1);
	clip[3] = parseInt(str.substring(i + 1, str.length), 10);
	return(clip);
}

function scrollLayerTo(obj,x,y,bound) {
	var dx = getClipLeft(obj) - x;
	var dy = getClipTop(obj) - y;
	scrollLayerBy(obj,-dx,-dy,bound);
}

function scrollLayerBy(obj,dx,dy,bound) {
	var cl = getClipLeft(obj);
	var ct = getClipTop(obj);
	var cr = getClipRight(obj);
	var cb = getClipBottom(obj);
	if (bound) {
		if (cl + dx < 0)
			dx = -cl;
		else if (cr + dx > getWidth(obj))
			dx = getWidth(obj) - cr;
		if (ct + dy < 0)
			dy = -ct;
		else if (cb + dy > getHeight(obj))
			dy = getHeight(obj) - cb;
	}
	clipLayer(obj,cl + dx,ct + dy,cr + dx,cb + dy);
	moveLayerBy(obj,-dx,-dy);
}

function setBgColor(id,color) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.bgColor = color;
	}
	if (ie4up)
		document.all[id].style.backgroundColor = color;
}

function setBgImage(id,img) {
	if (ns4) {
		// Does not work. This bug needs to be fixed.
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.background.src = img;
	}
	if (ie4up)
		document.all[id].style.backgroundImage = "url(" + img + ")";
}

function getLayer(id) {
	if (ns4)
		return findLayer(id,document);
	if (ie4up)
		return eval('document.all.' + id);
	return null;
}

function findLayer(id,doc) {
	var i, layer;
	for (i = 0; i < doc.layers.length; i++) {
		layer = doc.layers[i];
		if (layer.id == id)
			return layer;
		if (layer.document.layers.length > 0) {
			layer = findLayer(id,layer.document);
			if (layer != null)
				return layer;
		}
	}
	return null;
}

function writeLayer(id,html) {
	if (ns4) {
		var dom = (document.layers[id])? document.layers[id] : getLayer(id);
		dom.document.open();
		dom.document.write(html);
		dom.document.close();
	}
	if (ie4up)
		document.all[id].innerHTML = html;
}

function getWindowWidth() {
	if (ns4)
		return(window.innerWidth);
	if (ie4up)
		return(document.body.clientWidth);
	return(-1);
}

function getWindowHeight() {
	if (ns4)
		return(window.innerHeight);
	if (ie4up)
		return(document.body.clientHeight);
	return(-1);
}

function getPageWidth() {
	if (ns4)
		return(document.width);
	if (ie4up)
		return(document.body.scrollWidth);
	return(-1);
}

function getPageHeight() {
	if (ns4)
		return(document.height);
	if (ie4up)
		return(document.body.scrollHeight);
	return(-1);
}

function getPageScrollX() {
	if (ns4)
		return(window.pageXOffset);
	if (ie4up)
		return(document.body.scrollLeft);
	return(-1);
}

function getPageScrollY() {
	if (ns4)
		return(window.pageYOffset);
	if (ie4up)
		return(document.body.scrollTop);
	return(-1);
}
