var _loadfn;
if (document.createElement) {
// add style rules during document load for backward compatability
	if (!document.all) {
		var ss = document.createElement("STYLE");
		ss.innerHTML = "#navlist UL { display:none; } #navlist UL.alwaysShow { display:block; }";
		var hd = document.getElementsByTagName("HEAD");
		if (hd.length>0) hd[0].appendChild(ss);
	} else {
		if (document.styleSheets.length > 0) {
			document.styleSheets[0].addRule("#navlist UL","display:none");
			document.styleSheets[0].addRule("#navlist UL.alwaysShow","display:block;");
		}
	}
}
function QuickLinksOver(evnt)
{
	var src = document.getElementById("quicklinks");
	src.className = "hover";
}

function QuickLinksOut(evnt)
{
	var src = document.getElementById("quicklinks");
	src.className = "";
}
function open_events_block(blockId, moreId)
{
    var b = document.getElementById(blockId);
    var m = document.getElementById(moreId);
    
    if (b && m)
    {
        b.style.display = "block";
        b.parentNode.style.backgroundColor = "#F2F9FF";
        m.style.display = "none";
    }
}
function close_events_block(blockId, moreId)
{
    var b = document.getElementById(blockId);
    var m = document.getElementById(moreId);
    
    if (b && m)
    {
        b.style.display = "none";
        b.parentNode.style.backgroundColor = "";
        m.style.display = "block";
    }
}

function createPortal() 
{
//empty stub; define function in document body where needed
}
function startup()
{
    // script dependent on W3C DOM model; do not execute on older browsers
	if (!document.getElementById) return;
	createPortal();
	var nav = document.getElementById("navlist");
	if (!nav) return;
	var ultags = nav.getElementsByTagName("UL");
	for (var n=0; n < ultags.length; n++)  {
		if (ultags[n].className != "alwaysShow") {
			ultags[n].style.display = "none";
		}
	}
	var divtags = nav.getElementsByTagName("DIV");
	for (var n=0; n < divtags.length; n++)  {
		var p = divtags[n].parentNode;
		if (p && p.tagName == "LI") {
			if (p.id.length == 3) {
				divtags[n].innerHTML = '<img src="'+window.nav1closed+'" width="20" height="25" alt="" align="right" />' + divtags[n].innerHTML;
			} else {
				divtags[n].innerHTML = '<img src="'+window.nav2closed+'" width="11" height="9" alt="" align="left" style="margin-top:2px" />' + divtags[n].innerHTML;
			}
		}
		divtags[n].onmouseover = swapImage;
		divtags[n].onmouseout = swapImage;
		if (window.isHomePage) continue;
		divtags[n].onclick = toggleListDisplay;
	}
	document.onclick = openPage;   // trap all click events and pass to openPage function
	if (window.isHomePage) return; // Home page is not to allow opening menu
	var target = location.search;
	if (target) {
		if (target.substr(1,2) == "cn") {
			var n = target.indexOf("=&");
			if (n > 0) target = target.substr(0,n);
			top.navNode = target.substr(1);
		}
	}
	if (!top.navNode && window.opener) {
	    // make menu function in RedDot
	    if (window.opener.top.navNode) {
			top.navNode = window.opener.top.navNode;
		}
	    else if (window.opener.top.opener) {
			if (window.opener.top.opener.top.navNode) {
				top.navNode = window.opener.top.opener.top.navNode;
			}
		}
	}
	if (top.navNode) {
		openNavToId(top.navNode);
	}
	if (_loadfn != undefined) _loadfn();
}

function openNavToId(id)
{
	var LI = document.getElementById(id);
	var UL;
	while (LI && LI.tagName == "LI") {
		if (LI.hasChildNodes()) {
			for (var n = 0; n < LI.childNodes.length; n++) {
				if (LI.childNodes[n].tagName == "DIV") {
					var evnt = new Object();
					evnt.currentTarget = LI.childNodes[n];
					swapImage(evnt);
				}
			}
		}
		openNode(LI);
		UL = LI.parentNode; 
		if (!UL || UL.tagName != "UL" || UL.id == "navlist") break;
		LI = UL.parentNode;
	}
}

