//REVISIONS:
//03/26/08 	JME	Creation:


// pass into the function the URI and the number of releases you want to display.  If you pass 0 it will display all the releases received from the feed.

//NOTE:  alerts are for debugging only.



var strQueryString = document.URL.substring(document.URL.indexOf('?'), document.URL.length);

function readRSS(URI, number) {
//alert ("I AM HERE NOW uri  = "+URI);
 //Sends a request to read the feed.
	var req_feed;
      // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
      req_feed = new XMLHttpRequest();
      req_feed.abort();
      req_feed.onreadystatechange = GotAsyncData;
	  		//alert("i am before .open");
      req_feed.open("GET", URI, false);// not working if use true instead of false - if server fails the rest of the code will not
																 // fire - this could be an issue - however if it was async and server fails the result
																 // to the user would be the same - no "in the news" details
	  		//alert("I am before req_feed.send");
      req_feed.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
		//alert("i am in elseif");
      	req_feed = new ActiveXObject("Microsoft.XMLHTTP");
      	if (req_feed) {
        	req_feed.abort();
        	req_feed.onreadystatechange = GotAsyncData;
        	req_feed.open("GET", URI, false);// not working if use true instead of false - if server fails the rest of the code will not
																		// fire - this could be an issue - however if it was async and server fails the result
																		// to the user would be the same - no "in the news" details
        	req_feed.send();
      	}
    }
	//after data is retrieved from server.
	items=req_feed.responseXML;  //used in the formatRSS function
	//	alert('help!!!'+  items.getElementsByTagName('item').length);
	//alert("i am before formatRSS");
	// GotAsyncData is the read callback for the above XMLHttpRequest() call.
  	// This routine is not executed until data arrives from the request.
 	// We update the "req_feed" area on the page when data does arrive.
  function GotAsyncData() {
    // only if req_feed shows "loaded"
    if (req_feed.readyState != 4 || req_feed.status != 200) {
		//alert( "readystate = "+req_feed.readyState+" status = "+req_feed.status);
      return;
    }
	//alert( "readystateoutside of if = "+req_feed.readyState+" status = "+req_feed.status);
	//alert("responseText = "+req_feed.responseXML);
	//alert("req_feed = "+req_feed.responseXML.getElementsByTagName('item').length);
    // Schedule next call to wait for req_feed data
	 //alert("I am before return");
   return;
  }
	function formatRSS(number) {
	//	alert('I AM HERE start function formatRSS');
		var items_count;
					//alert("items.getElementsByTagName('item').length = "+items.getElementsByTagName('item').length);
					//alert("items.getElementsByTagName('item').length = "+xmlDoc.getElementsByTagName('item').length);
		if ((number == 0) || (number > items.getElementsByTagName('item').length))
			items_count=items.getElementsByTagName('item').length;
		else
			items_count=number;
			//alert('Iems_count = '+items_count);

//Collect info from feed into arrays  NOTE - -used the comments RSS tag to pass any audio url associated with the feed.
		var date=new Array(), time=new Array(), link=new Array(), title=new Array(), description=new Array(), comments=new Array(), pubDate=new Array(),  body=new Array(), image=new Array(), temp, temp2;
				//alert('I AM HERE 10 items count = '+items_count);
		for(var i=0; i<items_count; i++) {
				//alert("i am here 10.5");
			if(items.getElementsByTagName('item')[i].getElementsByTagName('link').length==1){
				link[i]=items.getElementsByTagName('item')[i].getElementsByTagName('link')[0];
			}//end link
			if(items.getElementsByTagName('item')[i].getElementsByTagName('title').length==1){
				title[i]=items.getElementsByTagName('item')[i].getElementsByTagName('title')[0];
			}//end title
			//alert('i am here 11');
			//alert("this is the description length = "+ items.getElementsByTagName('item')[i].getElementsByTagName('description').length);
			if(items.getElementsByTagName('item')[i].getElementsByTagName('description').length==1){
				description[i]=items.getElementsByTagName('item')[i].getElementsByTagName('description')[0];
			}//end description
			//alert("items.getElementsByTagName('item')[i].getElementsByTagName('comments').length="+items.getElementsByTagName('item')[i].getElementsByTagName('comments').length);
			if(items.getElementsByTagName('item')[i].getElementsByTagName('comments').length==1){
				comments[i]=items.getElementsByTagName('item')[i].getElementsByTagName('comments')[0];
			}
			if(items.getElementsByTagName('item')[i].getElementsByTagName('pubDate').length==1){
				pubDate[i]=items.getElementsByTagName('item')[i].getElementsByTagName('pubDate')[0];
			}
		}//end for 
//alert("Description.length  = "+ description.length);
//alert("title.length = "+title.length);
		if((description.length==0)&&(title.length==0)) return false;

		var ws=/\S/;
			//alert('I AM HERE 12');
		for(var i=0; i<items_count; i++) {
			var title_w, link_w,  image_w;
		//	alert('I AM HERE 12.1');
			if(document.all)
				title_w=(title.length>0)?title[i].text:"<i>Untitled</i>";
			else
				title_w=(title.length>0)?title[i].firstChild.nodeValue:"<i>Untitled</i>";

			link_w=(link.length>0)?link[i].firstChild.nodeValue:"";
			//alert('I AM HERE 13');
		//alert("this is the description value data = "+description[i].firstChild.data );	
	//display data from feed - note, add any html formatting you want to customize the look.	
//	 	document.write('<h3>'+title_w+'</h3><p>');
//		alert("i am here 13.2");
//			if(i=0 ||(document.write(pubDate[i].firstChild.nodeValue != document.write(pubDate[i-1].firstChild.nodeValue)){

	//if (i = 0){
//					document.write(pubDate[i].firstChild.nodeValue+'<BR><ul>');
	//	else 
if  (i==0){
	document.write('<strong>'+pubDate[0].firstChild.nodeValue+'</strong><BR><ul>');
}
if (i>0){
			if (pubDate[i].firstChild.nodeValue != pubDate[i-1].firstChild.nodeValue){
				
					document.write('</ul><strong>'+pubDate[i].firstChild.nodeValue+'</strong><BR><ul>');
			}
//else  document.write(pubDate[0].firstChild.nodeValue+'<BR><ul>');
}
//	}
if (isNaN(comments[i].firstChild.data)) {
		document.write('<li><a href='+comments[i].firstChild.data+'>'+description[i].firstChild.data+'</a><BR></li>');
} else {
	document.write('<li><a href='+strQueryString+'?id='+comments[i].firstChild.data+'>'+description[i].firstChild.data+'</a><BR></li>');
} 

	//document.write('<li>'+comments[i].firstChild.data+'<BR></li>');
//		alert("i am here 13.3");
	//	document.write('<hr size="1" color = "#d9d9d9">');
		}//end for
	//		alert('I AM HERE 14');
	}//end function formatrss
		formatRSS(arguments[1]);
}//end function readrss
