/**
 *  dhtml_utils.js
 *
 *  Set of utilities for working with DHTML in a cross-browser
 *  compatible way.
 *
 *  Requires the browser_sniffer.js script.
 *
 *  Author: Don Miller
 *  Version: 1.0 4/21/2003
 */

// return object by id in a cross-browser compatible way
function getObject(objID) {
	return getObjectEx(objID, "document");
}

// return object by id in a cross-browser compatible way
function getObjectEx(objID, docID) {
	if (isNetscape6() || isInternetExplorer6()) {
			return eval(docID + ".getElementById('" + objID + "')");
	} else if (isNetscape()) {
			return eval(docID + "." + objID);
	} else {
			return eval(docID + ".all." + objID);
	}
}

// returns array of objects by id in a cross-browser compatible way
function getObjects(objID) {
	return getObjectsEx(objID, "document");
}

// return object by id in a cross-browser compatible way
function getObjectsEx(objID, docID) {
	if (isNetscape6() || isInternetExplorer6()) {
			return eval(docID + ".getElementsByName('" + objID + "')");
	} else if (isNetscape()) {
			return eval(docID + "." + objID);
	} else {
			return eval(docID + ".all." + objID);
	}
}

// return object style by id in a cross-browser compatible way
function getObjectStyle(objID) {	
	if (isNetscape4()) {
		return getObject(objID);
	} else {
		return getObject(objID).style;
	}
}

// return object style by id in a cross-browser compatible way
function getObjectStyleEx(objID, docID) {	
	if (isNetscape4()) {
		return getObjectEx(objID, docID);
	} else {
		return getObjectEx(objID, docID).style;
	}
}

// displays the object by id in a cross-browser compatible way
function showObject(objID) {
	getObjectStyle(objID).visibility = "visible";
}

// hides the object by id in a cross-browser compatible way
function hideObject(objID) {
	getObjectStyle(objID).visibility = "hidden";
}

// moves the object by id to the specified pixel coordinates
function shiftTo(objID, x, y) {
	var obj = getObjectStyle(objID);
	
	if (isNetscape6() || isInternetExplorer6()) {
		obj.left = x;
		obj.top = y;
	} else if (isNetscape()) {
		obj.moveTo(x, y);
	} else {
		obj.pixelLeft = x;
		obj.pixelTop = y;
	}
}

// moves the object by id to the specified pixel coordinates
function shiftBy(objID, x, y) {
	var obj = getObjectStyle(objID);
	
	if (isNetscape6() || isInternetExplorer6()) {		
		obj.left = toInt(obj.left) + x;
		obj.top = toInt(obj.top) + y;
	} else if (isNetscape()) {
		obj.moveBy(x, y);
	} else {
		obj.pixelLeft = obj.pixelLeft + x;
		obj.pixelTop = obj.pixelTop + y;
	}
}

// sets the z-order of the object by id
function setZOrder(objID, z) {
	getObjectStyle(objID).zIndex = z;
}

// sets the background color of the object by id
function setBackgroundColor(objID, color) {
	var obj = getObjectStyle(objID);
	
	if (isNetscape4()) {
		obj.bgColor = color;
	} else {
		obj.backgroundColor = color;
	}
}

// converts given string value to integer
// if value is null or empty string, 0 is returned
function toInt(value) {
	if (value == null || value.length == 0) {
		return 0;
	} else {
		return parseInt(value);
	}
}

/***********************************************************************************
 * Following methods are only compatible with Netscape v6+ and Internet Explorer v5+
 ***********************************************************************************/

// returns the absolute left x coordinate of an object by id
function getAbsoluteLeft(objID) {
	var obj = getObject(objID);
	return getObjectAbsoluteLeft(obj);
}

// returns the absolute left x coordinate of an object
function getObjectAbsoluteLeft(obj) {
	var left = obj.offsetLeft;
	
	if (obj.offsetParent) {
		var tmp = getObjectAbsoluteLeft(obj.offsetParent);
		left += tmp;
	}
	return left;
}

// returns the absolute top y coordinate of an object by id
function getAbsoluteTop(objID) {
	var obj = getObject(objID);
	return getObjectAbsoluteTop(obj);
}

// returns the absolute top y coordinate of an object
function getObjectAbsoluteTop(obj) {
	var top = obj.offsetTop;
	
	if (obj.offsetParent) {
		var tmp = getObjectAbsoluteTop(obj.offsetParent);
		top += tmp;
	}
	return top;
}