function openPage(evnt)
{
	var A;
	if (evnt) {
		A = evnt.target;
	} else  if (window.event) {
		A = window.event.srcElement;
	}
	while (A && A.tagName != "A") A = A.parentNode;
	if (A == null) return;
	if (A.tagName == "A") {
		if (A.hostname != location.hostname || !(A.protocol == "http:" || A.protocol == "file:")) {
		    return;
		}
		for (var n = 0; n < A.attributes.length; n++) {
	    		if (A.attributes[n].value.substr(0,2) == "//") {
				if (A.attributes[n].value.length > 2)
			    		top.navNode = "cn" + A.attributes[n].value.substr(2);
				else 
			   		 top.navNode = "";
			}
		}
		if (!top.navNode) return;
		if (top != window) return;
		if (A.href.indexOf("?") >= 0) {
			A.href = A.href.replace("?", "?"+top.navNode+"=&");
		} else if (A.href.indexOf("#") >= 0) {
			A.href = A.href.replace("#", "?"+top.navNode+"#");
		} else {
			A.href += "?"+top.navNode;
		}
	}
	return;
}

function closeNode(LI)
{
	var ultags = LI.getElementsByTagName("UL");
	for (var n = 0; n < ultags.length; n++) {
		if (ultags[n].className != "alwaysShow") {
			ultags[n].style.display = "none";
		}
	}
	var imgtags = LI.getElementsByTagName("IMG");
	for (var n = 0; n < imgtags.length; n++) {
		temp = imgtags[n].src;
		if (temp.indexOf("open") >= 0) {
			imgtags[n].src = temp.replace("open","closed");
		}
	}
	var divtags = LI.getElementsByTagName("DIV");
	for (var n = 0; n < divtags.length; n++) {
		if (divtags[n].className == "showList") {
			divtags[n].className = "";
			divtags[n].onmouseover = swapImage;
			divtags[n].onmouseout = swapImage;
		}
	}
}

function openNode(LI)
{
	if (LI.hasChildNodes()) {
		for (var n = 0; n < LI.childNodes.length; n++) {
			if (LI.childNodes[n].tagName == "DIV") {
				if (LI.childNodes[n].className == "") {
					LI.childNodes[n].onmouseover = null;
					LI.childNodes[n].onmouseout = null;
					LI.childNodes[n].className = "showList";
				}
			}
			if (LI.childNodes[n].tagName == "UL") {
				if (LI.childNodes[n].className != "alwaysShow") {
					LI.childNodes[n].style.display = "block";
				}
			}
		}
	}
}


// this function will expand or collapse a hierarchical navigation list
// div is a DIV tag within a LI that has a sublist (UL)
function toggleListDisplay(evnt)
{
	var DIV;
	if (evnt) {
		DIV = evnt.currentTarget;
	} else  if (window.event) {
		DIV = this;
	}
	if (!DIV) return;

	// div.parentNode should be a LI
	if (!DIV.parentNode) return;

	// div would have className == showList if already open
	if (DIV.className == "") {
		// expand all UL tags that are children of the LI
		openNode(DIV.parentNode);


		// collapse all UL tags that are children of LI siblings
		if (!DIV.parentNode.parentNode) return;
		children = DIV.parentNode.parentNode.childNodes;
		for (var index = 0; index < children.length; index++) {
			if (children[index].tagName == "LI" && children[index] != DIV.parentNode) {
				closeNode(children[index]);
			}
		}
	} else {
		// collapse all UL tags that are children of the LI
		closeNode(DIV.parentNode);
		// because the cursor is still over the DIV tag toggle IMG open
		var evnt = new Object();
		evnt.currentTarget = DIV;
		swapImage(evnt);
	}

}

function swapImage(evnt)
{
	var DIV = null;
	if (evnt) {
		DIV = evnt.currentTarget;
	} else  if (window.event) {
		DIV = this;
	}
	if (DIV == null) return;
	if (DIV.className == "" && DIV.hasChildNodes()) {
		for (var n = 0; n < DIV.childNodes.length; n++) {
			if (DIV.childNodes[n].tagName == "IMG") {
				temp = DIV.childNodes[n].src;
				if (temp.indexOf("open") >= 0)
					DIV.childNodes[n].src = temp.replace("open","closed");
				else if (temp.indexOf("closed") >= 0)
					DIV.childNodes[n].src = temp.replace("closed","open");
			}
		}
	}
}

