// change the text in a node (caption) to (title)
function reText(caption, title){
	while(caption.hasChildNodes())
		caption.removeChild(caption.firstChild);
	var p1= document.createElement('span');
	p1.style.fontWeight= 'bold';
	p1.appendChild(document.createTextNode(title));
	caption.appendChild(p1);
	return p1;
}
var fortun= {
	idcounter: 0,
	myConn:	false,
	init: function(){
		if( !document.getElementById ||	!document.getElementsByTagName)
			return;
		fortun.myConn= new XHConn();
		if( !fortun.myConn )
			return;
	},
	getTitle: function (caption){
		var fnWhenDone= function(oXML) {
			var xml= null;
			try{
				xml= oXML.responseXML;
				var title= fortun.getNodeValue( xml, 'fortune' );
				var status= fortun.getNodeValue( xml, 'status' );
				var p1= reText(caption, title);
				p1.style.color= '#AA0088';
				
			}catch(e){
				if(xml != null)
					caption.appendChild(document.createTextNode('response:' + xml.responseText+'\n'+e.description))
				else
					caption.appendChild(document.createTextNode(e.description));
			}
		};
		++(fortun.idcounter);
		reText(caption, 'Wait for it...');
		return fortun.myConn.connect("http://"+location.hostname+"/cgi-bin/ajfortun.pl", "GET", "id" + fortun.idcounter, fnWhenDone);
	},	
  getNodeValue: function( scope, node ){
	return scope.getElementsByTagName( node )[0].firstChild.nodeValue;
  },
  addEvent: function( obj, type, fn ){  // the add event function
    if (obj.addEventListener) obj.addEventListener( type, fn, false );
    else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() {
        obj["e"+type+fn]( window.event );
      };
      obj.attachEvent( "on"+type, obj[type+fn] );
    }
  }
};
/////fortun.addEvent(window, 'load', function(){ fortun.init('reservd', akey);} );
