// Global variables
var isCSS, isW3C, isIE4, isNN4, isSafari, isMac, isIEMac, isFF;
// initialize upon load to let all browsers establish content objects
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;
	}
	if (navigator.userAgent.indexOf("Firefox") > -1) isFF = true;
	if (navigator.platform.indexOf("Mac") > -1)	isMac = true;
	if (isMac && (navigator.appName.indexOf("Microsoft") > -1)) isIEMac = true;
	// assume this is true for safari
	if (navigator.vendor == "Apple Computer, Inc.") {
		isSafari = true;
	}
	mudBlur();
}

// Seek nested NN4 layer from string name
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].document.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) {
            theObj = seekLayer(document, obj);
        }
    } else {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid style (or NN4 layer) reference
function getObject(obj) {
    var theObj = getRawObject(obj);
    if (theObj && isCSS) {
        theObj = theObj.style;
    }
    return theObj;
}

// blur out those annoying blue boxes around links
function mudBlur() {
	if (isIEMac || isFF) {
		var anchors = getTagObject('a');
		for( i = 0; i < anchors.length; i++ ) {
			anchors[i].onfocus = function() { this.blur() };
		}
		var cbox = getTagObject('input');
		for( i = 0; i < cbox.length; i++ ) {
			if ((cbox[i].type == 'checkbox') || (cbox[i].type == 'radio') || (cbox[i].type == 'submit') || (cbox[i].type == 'reset')) {
				cbox[i].onfocus = function() { this.blur() };
			}
		}
	}
}


// grab objects by html tag name
function getTagObject(obj) {
	var mudObj;
	if (isW3C) {
		mudObj = document.getElementsByTagName(obj);
	}
	else if (isIE4) {
		mudObj = document.all(obj);
	}
	else if (isNN4) {
		mudObj = obj;
	}
	return mudObj;
}



function setDisplay(id) {
	var obj = getObject(id);
	obj.display = "block";
}

function unsetDisplay(id) {
	var obj = getObject(id);
	obj.display = "none";
}

function hideLayer(id) {
	var obj = getObject(id);
	if (isNN4) {
		obj.visibility = "hide";
	}
	if (isIE4 || isW3C) {
		obj.visibility = "hidden";
	}
}

function showLayer(id) {
	var obj = getObject(id);
	if (isNN4) {
		obj.visibility = "show";
	}
	if (isIE4 || isW3C) {
		obj.visibility = "visible";
	}
}

function changeClass(id, newClass) {
	var obj = getRawObject(id);
	if (obj == null) {
		if (jdebug == true) {
			alert("can't find id: " + id);
		}
		return;
	}
	obj.className=newClass;
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KhtmlOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

//from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html)
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}