//	DHTML.js
//
//	None of this code allows for Netscape 4 compatibility.
//////////////////////////////////////////////////////////////////////////////

//	Document properties.

function getViewportRect()
	{
	var rect = new Object

	//	All except Explorer
	if (self.pageYOffset)
		{
		rect.top = self.pageYOffset
		rect.left = self.pageXOffset
		rect.bottom = rect.top + self.innerHeight - 1
		rect.right = rect.left + self.innerWidth - 1
		}

	// Explorer 6 Strict Mode
	else if (document.documentElement && document.documentElement.scrollTop)
		{
		rect.top = document.documentElement.scrollTop
		rect.left = document.documentElement.scrollLeft
		rect.bottom = rect.top + document.documentElement.clientHeight - 1
		rect.right = rect.left + document.documentElement.clientWidth - 1
		}

	//	Other Explorers
	else if (document.body) // other Explorers
		{
		rect.top = document.body.scrollTop
		rect.left = document.body.scrollLeft
		rect.bottom = rect.top + document.body.clientHeight - 1
		rect.right = rect.left + document.body.clientWidth - 1
		}

	return rect
	}

function getViewportDims()
	{
	dims = new Object

	if (self.innerWidth && document.body)				//	Everyone except Explorer
		{
		dims.width = document.body.clientWidth
		dims.height = document.body.clientHeight
		}
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		{
		dims.width = document.documentElement.clientWidth
		dims.height = document.documentElement.clientHeight
		}
	else if (document.body) // other Explorers
		{
		dims.width = document.body.clientWidth
		dims.height = document.body.clientHeight
		}
	return dims
	}

function getScrollingOffsets()
	{
	var offsets = Object
	if (self.pageYOffset) // all except Explorer
		{
		offsets.x = self.pageXOffset
		offsets.y = self.pageYOffset
		}
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
		{
		offsets.x = document.documentElement.scrollLeft
		offsets.y = document.documentElement.scrollTop
		}
	else if (document.body) // all other Explorers
		{
		offsets.x = document.body.scrollLeft
		offsets.y = document.body.scrollTop
		}
	return offsets
	}

function getElementDims(element)
	{
	var dims = Object
	dims.width = element.offsetWidth
	dims.height = element.offsetHeight
	return dims
	}

function getMouseXY(e)
	{
	if (!e) var e = window.event

	var mouse = new Object
	mouse.x = 0
	mouse.y = 0
	if (e.pageX || e.pageY)
		{
		mouse.x = e.pageX
		mouse.y = e.pageY
		}
	else if (e.clientX || e.clientY)
		{
		if (document.documentElement && document.documentElement.scrollTop)
			{
			mouse.x = e.clientX + document.documentElement.scrollLeft
			mouse.y = e.clientY + document.documentElement.scrollTop
			}
		else
			{
			mouse.x = e.clientX + document.body.scrollLeft
			mouse.y = e.clientY + document.body.scrollTop
			}
		}
	return mouse
	}

//	Basic GetElement Function.

function getElement(name)
	{
	var element = null
	if (document.getElementById)
		element = document.getElementById(name)
	else if (document.all)
		element = document.all[name]
	else if (document.layers)
		element = document.layers[name]
	return element
	}

function getElementInlineStyleObject(element)
	{
	if ((document.getElementById || document.all) && (element != null))
		return element.style
	return element
	}

function getCompoundElement(name)
	{
	var element = getElement(name)
	if (element != null)
		return makeCompoundElement(element)
	return null
	}

function makeCompoundElement(element)
	{
	var cElement = null
	if (element != null)
		{
		cElement = new Object
		cElement.element = element
		cElement.style = getElementInlineStyleObject(element)
		}
	return cElement
	}

function getElementStyleProperty(element, property)
	{
//	alert('getStyle')
	var y
	if (document.defaultView && document.defaultView.getComputedStyle)
		y = document.defaultView.getComputedStyle(element,null).getPropertyValue(property)
	else if (element.currentStyle)
		y = element.currentStyle[property]
	return y
	}

//	Dynamic Selection Box Code.

//function selectOptionByPosition(box, posn)
//	{
//	if (box == null) return
//	if ((posn < 0) || (posn >= box.options.length)) return

//	box.options[posn].selected = true
//	}

function selectOptionByName(box, name)
	{
//D	alert('in selectOptionByName')
	if (box == null)
		return

	for (var i = 0; i < box.options.length; i++)
		{
		if (box.options[i].text == name)
			{
			box.options[i].selected = true
//D			alert('Selected: ' + box.options[i].text)
			return
			}
		}
	}

function selectOptionByValue(box, value)
	{
//D	alert('in selectOptionByValue')
	if (box == null)
		return

	for (var i = 0; i < box.options.length; i++)
		{
		if (parseInt(box.options[i].value) == parseInt(value))
			{
			box.options[i].selected = true
//D			alert('Selected: ' + box.options[i].text)
			return
			}
		}
	}

