/* 
All material herein (c) 2004 TechniCon Corporation              
All Rights Reserved.

The source code is owned by TechniCon Corporation and is protected by  
copyright laws and international copyright treaties, as well as other   
intellectual property laws and treaties. COPYRIGHT. The source code is  
licensed, not sold. All right, title and interest in the source code    
(including any images, "applets," photographs, animations, video, audio,
music, and text incorporated into the source code), accompanying printed
materials, and any copies you are permitted to make herein, are owned by
TechniCon Corporation, and the source code is protected by United States 
copyright laws and international treaty provisions.  Therefore, you must
treat the source code like any other copyrighted material.      
*/

/* standard_dom_util.js */

// initialization code
if (!window.Node) {
	// Replacements for DOM node constants if not defined
	var Node = {
		ELEMENT_NODE:	1,
		ATTRIBUTE_NODE: 2,
		TEXT_NODE:   	3,
		COMMENT_NODE:	8,
		DOCUMENT_NODE:	9,
		DOCUMENT_FRAGMENT_NODE: 11
	}
}

function swapDOMNodes(node1, node2) {
	var parentNode = node1.parentNode;
	if (node1.nextSibling === node2) {
		parentNode.insertBefore(node2, node1);// insert node2 before node1
	} else if (node2.nextSibling === node1) {
		parentNode.insertBefore(node1, node2);// insert node1 before node2
	} else {
		node2.parentNode.replaceChild(node1, node2);
		parentNode.insertBefore(node2, nextSibling); // insert node2 before nextSibling
	}
}

function removeAllChildren(oNode)
{
	while (oNode.hasChildNodes()) {
		oNode.removeChild(oNode.lastChild);
	}
}

// sStyle - optional argument: structure containing style name/value pairs
function createDivElement(sID, oStyle)
{
	var oDiv = document.createElement("div");
	oDiv.setAttribute("id", sID);
	if (arguments.length > 1) {
		setElementStyles(oDiv, oStyle);
	}
	return oDiv;
}

//nBorder, sAlt and sStyle are optional
function createImgElement(sSrc, nWidth, nHeight, nBorder, sAlt, oStyle)
{
	var oImg = document.createElement("img");
	oImg.setAttribute("src", sSrc);
	oImg.setAttribute("width", nWidth);
	oImg.setAttribute("height", nHeight);
	if (arguments.length > 3) {
		oImg.setAttribute("border", nBorder);
		if (arguments.length > 4) {
			oImg.setAttribute("alt", sAlt);
			if (arguments.length > 5) {
				setElementStyles(oImg, oStyle);
			}
		}
	}
	return oImg;
}

//oStyle is a structure that contains style properties. 
//Capitalization of style property names must be correct.
function setElementStyles(oEle, oStyle)
{
	var oEleStyle = oEle.style;
	for (var styleprop in oStyle) {
		if (oStyle.hasOwnProperty(styleprop)){
			oEleStyle[styleprop] = oStyle[styleprop];
		}
	}
}

