/**
 * Display email for the given user anem
 */
function displayEmail(userName, linkText) {
   var email = userName + "@utmb.edu";
   document.write("<a href='mailto:" + email + "'>" + ((linkText) ? linkText : email) + "</a>");
}

/**
 * Confirm dialog
 * @returns true if user confirms question
 */
function getConfirmation(question) {
   return (confirm(question));
}

/**
 * Load the given array of feeds
 * @param feedsArray Array of feeds data. format: ([container, title, rss, resultCount],...)
 */
function loadFeeds(feedsArray) {
   for (var i = 0; i < feedsArray.length; i++) {
      var containerId = feedsArray[i][0];
      var title = feedsArray[i][1];
      var rss = feedsArray[i][2];
      var resultCount = feedsArray[i][3];
      var reader = new RSSReader(rss, containerId, "/pathology/images/loading.gif", resultCount);
      reader.setTitle(title);
      reader.loadRSS();
   }
}


/**
 * Construct an RSS reader
 * param url  the URL to retrieve RSS from
 * param containerId  the id for the DOM container to put results in
 * param loadingImage the path for the image that indicates loading
 * param resultCount  the number of results to retrieve
 * @author Anthony E. (Jere) Okorodudu
 */
function RSSReader(url, containerId, loadingImage, resultCount) {
   var _url = url;
   var _containerId = containerId;
   var _loadingImage = loadingImage;
   var _resultCount = resultCount;
   var _title = "";
   var _footer = "";

   /**
    * Create an Http Request object
    * @param isXML  is the response XML data
    * @return  a Http Request object
    */
   function createHttpRequest(isXML) {
      var httpRequest = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         httpRequest = new XMLHttpRequest();
         if ((isXML) && (httpRequest.overrideMimeType)) {
            httpRequest.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (ex) {
            try {
               httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (ex) {}
         }
      }

      return httpRequest;
   }

   /**
    * Set the title for the RSS contents
    * @param title  the title for the RSS contents
    */
   this.setTitle = function(title) {
      _title = title;
   }

   /**
    * Set the footer for the RSS contents
    * @param footer  the footer for the RSS contents
    */
   this.setFooter = function(footer) {
      _footer = footer;
   }


   /**
    * Send request to retrieve RSS
    * @param url  the RSS url
    * @param containerId  the ID of the container to store results
    * @param loadingImage  the image to display while loading
    */
   this.loadRSS = function() {
      if (document.getElementById) {
         showBusy(true);
         var httpRequest = createHttpRequest(false);

         if (!httpRequest) {
            // Failed to create request
            showBusy(false);
            return;
         }

         try {
            httpRequest.onreadystatechange = function() {displayRssResultsHandler(httpRequest);};
            httpRequest.open("GET", _url, true);
            httpRequest.send(null);
         } catch(e) {
            showBusy(false);
         }
      }
   }

   /**
    * Display the RSS results
    * @param httpRequest  the Http request
    */
   function displayRssResultsHandler(httpRequest) {
      if (httpRequest.readyState == 4) {
         if ((httpRequest.status == 200) || (httpRequest.status == 0)) {
            var response = httpRequest.responseXML;

            if (!response) {
               showBusy(false);
            } else {
               var results = parseResults(response);
               showBusy(false);
               var container = document.getElementById(containerId);

               if (container) {
                  container.innerHTML = results;
               }
            }
         } else {
            showBusy(false);
         }
      }
   }

   /**
    * Parse RSS results
    * @param doc  the xml document to parse
    * @return  RSS content links in HTML format
    */
   function parseResults(doc) {
      var html = "";

      if (document.getElementById && document.evaluate) {
         var iterator = document.evaluate("//channel/item", doc, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

         try {
            var node;
            var count = 0;
            while ((node = iterator.iterateNext()) && (count++ < _resultCount)) {
               //alert(node.textContent);
               var titleNode = document.evaluate("title/text()", node, null, XPathResult.STRING_TYPE, null);
               var linkNode = document.evaluate("link/text()", node, null, XPathResult.STRING_TYPE, null);

               html += "<li><a href='" + linkNode.stringValue + "'>" + titleNode.stringValue + "</a></li>";
            }	
         } catch (e) {
            dump( 'Error: Document tree modified during iteraton ' + e );
         }

         if (html != "") {
            html = "<dl><dt>" + _title + "</dt><dd><ul>" + html + "</ul></dd></dl>";
   
            if (_footer != "") {
               html += "<p>" + _footer + "</p>";
            }
         }
      }

      return html;
   }

   /**
    * Parse RSS results using DOM
    * @param dom  the DOM object
    * @return  RSS content links in HTML format
    */
   function parseIEFeedList(dom) {
      var container = document.getElementById(_containerId);
      var html = "";

      var nl = dom.getElementsByTagName("item");

      for( var i = 0; i < nl.length && i < _resultCount; i++ ) {
         var nli = nl.item(i);
         var linkNode = nli.getElementsByTagName("link").item(0);
         var titleNode = nli.getElementsByTagName("title").item(0);

         html += "<li><a href='" + linkNode.textContent + "'>" + titleNode.textContent + "</a></li>";
      }

      if (html != "") {
         html = "<dl><dt>" + _title + "</dt><dd><ul>" + html + "</ul></dd></dl>";
   
         if (_footer != "") {
            html += "<p>" + _footer + "</p>";
         }
      }

      return html;      
   }


   /**
    * Show a busy indicator
    */
   function showBusy(show) {
      showBusyIndicator(show, _containerId, _loadingImage);
      //setTimeout("showBusyIndicator(true, '" + _containerId + "', '" + _loadingImage + "')", 0);
   }
}


/**
 * Show a busy indicator
 */
function showBusyIndicator(show, containerId, loadingImage) {
   if (document.getElementById) {
      var container = document.getElementById(containerId);
       if (container) {
         var html = (show)
            ? ("<img src=\"" + loadingImage + "\" alt=\"loading\" />")
            : "";

         container.innerHTML = html;
      }
   }
}