// -*- mode: javascript -*-

dodo.docImportNode = function (doc, node, deep) {
	if (!doc.importNode) {
		var target;

		if (node.nodeType == 1) {
			target = doc.createElement (node.nodeName);
			for (var i = 0; i < node.attributes.length; i++) {
				var attr = node.attributes[i];
				if (attr.name == "style") {
					target.style.cssText = attr.value;
				} else {
					target.setAttribute (attr.name, attr.value);
				}
			}
		} else if (node.nodeType == 3) {
			target = doc.createTextNode (node.nodeValue);
		}
		
		if (deep && node.hasChildNodes ()) {
			for (var child = node.firstChild; child; child = child.nextSibling) {
				target.appendChild (docImportNode (doc, child, true));
			}
		}
		return target;
	}
	return doc.importNode (node, deep);
}

dodo.nodeLastChild = function (node) {
	if (node.hasChildNodes ()) {
		return node.childNodes[node.childNodes.length - 1];
	}
	return null;
}

dodo.nodeReplace = function (node, nodeName, values, idx) {
	if (!idx)
		idx = 0;
	if (idx < values.length) {
		if (node.nodeName.toLowerCase () == nodeName) {
			var value = values[idx++];
			if (value != undefined)
				node.innerHTML = value;
		} else
			for (var i = 0; i < node.childNodes.length; i++) {
				idx = dodo.nodeReplace (node.childNodes[i], nodeName, values, idx);
				if (idx == values.length)
					break;
			}
	}
	return idx;
}

dodo.adminClassDisplay = function (tagName, display) {
	var nodes = document.getElementsByTagName (tagName);
	for (var i = 0; i < nodes.length; i++) {
		var node = nodes[i];
		if (node.className.indexOf ("admin") == 0 ||
			node.className.indexOf (" admin") != -1)
			node.style.display = display;
	}
}

dodo.replaceLinkTargets = function (doc) {
	var links = doc.getElementsByTagName ("a");
	for (var i = 0; i < links.length; i++) {
		var link = links[i];
		if (link.target == "") {
			if (link.className.indexOf ("new-win") != -1) {
				link.target = "_blank";
			}
		}
	}
}

dodo.httpReqGet = function () {
	if(window.ActiveXObject) {
       		try {
        		return new ActiveXObject("Msxml2.XMLHTTP");
      		} catch(e) {
        		try {
          			return new ActiveXObject("Microsoft.XMLHTTP");
        		} catch(e) {}
		}
	} else if (window.XMLHttpRequest && !(window.ActiveXObject)) {
    		try {
			return new XMLHttpRequest();
		} catch(e) {}
	}

	return undefined;
}

dodo.httpReqLoad = function (req, url, callback, bypassCache)
{
	req.onreadystatechange = callback;

	var flags;
	if (bypassCache == undefined || bypassCache)
		url += (url.match(/\?/) == null ? "?" : "&") + (new Date()).getTime();
		
	req.open ("GET", url, true);
	req.send ("");
}

