// 180805 embedxml.js
// copyright mike capstick
// my javascript routine to retrieve xml files and embed the data in xml tags, 
//  particularly rss files, in webpages. 
// reads the contents of rss title tag, item - title, description link and pubDate tags 
// 310108 - upgraded to allow mp3's to be played in the page rather than jump mp3
//          needs access to mp3playerscript.js 
var xmlhttp

function getXMLDoc(url)
{// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {xmlhttp=new XMLHttpRequest()
   xmlhttp.onreadystatechange=state_Change
   xmlhttp.open("GET",url,true)
   xmlhttp.send(null)
  }
// code for IE
else if (window.ActiveXObject)
        {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
         if (xmlhttp)
            {xmlhttp.onreadystatechange=state_Change
             xmlhttp.open("GET",url,true)
             xmlhttp.send()
            }
        }
}

function state_Change()
{// if xmlhttp shows "loaded" we can go ahead
 if (xmlhttp.readyState==4)
    {// if "OK"
     if   (xmlhttp.status==200)
          {displayXML()} //now we have the xml file lets display it
     else {alert("Problem retrieving data:" + xmlhttp.statusText)}
    }
}

function displayXML(){
   var items;           // for the rss <item> tag
   var pubDatetext=" "; // for the rss <pubdate> tag
   var desctext=" ";
   var titletext=" ";   // for the rss <title> tag
   var displaytext=" "; // to concatenate news story and display it
   var rsstitle=" ";    // for the rss title of the whole feed

   rsstitle=xmlhttp.responseXML.getElementsByTagName("title")[0].firstChild.data;
  
   items=xmlhttp.responseXML.getElementsByTagName("item") //get tags called item
   for (count=0;count<items.length;count++)
       {// get the text in each title tag which is inside each item tag 
        // turn it into an html string 

       // get the date as either pubDate
       if (items[count].getElementsByTagName('pubDate').length==1)  // get it only if it exists
          {pubDatetext=items[count].getElementsByTagName("pubDate")[0].firstChild.nodeValue
          }

//  alert(items[count].getElementsByTagName('dc:date').length)

    // description and length of clip in mb
       desctext='';
       len="Unknown"; 
       if (items[count].getElementsByTagName('description').length==1)  // get it only if it exists
          {// calculate length, if avaialble
           if(items[count].getElementsByTagName('enclosure')[0].getAttribute('length'))
             {
               len=items[count].getElementsByTagName('enclosure')[0].getAttribute('length')/1000000;
             }

           desctext=items[count].getElementsByTagName("description")[0].firstChild.nodeValue+"["+len+" MB]";          
          }

       titletext=items[count].getElementsByTagName("title")[0].firstChild.data
    // 300805 - now using standard enclosure tag for link to asset rather than title tag converted to a link
       mp3file=items[count].getElementsByTagName("enclosure")[0].getAttribute("url")

       // strip any path form filename to use as text on button
       mp3filename=mp3file.slice(mp3file.lastIndexOf("/")+1,mp3file.length);

// call function use the javascript in page mp3player
       linktext='<a href=\"#\" onclick=\"mp3player(\''+mp3file+'\');\">'+mp3filename+'</a>';
       displaytext=displaytext+"<tr><td>"+pubDatetext+"</td><td><b>"+linktext+"</b></td><td>"+desctext+"</td></tr>"
       } // end of for loop

   // add the xml/html data to the display element - 'xmlstuff'
   document.getElementById('xmlstuff').innerHTML=rsstitle+"<table>"+displaytext+"</table>"
}
