// DHTMLapi.js
// Taken from DynamicHTML: The Definitive Reference by Danny Goodman (O'Reilly) pg.89

var isCSS, isW3C, isIE4, isNN4, isIE6CSS

// Initialize variables
function initDHTMLAPI() {
	if (document.images) {
		isCSS = (document.body && document.body.style) ? true : false;
		isW3C = (isCSS && document.getElementById) ? true : false;
		isIE4 = (isCSS && document.all) ? true : false;
		isNN4 = (document.layers) ? true : false;
		isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}
}

window.onload = initDHTMLAPI;

// Seek nested NN4 layer from string
function seekLayer(doc, name) {
	var theObj;
	for (var i = 0; i < doc.layers.length; i++) {
		if (doc.layers[i].name == name) {
			theObj = doc.layers[i];
			break;
		}
		// dive into nested layers if necessary
		if (doc.layers[i].documents.layers.length > 0) {
			theObj = seekLayer(document.layers[i].document, name);
		}
	}
	return theObj;
}

// Convert object name string or object reference
// into a valid element object reference
function getRawObject(obj) {
	var theObj;
	if (typeof obj == "string") {
		if (isW3C) {
			theObj = document.getElementById(obj);
		} else if (isIE4) {
			theObj = document.all(obj);
		} else if (isNN4) {
			// NN4 scripting not supported
			//theObj = seekLayer(document, obj);
		}
	} else {
		theObj = obj;
	}
	return theObj;
}

// Convert object name string or object reference
// into a valid style (or NN4 layer) reference
function getObjectStyle(obj) {
	var theObj = getRawObject(obj);
	if (theObj && isCSS) {
		theObj = theObj.style;
	}
	return theObj;
}

// Set the background color of an object
function setBGColor(obj, color) {
	var theObj = getObjectStyle(obj);
	if (theObj) {
		if (isNN4) {
			theObj.bgColor = color;
		} else if (isCSS) {
			theObj.backgroundColor = color;
		}
	}
}

// Set the text color of an object
function setColor(obj, color, fontWeight) {
	var theObj = getObjectStyle(obj);
	if (theObj) {
		if (isCSS) {
			theObj.color = color;
			if (fontWeight) {
				theObj.fontWeight = fontWeight;
			}
		}
	}
}

// Set the CSS class of an object
function setCSSClass(obj, cls) {
	var theObj = getRawObject(obj);
	if (theObj) {
		if (isCSS) {
			theObj.className = cls;
		}
	}
}

// Set the visibility of an object to visible
function show(obj) {
	var theObj = getObjectStyle(obj);
	if (theObj) {
		theObj.visibility = "visible";
	}
}

// Set the visibility of an object to hidden
function hide(obj) {
	var theObj = getObjectStyle(obj);
	if (theObj) {
		theObj.visibility = "hidden";
	}
}

// Gets an event target's element id
function getEventTargetID(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		if (elem) {
			if (elem.nodeType) {
				elem = (elem.nodeType == 1 || elem.nodeType == 9 ) ? elem : elem.parentNode;
			}
			if (elem.id) {
				return elem.id;
			}
		}
	}
}
function getEventRelatedTargetID(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		var elem = (evt.relatedTarget) ? evt.relatedTarget : ((evt.toElement) ? evt.toElement : null);
		if (elem) {
			if (elem.nodeType) {
				elem = (elem.nodeType == 1 || elem.nodeType == 9 ) ? elem : elem.parentNode;
			}
			if (elem.id) {
				return elem.id;
			}
		}
	}
}