function RD_form_submit(anc,query)
{
	var f = document.getElementById("myForm1");
	if (f) {
		var pairs = query.split("&");
		for ( var i=0; i < pairs.length; i++ ) {
			var keyval = pairs[i].split("=");
			if (keyval.length == 2) {
				var elm = document.createElement("INPUT");
				if (elm) {
					elm.type = "hidden";
					elm.name = keyval[0];
					elm.value = keyval[1];
					f.appendChild(elm);
				}
			}
		}
		f.action = anc.href;
		f.submit();
	}
	return false;
}

function RSSFeed(feedURI, channelNodeId)
{
    this.request = null;
    this.requestURI = feedURI;
    this.displayNode = document.getElementById(channelNodeId);
    this.display = function (text)
    {
        if (this.displayNode)
            this.displayNode.innerHTML = text;
    }
    this.readyhandler = function () 
    {
        if (this.request.readyState == 4)
        {
            if (this.request.status == 200)
            {
                if (this.request.responseText != null)
                {
                    this.display(clean(this.request.responseText));
                }
                else
                {
                    this.display("No data.");
                }
            }
            else
            {
                this.display("Error " + this.request.status + ": " + this.request.statusText);
            }
        }
    }
    this.getFeed = function ()
    {
        if (this.request == null)
        {
            this.display("AJAX Request unsupported.");
        } 
        else 
        {
            try {
                var thisFeed = this;
                this.request.open("GET", this.requestURI, true);
                this.request.setRequestHeader("Cache-Control", "no-cache");
                this.request.setRequestHeader("Pragma", "no-cache");
                this.request.onreadystatechange = function() { thisFeed.readyhandler(); };
                this.request.send(null);
            }
            catch (e) {
                this.display("AJAX Request error.");
            }
        }
    }
    this.refresh = function ()
    {
        this.getFeed();
    }
    if (window.XMLHttpRequest)
    {
        this.request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        try {
            this.request = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
    }
    this.getFeed();
}

function addChannelStyles(styles)
{
    var ss = document.createElement("STYLE");
    document.getElementsByTagName("HEAD")[0].appendChild(ss);
    if (ss.styleSheet == null) {
        ss.innerHTML = styles;
    } else {
        ss.styleSheet.cssText = styles;
    }
}

function clean(str)
{
    var re1 = /(?:<[^>]*?)?<(script|object|iframe|embed|link|form|input|xml|applet|bgsound|button|textarea|select|frameset|frame|base|body|head|html)[^>]*?>(?:[\s\S]*?<\/\1[^>]*?>)?/ig;
    var re2 = /(<[^>]*?\s)(?:on[^=>\s]*?=[^>]*?)+>/ig;
    var re3 = /(<[^>]*?\s)(?:(?:href|src)=["']?javascript:[^>]*?)+>/ig;
    str = str.replace(re1, "");
    str = str.replace(re2, "$1>");
    str = str.replace(re3, "$1>");
    return str;
}


function encodeHTML(s)
{
    var requot = new RegExp("\"","g");
    s = s.replace(requot,"&quot;");
    s = s.replace(/</g,"&lt;");
    s = s.replace(/>/g,"&gt;");
    s = s.replace(/&/g,"&amp;");
    return s;
}

function decodeHTML(s)
{
    s = s.replace(/&quot;/g,"\"");
    s = s.replace(/&lt;/g,"<");
    s = s.replace(/&gt;/g,">");
    s = s.replace(/&amp;/g,"&");
    return s;
}
function mailStory(subject, teaser, link)
{
    var url = "mailto:";
    url += "?subject=" + escape(decodeHTML(subject));
    url += "&body=" + escape(decodeHTML(teaser) + "\n\n" + location.protocol + "//" + location.host + link);
    location.href = url;
}
function submitStory(href, title, link)
{
    href = href.replace(/\{title\}/, encodeURIComponent(decodeHTML(title)));
    if (link.match(/^https?:\/{2}/) == null)
    {
        link = location.protocol + "//" + location.host + link;
    }
    href = href.replace(/\{link\}/, encodeURIComponent(decodeHTML(link)));
    
    window.open(href);
}

function flashPutHref(href) { location.href = href; }

