
//This is tricky
//This function causes a Request to an remote server and manages to assign a new callback for each connection
// The function contains the variable required as well as a dynamically built function (see invoke =) that will handle the asynchronous reply	
function ReqChange(objid) {
	//maintain a handle to the object to which we will write the RSS details when the finally are returned some time later
	this.obj = document.getElementById(objid);
	if (window.XMLHttpRequest) {// try to create XMLHttpRequest 
	    this.AjaxRequestObject = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)     {       // if ActiveXObject use the Microsoft.XMLHTTP
	     this.AjaxRequestObject = new ActiveXObject("Microsoft.XMLHTTP");	// If data received correctly
	}
	var me = this;
	
	//function to be used as the callback after the async reply
	this.invoke = function () {
		me.obj.innerHTML = "connected..." + me.AjaxRequestObject.readyState;
		if (me.AjaxRequestObject.readyState==4) {
			// if data is valid
			// Parsing RSS.. add statements here for any different kind of response type
			if (me.obj.getAttribute('name') == 'rssFeed') {
				me.obj.innerHTML = "displaying...";
		    //displayRSSFeed(me.obj, me.AjaxRequestObject.responseXML.documentElement);
		    if(me.AjaxRequestObject.responseXML.documentElement)
                {
                    displayRSSFeed(me.obj, me.AjaxRequestObject.responseXML.documentElement);
                }
                else
                {

                    if(window.ActiveXObject && me.AjaxRequestObject.responseText)
                    {
	                var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	                var xmlString = me.AjaxRequestObject.responseText.replace(/"/g,'abx123..');
	                xmlString = xmlString.replace(/abx123../g,'"');				        
	                xmlDoc.loadXML(xmlString);
	                displayRSSFeed(me.obj, xmlDoc);
                    }

                }
			}
		}
	}	
}

//we have a returned RSS document and will now parse that document and display it inside a specific area in the DOM (RSSContent)
function displayRSSFeed (RSSContent, returnedDoc ) {
	var _maxitems = RSSContent.getAttribute('maxItems');
 // Get Channel information
	try {
	    var channel = returnedDoc.getElementsByTagName('channel').item(0);
	    var title = channel.getElementsByTagName('title').item(0).firstChild.data;
	    var link = channel.getElementsByTagName('link').item(0).firstChild.data;
	}
	catch (rsserr) {
	    RSSContent.innerHTML = 'No headlines found.';
	    return;
	}
	//clear out existing content
	RSSContent.innerHTML = '';

	// Browse items
	var items = channel.getElementsByTagName('item');

	if (items.length < _maxitems)
		_maxitems = items.length;

	for (var n=0; n < _maxitems; n++)
	{
		var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
		var itemDescription = '...';
		try {
			itemDescription = items[n].getElementsByTagName('description').item(0).firstChild.data
		}
		catch (err) {
		}

		var itemLink = '';
		try {
			var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
		}
		catch (err) {
		}

		//use same markup as existing webboxes
		var RSSp = document.createElement('p');
		var RSSLink = document.createElement('a');
		
		RSSLink.innerHTML = itemTitle;
		RSSLink.setAttribute('description', itemDescription);

		RSSLink.setAttribute('href',  "javascript:displayLink('" + itemLink + "')");

		RSSContent.appendChild(RSSp);
		RSSp.appendChild(RSSLink);
		
	}

}


//for some RSS feeds, they will supply some form of detail for the line-item. here, we simply hide/show the link upon some event

function RSShowDetails() {
}

function RSSHideDetails() {
}


//when a user clicks on the link, we pop up a new window and take the user to that link for the details

function displayLink(url) {
	var rsswin = window.open(url, "RSSDetails");
	rsswin.focus();
}


// Send a request. Need to know where to send the request and into which box to put the response
function AjaxSendRequest(id, requestedURL) {
	 // Prepare the request
	// Set the onreadystatechange function
	request = new ReqChange(id);
	try {
	    //TT97687
	    var qsTimeStamp='';
	    if(request.obj.getAttribute('name')=='rssFeed')
	        qsTimeStamp='&timestamp=no';
	    //End of TT97687    
		proxiedURL = "/ReverseProxyServer.asp?type=passthru" + qsTimeStamp + "&dest=" + escape(requestedURL);
		request.AjaxRequestObject.open("GET", proxiedURL , true);
		request.AjaxRequestObject.onreadystatechange = request.invoke;
		// Send
		request.AjaxRequestObject.send(null);
	}
	catch (err) {
		document.getElementById(id).innerHTML = 'permission denied.' + err;
	}
}


//round up all web boxes and send a request for each web box
function AjaxSendRequests() {
	for(var i = 0; i < AJAXItems.length; i++) {
		var wb = document.getElementById(AJAXItems[i]);
		if (wb.getAttribute('rssURL') ) {
			wb.innerHTML = "Contacting '" + wb.getAttribute('rssURL') + "'...";
			AjaxSendRequest(wb.id, wb.getAttribute('rssURL'));
		}
	}
}
var AJAXItems = null;
function registerAJAXItem(id) {
	if (AJAXItems == null) {
		//then we need to set up the onload event and initialize the array list that will hold the items to be processed
		if (window.attachEvent)
			window.attachEvent('onload', AjaxSendRequests);
		else
			window.addEventListener('load', AjaxSendRequests, false);
		AJAXItems = new Array(0);
	}
	AJAXItems[AJAXItems.length] = id;
}

