var xmlHttp;
		
      function ajaxFunction()
      {
        xmlHttp = createXML();
        xmlHttp.onreadystatechange=function()
        {
          if(xmlHttp.readyState==4)
          {
						//if (!xmlHttp.responseXML.documentElement && xmlHttp.responseStream)
  						//xmlHttp.responseXML.load(xmlHttp.responseStream);
						parseXml();
          }
        }
        xmlHttp.open("GET","/sermons/sermonsXml.aspx",true);
        xmlHttp.send(null);
      }
			
			
		function parseXml() {
				var container = document.getElementById("feed");
        var items = xmlHttp.responseXML.getElementsByTagName('item');
				
        for (var i = 0; i < items.length; i++) {
					var entry = items[i];
					var author = getElementTextNS("itunes", "author", items[i], 0);
					var title = getElementTextNS("", "title", items[i], 0);
					var pubDate = getElementTextNS("", "pubDate", items[i], 0);
					var linktext= getElementTextNS("", "link", items[i], 0);
					var descript = getElementTextNS("", "description", items[i], 0);
					
					var sermonDiv = document.createElement("div");
          sermonDiv.className = "sermonDiv";
					
					var textDiv = document.createElement("div")
					textDiv.className = "textDiv";
					
  					var titleDiv = document.createElement("div");
            titleDiv.className = "titleDiv";
						var titleA = document.createElement("a");
						titleA.href = "#";
						titleA.onclick = new Function('downloadSermon(\'' + linktext.replace("http://www.delmarfullgospel.org/dnn/Portals/0/sermons/","") + '\')');
            titleA.appendChild(document.createTextNode(title));
						titleDiv.appendChild(titleA);
  					
  					var authorDiv = document.createElement("div");
  					authorDiv.className = "authorDiv";
            authorDiv.appendChild(document.createTextNode(author));
  					
  					var infoDiv = document.createElement("div");
            infoDiv.className = "infoDiv";
            //var curDate = new Date(pubDate)
            infoDiv.appendChild(document.createTextNode(formatDate(pubDate)));
						
						textDiv.appendChild(titleDiv);
						textDiv.appendChild(authorDiv);
						textDiv.appendChild(infoDiv);
					 					
  					
					var playDiv = document.createElement("div");
          playDiv.className = "playDiv";
          var img = document.createElement("img")
          img.id = 'img' + i;
          img.src = "images/playUp_40.png";
          img.onmouseout = new Function('IMG_out(\'img' + i + '\',\'images/playUp_40.png\')');
          img.onmouseover = new Function('IMG_over(\'img' + i + '\',\'images/playDown_40.png\')');
					img.title = "Click here to play sermon online";
					
					var a = document.createElement("a");
          a.href = "#";
          a.onclick = new Function('popupSermon(\'' + linktext.replace("http://www.delmarfullgospel.org/dnn/Portals/0/sermons/","") + '\')');
          a.appendChild(img);
          playDiv.appendChild(a);
					
					var downloadDiv = document.createElement("div");
					downloadDiv.className = "downloadDiv";
					var img2 = document.createElement("img");
					img2.id = 'ima' + i;
					img2.src = "images/downloadUp_40.png";
					img2.onmouseout = new Function('IMG_out(\'ima' + i + '\',\'images/downloadUp_40.png\')');
          img2.onmouseover = new Function('IMG_over(\'ima' + i + '\',\'images/downloadDown_40.png\')');
					
					var da = document.createElement("a");
					da.href = "#";
					da.onclick = new Function('downloadSermon(\'' + linktext.replace("http://www.delmarfullgospel.org/dnn/Portals/0/sermons/","") + '\')');
					da.appendChild(img2);
					downloadDiv.appendChild(da);
					
					sermonDiv.appendChild(textDiv);
          sermonDiv.appendChild(playDiv);
					sermonDiv.appendChild(downloadDiv);
					container.appendChild(sermonDiv);
				}
    }
		
		function getElementTextNS(prefix, local, parentElem, index) {
        var result = "";
				
				result = parentElem.getElementsByTagName(local)[index];
				
				if (!result) {
					result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
				}
				
        if (result) {
            // get text, accounting for possible
            // whitespace (carriage return) text nodes 
            if (result.childNodes.length > 1) {
                return result.childNodes[1].nodeValue;
            } else {
								if (result.childNodes.length > 0) {
                	return result.firstChild.nodeValue;
								} else {
									return "";
								}
            }
        } else {
            return "";
        }
    }

        function IMG_over(imageID,newImagePath) {
            document.getElementById(imageID).src = newImagePath;
        }
        function IMG_out(imageID,newImagePath) {
            document.getElementById(imageID).src = newImagePath;
        }
        function popupSermon(pathToFile){
            window.open('mp3Player.html?audioUrl=' + pathToFile,'DFG_Sermon_Player','status=0,toolbar=0,menubar=0,scrollbars=0,resizable=1,directories=0,height=165,width=435');
        }
				function downloadSermon(pathToFile){
            frames['downloadFrame'].location.href = 'mp3Download.aspx?audioUrl=' + pathToFile;
        }