function insertOptionTo(box, text, value)	// Optional arguments: (index = -1).
	{
//D	alert('in insertOptionTo')
	if (box == null)
		return

	//	Handle the optional arguments.
	var index = -1
	if (arguments.length >= 4)
		index = arguments[3]

	if (index == -1)
		box.options[box.options.length] = new Option(text, value)
	else if ((index >= 0) && (index <= box.options.length))
		{
		var j = box.options.length
		box.options[j] = new Option(' ', 0)
		while (j > index)
			{
			box.options[j].text = box.options[j-1].text
			box.options[j].value = box.options[j-1].value
			j--
			}
		box.options[index].text = text
		box.options[index].value = value
		}
	}

function insertOptionByValue(box, text, value)
	{
//D	alert('in insertOptionByValue')
	if (box == null)
		return

//	alert('size: ' + box.options.length)
	var i
	for (i = 0; i < box.options.length; i++)
		{
//		alert(box.options[i].value)
		if (parseInt(value) < parseInt(box.options[i].value))
			{
//			alert('i: ' + i + "\nvalue: " + value + "\noption.value: " + box.options[i].value)
			break
			}
		}
	insertOptionTo(box, text, value, i)
	}

//	Option xfer functions.
function xferOption(from, fIndex, to)	// Optional arguments:  (sortedInsert = false, isCopy = false)
	{
//D	alert('in xferOption')
	if ((from == null) || (to == null))
		return

	//	Handle the optional arguments.
	var sortedInsert = false
	var isCopy = false
	if (arguments.length >= 4)
		sortedInsert = arguments[3]
	if (arguments.length >= 5)
		isCopy = arguments[4]

	if (sortedInsert == true)
		insertOptionByValue(to, from.options[fIndex].text, from.options[fIndex].value)
	else
		insertOptionTo(to, from.options[fIndex].text, from.options[fIndex].value)

	if (isCopy == false)
		from.options[fIndex] = null
	}

function xferSelectedOption(from, to)	// Optional arguments: (sortedInsert = false)
	{
//D	alert('in xferSelectedOption')
	if ((from == null) || (to == null))
		return

	//	Handle the optional arguments.
	var sortedInsert = false
	if (arguments.length >= 3)
		sortedInsert = arguments[2]

	xferOption(from, from.selectedIndex, to, sortedInsert)
	}

//	for multi-choice selects
function xferSelectedOptions(from, to)	// Optional arguments: (sortedInsert = false)
	{
//D	alert('in xferSelectedOptions')
	if ((from == null) || (to == null))
		return

	//	Handle the optional arguments.
	var sortedInsert = false
	if (arguments.length >= 3)
		sortedInsert = arguments[2]

	for (index = 0; index < from.options.length; )
		if (from.options[index].selected)
			{
//			alert(from.options[index].text + '=' + from.options[index].value)
			xferOption(from, index, to, sortedInsert)
//			alert(from.options[index].text + '=' + from.options[index].value)
//			alert(from.options.length + '/' + to.options.length)
			}
		else
			index++
	}

function conditionalXferSelectedOption(from, to, sortedInsert, how, value)
	{
//D	alert('in conditionalXferSelectedOption')
	if ((from == null) || (to == null))
		return

	if ((how == 'position') && (from.selectedIndex != parseInt(value)))
		xferOption(from, from.selectedIndex, to, sortedInsert)
	}

function conditionalXferSelectedOptions(from, to, sortedInsert, how, value)
	{
//	alert('in conditionalXferSelectedOptions: ' + how + ',' + value)
	if ((from == null) || (to == null))
		return

	for (var index = 0; index < from.options.length; )
		if (from.options[index].selected &&
				((how == 'position') && (index != value)))
			{
//			alert('At ' + index + ':  ' + from.options[index].text + '=' + from.options[index].value)
			xferOption(from, index, to, sortedInsert)
//			alert(from.options[index].text + '=' + from.options[index].value)
//			alert(from.options.length + '/' + to.options.length)
			}
		else
			{
//			alert('At ' + index + ':  ' + from.options[index].text + '=' + from.options[index].value)
			if (from.options[index].selected) from.options[index].selected = false
			index++
			}
	}

function wtf() { alert('func: wtf') }

//	Event Callbacks API

function doWindowOnloadCallbacks()
	{
	alert('doWindowOnloadCallbacks')
	for (var n = 0; n < document.wincbs['onload'].length; n++)
		document.wincbs['onload'][n]()
	}

function registerWindowCallback(which, func)
	{
	alert('registerWindowCallback')
	if (! document.wincbs)
		document.wincbs = new Object

	if (document.wincbs[which])
		document.wincbs[which][document.wincbs[which].length] = func
	else
		{
		document.wincbs[which] = new Array()
		if (which == 'onload')
			{
			if (window.onload) document.wincbs[which][0] = window.onload
			window.onload = doWindowOnloadCallbacks
			}
		document.wincbs[which][document.wincbs[which].length] = func
		}
	}


//	DOM Node API.

function getNodeTypedChildren(node, type)
	{
	subset = new Array()
	j = 0
	for (var i = 0; i < node.childNodes.length; i++)
		if (node.childNodes[i].nodeType == type)
			{
			subset[j] = node.childNodes[i]
			j++
			}
	return subset
	}

function getNodeElements(node) { return getNodeTypedChildren(node, 1); }
function getNodeAttributes(node) { return getNodeTypedChildren(node, 2); }
function getNodeTexts(node) { return getNodeTypedChildren(node, 3); }
