// getDynamicContent.js 2010-02-14 JBR

// Provide the XMLHttpRequest class for IE 5.x-6.x:
// Other browsers (including IE 7.x-8.x) ignore this
//   when XMLHttpRequest is predefined
if (typeof(XMLHttpRequest)  === "undefined") {
	XMLHttpRequest = function() {
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
			catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
			catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP"); }
			catch(e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e) {}
		throw new Error("This browser does not support XMLHttpRequest.");
	};
}

function pasteToSidebar( text )
{
	var sidebar2 = document.getElementById( "sidebar2" );
	var objP = document.createElement( "p" );
	objP.appendChild( document.createTextNode( text ));
	sidebar2.appendChild( objP );
}

function getDynamicContent( maxPosts )
{
	var sidebar2 = document.getElementById( "sidebar2" );

	RssRequestor( "/cgi-bin/wa?RSS&L=CONSULT-MN&v=2.0", 
				 pasteDynamicContent );
	function pasteDynamicContent( rowset )
	{
		// first clear old junk from this div
		while ( true ) {
			var clearThis = document.getElementById( "clearThis" );
			if	( clearThis == null )
				break;
			clearThis.parentNode.removeChild( clearThis );
		}
		// now make new junk
		rowset.sort( sortfunc );
		for	( var j in rowset ) {
			var rowData = rowset[ j ];
			var p = document.createElement( "p" );
			p.id = "clearThis";
			var b = document.createElement( "b" );
			p.appendChild( document.createElement( "br" ));
			var au = new String( rowData.author ).replace( '&lt;', '<' ).replace( '&gt;', '>' );
			var lt = au.indexOf( '<' );
			if	( lt > -1 )
				au = au.substr( 0, lt );
			b.appendChild( document.createTextNode( au ));
			p.appendChild( b );
			p.appendChild( document.createElement( "br" ));
			var i = document.createElement( "i" );
			var D = new String( rowData.pubDate );
			i.appendChild( document.createTextNode( D.substr( 0, 25 )));
			p.appendChild( i );
			p.appendChild( document.createElement( "br" ));
			var a = document.createElement( "a" );
			p.appendChild( a );
			a.href = rowData.link;
			a.target = "_blank";
			a.appendChild( document.createTextNode( rowData.title ));
			sidebar2.appendChild( p );
			if	( j > maxPosts )
				break;
		}
		function sortfunc( a, b ) {
			var x = new Date( a.pubDate );
			var y = new Date( b.pubDate );
			return y - x;
		}
	}
}

function RssRequestor( strRssString, handlerFunc )
{
	try {
		var req = new XMLHttpRequest;
		if	( req == null )
			throw( "Error, no XMLHttpRequest;" );
		req.open( "GET", strRssString, true );
		req.onreadystatechange = RssHandler;
		req.send( null );
	} catch( e ) {
		var pasteToSidebar = handlerFunc.pasteToSidebar;
		var errorText = e.description;
		pasteToSidebar( errorText );
	}
	function RssHandler() {
		if	( this.readyState != 4 ) {
			return;
		}
		if	( this.status != 200
		&&	this.status != 0 ) {
			handlerFunc.pasteToSidebar( this.status + ' ' + this.statusText );
		} else {
			var xmlDoc = this.responseXML;
			var xmlRowSet;
			for	( var k = 0; k < xmlDoc.documentElement.childNodes.length; k++ ) {
				xmlRowSet = xmlDoc.documentElement.childNodes[ k ]
				if	( xmlRowSet.nodeType == 1 ) {
					break;
				}
			}
			var rowSet = new Array;
			for	( var iRowNo = 0; iRowNo < xmlRowSet.childNodes.length; iRowNo++ ) {
				var xmlRow = xmlRowSet.childNodes[ iRowNo ];
				if	( xmlRow.nodeType != 1 ) continue;
				var rowData = new Object;
				for	( var iEleNo = 0; iEleNo < xmlRow.childNodes.length; iEleNo++ ) {
					var xmlEle = xmlRow.childNodes[ iEleNo ];
					if	( xmlEle.nodeType != 1 ) continue;
					rowData[ xmlEle.nodeName ] =  textOf( xmlEle );
				}
				if	( rowData.guid != null )
					rowSet.push( rowData );
			}
			handlerFunc( rowSet );
		}
	}
}

function textOf( z ) {
	for	( var k = 0; k < z.childNodes.length; k++ ) {
		var q = z.childNodes[ k ]
		if	( q.nodeType == 3 )
			return q.nodeValue;
	}
}


